fix: derive PR number and head SHA for issue_comment events in CLA status check#6
Conversation
… check Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
0f3c5cf
into
fix/cla-assistant-issues-8608742043899548302
There was a problem hiding this comment.
Pull request overview
This PR updates the reusable CLA check workflow’s “Create Success Status Check” step to correctly resolve the PR number and head SHA when the triggering event is issue_comment, where context.payload.pull_request and context.sha may be unavailable.
Changes:
- Add PR number fallback from
context.payload.issue.numberwhenpull_request.numberis missing. - Resolve head SHA by fetching the PR via
github.rest.pulls.getwhen SHA isn’t present in the event payload. - Avoid generating malformed
target_urlvalues by only setting it whenprNumberis defined.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let prNumber = context.payload.pull_request?.number; | ||
| let sha = context.payload.pull_request?.head?.sha; | ||
|
|
||
| if (!prNumber && context.payload.issue?.number) { |
There was a problem hiding this comment.
issue_comment fires for both issues and PRs. Falling back to context.payload.issue.number without verifying that the issue is a PR (e.g., context.payload.issue.pull_request exists) can treat an issue number as a PR number, causing the subsequent pulls.get call to 404 and fail this job. Consider guarding the fallback so you only derive prNumber from issue.number when the issue is actually a PR, otherwise skip creating a status.
| if (!prNumber && context.payload.issue?.number) { | |
| // For issue_comment events, ensure the issue is actually a PR | |
| // by checking for the presence of the pull_request field. | |
| if (!prNumber && context.payload.issue?.pull_request) { |
| const { data: pr } = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: prNumber, | ||
| }); | ||
| sha = pr.head.sha; |
There was a problem hiding this comment.
The github.rest.pulls.get call is not wrapped in error handling. If the PR lookup fails (non-PR issue_comment, permission issue, API outage/rate limit), the script will throw and fail the workflow, potentially preventing the CLA success status from being set. Recommend catching errors around the fetch and logging + returning (or otherwise falling back) so this step degrades gracefully when SHA resolution is not possible.
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber, | |
| }); | |
| sha = pr.head.sha; | |
| try { | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber, | |
| }); | |
| sha = pr.head.sha; | |
| } catch (error) { | |
| console.log(`Failed to fetch PR ${prNumber} for SHA resolution:`, error); | |
| console.log('Skipping status check because SHA could not be resolved'); | |
| return; | |
| } |
In
issue_commentevents,context.payload.pull_requestandcontext.shaare bothundefined, causing the success status check to silently no-op and emit a malformedtarget_url.Changes
context.payload.pull_request?.numberis absent, fall back tocontext.payload.issue.numbergithub.rest.pulls.getusing the resolved PR numbertarget_urlguarded: Only set whenprNumberis defined to avoid/pull/trailing-slash URLs🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.