Copilot commented on code in PR #24940:
URL: https://github.com/apache/pulsar/pull/24940#discussion_r2667509714


##########
.github/workflows/ci-pulsarbot.yaml:
##########
@@ -33,7 +16,242 @@ jobs:
     if: github.event_name == 'issue_comment' && 
contains(github.event.comment.body, '/pulsarbot')
     steps:
       - name: Execute pulsarbot command
-        id: pulsarbot
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        uses: apache/pulsar-test-infra/pulsarbot@master
+        uses: actions/github-script@v8
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          script: |
+            const commentBody = (context.payload.comment?.body || '').trim();
+            const prefix = '/pulsarbot';
+
+            if (!commentBody.startsWith(prefix)) {
+              console.log('Not a pulsarbot command, skipping ...');
+              return;
+            }
+            if (!context.payload.issue || !context.payload.issue.pull_request) 
{
+              console.error('This comment is not on a Pull Request. pulsarbot 
only works on PRs.');
+              return;
+            }
+
+            const parts = commentBody.split(/\s+/);
+            const sub = (parts[1] || '').toLowerCase();
+            const arg = parts.length > 2 ? parts.slice(2).join(' ') : '';
+
+            const supported = ['rerun', 'stop', 'cancel', 
'rerun-failure-checks'];
+            if (!supported.includes(sub)) {
+              console.log(
+                `Unsupported command '${sub}'. Supported: ${supported
+                  .map(cmd => `'/pulsarbot ${cmd}${cmd === 'rerun' ? ' 
[jobName?]' : ''}'`)
+                  .join(', ')}.`
+              );
+              return;
+            }
+
+            const prNum = context.payload.issue.number;
+
+            // Get PR info
+            let pr;
+            try {
+              ({ data: pr } = await github.rest.pulls.get({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                pull_number: prNum,
+              }));
+            } catch (e) {
+              console.error(`Failed to fetch PR #${prNum}: ${e.message}`);
+              return;
+            }
+
+            const headSha = pr.head.sha;
+            const prBranch = pr.head.ref;
+            const prUser = pr.user.login;
+            const prUrl = pr.html_url;
+
+            console.log(`pulsarbot handling PR #${prNum} ${prUrl}`);
+            console.log(`PR branch='${prBranch}', headSha='${headSha}', 
author='${prUser}'`);
+            console.log(`Command parsed => sub='${sub}', arg='${arg || ''}'`);
+
+            // Most reliable: list workflow runs by head_sha (no guessing by 
actor/branch/event)
+            const runsAtHeadRaw = await github.paginate(
+              github.rest.actions.listWorkflowRunsForRepo,
+              {
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                head_sha: headSha,
+                per_page: 100,
+              },
+            );
+            console.log(`DEBUG runs for head_sha=${headSha}: 
total_count=${runsAtHeadRaw.total_count}, 
returned=${(runsAtHeadRaw.workflow_runs||[]).length}`);

Review Comment:
   The debug log references `total_count` property which does not exist on the 
paginated response. The `github.paginate` method returns an array of items 
directly, not an object with a `total_count` property. This will log 
`undefined` for the total_count value.
   ```suggestion
               console.log(`DEBUG runs for head_sha=${headSha}: 
runs_returned=${runsAtHeadRaw.length}`);
   ```



##########
.github/workflows/ci-pulsarbot.yaml:
##########
@@ -33,7 +16,242 @@ jobs:
     if: github.event_name == 'issue_comment' && 
contains(github.event.comment.body, '/pulsarbot')
     steps:
       - name: Execute pulsarbot command
