-
Notifications
You must be signed in to change notification settings - Fork 131
64 lines (55 loc) · 2.34 KB
/
Copy pathauto-approve.yml
File metadata and controls
64 lines (55 loc) · 2.34 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
name: Auto Approve Workflow Runs
on:
pull_request_target:
types: [opened, synchronize, reopened]
jobs:
approve-workflows:
runs-on: ubuntu-latest
steps:
- name: Approve Action Required Workflows
uses: actions/github-script@v9
with:
# Note: A Personal Access Token (PAT) is REQUIRED to approve workflow runs.
# The default GITHUB_TOKEN does not have the necessary permissions.
github-token: ${{ secrets.WORKFLOW_APPROVER_PAT }}
script: |
const { owner, repo } = context.repo;
const pull_number = context.issue.number;
// Get the commit SHA for the PR head
const { data: pr } = await github.rest.pulls.get({
owner,
repo,
pull_number
});
const sha = pr.head.sha;
// Retry loop to allow workflows to transition to 'action_required' state
let runsToApprove = [];
const maxRetries = 6;
for (let i = 0; i < maxRetries; i++) {
console.log(`Checking for action_required workflows (attempt ${i + 1}/${maxRetries})...`);
await new Promise(r => setTimeout(r, 5000));
const { data: runs } = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
status: 'action_required'
});
// Filter runs associated with this specific PR's head commit
runsToApprove = runs.workflow_runs.filter(run => run.head_sha === sha);
if (runsToApprove.length > 0) {
break;
}
}
console.log(`Found ${runsToApprove.length} workflow runs requiring approval.`);
for (const run of runsToApprove) {
console.log(`Approving workflow run ${run.id}...`);
try {
await github.rest.actions.approveWorkflowRun({
owner,
repo,
run_id: run.id
});
console.log(`Successfully approved workflow run ${run.id}.`);
} catch (error) {
console.error(`Failed to approve workflow run ${run.id}:`, error.message);
}
}