Skip to content

Commit a4e93a0

Browse files
authored
chore: prlinter should use HEAD instead of base commit (#25779)
The `sha` for the `pull_request_target` event is for the base branch (i.e. `main`) so we were checking the status from the last commit to `main`. We should instead get the `sha` from the event payload which contains the `head` sha. This also fixes an issue processing the status event where we were trying to parse the latest commit from the status URL, but that value is always equal to `{sha}`. Instead get it from the `head` info. Added some debug logs to help us see what is getting processed. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d24f02c commit a4e93a0

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

tools/@aws-cdk/prlint/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as core from '@actions/core';
22
import * as github from '@actions/github';
33
import { Octokit } from '@octokit/rest';
44
import * as linter from './lint';
5-
import { StatusEvent } from '@octokit/webhooks-definitions/schema';
5+
import { StatusEvent, PullRequestEvent } from '@octokit/webhooks-definitions/schema';
66

77
async function run() {
88
const token: string = process.env.GITHUB_TOKEN!;
@@ -11,25 +11,28 @@ async function run() {
1111
try {
1212
switch (github.context.eventName) {
1313
case 'status':
14-
const pr = await linter.PullRequestLinter.getPRFromCommit(client, 'aws', 'aws-cdk', github.context.payload.sha);
14+
const statusPayload = github.context.payload as StatusEvent;
15+
const pr = await linter.PullRequestLinter.getPRFromCommit(client, 'aws', 'aws-cdk', statusPayload.sha);
1516
if (pr) {
1617
const prLinter = new linter.PullRequestLinter({
1718
client,
1819
owner: 'aws',
1920
repo: 'aws-cdk',
2021
number: pr.number,
2122
});
23+
console.log('validating status event');
2224
await prLinter.validateStatusEvent(pr, github.context.payload as StatusEvent);
2325
}
2426
break;
2527
default:
28+
const payload = github.context.payload as PullRequestEvent;
2629
const prLinter = new linter.PullRequestLinter({
2730
client,
2831
owner: github.context.repo.owner,
2932
repo: github.context.repo.repo,
3033
number: github.context.issue.number,
3134
});
32-
await prLinter.validatePullRequestTarget(github.context.sha);
35+
await prLinter.validatePullRequestTarget(payload.pull_request.head.sha);
3336
}
3437
} catch (error: any) {
3538
core.setFailed(error.message);

tools/@aws-cdk/prlint/lint.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export class PullRequestLinter {
183183
const prs = await client.search.issuesAndPullRequests({
184184
q: sha,
185185
});
186+
console.log('Found PRs: ', prs);
186187
const foundPr = prs.data.items.find(pr => pr.state === 'open');
187188
if (foundPr) {
188189
// need to do this because the list PR response does not have
@@ -192,9 +193,9 @@ export class PullRequestLinter {
192193
repo,
193194
pull_number: foundPr.number,
194195
})).data;
195-
const latestCommit = pr.statuses_url.split('/').pop();
196+
console.log(`PR: ${foundPr.number}: `, pr);
196197
// only process latest commit
197-
if (latestCommit === sha) {
198+
if (pr.head.sha === sha) {
198199
return pr;
199200
}
200201
}

0 commit comments

Comments
 (0)