-
Notifications
You must be signed in to change notification settings - Fork 0
175 lines (149 loc) · 5.76 KB
/
gc-auto-approve.yml
File metadata and controls
175 lines (149 loc) · 5.76 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: GC Auto Approve
on:
workflow_run:
workflows: ["Code Quality", "Build and Push F1 API", "CodeQL"]
types: [completed]
permissions:
contents: read
checks: read
pull-requests: write
jobs:
approve-pr:
runs-on: ubuntu-latest
if: ${{ secrets.GC_PAT != '' }}
steps:
- name: GC Approval
uses: actions/github-script@v8
with:
github-token: ${{ secrets.GC_PAT }}
script: |
const run = context.payload.workflow_run;
if (run.event !== 'pull_request') {
core.info(`Skipping: workflow_run event is '${run.event}', not 'pull_request'.`);
return;
}
if (!run.pull_requests || run.pull_requests.length === 0) {
core.info('Skipping: no pull request found on workflow_run payload.');
return;
}
const pullNumber = run.pull_requests[0].number;
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber
});
if (pr.data.state !== 'open') {
core.info(`Skipping: PR #${pullNumber} is not open.`);
return;
}
const headSha = pr.data.head.sha;
const authUser = await github.rest.users.getAuthenticated();
const reviewerLogin = authUser.data.login;
const existingReviews = await github.paginate(github.rest.pulls.listReviews, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber,
per_page: 100
});
const hasApprovalForHead = existingReviews.some((review) =>
review.user?.login === reviewerLogin &&
review.state === 'APPROVED' &&
review.commit_id === headSha
);
if (hasApprovalForHead) {
core.info(`Skipping: ${reviewerLogin} already approved head commit ${headSha}.`);
return;
}
const checkRuns = await github.paginate(github.rest.checks.listForRef, {
owner: context.repo.owner,
repo: context.repo.repo,
ref: headSha,
per_page: 100
});
// Exclude this workflow's own check run to avoid self-blocking while in progress.
const relevantRuns = checkRuns.filter((check) => !check.name.startsWith('GC Auto Approve /'));
if (relevantRuns.length === 0) {
core.info('Skipping: no relevant check runs found for PR head commit.');
return;
}
const notCompleted = relevantRuns.filter((check) => check.status !== 'completed');
if (notCompleted.length > 0) {
core.info(`Skipping: waiting for checks to complete: ${notCompleted.map((c) => c.name).join(', ')}`);
return;
}
const invalidConclusions = relevantRuns.filter(
(check) => check.conclusion !== 'success' && check.conclusion !== 'skipped'
);
if (invalidConclusions.length > 0) {
core.info(
`Skipping: checks not in success/skipped state: ${invalidConclusions
.map((c) => `${c.name}=${c.conclusion}`)
.join(', ')}`
);
return;
}
const unresolvedQuery = `
query($owner: String!, $repo: String!, $pullNumber: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $pullNumber) {
reviewThreads(first: 100, after: $cursor) {
nodes {
isResolved
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
`;
let unresolvedThreadCount = 0;
let cursor = null;
do {
const response = await github.graphql(unresolvedQuery, {
owner: context.repo.owner,
repo: context.repo.repo,
pullNumber,
cursor
});
const threads = response.repository.pullRequest.reviewThreads;
unresolvedThreadCount += threads.nodes.filter((thread) => !thread.isResolved).length;
cursor = threads.pageInfo.hasNextPage ? threads.pageInfo.endCursor : null;
} while (cursor);
if (unresolvedThreadCount > 0) {
core.info(`Skipping: unresolved review threads: ${unresolvedThreadCount}`);
return;
}
const verifiedChecks = relevantRuns.map((check) => ({
name: check.name,
conclusion: check.conclusion
}));
const body = [
'GC Agent Auto-Review',
'',
'Verified checks:',
...verifiedChecks.map((check) => `- ${check.name}: ${check.conclusion}`),
'- All review comments resolved',
'',
'```json',
JSON.stringify(
{
decision: 'APPROVE',
verifiedChecks,
unresolvedReviewThreads: unresolvedThreadCount,
headSha
},
null,
2
),
'```'
].join('\n');
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber,
event: 'APPROVE',
body
});