-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathassign-bug-priority.js
110 lines (96 loc) · 3.89 KB
/
assign-bug-priority.js
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
const { PRIORITIES, LABELS, STATUS, ...PROJECT_CONFIG } = require('./project-config');
const {
updateProjectField,
updateProjectDateField,
addItemToProject,
fetchProjectFields,
fetchProjectItem,
} = require('./project-api');
module.exports = async ({ github, context }) => {
async function addToProject(issue) {
console.log(`Processing issue #${issue.number}...`);
// Get project fields
const projectFields = await fetchProjectFields({
github,
org: PROJECT_CONFIG.org,
number: PROJECT_CONFIG.projectNumber
});
const priorityField = projectFields.organization.projectV2.fields.nodes.find(
(field) => field.id === PROJECT_CONFIG.priorityFieldId
);
const statusField = projectFields.organization.projectV2.fields.nodes.find(
(field) => field.id === PROJECT_CONFIG.statusFieldId
);
// Check if issue is already in project
const result = await fetchProjectItem({
github,
contentId: issue.node_id
});
// Filter our specific project
const projectItem = result?.node?.projectItems?.nodes
?.find(item => item.project.id === PROJECT_CONFIG.projectId);
if (projectItem) {
// Issue already in project - update only priority if different
const currentPriority = projectItem.fieldValues.nodes
.find(fv => fv.field?.name === 'Priority')?.name;
if (currentPriority === PRIORITIES.R6) {
console.log(`Issue #${issue.number} already has ${PRIORITIES.R6} priority. Skipping.`);
return;
}
const priorityR6OptionId = priorityField.options.find(
(option) => option.name === PRIORITIES.R6
)?.id;
// Update priority only, retain existing status
await updateProjectField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: projectItem.id,
fieldId: PROJECT_CONFIG.priorityFieldId,
value: priorityR6OptionId,
});
} else {
// Add new issue to project
const addResult = await addItemToProject({
github,
projectId: PROJECT_CONFIG.projectId,
contentId: issue.node_id,
});
const itemId = addResult.addProjectV2ItemById.item.id;
const priorityR6OptionId = priorityField.options.find(
(option) => option.name === PRIORITIES.R6
)?.id;
const readyStatusId = statusField.options.find(
(option) => option.name === STATUS.READY
)?.id;
// Set priority, Ready status and current date for new items
await Promise.all([
updateProjectField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: itemId,
fieldId: PROJECT_CONFIG.priorityFieldId,
value: priorityR6OptionId,
}),
updateProjectField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: itemId,
fieldId: PROJECT_CONFIG.statusFieldId,
value: readyStatusId,
}),
updateProjectDateField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: itemId,
fieldId: PROJECT_CONFIG.addedOnFieldId,
date: new Date().toISOString(),
})
]);
}
}
const issue = context.payload.issue;
const labels = issue.labels.map((l) => l.name);
if (labels.includes(LABELS.P1) && labels.includes(LABELS.BUG)) {
await addToProject(issue);
}
};