[GitHub] Support issue_comment workflows in issue_write#192205
Conversation
This is split off from llvm#190010. We want to add a new workflow triggered whenever a comment is added to an issue (workflow_run.event == 'issue_comment'), that also writes an comment back via the issue_write workflow. However for issue_comment workflows, the head branch for the workflow won't be the head of the PR, but the default branch of the repository. So trying to fetch the PR based on the branch will fail. GitHub docs seem to recommend that the PR number is explicitly passed via an artifact in these cases: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#using-data-from-the-triggering-workflow This PR adds support for this so we can eventually leave comments from the test-suite.yml workflow
|
@llvm/pr-subscribers-github-workflow Author: Luke Lau (lukel97) ChangesThis is split off from #190010. We want to add a new workflow triggered whenever a comment is added to an issue (workflow_run.event == 'issue_comment'), that also writes an comment back via the issue_write workflow. However for issue_comment workflows, the head branch for the workflow won't be the head of the PR, but the default branch of the repository. So trying to fetch the PR based on the branch will fail. GitHub docs seem to recommend that the PR number is explicitly passed via an artifact in these cases: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#using-data-from-the-triggering-workflow This PR adds support for this so we can eventually leave comments from the test-suite.yml workflow Full diff: https://github.com/llvm/llvm-project/pull/192205.diff 1 Files Affected:
diff --git a/.github/workflows/issue-write.yml b/.github/workflows/issue-write.yml
index db378e286932d..7ad57e3ade2d6 100644
--- a/.github/workflows/issue-write.yml
+++ b/.github/workflows/issue-write.yml
@@ -22,7 +22,8 @@ jobs:
permissions:
pull-requests: write
if: >
- github.event.workflow_run.event == 'pull_request' &&
+ (github.event.workflow_run.event == 'pull_request' ||
+ github.event.workflow_run.event == 'issue_comment') &&
(
github.event.workflow_run.conclusion == 'success' ||
github.event.workflow_run.conclusion == 'failure'
@@ -50,10 +51,14 @@ jobs:
script: |
var fs = require('fs');
var comments = []
+ var pr_number = 0
for (local_file of fs.readdirSync('.')) {
if (local_file.startsWith("comments")) {
comments.push(...JSON.parse(fs.readFileSync(local_file)))
}
+ if (local_file.startsWith("pr_number")) {
+ pr_number = parseInt(fs.readFileSync(local_file), 10)
+ }
}
if (!comments || comments.length == 0) {
return;
@@ -68,60 +73,61 @@ jobs:
console.log(runInfo);
- // Query to find the number of the pull request that triggered this job.
- // The associated pull requests are based off of the branch name, so if
- // you create a pull request for a branch, close it, and then create
- // another pull request with the same branch, then this query will return
- // two associated pull requests. This is why we have to fetch all the
- // associated pull requests and then iterate through them to find the
- // one that is open.
- const gql_query = `
- query($repo_owner : String!, $repo_name : String!, $branch: String!) {
- repository(owner: $repo_owner, name: $repo_name) {
- ref (qualifiedName: $branch) {
- associatedPullRequests(first: 100) {
- nodes {
- baseRepository {
- owner {
- login
+ if (!pr_number) {
+ // Query to find the number of the pull request that triggered this job.
+ // The associated pull requests are based off of the branch name, so if
+ // you create a pull request for a branch, close it, and then create
+ // another pull request with the same branch, then this query will return
+ // two associated pull requests. This is why we have to fetch all the
+ // associated pull requests and then iterate through them to find the
+ // one that is open.
+ const gql_query = `
+ query($repo_owner : String!, $repo_name : String!, $branch: String!) {
+ repository(owner: $repo_owner, name: $repo_name) {
+ ref (qualifiedName: $branch) {
+ associatedPullRequests(first: 100) {
+ nodes {
+ baseRepository {
+ owner {
+ login
+ }
}
+ number
+ state
}
- number
- state
}
}
}
}
+ `
+ const gql_variables = {
+ repo_owner: runInfo.data.head_repository.owner.login,
+ repo_name: runInfo.data.head_repository.name,
+ branch: runInfo.data.head_branch
}
- `
- const gql_variables = {
- repo_owner: runInfo.data.head_repository.owner.login,
- repo_name: runInfo.data.head_repository.name,
- branch: runInfo.data.head_branch
- }
- const gql_result = await github.graphql(gql_query, gql_variables);
- console.log(gql_result);
- // If the branch for the PR was deleted before this job has a chance
- // to run, then the ref will be null. This can happen if someone:
- // 1. Rebase the PR, which triggers some workflow.
- // 2. Immediately merges the PR and deletes the branch.
- // 3. The workflow finishes and triggers this job.
- if (!gql_result.repository.ref) {
- console.log("Ref has been deleted");
- return;
- }
- console.log(gql_result.repository.ref.associatedPullRequests.nodes);
+ const gql_result = await github.graphql(gql_query, gql_variables);
+ console.log(gql_result);
+ // If the branch for the PR was deleted before this job has a chance
+ // to run, then the ref will be null. This can happen if someone:
+ // 1. Rebase the PR, which triggers some workflow.
+ // 2. Immediately merges the PR and deletes the branch.
+ // 3. The workflow finishes and triggers this job.
+ if (!gql_result.repository.ref) {
+ console.log("Ref has been deleted");
+ return;
+ }
+ console.log(gql_result.repository.ref.associatedPullRequests.nodes);
- var pr_number = 0;
- gql_result.repository.ref.associatedPullRequests.nodes.forEach((pr) => {
+ gql_result.repository.ref.associatedPullRequests.nodes.forEach((pr) => {
- // The largest PR number is the one we care about. The only way
- // to have more than one associated pull requests is if all the
- // old pull requests are in the closed state.
- if (pr.baseRepository.owner.login = context.repo.owner && pr.number > pr_number) {
- pr_number = pr.number;
- }
- });
+ // The largest PR number is the one we care about. The only way
+ // to have more than one associated pull requests is if all the
+ // old pull requests are in the closed state.
+ if (pr.baseRepository.owner.login = context.repo.owner && pr.number > pr_number) {
+ pr_number = pr.number;
+ }
+ });
+ }
if (pr_number == 0) {
console.log("Error retrieving pull request number");
return;
|
tstellar
left a comment
There was a problem hiding this comment.
LGTM. It would be great if the PR number was available in the event payload, but I haven't found any documentation that says that it is.
This is split off from #190010. We want to add a new workflow triggered whenever a comment is added to an issue (workflow_run.event == 'issue_comment'), that also writes an comment back via the issue_write workflow.
However for issue_comment workflows, the head branch for the workflow won't be the head of the PR, but the default branch of the repository. So trying to fetch the PR based on the branch will fail.
GitHub docs seem to recommend that the PR number is explicitly passed via an artifact in these cases: https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows#using-data-from-the-triggering-workflow
This PR adds support for this so we can eventually leave comments from the test-suite.yml workflow