.github/workflows/pr-tasks.yaml #3
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
issue_comment: | |
pull_request_review_comment: | |
pull_request_review: | |
pull_request: | |
types: | |
- opened | |
- edited | |
push: | |
jobs: | |
pr_commented: | |
name: PR comment | |
runs-on: self-hosted | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
script: | | |
async function processPullRequest(github, owner, repo, pull_number) { | |
const pr = await github.rest.pulls.get({ | |
owner, | |
repo, | |
pull_number | |
}) | |
if (pr.status !== 200) { | |
throw "Failed to load pr: " + pr | |
} | |
var comments = [] | |
comments.push(pr.data.body) | |
const issueComments = await github.paginate(github.rest.issues.listComments.endpoint.merge({ | |
owner, | |
repo, | |
issue_number: pull_number | |
})) | |
comments.push(...issueComments.map(comment => comment.body)) | |
const prComments = await github.paginate(github.rest.pulls.listReviewComments.endpoint.merge({ | |
owner, | |
repo, | |
pull_number | |
})) | |
comments.push(...prComments.map(comment => comment.body)) | |
const prReviews = await github.paginate(github.rest.pulls.listReviews.endpoint.merge({ | |
owner, | |
repo, | |
pull_number | |
})) | |
comments.push(...prReviews.map(comment => comment.body)) | |
var openTasks = [] | |
var closedTasks = [] | |
comments | |
.filter(comment => comment !== null) | |
.filter(comment => comment !== undefined) | |
.flatMap(comment => comment.split("\n")) | |
.map(line => line.trim()) | |
.filter(line => line.length > 0) | |
.forEach(line => { | |
if (line.startsWith("- [ ]")) { | |
openTasks.push(line.substring(5).trim()) | |
} else if (line.startsWith("- [x]")) { | |
closedTasks.push(line.substring(5).trim()) | |
} | |
}) | |
var state = { | |
state: "success", | |
description: "No tasks in this PR" | |
} | |
if (openTasks.length === 1) { | |
state.state = "failure" | |
state.description = "Task is not completed: '" + openTasks[0] + "'" | |
} else if (openTasks.length > 1) { | |
state.state = "failure" | |
state.description = openTasks.length + " open task(s)" | |
} else if (openTasks.length === 0) { | |
if (closedTasks.length > 0) { | |
state.description = closedTasks.length + " tasks closed" | |
} | |
} | |
state.description = state.description.substring(0, 139) | |
await github.rest.repos.createCommitStatus({ | |
...state, | |
owner, | |
repo, | |
sha: pr.data.head.sha, | |
context: "pr-tasks-workflow" | |
}) | |
} | |
if (context.eventName === "issue_comment" && context.payload.issue.pull_request === undefined) { | |
console.log("Issue comment is ignored") | |
return | |
} | |
if (context.eventName === "push") { | |
const result = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
owner: context.payload.repository.owner.name, | |
repo: context.payload.repository.name, | |
commit_sha: context.payload.head_commit.id | |
}) | |
if (result.status !== 200) { | |
throw "Failed to PRs for commit: " + result | |
} | |
result.data.forEach(pr => { | |
processPullRequest(github, context.issue.owner, context.issue.repo, pr.number) | |
}) | |
} else { | |
processPullRequest(github, context.issue.owner, context.issue.repo, context.issue.number) | |
} |