-        id: pulsarbot
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        uses: apache/pulsar-test-infra/pulsarbot@master
+        uses: actions/github-script@v8
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          script: |
+            const commentBody = (context.payload.comment?.body || '').trim();
+            const prefix = '/pulsarbot';
+
+            if (!commentBody.startsWith(prefix)) {
+              console.log('Not a pulsarbot command, skipping ...');
+              return;
+            }
+            if (!context.payload.issue || !context.payload.issue.pull_request) 
{
+              console.error('This comment is not on a Pull Request. pulsarbot 
only works on PRs.');
+              return;
+            }
+
+            const parts = commentBody.split(/\s+/);
+            const sub = (parts[1] || '').toLowerCase();
+            const arg = parts.length > 2 ? parts.slice(2).join(' ') : '';
+
+            const supported = ['rerun', 'stop', 'cancel', 
'rerun-failure-checks'];
+            if (!supported.includes(sub)) {
+              console.log(
+                `Unsupported command '${sub}'. Supported: ${supported
+                  .map(cmd => `'/pulsarbot ${cmd}${cmd === 'rerun' ? ' 
[jobName?]' : ''}'`)
+                  .join(', ')}.`
+              );
+              return;
+            }
+
+            const prNum = context.payload.issue.number;
+
+            // Get PR info
+            let pr;
+            try {
+              ({ data: pr } = await github.rest.pulls.get({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                pull_number: prNum,
+              }));
+            } catch (e) {
+              console.error(`Failed to fetch PR #${prNum}: ${e.message}`);
+              return;
+            }
+
+            const headSha = pr.head.sha;
+            const prBranch = pr.head.ref;
+            const prUser = pr.user.login;
+            const prUrl = pr.html_url;
+
+            console.log(`pulsarbot handling PR #${prNum} ${prUrl}`);
+            console.log(`PR branch='${prBranch}', headSha='${headSha}', 
author='${prUser}'`);
+            console.log(`Command parsed => sub='${sub}', arg='${arg || ''}'`);
+
+            // Most reliable: list workflow runs by head_sha (no guessing by 
actor/branch/event)
+            const runsAtHeadRaw = await github.paginate(
+              github.rest.actions.listWorkflowRunsForRepo,
+              {
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                head_sha: headSha,
+                per_page: 100,
+              },
+            );
+            console.log(`DEBUG runs for head_sha=${headSha}: 
total_count=${runsAtHeadRaw.total_count}, 
returned=${(runsAtHeadRaw.workflow_runs||[]).length}`);
+            const runsAtHead = runsAtHeadRaw.filter(r => r && typeof r === 
'object');

Review Comment:
   The filter on line 84 is redundant. The `github.paginate` method already 
returns an array of workflow run objects. This filter checking if items are 
objects doesn't add value and could be removed for cleaner code.
   ```suggestion
               const runsAtHead = runsAtHeadRaw;
   ```



##########
.github/workflows/ci-pulsarbot.yaml:
##########
@@ -33,7 +16,242 @@ jobs:
     if: github.event_name == 'issue_comment' && 
contains(github.event.comment.body, '/pulsarbot')
     steps:
       - name: Execute pulsarbot command
