forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfr-add-docs-reviewers-requests.js
228 lines (209 loc) · 6.94 KB
/
fr-add-docs-reviewers-requests.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
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
// eslint-disable-next-line import/no-extraneous-dependencies
import { graphql } from '@octokit/graphql'
import {
addItemsToProject,
isDocsTeamMember,
findFieldID,
findSingleSelectID,
generateUpdateProjectV2ItemFieldMutation,
} from './projects.js'
async function getAllOpenPRs() {
let prsRemaining = true
let cursor
let prData = []
while (prsRemaining) {
const data = await graphql(
`
query ($organization: String!, $repo: String!) {
repository(name: $repo, owner: $organization) {
pullRequests(last: 100, states: OPEN${cursor ? ` before:"${cursor}"` : ''}) {
pageInfo{startCursor, hasPreviousPage},
nodes {
id
isDraft
reviewRequests(first: 10) {
nodes {
requestedReviewer {
... on Team {
name
}
}
}
}
labels(first: 5) {
nodes {
name
}
}
reviews(first: 10) {
nodes {
onBehalfOf(first: 1) {
nodes {
name
}
}
}
}
author {
login
}
}
}
}
}
`,
{
organization: process.env.ORGANIZATION,
repo: process.env.REPO,
headers: {
authorization: `token ${process.env.TOKEN}`,
},
},
)
prsRemaining = data.repository.pullRequests.pageInfo.hasPreviousPage
cursor = data.repository.pullRequests.pageInfo.startCursor
prData = [...prData, ...data.repository.pullRequests.nodes]
}
return prData
}
async function run() {
// Get info about open github/github PRs
const prData = await getAllOpenPRs()
// Get the PRs that are:
// - not draft
// - not a train
// - are requesting a review by docs-reviewers
// - have not already been reviewed on behalf of docs-reviewers
const prs = prData.filter(
(pr) =>
!pr.isDraft &&
!pr.labels.nodes.find((label) => label.name === 'Deploy train 🚂') &&
pr.reviewRequests.nodes.find(
(requestedReviewers) => requestedReviewers.requestedReviewer?.name === process.env.REVIEWER,
) &&
!pr.reviews.nodes
.flatMap((review) => review.onBehalfOf.nodes)
.find((behalf) => behalf.name === process.env.REVIEWER),
)
if (prs.length === 0) {
console.log('No PRs found. Exiting.')
return
}
const prIDs = prs.map((pr) => pr.id)
const prAuthors = prs.map((pr) => pr.author.login)
console.log(`PRs found: ${prIDs}`)
// Get info about the docs-content review board project
const projectData = await graphql(
`
query ($organization: String!, $projectNumber: Int!) {
organization(login: $organization) {
projectV2(number: $projectNumber) {
id
items(last: 100) {
nodes {
id
}
}
fields(first: 100) {
nodes {
... on ProjectV2Field {
id
name
}
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}
`,
{
organization: process.env.ORGANIZATION,
projectNumber: parseInt(process.env.PROJECT_NUMBER),
headers: {
authorization: `token ${process.env.TOKEN}`,
},
},
)
// Get the project ID
const projectID = projectData.organization.projectV2.id
// Get the IDs of the last 100 items on the board.
// Until we have a way to check from a PR whether the PR is in a project,
// this is how we (roughly) avoid overwriting PRs that are already on the board.
// If we are overwriting items, query for more items.
const existingItemIDs = projectData.organization.projectV2.items.nodes.map((node) => node.id)
// Get the ID of the fields that we want to populate
const datePostedID = findFieldID('Date posted', projectData)
const reviewDueDateID = findFieldID('Review due date', projectData)
const statusID = findFieldID('Status', projectData)
const featureID = findFieldID('Feature', projectData)
const contributorTypeID = findFieldID('Contributor type', projectData)
const sizeTypeID = findFieldID('Size', projectData)
const authorID = findFieldID('Contributor', projectData)
// Get the ID of the single select values that we want to set
const readyForReviewID = findSingleSelectID('Ready for review', 'Status', projectData)
const hubberTypeID = findSingleSelectID('Hubber or partner', 'Contributor type', projectData)
const docsMemberTypeID = findSingleSelectID('Docs team', 'Contributor type', projectData)
const sizeMediumID = findSingleSelectID('M', 'Size', projectData)
// Add the PRs to the project
const itemIDs = await addItemsToProject(prIDs, projectID)
// If an item already existed on the project, the existing ID will be returned.
// Exclude existing items going forward.
// Until we have a way to check from a PR whether the PR is in a project,
// this is how we (roughly) avoid overwriting PRs that are already on the board
const newItemIDs = []
const newItemAuthors = []
itemIDs.forEach((id, index) => {
if (!existingItemIDs.includes(id)) {
newItemIDs.push(id)
newItemAuthors.push(prAuthors[index])
}
})
if (newItemIDs.length === 0) {
console.log('All found PRs are already on the project. Exiting.')
return
}
// Populate fields for the new project items
// (Using for...of instead of forEach since the function uses await)
for (const [index, itemID] of newItemIDs.entries()) {
const updateProjectV2ItemMutation = generateUpdateProjectV2ItemFieldMutation({
item: itemID,
author: newItemAuthors[index],
turnaround: 2,
feature: process.env.FEATURE,
})
const contributorType = (await isDocsTeamMember(newItemAuthors[index]))
? docsMemberTypeID
: hubberTypeID
console.log(`Populating fields for item: ${itemID} with author ${newItemAuthors[index]}`)
await graphql(updateProjectV2ItemMutation, {
project: projectID,
statusID,
statusValueID: readyForReviewID,
datePostedID,
reviewDueDateID,
contributorTypeID,
contributorType,
sizeTypeID,
sizeType: sizeMediumID, // We need to provide something here, defaulting to 'medium' or 'M'
featureID,
authorID,
headers: {
authorization: `token ${process.env.TOKEN}`,
},
})
console.log('Done populating fields for item')
}
return newItemIDs
}
run().catch((error) => {
console.log(`#ERROR# ${error}`)
process.exit(1)
})