|
1 | | -name: Add Linked Issue Labels to PR |
| 1 | +name: Sync Linked Issue Labels - Apply |
2 | 2 |
|
3 | 3 | on: |
4 | | - workflow_dispatch: |
5 | | - inputs: |
6 | | - upstream_run_id: |
7 | | - description: "Upstream compute workflow run ID" |
8 | | - required: true |
9 | | - type: string |
10 | | - pr_number: |
11 | | - description: "Pull request number" |
12 | | - required: true |
13 | | - type: string |
14 | | - dry_run: |
15 | | - description: "Dry run flag" |
16 | | - required: false |
17 | | - type: string |
18 | | - default: "true" |
19 | | - is_fork_pr: |
20 | | - description: "Fork PR flag" |
21 | | - required: false |
22 | | - type: string |
23 | | - default: "false" |
24 | | -defaults: |
25 | | - run: |
26 | | - shell: bash |
27 | | -permissions: |
28 | | - actions: read |
29 | | - issues: write |
30 | | - contents: read |
31 | | - pull-requests: write |
| 4 | + workflow_run: |
| 5 | + workflows: ["Sync Linked Issue Labels - Compute"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.workflow }}-${{ github.event.workflow_run.id }} |
| 10 | + cancel-in-progress: true |
32 | 11 |
|
33 | 12 | jobs: |
34 | | - add-labels: |
35 | | - concurrency: |
36 | | - group: sync-issue-labels-pr-${{ github.event.inputs.pr_number }} |
37 | | - cancel-in-progress: true |
| 13 | + apply: |
| 14 | + if: github.event.workflow_run.conclusion == 'success' |
38 | 15 | runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + pull-requests: write |
| 18 | + |
39 | 19 | steps: |
40 | | - - name: Harden the runner |
41 | | - uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0 |
42 | | - with: |
43 | | - egress-policy: audit |
44 | | - |
45 | | - - name: Download labels artifact |
46 | | - id: download |
47 | | - continue-on-error: true |
48 | | - uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0 |
| 20 | + - name: Download Artifact |
| 21 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
49 | 22 | with: |
50 | | - name: pr-labels-${{ github.event.inputs.pr_number }} |
51 | | - path: artifacts |
52 | | - run-id: ${{ github.event.inputs.upstream_run_id }} |
| 23 | + name: pr-labels-data |
| 24 | + run-id: ${{ github.event.workflow_run.id }} |
53 | 25 | github-token: ${{ secrets.GITHUB_TOKEN }} |
54 | 26 |
|
55 | | - - name: Read labels payload |
56 | | - id: read |
57 | | - env: |
58 | | - INPUT_PR_NUMBER: ${{ github.event.inputs.pr_number }} |
59 | | - INPUT_IS_FORK_PR: ${{ github.event.inputs.is_fork_pr }} |
60 | | - INPUT_DRY_RUN: ${{ github.event.inputs.dry_run }} |
61 | | - run: | |
62 | | - labels_file="artifacts/labels.json" |
63 | | - if [ ! -f "$labels_file" ]; then |
64 | | - echo "::error::Labels artifact not found. Cross-workflow handoff is broken." |
65 | | - echo "labels=[]" >> "$GITHUB_OUTPUT" |
66 | | - echo "labels_count=0" >> "$GITHUB_OUTPUT" |
67 | | - echo "labels_multiline=" >> "$GITHUB_OUTPUT" |
68 | | - echo "pr_number=$INPUT_PR_NUMBER" >> "$GITHUB_OUTPUT" |
69 | | - echo "is_fork_pr=$INPUT_IS_FORK_PR" >> "$GITHUB_OUTPUT" |
70 | | - echo "dry_run=$INPUT_DRY_RUN" >> "$GITHUB_OUTPUT" |
71 | | - echo "source_event=workflow_dispatch" >> "$GITHUB_OUTPUT" |
72 | | - exit 1 |
73 | | - fi |
74 | | - labels=$(jq -c '.labels // []' "$labels_file") |
75 | | - pr_number=$(jq -r '.pr_number // 0' "$labels_file") |
76 | | - is_fork_pr=$(jq -r '.is_fork_pr // false' "$labels_file") |
77 | | - dry_run=$(jq -r '.dry_run // "true"' "$labels_file") |
78 | | - source_event=$(jq -r '.source_event // ""' "$labels_file") |
79 | | - labels_multiline=$(jq -r '.labels // [] | .[]' "$labels_file") |
80 | | - labels_count=$(echo "$labels" | jq 'length') |
81 | | - echo "labels=$labels" >> "$GITHUB_OUTPUT" |
82 | | - echo "labels_count=$labels_count" >> "$GITHUB_OUTPUT" |
83 | | - { |
84 | | - echo "labels_multiline<<EOF" |
85 | | - echo "$labels_multiline" |
86 | | - echo "EOF" |
87 | | - } >> "$GITHUB_OUTPUT" |
88 | | - echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT" |
89 | | - echo "is_fork_pr=$is_fork_pr" >> "$GITHUB_OUTPUT" |
90 | | - echo "dry_run=$dry_run" >> "$GITHUB_OUTPUT" |
91 | | - echo "source_event=$source_event" >> "$GITHUB_OUTPUT" |
92 | | -
|
93 | | - - name: Validate labels payload |
94 | | - id: validate |
95 | | - run: | |
96 | | - if [ "$PR_NUMBER" = "0" ] || [ "$(echo "$LABELS" | jq -r '. | length')" = "0" ]; then |
97 | | - echo "Invalid payload: pr_number=$PR_NUMBER or labels empty. Skipping label addition." |
98 | | - echo "valid_payload=false" >> "$GITHUB_OUTPUT" |
99 | | - else |
100 | | - echo "valid_payload=true" >> "$GITHUB_OUTPUT" |
101 | | - fi |
102 | | - env: |
103 | | - PR_NUMBER: ${{ steps.read.outputs.pr_number }} |
104 | | - LABELS: ${{ steps.read.outputs.labels }} |
105 | | - |
106 | | - - name: Determine if labels should be applied |
107 | | - id: should_apply |
108 | | - run: | |
109 | | - if [ "${{ steps.read.outputs.is_fork_pr }}" = "true" ]; then |
110 | | - echo "apply=false" >> "$GITHUB_OUTPUT" |
111 | | - echo "reason=fork PR" >> "$GITHUB_OUTPUT" |
112 | | - elif [ "${{ steps.validate.outputs.valid_payload }}" != "true" ]; then |
113 | | - echo "apply=false" >> "$GITHUB_OUTPUT" |
114 | | - echo "reason=invalid payload" >> "$GITHUB_OUTPUT" |
115 | | - elif [ "${{ steps.read.outputs.source_event }}" = "workflow_dispatch" ] && [ "${{ steps.read.outputs.dry_run }}" = "true" ]; then |
116 | | - echo "apply=false" >> "$GITHUB_OUTPUT" |
117 | | - echo "reason=dry run" >> "$GITHUB_OUTPUT" |
118 | | - else |
119 | | - echo "apply=true" >> "$GITHUB_OUTPUT" |
120 | | - echo "reason=" >> "$GITHUB_OUTPUT" |
121 | | - fi |
122 | | -
|
123 | | - - name: Add labels to PR |
124 | | - if: ${{ steps.should_apply.outputs.apply == 'true' }} |
125 | | - uses: actions-ecosystem/action-add-labels@1a9c3715c0037e96b97bb38cb4c4b56a1f1d4871 # main |
| 27 | + - name: Apply Labels |
| 28 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
126 | 29 | with: |
127 | | - github_token: ${{ secrets.GITHUB_TOKEN }} |
128 | | - labels: ${{ steps.read.outputs.labels_multiline }} |
129 | | - number: ${{ steps.read.outputs.pr_number }} |
| 30 | + script: | |
| 31 | + const fs = require('fs'); |
| 32 | + if (!fs.existsSync('labels.json')) { |
| 33 | + console.log("No payload file found. Nothing to apply."); |
| 34 | + return; |
| 35 | + } |
| 36 | +
|
| 37 | + const data = JSON.parse(fs.readFileSync('labels.json', 'utf8')); |
| 38 | + console.log(`Applying labels to PR #${data.pr_number}: ${data.labels.join(', ')}`); |
| 39 | + |
| 40 | + await github.rest.issues.addLabels({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + issue_number: data.pr_number, |
| 44 | + labels: data.labels |
| 45 | + }); |
| 46 | + console.log("Successfully applied labels!"); |
0 commit comments