forked from bpg/terraform-provider-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
328 lines (292 loc) · 12.1 KB
/
Copy pathproject-automation.yml
File metadata and controls
328 lines (292 loc) · 12.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
name: Project Automation
on:
issues:
types: [opened, labeled, closed, reopened]
pull_request_target:
types: [opened, closed]
workflow_dispatch:
env:
PROJECT_NUMBER: 3
PROJECT_OWNER: bpg
jobs:
add-to-project:
name: Add issue/PR to project
runs-on: ubuntu-latest
if: github.event.action == 'opened'
steps:
- name: Add to project
uses: actions/add-to-project@5afcf98fcd03f1c2f92c3c83f58ae24323cc57fd # v2
with:
project-url: https://github.com/orgs/${{ env.PROJECT_OWNER }}/projects/${{ env.PROJECT_NUMBER }}
github-token: ${{ secrets.PROJECT_TOKEN }}
sync-priority-label-to-field:
name: Sync priority label to Priority field
runs-on: ubuntu-latest
if: github.event.action == 'labeled' && startsWith(github.event.label.name, 'priority:')
steps:
- name: Get priority from label
id: priority
run: |
LABEL="${{ github.event.label.name }}"
PRIORITY="${LABEL#priority:}"
echo "value=$PRIORITY" >> $GITHUB_OUTPUT
echo "Priority: $PRIORITY"
- name: Remove other priority labels
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const newLabel = '${{ github.event.label.name }}';
const issueNumber = context.payload.issue.number;
const allPriorityLabels = ['priority:P0', 'priority:P1', 'priority:P2', 'priority:P3'];
for (const label of allPriorityLabels) {
if (label !== newLabel) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
name: label
});
console.log(`Removed ${label}`);
} catch (e) {
// Label might not exist on issue, that's ok
}
}
}
- name: Update Priority field in project
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
github-token: ${{ secrets.PROJECT_TOKEN }}
script: |
const priority = '${{ steps.priority.outputs.value }}';
const issueNodeId = context.payload.issue.node_id;
// Get project and field info
const projectQuery = `
query($owner: String!, $number: Int!) {
organization(login: $owner) {
projectV2(number: $number) {
id
field(name: "Priority") {
... on ProjectV2SingleSelectField {
id
options { id name }
}
}
}
}
}
`;
const projectResult = await github.graphql(projectQuery, {
owner: '${{ env.PROJECT_OWNER }}',
number: ${{ env.PROJECT_NUMBER }}
});
const project = projectResult.organization.projectV2;
const priorityField = project.field;
if (!priorityField) {
console.log('Priority field not found');
return;
}
const option = priorityField.options.find(o => o.name === priority);
if (!option) {
console.log(`Priority option ${priority} not found`);
return;
}
// Add issue to project (or get existing item ID)
const addMutation = `
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {
projectId: $projectId
contentId: $contentId
}) {
item { id }
}
}
`;
let itemId;
try {
const addResult = await github.graphql(addMutation, {
projectId: project.id,
contentId: issueNodeId
});
itemId = addResult.addProjectV2ItemById.item.id;
console.log(`Added/found issue in project, item ID: ${itemId}`);
} catch (e) {
console.log(`Error adding to project: ${e.message}`);
return;
}
// Update the Priority field
const mutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $optionId }
}) {
projectV2Item { id }
}
}
`;
await github.graphql(mutation, {
projectId: project.id,
itemId: itemId,
fieldId: priorityField.id,
optionId: option.id
});
console.log(`Updated Priority to ${priority}`);
move-to-column-on-label:
name: Move to column based on label
runs-on: ubuntu-latest
if: github.event.action == 'labeled'
steps:
- name: Determine target column
id: column
run: |
LABEL="${{ github.event.label.name }}"
case "$LABEL" in
"priority:P0") echo "column=P0" >> $GITHUB_OUTPUT ;;
"priority:P1") echo "column=P1" >> $GITHUB_OUTPUT ;;
"priority:P2") echo "column=Triage" >> $GITHUB_OUTPUT ;;
"priority:P3") echo "column=Backlog" >> $GITHUB_OUTPUT ;;
"lifecycle:needs-info") echo "column=Needs Info" >> $GITHUB_OUTPUT ;;
"status:in-progress") echo "column=In Progress" >> $GITHUB_OUTPUT ;;
"status:blocked") echo "column=Blocked" >> $GITHUB_OUTPUT ;;
*) echo "column=" >> $GITHUB_OUTPUT ;;
esac
- name: Add to project and move to column
if: steps.column.outputs.column != ''
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
github-token: ${{ secrets.PROJECT_TOKEN }}
script: |
const targetColumn = '${{ steps.column.outputs.column }}';
const issueNodeId = context.payload.issue.node_id;
// Get project info
const projectQuery = `
query($owner: String!, $number: Int!) {
organization(login: $owner) {
projectV2(number: $number) {
id
field(name: "Status") {
... on ProjectV2SingleSelectField {
id
options { id name }
}
}
}
}
}
`;
const projectResult = await github.graphql(projectQuery, {
owner: '${{ env.PROJECT_OWNER }}',
number: ${{ env.PROJECT_NUMBER }}
});
const project = projectResult.organization.projectV2;
const statusField = project.field;
const option = statusField.options.find(o => o.name === targetColumn);
if (!option) {
console.log(`Column ${targetColumn} not found`);
return;
}
// First, try to add the issue to the project (will return existing item if already added)
const addMutation = `
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {
projectId: $projectId
contentId: $contentId
}) {
item { id }
}
}
`;
let itemId;
try {
const addResult = await github.graphql(addMutation, {
projectId: project.id,
contentId: issueNodeId
});
itemId = addResult.addProjectV2ItemById.item.id;
console.log(`Added/found issue in project, item ID: ${itemId}`);
} catch (e) {
console.log(`Error adding to project: ${e.message}`);
return;
}
// Update Status field
const updateMutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $optionId }
}) {
projectV2Item { id }
}
}
`;
await github.graphql(updateMutation, {
projectId: project.id,
itemId: itemId,
fieldId: statusField.id,
optionId: option.id
});
console.log(`Moved to ${targetColumn}`);
pr-link-to-in-progress:
name: Move linked issue to In Progress when PR opened
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_target' && github.event.action == 'opened'
steps:
- name: Add in-progress label to linked issues
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prBody = context.payload.pull_request.body || '';
const prTitle = context.payload.pull_request.title || '';
const issuePattern = /(?:fixes|closes|resolves|ref|references?)?\s*#(\d+)/gi;
const allText = `${prTitle} ${prBody}`;
const matches = [...allText.matchAll(issuePattern)];
const uniqueIssues = [...new Set(matches.map(m => parseInt(m[1])))];
for (const issueNumber of uniqueIssues) {
console.log(`PR references issue #${issueNumber}, adding status:in-progress label`);
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
labels: ['status:in-progress']
});
} catch (e) {
console.log(`Could not add label to issue #${issueNumber}: ${e.message}`);
}
}
pr-merged-to-review:
name: Move linked issue to Review when PR merged
runs-on: ubuntu-latest
if: github.event_name == 'pull_request_target' && github.event.action == 'closed' && github.event.pull_request.merged == true
steps:
- name: Update linked issues
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prBody = context.payload.pull_request.body || '';
const prTitle = context.payload.pull_request.title || '';
const issuePattern = /(?:fixes|closes|resolves|ref|references?)?\s*#(\d+)/gi;
const allText = `${prTitle} ${prBody}`;
const matches = [...allText.matchAll(issuePattern)];
const uniqueIssues = [...new Set(matches.map(m => parseInt(m[1])))];
for (const issueNumber of uniqueIssues) {
console.log(`PR merged, removing status:in-progress from #${issueNumber}`);
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
name: 'status:in-progress'
});
} catch (e) {
// Label might not exist
}
}