-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathassign-r5-priority.js
153 lines (127 loc) · 4.51 KB
/
assign-r5-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
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
/**
* Monitors open PRs once daily during weekdays to identify stale community review requests. When a PR
* with the community review label hasn't been updated for the specified threshold
* period (default 21 days), it's assigned R5 priority. These PRs are added to the
* project board and set to Ready status to ensure visibility of long-pending
* community reviews.
*/
const { PRIORITIES, LABELS, STATUS, DAYS_THRESHOLD, ...PROJECT_CONFIG } = require("./project-config");
const {
updateProjectField,
updateProjectDateField,
addItemToProject,
fetchProjectFields,
fetchOpenPullRequests,
fetchProjectItem,
} = require('./project-api');
const MS_PER_DAY = 1000 * 60 * 60 * 24;
module.exports = async ({ github }) => {
let allPRs = [];
let hasNextPage = true;
let cursor = null;
// Fetch all PRs using pagination
while (hasNextPage) {
const result = await fetchOpenPullRequests({
github,
org: PROJECT_CONFIG.org,
repo: PROJECT_CONFIG.repo,
cursor: cursor,
});
const pullRequests = result.organization.repository.pullRequests;
allPRs = allPRs.concat(pullRequests.nodes);
// Update pagination info
hasNextPage = pullRequests.pageInfo.hasNextPage;
cursor = pullRequests.pageInfo.endCursor;
}
console.log(`Total PRs fetched: ${allPRs.length}`);
// 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
);
const r5OptionId = priorityField.options.find(
(option) => option.name === PRIORITIES.R5
)?.id;
const readyStatusId = statusField.options.find(
(option) => option.name === STATUS.READY
)?.id;
for (const pr of allPRs) {
const labels = pr.labels.nodes.map((l) => l.name);
const isDraft = pr.draft === true;
// Skip draft PRs
if (isDraft) {
console.log(`Skipping draft PR #${pr.number}`);
continue;
}
const hasExemptionOrClarification = labels.some(label =>
[LABELS.CLARIFICATION_REQUESTED, LABELS.EXEMPTION_REQUESTED].includes(label)
);
// Skip if PR doesn't have community review label or has exemption/clarification
if (!labels.includes(LABELS.COMMUNITY_REVIEW) || hasExemptionOrClarification) {
continue;
}
const lastUpdated = new Date(pr.updatedAt);
const daysSinceUpdate = (Date.now() - lastUpdated) / MS_PER_DAY;
// Skip if PR update is within the days threshold
if (daysSinceUpdate <= DAYS_THRESHOLD) {
continue;
}
console.log(`Processing PR #${pr.number} for ${PRIORITIES.R5} priority consideration`);
try {
// Get all projects the PR added to
const result = await fetchProjectItem({
github,
contentId: pr.id
});
// Filter specific project
const projectItem = result?.node?.projectItems?.nodes
?.find(item => item.project.id === PROJECT_CONFIG.projectId);
// Skip if PR is already in project
if (projectItem) {
console.log(`PR #${pr.number} is already in project. Skipping.`);
continue;
}
// Add new PR to project with R5 priority
console.log(`Adding PR #${pr.number} to project with ${PRIORITIES.R5} priority`);
const addResult = await addItemToProject({
github,
projectId: PROJECT_CONFIG.projectId,
contentId: pr.id,
});
// Set priority, Ready status and current date for new items
await Promise.all([
updateProjectField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: addResult.addProjectV2ItemById.item.id,
fieldId: PROJECT_CONFIG.priorityFieldId,
value: r5OptionId,
}),
updateProjectField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: addResult.addProjectV2ItemById.item.id,
fieldId: PROJECT_CONFIG.statusFieldId,
value: readyStatusId,
}),
updateProjectDateField({
github,
projectId: PROJECT_CONFIG.projectId,
itemId: itemId,
fieldId: PROJECT_CONFIG.addedOnFieldId,
date: new Date().toISOString(),
})
]);
} catch (error) {
console.error(`Error processing PR #${pr.number}:`, error);
continue;
}
}
}