Skip to content

[GitHub] Support issue_comment workflows in issue_write#192205

Merged
lukel97 merged 3 commits into
llvm:mainfrom
lukel97:issue-write-issue_comment
May 11, 2026
Merged

[GitHub] Support issue_comment workflows in issue_write#192205
lukel97 merged 3 commits into
llvm:mainfrom
lukel97:issue-write-issue_comment

Conversation

@lukel97
Copy link
Copy Markdown
Contributor

@lukel97 lukel97 commented Apr 15, 2026

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

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
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Apr 15, 2026

@llvm/pr-subscribers-github-workflow

Author: Luke Lau (lukel97)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/192205.diff

1 Files Affected:

  • (modified) .github/workflows/issue-write.yml (+52-46)
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;

lukel97 added a commit to lukel97/llvm-project that referenced this pull request Apr 16, 2026
Copy link
Copy Markdown
Contributor

@tstellar tstellar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lukel97 lukel97 enabled auto-merge (squash) May 11, 2026 18:26
@lukel97 lukel97 merged commit 53ff447 into llvm:main May 11, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants