Skip to content

Commit

Permalink
chore: Make ready-for-e2e label check more robust (#15536)
Browse files Browse the repository at this point in the history
* chore: Make ready-for-e2e label check more robust

* fixed owner issue

* Try that again

* multi-step check for PR

* added catch

* Using the output from changes

* more fixes

* logging the sha

* Attemptig to fix the sha problem

* Cleanup

* Cleanup

* Testing with the same sha reference as checkout

* Reverted back

* cleanup
  • Loading branch information
keithwillcode authored Jun 22, 2024
1 parent 7ff255d commit a31cf99
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
pull-requests: read
outputs:
has-files-requiring-all-checks: ${{ steps.filter.outputs.has-files-requiring-all-checks }}
commit-sha: ${{ steps.get_sha.outputs.commit-sha }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/dangerous-git-checkout
Expand All @@ -28,59 +29,57 @@ jobs:
filters: |
has-files-requiring-all-checks:
- "!(**.md|.github/CODEOWNERS)"
- name: Get Latest Commit SHA
id: get_sha
run: |
echo "commit-sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
check-label:
needs: [changes]
runs-on: buildjet-2vcpu-ubuntu-2204
name: Check for E2E label
outputs:
run-e2e: ${{ steps.check-if-pr-has-label.outputs.run-e2e == 'true' && (github.event_name != 'labeled' || (github.event_name == 'labeled' && github.event.label.name == 'ready-for-e2e')) }}
run-jobs: ${{ github.event_name != 'labeled' }}
run-e2e: ${{ steps.check-if-pr-has-label.outputs.run-e2e == 'true' && (github.event.action != 'labeled' || (github.event.action == 'labeled' && github.event.label.name == 'ready-for-e2e')) }}
run-jobs: ${{ github.event.action != 'labeled' }}
steps:
- name: Get PR from branch name
- name: Check if PR exists with ready-for-e2e label for this SHA
id: check-if-pr-has-label
uses: actions/github-script@v7
with:
script: |
let pr;
console.log('github.event_name', '${{ github.event_name }}');
const parsedEvent = ${{ github.event }};
if (parsedEvent && parsedEvent.pull_request) {
const response = await github.rest.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: parsedEvent.pull_request.number
});
pr = response.data;
} else {
const ref = '${{ github.ref }}';
const branch = ref.replace('refs/heads/', '');
console.log('ref', ref);
console.log('branch', branch);
const response = await github.rest.pulls.list({
console.log('github.event.action', '${{ github.event.action }}');
try {
const sha = '${{ needs.changes.outputs.commit-sha }}';
console.log('sha', sha);
const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${context.repo.owner}:${branch}`
commit_sha: sha
});
if (response.data.length > 0) {
pr = response.data[0];
if (prs.length === 0) {
core.setOutput('run-e2e', false);
console.log(`No pull requests found for commit SHA ${sha}`);
return;
}
}
if (!pr) {
const pr = prs[0];
console.log(`PR number: ${pr.number}`);
console.log(`PR title: ${pr.title}`);
console.log(`PR state: ${pr.state}`);
console.log(`PR URL: ${pr.html_url}`);
const labels = pr.labels.map(label => label.name);
const labelFound = labels.includes('ready-for-e2e');
console.log('PR #', pr.number);
console.log('Found the label?', labelFound);
core.setOutput('run-e2e', labelFound);
}
catch (e) {
core.setOutput('run-e2e', false);
console.log('No PR found');
return;
console.log(e);
}
const labels = pr.labels.map(label => label.name);
const labelFound = labels.includes('ready-for-e2e');
console.log('PR #', pr.number);
console.log('Found the label?', labelFound);
core.setOutput('run-e2e', labelFound);
type-check:
name: Type check
needs: [changes, check-label]
Expand Down

0 comments on commit a31cf99

Please sign in to comment.