-
Notifications
You must be signed in to change notification settings - Fork 50
266 lines (231 loc) · 9.4 KB
/
team-assignment.yml
File metadata and controls
266 lines (231 loc) · 9.4 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
name: Team Assignment
on:
issues:
types: [labeled]
jobs:
assign-to-team:
runs-on: ubuntu-latest
steps:
- name: Process team label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.PROJECTS_PAT }}
script: |
const label = context.payload.label.name;
const issueNumber = context.payload.issue.number;
// Check if label matches team:<team_name> pattern
const teamMatch = label.match(/^team:(.+)$/);
if (!teamMatch) {
console.log(`Label "${label}" does not match team:<team_name> pattern, skipping`);
return;
}
const teamName = teamMatch[1];
console.log(`Processing team assignment for team: ${teamName}`);
const podaacProjectNumber = 75;
const org = 'podaac';
const repo = context.repo.repo;
// Define reusable GraphQL mutation for updating project status
const updateStatusMutation = `
mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $value: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $projectId
itemId: $itemId
fieldId: $fieldId
value: { singleSelectOptionId: $value }
}
) {
projectV2Item {
id
}
}
}
`;
// ========================================
// Step 1: Update status to "triaged" in podaac project
// ========================================
console.log('Step 1: Updating podaac project status to "triaged"...');
// Get the podaac project information
const podaacProjectQuery = `
query($org: String!, $number: Int!) {
organization(login: $org) {
projectV2(number: $number) {
id
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
`;
const podaacProjectData = await github.graphql(podaacProjectQuery, {
org: org,
number: podaacProjectNumber
});
const podaacProject = podaacProjectData.organization.projectV2;
const podaacStatusField = podaacProject.fields.nodes.find(field => field.name === 'Status');
const triagedOption = podaacStatusField?.options.find(option => option.name === 'triaged');
let podaacProjectItem = null;
let podaacStatusUpdated = false;
if (!triagedOption) {
core.warning('Could not find "triaged" status option in podaac project');
} else {
// Get the issue's project item in podaac project
const issueProjectItemQuery = `
query($org: String!, $repo: String!, $number: Int!) {
repository(owner: $org, name: $repo) {
issue(number: $number) {
projectItems(first: 10) {
nodes {
id
project {
id
number
}
}
}
}
}
}
`;
const issueData = await github.graphql(issueProjectItemQuery, {
org: org,
repo: repo,
number: issueNumber
});
const projectItems = issueData.repository.issue.projectItems.nodes;
podaacProjectItem = projectItems.find(item => item.project.id === podaacProject.id);
if (!podaacProjectItem) {
core.warning('Issue is not in podaac project, cannot update status');
} else {
// Update the status to "triaged"
await github.graphql(updateStatusMutation, {
projectId: podaacProject.id,
itemId: podaacProjectItem.id,
fieldId: podaacStatusField.id,
value: triagedOption.id
});
podaacStatusUpdated = true;
console.log('✅ Successfully updated status to "triaged" in podaac project');
}
}
// ========================================
// Step 2: Add to team project if it exists
// ========================================
console.log(`Step 2: Looking for team project named "${teamName}"...`);
// Search for team project by name
const teamProjectSearchQuery = `
query($org: String!, $searchQuery: String!) {
organization(login: $org) {
projectsV2(first: 20, query: $searchQuery) {
nodes {
id
number
title
fields(first: 20) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
}
`;
const teamProjectSearchData = await github.graphql(teamProjectSearchQuery, {
org: org,
searchQuery: teamName
});
const teamProjects = teamProjectSearchData.organization.projectsV2.nodes;
const teamProject = teamProjects.find(p => p.title.toLowerCase() === teamName.toLowerCase());
if (!teamProject) {
console.log(`⚠️ No project found with name "${teamName}", skipping team project assignment`);
core.notice(`No project found for team "${teamName}". If you want the issue added to a team project, create a project named "${teamName}".`);
return;
}
console.log(`Found team project: ${teamProject.title} (number: ${teamProject.number})`);
// Check if issue is already in the team project
const issueWithProjectsQuery = `
query($org: String!, $repo: String!, $number: Int!) {
repository(owner: $org, name: $repo) {
issue(number: $number) {
id
projectItems(first: 20) {
nodes {
id
project {
id
}
}
}
}
}
}
`;
const issueWithProjects = await github.graphql(issueWithProjectsQuery, {
org: org,
repo: repo,
number: issueNumber
});
const issueNodeId = issueWithProjects.repository.issue.id;
const existingTeamProjectItem = issueWithProjects.repository.issue.projectItems.nodes.find(
item => item.project.id === teamProject.id
);
let teamProjectItemId;
let wasAlreadyInProject = false;
if (existingTeamProjectItem) {
// Issue is already in team project
teamProjectItemId = existingTeamProjectItem.id;
wasAlreadyInProject = true;
console.log(`ℹ️ Issue is already in team project "${teamName}"`);
} else {
// Add issue to team project
const addToProjectMutation = `
mutation($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {
projectId: $projectId
contentId: $contentId
}) {
item {
id
}
}
}
`;
const addResult = await github.graphql(addToProjectMutation, {
projectId: teamProject.id,
contentId: issueNodeId
});
teamProjectItemId = addResult.addProjectV2ItemById.item.id;
console.log(`✅ Successfully added issue to team project "${teamName}"`);
}
// Summary
const summaryItems = [`Processed team label: ${teamName}`];
if (podaacStatusUpdated) {
summaryItems.push(`Updated podaac project status to "triaged"`);
}
if (wasAlreadyInProject) {
summaryItems.push(`Issue was already in team project "${teamName}"`);
} else {
summaryItems.push(`Added issue to team project "${teamName}"`);
}
core.summary
.addHeading(`Team Assignment Complete: ${teamName}`)
.addList(summaryItems)
.write();