Mirror Repo #24
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: | |
- name: Get branch name (PR) | |
shell: bash | |
run: echo "BRANCH_NAME=$(echo ${GITHUB_REF_NAME} | tr / -)" >> $GITHUB_ENV | |
- uses: actions/github-script@v7 | |
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) | |
.filter(line => !line.includes("rebase/retry this PR")) | |
.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" | |
} | |
const branchName = process.env.BRANCH_NAME; | |
var skipCommentCheck = branchName && branchName.startsWith('renovate'); | |
if (!skipCommentCheck) { | |
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" | |
} | |
} | |
} else { | |
state.description = "Skipped task check for branch name " + branchName | |
} | |
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) | |
} |