-
-
Notifications
You must be signed in to change notification settings - Fork 210
167 lines (146 loc) · 5.71 KB
/
sync-project-status.yml
File metadata and controls
167 lines (146 loc) · 5.71 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
name: Sync Project Status
on:
issues:
types: [labeled]
jobs:
update-project:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
repository-projects: write
steps:
- name: Update project status from label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PROJECT_AUTOMATION_TOKEN != '' && secrets.PROJECT_AUTOMATION_TOKEN || github.token }}
script: |
const labelMap = {
"backlog": "Backlog",
"needs discussion": "Needs discussion",
"approved": "Approved",
"ready": "Ready",
"in progress": "In progress",
"in review": "In review",
"done": "Done"
};
const label = context.payload.label.name.toLowerCase();
const targetOptionName = labelMap[label];
if (!targetOptionName) return;
const issueNodeId = context.payload.issue.node_id;
const configuredProjectId = "PVT_kwHOBeIeKs4AfmUO";
const fieldId = "PVTSSF_lAHOBeIeKs4AfmUOzgU5pCI";
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function fetchIssueProjectItems() {
const result = await github.graphql(`
query($issueId: ID!) {
node(id: $issueId) {
... on Issue {
projectItems(first: 100) {
nodes {
id
project {
id
title
}
}
}
}
}
}
`, {
issueId: issueNodeId
});
return result.node?.projectItems?.nodes ?? [];
}
// Retry a few times in case project membership is still syncing.
let issueProjectItems = [];
for (let attempt = 1; attempt <= 4; attempt++) {
issueProjectItems = await fetchIssueProjectItems();
if (issueProjectItems.length > 0) break;
if (attempt < 4) await sleep(3000);
}
let exactMatch = issueProjectItems.find(
(node) => node.project?.id === configuredProjectId
);
let itemId = exactMatch?.id;
let projectId = exactMatch?.project?.id;
// If issue is not in the configured project yet, try to add it.
if (!itemId) {
try {
const added = await github.graphql(`
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: { projectId: $projectId, contentId: $contentId }) {
item {
id
}
}
}
`, {
projectId: configuredProjectId,
contentId: issueNodeId
});
itemId = added.addProjectV2ItemById?.item?.id;
projectId = configuredProjectId;
} catch (error) {
const availableProjects = issueProjectItems
.map((node) => `${node.project?.title ?? "(untitled)"} [${node.project?.id ?? "no-id"}]`)
.join(", ");
console.log(`Issue not in configured project ${configuredProjectId}. Available: ${availableProjects || "none"}`);
console.log(`Failed to auto-add issue to configured project: ${error.message}`);
return;
}
}
if (!itemId || !projectId) {
const availableProjects = issueProjectItems
.map((node) => `${node.project?.title ?? "(untitled)"} [${node.project?.id ?? "no-id"}]`)
.join(", ");
console.log(`Issue not in configured project ${configuredProjectId}. Available: ${availableProjects || "none"}`);
return;
}
const fieldResult = await github.graphql(`
query($fieldId: ID!) {
node(id: $fieldId) {
... on ProjectV2SingleSelectField {
id
options {
id
name
}
}
}
}
`, {
fieldId: fieldId
});
const options = fieldResult.node?.options ?? [];
const selectedOption = options.find(
(option) => option.name?.toLowerCase() === targetOptionName.toLowerCase()
);
if (!selectedOption?.id) {
const availableOptions = options.map((option) => option.name).join(", ");
console.log(`Could not find option '${targetOptionName}' for field ${fieldId}. Available options: ${availableOptions || "none"}`);
return;
}
// update status field
await github.graphql(`
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $optionId }
}
) {
projectV2Item {
id
}
}
}
`, {
projectId: projectId,
itemId: itemId,
fieldId: fieldId,
optionId: selectedOption.id
});