-        id: pulsarbot
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        uses: apache/pulsar-test-infra/pulsarbot@master
+        uses: actions/github-script@v8
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          script: |
+            const commentBody = (context.payload.comment?.body || '').trim();
+            const prefix = '/pulsarbot';
+
+            if (!commentBody.startsWith(prefix)) {
+              console.log('Not a pulsarbot command, skipping ...');
+              return;
+            }
+            if (!context.payload.issue || !context.payload.issue.pull_request) 
{
+              console.error('This comment is not on a Pull Request. pulsarbot 
only works on PRs.');
+              return;
+            }
+
+            const parts = commentBody.split(/\s+/);
+            const sub = (parts[1] || '').toLowerCase();
+            const arg = parts.length > 2 ? parts.slice(2).join(' ') : '';
+
+            const supported = ['rerun', 'stop', 'cancel', 
'rerun-failure-checks'];
+            if (!supported.includes(sub)) {
+              console.log(
+                `Unsupported command '${sub}'. Supported: ${supported
+                  .map(cmd => `'/pulsarbot ${cmd}${cmd === 'rerun' ? ' 
[jobName?]' : ''}'`)
+                  .join(', ')}.`
+              );
+              return;
+            }
+
+            const prNum = context.payload.issue.number;
+
+            // Get PR info
+            let pr;
+            try {
+              ({ data: pr } = await github.rest.pulls.get({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                pull_number: prNum,
+              }));
+            } catch (e) {
+              console.error(`Failed to fetch PR #${prNum}: ${e.message}`);
+              return;
+            }
+
+            const headSha = pr.head.sha;
+            const prBranch = pr.head.ref;
+            const prUser = pr.user.login;
+            const prUrl = pr.html_url;
+
+            console.log(`pulsarbot handling PR #${prNum} ${prUrl}`);
+            console.log(`PR branch='${prBranch}', headSha='${headSha}', 
author='${prUser}'`);
+            console.log(`Command parsed => sub='${sub}', arg='${arg || ''}'`);
+
+            // Most reliable: list workflow runs by head_sha (no guessing by 
actor/branch/event)
+            const runsAtHeadRaw = await github.paginate(
+              github.rest.actions.listWorkflowRunsForRepo,
+              {
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                head_sha: headSha,
+                per_page: 100,
+              },
+            );
+            console.log(`DEBUG runs for head_sha=${headSha}: 
total_count=${runsAtHeadRaw.total_count}, 
returned=${(runsAtHeadRaw.workflow_runs||[]).length}`);
+            const runsAtHead = runsAtHeadRaw.filter(r => r && typeof r === 
'object');
+
+            console.log(`runsAtHead total=${runsAtHead.length} for 
head_sha=${headSha}`);
+
+            if (runsAtHead.length === 0) {
+              console.error(`No workflow runs found for head SHA ${headSha} 
(PR branch ${prBranch}).`);
+              return;
+            }
+
+            // Only keep the latest run for each workflow_id
+            runsAtHead.sort((a, b) => {
+              if (a.workflow_id !== b.workflow_id) return a.workflow_id - 
b.workflow_id;
+              return new Date(b.created_at) - new Date(a.created_at);
+            });
+
+            const latestRuns = [];
+            const seen = new Set();
+            for (const r of runsAtHead) {
+              if (!seen.has(r.workflow_id)) {
+                seen.add(r.workflow_id);
+                latestRuns.push(r);
+              }
+            }
+
+            function runKey(r) {
+              return `[run_id=${r.id}] ${r.name || '(unnamed)'} | 
status=${r.status} | conclusion=${r.conclusion || '-'} | ${r.html_url}`;
+            }
+
+            console.log('--- Latest workflow runs for this PR headSHA (one per 
workflow) ---');
+            for (const r of latestRuns) console.log('- ' + runKey(r));
+
+            async function listAllJobs(runId) {
+              const jobs = [];
+              let p = 1;
+              while (true) {
+                const { data } = await 
github.rest.actions.listJobsForWorkflowRun({
+                  owner: context.repo.owner,
+                  repo: context.repo.repo,
+                  run_id: runId,
+                  per_page: 100,
+                  page: p,
+                });
+                const js = data.jobs || [];
+                if (js.length === 0) break;
+                jobs.push(...js);
+                if (js.length < 100) break;
+                p++;
+              }

Review Comment:
   The manual pagination implementation for listing jobs is unnecessary. The 
`github.paginate` utility method can be used here instead, which would simplify 
the code and reduce the risk of pagination bugs.
   ```suggestion
                 const jobs = await github.paginate(
                   github.rest.actions.listJobsForWorkflowRun,
                   {
                     owner: context.repo.owner,
                     repo: context.repo.repo,
                     run_id: runId,
                     per_page: 100,
                   },
                   (response) => response.data.jobs || []
                 );
   ```



##########
.github/workflows/ci-pulsarbot.yaml:
##########
@@ -33,7 +16,242 @@ jobs:
     if: github.event_name == 'issue_comment' && 
contains(github.event.comment.body, '/pulsarbot')
     steps:
       - name: Execute pulsarbot command
-        id: pulsarbot
-        env:
-          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-        uses: apache/pulsar-test-infra/pulsarbot@master
+        uses: actions/github-script@v8
+        with:
+          github-token: ${{ secrets.GITHUB_TOKEN }}
+          script: |
+            const commentBody = (context.payload.comment?.body || '').trim();
+            const prefix = '/pulsarbot';
+
+            if (!commentBody.startsWith(prefix)) {
+              console.log('Not a pulsarbot command, skipping ...');
+              return;
+            }
+            if (!context.payload.issue || !context.payload.issue.pull_request) 
{
+              console.error('This comment is not on a Pull Request. pulsarbot 
only works on PRs.');
+              return;
+            }
+
+            const parts = commentBody.split(/\s+/);
+            const sub = (parts[1] || '').toLowerCase();
+            const arg = parts.length > 2 ? parts.slice(2).join(' ') : '';
+
+            const supported = ['rerun', 'stop', 'cancel', 
'rerun-failure-checks'];
+            if (!supported.includes(sub)) {
+              console.log(
+                `Unsupported command '${sub}'. Supported: ${supported
+                  .map(cmd => `'/pulsarbot ${cmd}${cmd === 'rerun' ? ' 
[jobName?]' : ''}'`)
+                  .join(', ')}.`
+              );
+              return;
+            }
+
+            const prNum = context.payload.issue.number;
+
+            // Get PR info
+            let pr;
+            try {
+              ({ data: pr } = await github.rest.pulls.get({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                pull_number: prNum,
+              }));
+            } catch (e) {
+              console.error(`Failed to fetch PR #${prNum}: ${e.message}`);
+              return;
+            }
+
+            const headSha = pr.head.sha;
+            const prBranch = pr.head.ref;
+            const prUser = pr.user.login;
+            const prUrl = pr.html_url;
+
+            console.log(`pulsarbot handling PR #${prNum} ${prUrl}`);
+            console.log(`PR branch='${prBranch}', headSha='${headSha}', 
author='${prUser}'`);
+            console.log(`Command parsed => sub='${sub}', arg='${arg || ''}'`);
+
+            // Most reliable: list workflow runs by head_sha (no guessing by 
actor/branch/event)
+            const runsAtHeadRaw = await github.paginate(
+              github.rest.actions.listWorkflowRunsForRepo,
+              {
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                head_sha: headSha,
+                per_page: 100,
+              },
+            );
+            console.log(`DEBUG runs for head_sha=${headSha}: 
total_count=${runsAtHeadRaw.total_count}, 
returned=${(runsAtHeadRaw.workflow_runs||[]).length}`);
+            const runsAtHead = runsAtHeadRaw.filter(r => r && typeof r === 
'object');
+
+            console.log(`runsAtHead total=${runsAtHead.length} for 
head_sha=${headSha}`);
+
+            if (runsAtHead.length === 0) {
+              console.error(`No workflow runs found for head SHA ${headSha} 
(PR branch ${prBranch}).`);
+              return;
+            }
+
+            // Only keep the latest run for each workflow_id
+            runsAtHead.sort((a, b) => {
+              if (a.workflow_id !== b.workflow_id) return a.workflow_id - 
b.workflow_id;
+              return new Date(b.created_at) - new Date(a.created_at);

Review Comment:
   The sorting logic uses numeric subtraction for workflow_id comparison, but 
workflow_id values can be very large numbers that might exceed JavaScript's 
safe integer range. This could lead to incorrect sorting. Use a comparison that 
works reliably with large numbers.
   ```suggestion
                 const aw = String(a.workflow_id);
                 const bw = String(b.workflow_id);
                 if (aw !== bw) {
                   if (aw.length < bw.length) return -1;
                   if (aw.length > bw.length) return 1;
                   return aw < bw ? -1 : 1;
                 }
                 const at = new Date(a.created_at).getTime();
                 const bt = new Date(b.created_at).getTime();
                 if (bt > at) return 1;
                 if (bt < at) return -1;
                 return 0;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to