-
Notifications
You must be signed in to change notification settings - Fork 1.6k
84 lines (77 loc) · 3.09 KB
/
add-ci-passed-label.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This workflow adds the 'ci-passed' label to a pull request once the 'CI Check' workflow completes successfully.
# Resets the 'ci-passed' label status when a pull request is synchronized or reopened,
# indicating that changes have been pushed and CI needs to rerun.
name: Add CI Passed Label
on:
workflow_run:
workflows: ["CI Check"]
types:
- completed
permissions:
pull-requests: write
checks: read
actions: read
jobs:
fetch_data:
name: Fetch workflow payload
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
outputs:
pr_number: ${{ steps.extract.outputs.pr_number }}
event_action: ${{ steps.extract.outputs.event_action }}
steps:
- name: 'Download artifact'
uses: actions/[email protected]
with:
script: |
var artifacts = await github.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id}},
});
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr"
})[0];
var download = await github.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data));
- name: Unzip artifact
run: unzip pr.zip
- name: Extract PR information
id: extract
run: |
pr_number=$(cat ./pr_number)
event_action=$(cat ./event_action)
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
echo "event_action=${event_action}" >> $GITHUB_OUTPUT
reset_ci_passed_label:
name: Reset 'ci-passed' label on PR Synchronization
runs-on: ubuntu-latest
needs: fetch_data
steps:
- name: Check and reset label
run: |
if [[ "${{ needs.fetch_data.outputs.event_action }}" == "synchronize" || "${{ needs.fetch_data.outputs.event_action }}" == "reopened" ]]; then
echo "Resetting 'ci-passed' label as changes were pushed (event: ${{ needs.fetch_data.outputs.event_action }})."
gh pr edit ${{ needs.fetch_data.outputs.pr_number }} --remove-label "ci-passed" --repo $GITHUB_REPOSITORY || echo "Label not present"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
add_ci_passed_label:
name: Add 'ci-passed' label
runs-on: ubuntu-latest
needs: [fetch_data, reset_ci_passed_label]
steps:
- name: Add 'ci-passed' label
run: |
echo "Adding 'ci-passed' label to PR #${{ needs.fetch_data.outputs.pr_number }}"
gh pr edit ${{ needs.fetch_data.outputs.pr_number }} --add-label "ci-passed" --repo $GITHUB_REPOSITORY
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}