Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly detect PR number when run with workflow_run #294

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ async function main() {
const fromForkedRepo = payload.pull_request?.head.repo.fork;

if (payload.number && payload.pull_request) {
core.debug('prNumber retrieved from pull_request');
prNumber = payload.number;
} else {
const result =
await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
commit_sha: gitCommitSha,
});
const pr = result.data.length > 0 && result.data[0];
core.debug('listPullRequestsAssociatedWithCommit');
core.debug(JSON.stringify(pr, null, 2));
prNumber = pr ? pr.number : undefined;
core.debug('Not a pull_request, so doing a API search');
// Inspired by https://github.com/orgs/community/discussions/25220#discussioncomment-8697399
const query = {
q: `repo:${github.context.repo.owner}/${github.context.repo.repo} is:pr sha:${gitCommitSha}`,
per_page: 1,
};
try {
const result = await octokit.rest.search.issuesAndPullRequests(query);
const pr = result.data.items.length > 0 && result.data.items[0];
core.debug(`Found related pull_request: ${JSON.stringify(pr, null, 2)}`);
prNumber = pr ? pr.number : undefined;
} catch (e) {
// As mentioned in https://github.com/orgs/community/discussions/25220#discussioncomment-8971083
// from time to time, you may get rate limit errors given search API seems to use many calls internally.
core.warning(`Unable to get the PR number with API search: ${e}`);
}
}
if (!prNumber) {
core.info(`😢 No related PR found, skip it.`);
Expand Down
Loading