forked from baserow/baserow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-projects-pr-workflow.yml
More file actions
283 lines (249 loc) · 11.9 KB
/
database-projects-pr-workflow.yml
File metadata and controls
283 lines (249 loc) · 11.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
name: Database Team PR Automation
env:
PROJECT_NUMBER: '3'
DOMAIN_LABELS: '["domain::database"]'
STATUS_FIELD_NAME: 'Status'
REVIEW_STATUS_FIELD_NAME: 'Review Status'
STATUS_IN_PROGRESS: 'In Progress'
STATUS_IN_REVIEW: 'In Review'
STATUS_DONE: 'Done'
REVIEW_AWAITING: 'Awaiting'
REVIEW_FEEDBACK: 'Feedback'
REVIEW_MERGE: 'Merge'
on:
pull_request:
types: [opened, reopened, ready_for_review, converted_to_draft, review_requested, review_request_removed, synchronize, labeled, closed]
pull_request_review:
types: [submitted, dismissed]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to process'
required: true
type: number
jobs:
update-status:
runs-on: ubuntu-latest
steps:
- name: Update PR Project Status
uses: actions/github-script@v7
with:
github-token: ${{ secrets.DATABASE_PROJECT_WORKFLOW_TOKEN }}
script: |
const prNumber = context.eventName === 'workflow_dispatch'
? ${{ github.event.inputs.pr_number || 0 }}
: context.payload.pull_request.number;
console.log(`Processing PR #${prNumber} (event: ${context.eventName}/${context.payload.action || 'manual'})`);
// ============================================================
// FETCH ALL DATA WITH SINGLE GRAPHQL QUERY
// ============================================================
const data = await github.graphql(`
query($owner: String!, $repo: String!, $pr: Int!, $projectNumber: Int!) {
organization(login: $owner) {
projectV2(number: $projectNumber) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options { id, name }
}
}
}
}
}
repository(owner: $owner, name: $repo) {
pullRequest(number: $pr) {
id
isDraft
merged
mergeable
reviewDecision
labels(first: 20) {
nodes { name }
}
reviewRequests(first: 10) {
nodes {
requestedReviewer {
... on User { login }
... on Team { name }
}
}
}
latestOpinionatedReviews(first: 10) {
nodes {
state
author { login }
}
}
closingIssuesReferences(first: 10) {
nodes { id, number }
}
}
}
}
`, {
owner: context.repo.owner,
repo: context.repo.repo,
pr: prNumber,
projectNumber: parseInt('${{ env.PROJECT_NUMBER }}')
});
const project = data.organization.projectV2;
const pr = data.repository.pullRequest;
// ============================================================
// CHECK DOMAIN LABELS (early exit if not relevant)
// ============================================================
const labels = pr.labels.nodes.map(l => l.name);
const domainLabels = ${{ env.DOMAIN_LABELS }};
const hasDomainLabel = labels.some(label =>
domainLabels.some(domain => label.startsWith(domain))
);
if (!hasDomainLabel) {
console.log(`PR #${prNumber} has no domain label (${labels.join(', ') || 'none'}), skipping`);
return;
}
console.log(`PR has domain label: ${labels.filter(l => l.startsWith('domain::')).join(', ')}`);
const statusField = project.fields.nodes.find(f => f.name === '${{ env.STATUS_FIELD_NAME }}');
const reviewStatusField = project.fields.nodes.find(f => f.name === '${{ env.REVIEW_STATUS_FIELD_NAME }}');
const fieldOptions = {
status: {
id: statusField.id,
'In Progress': statusField.options.find(o => o.name === '${{ env.STATUS_IN_PROGRESS }}')?.id,
'In Review': statusField.options.find(o => o.name === '${{ env.STATUS_IN_REVIEW }}')?.id,
'Done': statusField.options.find(o => o.name === '${{ env.STATUS_DONE }}')?.id
},
reviewStatus: {
id: reviewStatusField.id,
'Awaiting': reviewStatusField.options.find(o => o.name === '${{ env.REVIEW_AWAITING }}')?.id,
'Feedback': reviewStatusField.options.find(o => o.name === '${{ env.REVIEW_FEEDBACK }}')?.id,
'Merge': reviewStatusField.options.find(o => o.name === '${{ env.REVIEW_MERGE }}')?.id
}
};
console.log(`PR state: isDraft=${pr.isDraft}, merged=${pr.merged}, reviewDecision=${pr.reviewDecision}, mergeable=${pr.mergeable}`);
// ============================================================
// HELPER FUNCTIONS
// ============================================================
async function getProjectItem(contentId) {
const result = await github.graphql(`
query($contentId: ID!) {
node(id: $contentId) {
... on Issue { projectItems(first: 10) { nodes { id, project { id } } } }
... on PullRequest { projectItems(first: 10) { nodes { id, project { id } } } }
}
}
`, { contentId });
const item = result.node.projectItems.nodes.find(i => i.project.id === project.id);
return item ? item.id : null;
}
async function addToProject(contentId) {
const result = await github.graphql(`
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: { projectId: $projectId, contentId: $contentId }) {
item { id }
}
}
`, { projectId: project.id, contentId });
return result.addProjectV2ItemById.item.id;
}
async function ensureInProject(contentId) {
let itemId = await getProjectItem(contentId);
if (!itemId) {
console.log('Adding item to project...');
itemId = await addToProject(contentId);
}
return itemId;
}
async function updateField(itemId, fieldId, optionId) {
if (!optionId) return console.log('Warning: Option ID undefined, skipping');
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: ProjectV2FieldValue!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId, itemId: $itemId, fieldId: $fieldId, value: $value
}) { projectV2Item { id } }
}
`, { projectId: project.id, itemId, fieldId, value: { singleSelectOptionId: optionId } });
}
async function clearField(itemId, fieldId) {
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!) {
clearProjectV2ItemFieldValue(input: { projectId: $projectId, itemId: $itemId, fieldId: $fieldId })
{ projectV2Item { id } }
}
`, { projectId: project.id, itemId, fieldId });
}
// ============================================================
// CALCULATE STATE
// ============================================================
let status, reviewStatus;
// Check if PR was merged
if (pr.merged) {
status = 'Done';
reviewStatus = null;
console.log('PR merged → Done, clear Review Status');
} else if (pr.isDraft) {
status = 'In Progress';
reviewStatus = null;
console.log('Draft PR → In Progress, clear Review Status');
} else {
status = 'In Review';
// Get pending reviewer identifiers (login for users, name for teams)
const pendingReviewers = pr.reviewRequests.nodes
.map(r => r.requestedReviewer?.login || r.requestedReviewer?.name)
.filter(Boolean);
// Get reviewers who requested changes
const changesRequestedBy = pr.latestOpinionatedReviews.nodes
.filter(r => r.state === 'CHANGES_REQUESTED')
.map(r => r.author.login);
// Check if any "changes requested" reviewer has been re-requested
const changesRequestedReviewerReRequested = changesRequestedBy.some(
reviewer => pendingReviewers.includes(reviewer)
);
if (pr.reviewDecision === 'CHANGES_REQUESTED' && changesRequestedReviewerReRequested) {
reviewStatus = 'Awaiting';
console.log(`Changes requested but reviewer re-requested (${changesRequestedBy.join(', ')}) → Awaiting`);
} else if (pr.reviewDecision === 'CHANGES_REQUESTED') {
reviewStatus = 'Feedback';
console.log('Changes requested → Feedback');
} else if (pendingReviewers.length > 0) {
reviewStatus = 'Awaiting';
console.log(`Has pending reviewers (${pendingReviewers.join(', ')}) → Awaiting`);
} else {
// No pending reviewers - check if at least one approval exists
const hasApproval = pr.latestOpinionatedReviews.nodes.some(r => r.state === 'APPROVED');
if (hasApproval) {
reviewStatus = 'Merge';
console.log('No pending reviewers + has approval → Merge');
} else {
reviewStatus = 'Awaiting';
console.log('No pending reviewers, no approval yet → Awaiting');
}
}
}
// ============================================================
// UPDATE PROJECT FIELDS
// ============================================================
const prItemId = await ensureInProject(pr.id);
console.log(`PR project item: ${prItemId}`);
await updateField(prItemId, fieldOptions.status.id, fieldOptions.status[status]);
console.log(`Status → ${status}`);
if (reviewStatus) {
await updateField(prItemId, fieldOptions.reviewStatus.id, fieldOptions.reviewStatus[reviewStatus]);
console.log(`Review Status → ${reviewStatus}`);
} else {
await clearField(prItemId, fieldOptions.reviewStatus.id);
console.log('Review Status → cleared');
}
// ============================================================
// UPDATE LINKED ISSUES
// ============================================================
const linkedIssues = pr.closingIssuesReferences.nodes;
if (linkedIssues.length > 0) {
console.log(`Updating ${linkedIssues.length} linked issue(s)...`);
for (const issue of linkedIssues) {
const itemId = await ensureInProject(issue.id);
await updateField(itemId, fieldOptions.status.id, fieldOptions.status[status]);
console.log(` Issue #${issue.number} → ${status}`);
}
}
console.log('Completed!');