forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
154 lines (139 loc) · 6.42 KB
/
pre-commit-status.yml
File metadata and controls
154 lines (139 loc) · 6.42 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This needs to be in a separate workflow because it requires higher permissions than the calling workflow
name: Report Pre-commit Check Status
on:
workflow_run:
workflows: [Pre-commit hooks]
types:
- completed
permissions:
statuses: write
pull-requests: write
jobs:
report-success:
name: Report pre-commit success
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Report success
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const owner = '${{ github.repository_owner }}';
const repo = '${{ github.repository }}'.split('/')[1];
const sha = '${{ github.event.workflow_run.head_sha }}';
core.debug(`owner: ${owner}`);
core.debug(`repo: ${repo}`);
core.debug(`sha: ${sha}`);
const { context: name, state } = (await github.rest.repos.createCommitStatus({
context: 'Pre-commit checks',
description: 'Pre-commit checks successful',
owner: owner,
repo: repo,
sha: sha,
state: 'success',
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
})).data;
core.info(`${name} is ${state}`);
report-pending:
name: Report pre-commit pending
if: github.event.workflow_run.conclusion != 'success'
runs-on: ubuntu-latest
steps:
- name: Report pending
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const owner = '${{ github.repository_owner }}';
const repo = '${{ github.repository }}'.split('/')[1];
const sha = '${{ github.event.workflow_run.head_sha }}';
core.debug(`owner: ${owner}`);
core.debug(`repo: ${repo}`);
core.debug(`sha: ${sha}`);
const { context: name, state } = (await github.rest.repos.createCommitStatus({
context: 'Pre-commit checks',
description: 'The pre-commit checks need to be successful before merging',
owner: owner,
repo: repo,
sha: sha,
state: 'pending',
target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}'
})).data;
core.info(`${name} is ${state}`);
manage-labels:
name: Manage PR labels
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Download and Extract Artifacts
uses: dawidd6/action-download-artifact@07ab29fd4a977ae4d2b275087cf67563dfdf0295 # v9
continue-on-error: true
with:
run_id: ${{ github.event.workflow_run.id }}
name: pr-artifacts
path: ./pr-artifacts
- name: Get PR information
id: pr-info
run: |
if [ -f "./pr-artifacts/pr_number.txt" ]; then
pr_number=$(cat ./pr-artifacts/pr_number.txt | tr -cd '[:digit:]')
pre_commit_outcome=$(cat ./pr-artifacts/pre_commit_outcome.txt | tr -cd '[:alpha:]_')
pending_commit=$(cat ./pr-artifacts/pending_commit.txt | tr -cd '[:digit:]')
has_retrigger_label=$(cat ./pr-artifacts/has_retrigger_label.txt | tr -cd '[:alpha:]')
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
echo "pre_commit_outcome=$pre_commit_outcome" >> $GITHUB_OUTPUT
echo "pending_commit=$pending_commit" >> $GITHUB_OUTPUT
echo "has_retrigger_label=$has_retrigger_label" >> $GITHUB_OUTPUT
echo "artifacts_found=true" >> $GITHUB_OUTPUT
echo "PR number: $pr_number"
echo "Pre-commit outcome: $pre_commit_outcome"
echo "Pending commit: $pending_commit"
echo "Has retrigger label: $has_retrigger_label"
else
echo "No PR artifacts found"
echo "artifacts_found=false" >> $GITHUB_OUTPUT
fi
- name: Remove re-trigger label if it was present
if: |
steps.pr-info.outputs.artifacts_found == 'true' &&
steps.pr-info.outputs.has_retrigger_label == 'true'
continue-on-error: true
run: |
gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --remove-label 'Re-trigger Pre-commit Hooks'
env:
GH_TOKEN: ${{ github.token }}
- name: Add label if pre-commit fixes are required
if: |
steps.pr-info.outputs.artifacts_found == 'true' &&
steps.pr-info.outputs.pre_commit_outcome == 'failure' &&
steps.pr-info.outputs.pending_commit == '0'
continue-on-error: true
run: |
gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --add-label 'Status: Pre-commit fixes required ⚠️'
env:
GH_TOKEN: ${{ github.token }}
- name: Remove label if pre-commit was successful
if: |
steps.pr-info.outputs.artifacts_found == 'true' &&
steps.pr-info.outputs.pre_commit_outcome == 'success'
continue-on-error: true
run: |
gh pr edit ${{ steps.pr-info.outputs.pr_number }} --repo ${{ github.repository }} --remove-label 'Status: Pre-commit fixes required ⚠️'
env:
GH_TOKEN: ${{ github.token }}
- name: Comment on PR about pre-commit failures
if: |
steps.pr-info.outputs.artifacts_found == 'true' &&
steps.pr-info.outputs.pre_commit_outcome == 'failure' &&
steps.pr-info.outputs.pending_commit == '0'
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
with:
pr-number: ${{ steps.pr-info.outputs.pr_number }}
message: |
## ⚠️ Pre-commit Hooks Failed
Some pre-commit hooks failed and require manual fixes. Please see the detailed error report below.
**What to do:**
1. 📋 [**View the detailed error report**](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}) to see which hooks failed
2. 🔧 Fix the issues locally in your code
3. 💾 Commit and push your changes
4. 🔄 The hooks will run again automatically
**Need help?** Ask in the comments below.