|
1 | 1 | name: Ready to Merge Workflow |
2 | 2 | on: |
3 | 3 | workflow_call: |
4 | | - inputs: |
5 | | - labels: |
6 | | - description: The GitHub event's labels |
7 | | - required: true |
8 | | - type: string |
9 | | - is-pr: |
10 | | - description: Whether the workflow is running on a PR |
11 | | - required: true |
12 | | - type: boolean |
13 | 4 |
|
14 | 5 | jobs: |
15 | 6 | ready-to-merge-gate: |
16 | 7 | name: 'Missing "ready-to-merge" label' |
17 | 8 | runs-on: ubuntu-latest |
18 | 9 | steps: |
19 | 10 | - name: Check for exact 'ready-to-merge' label |
20 | | - if: ${{ inputs.is-pr }} |
| 11 | + if: ${{ github.event_name == 'pull_request' }} |
21 | 12 | id: check-label |
22 | | - run: | |
23 | | - # Use jq to check for exact label match (avoids substring matching issues with contains()) |
24 | | - if echo '${{ inputs.labels }}' | jq -e '.[] | select(.name == "ready-to-merge")' > /dev/null; then |
25 | | - echo "label_found=true" >> $GITHUB_OUTPUT |
26 | | - else |
27 | | - echo "label_found=false" >> $GITHUB_OUTPUT |
28 | | - fi |
| 13 | + uses: actions/github-script@v7 |
| 14 | + with: |
| 15 | + script: | |
| 16 | + const expectedLabel = 'ready-to-merge'; |
| 17 | +
|
| 18 | + // Fetch labels from GitHub API |
| 19 | + // GitHub Actions may not contain the labels in the event, so we need to fetch them from the API. |
| 20 | + const { data: labels } = await github.rest.issues.listLabelsOnIssue({ |
| 21 | + owner: context.repo.owner, |
| 22 | + repo: context.repo.repo, |
| 23 | + issue_number: context.issue.number |
| 24 | + }); |
| 25 | +
|
| 26 | + const labelNames = labels.map(label => label.name); |
| 27 | + console.log(`Found labels: ${labelNames.join(', ')}`); |
| 28 | +
|
| 29 | + // Check for exact 'ready-to-merge' label match |
| 30 | + const labelFound = labelNames.includes(expectedLabel); |
| 31 | + core.setOutput('label_found', labelFound.toString()); |
29 | 32 | # Since Github also accepts `skipped` results for Required Workflows, we need |
30 | 33 | # to fail the job to ensure that the downstream jobs don't just skip. |
31 | 34 | - name: Fail until the 'ready-to-merge' label is present |
32 | | - if: ${{ inputs.is-pr && steps.check-label.outputs.label_found == 'false' }} |
| 35 | + if: ${{ github.event_name == 'pull_request' && steps.check-label.outputs.label_found == 'false' }} |
33 | 36 | run: | |
34 | 37 | echo "Add the 'ready-to-merge' label to run CI." |
35 | 38 | exit 1 |
36 | 39 | - name: Gate passed |
37 | | - if: ${{ inputs.is-pr && steps.check-label.outputs.label_found == 'true' }} |
| 40 | + if: ${{ github.event_name == 'pull_request' && steps.check-label.outputs.label_found == 'true' }} |
38 | 41 | run: echo "Label present. Proceeding with CI." |
39 | 42 | - name: Gate passed |
40 | | - if: ${{ !inputs.is-pr }} |
| 43 | + if: ${{ github.event_name != 'pull_request' }} |
41 | 44 | run: echo "Not a PR. Proceeding with CI." |
0 commit comments