forked from AndyMik90/Aperant
-
Notifications
You must be signed in to change notification settings - Fork 0
161 lines (141 loc) Β· 5.84 KB
/
Copy pathpr-status-gate.yml
File metadata and controls
161 lines (141 loc) Β· 5.84 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
155
156
157
158
159
160
161
name: PR Status Gate
on:
workflow_run:
workflows: [CI, Lint, Quality Security, Quality DCO, Quality Commit Lint]
types: [completed]
permissions:
pull-requests: write
actions: read
jobs:
update-status:
name: Update PR Status
runs-on: ubuntu-latest
# Only run if this workflow_run is associated with a PR
if: github.event.workflow_run.pull_requests[0] != null
timeout-minutes: 5
steps:
- name: Check all workflows and update label
uses: actions/github-script@v7
with:
retries: 3
retry-exempt-status-codes: 400,401,403,404,422
script: |
const { owner, repo } = context.repo;
const prNumber = context.payload.workflow_run.pull_requests[0].number;
const headSha = context.payload.workflow_run.head_sha;
const triggerWorkflow = context.payload.workflow_run.name;
// Required workflows configuration
const required = [
{ name: 'CI', file: 'ci.yml' },
{ name: 'Lint', file: 'lint.yml' },
{ name: 'Quality Security', file: 'quality-security.yml' },
{ name: 'Quality DCO', file: 'quality-dco.yml' },
{ name: 'Quality Commit Lint', file: 'quality-commit-lint.yml' }
];
const statusLabels = {
checking: 'π Checking',
passed: 'β
Ready for Review',
failed: 'β Checks Failed'
};
console.log(`::group::PR #${prNumber} - Checking workflow status`);
console.log(`Triggered by: ${triggerWorkflow}`);
console.log(`Head SHA: ${headSha}`);
console.log('');
let allComplete = true;
let anyFailed = false;
const results = [];
// Check all required workflows
for (const workflow of required) {
try {
const { data } = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflow.file,
head_sha: headSha,
per_page: 1
});
if (!data.workflow_runs.length) {
results.push({ name: workflow.name, status: 'β³ Pending', complete: false });
allComplete = false;
continue;
}
const run = data.workflow_runs[0];
if (run.status !== 'completed') {
results.push({ name: workflow.name, status: 'π Running', complete: false });
allComplete = false;
} else if (run.conclusion === 'success') {
results.push({ name: workflow.name, status: 'β
Passed', complete: true });
} else if (run.conclusion === 'skipped') {
results.push({ name: workflow.name, status: 'βοΈ Skipped', complete: true });
} else {
results.push({ name: workflow.name, status: 'β Failed', complete: true, failed: true });
anyFailed = true;
}
} catch (error) {
console.log(`::warning::Error checking ${workflow.name}: ${error.message}`);
results.push({ name: workflow.name, status: 'β οΈ Error', complete: false });
allComplete = false;
}
}
// Print results table
console.log('Workflow Status:');
console.log('β'.repeat(40));
for (const r of results) {
console.log(` ${r.status.padEnd(12)} ${r.name}`);
}
console.log('β'.repeat(40));
console.log('::endgroup::');
// Only update label if all workflows are complete
if (!allComplete) {
console.log('β³ Not all workflows complete yet - keeping current label');
return;
}
// Determine final label
const newLabel = anyFailed ? statusLabels.failed : statusLabels.passed;
console.log(`::group::Updating PR #${prNumber} label`);
// Remove old status labels
for (const label of Object.values(statusLabels)) {
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: label
});
console.log(` β Removed: ${label}`);
} catch (e) {
if (e.status !== 404) {
console.log(` β Could not remove ${label}: ${e.message}`);
}
}
}
// Add final status label
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: [newLabel]
});
console.log(` β Added: ${newLabel}`);
} catch (e) {
if (e.status === 404) {
core.warning(`Label '${newLabel}' does not exist. Please create it in repository settings.`);
}
throw e;
}
console.log('::endgroup::');
// Summary
if (anyFailed) {
console.log(`β PR #${prNumber} has failing checks`);
core.summary.addRaw(`## β PR #${prNumber} - Checks Failed\n\n`);
} else {
console.log(`β
PR #${prNumber} is ready for review`);
core.summary.addRaw(`## β
PR #${prNumber} - Ready for Review\n\n`);
}
// Add results to summary
core.summary.addTable([
[{data: 'Workflow', header: true}, {data: 'Status', header: true}],
...results.map(r => [r.name, r.status])
]);
await core.summary.write();