-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsync-github-project-issues.ts
More file actions
276 lines (239 loc) · 6.98 KB
/
Copy pathsync-github-project-issues.ts
File metadata and controls
276 lines (239 loc) · 6.98 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
267
268
269
270
271
272
273
274
275
276
import githubQuery from "./common/github-graphql-query"
// Ref: <https://docs.github.com/en/issues/planning-and-tracking-with-projects/automating-your-project/using-the-api-to-manage-projects>.
main().catch((error) => {
console.error(error)
process.exit(1)
})
type Project = {
title: string
number: number
readme: null | string
}
type ProjectSyncConfig = {
label: string
}
async function main(): Promise<void> {
const githubToken = process.env.GITHUB_TOKEN_FOR_SYNC_PROJECTS
if (githubToken == null) {
throw new Error("GITHUB_TOKEN_FOR_SYNC_PROJECTS environment variable not set")
}
const projects: Project[] = await loadAllProjects(githubToken)
for (const project of projects) {
if (project.readme == null) {
continue
}
const match = project.readme.match(/^```autosync\n.+```$/ms)
if (match == null) {
continue
}
const jsonConfig = match[0].substring("^```autosync\n".length-1, match[0].length - 3).trim()
console.log(`JSON Config for project ${project.title}: `, jsonConfig)
const config: ProjectSyncConfig = JSON.parse(jsonConfig)
console.log(`Syncing project ${project.title} with issues labelled ${config.label}`)
await doSyncProject(githubToken, project.number, config.label)
}
}
async function doSyncProject(githubToken: string, projectNumber: number, label: string) {
const parts = await Promise.all([
fetchOpenIssuesWithLabel(githubToken, "appsmith", label),
fetchOpenIssuesWithLabel(githubToken, "appsmith-ee", label),
])
const itemsExpectedToBeInProject: Set<string> = new Set([...parts[0], ...parts[1]])
console.log("itemsExpectedToBeInProject", itemsExpectedToBeInProject)
const projectInfo = await fetchIssuesInProject(githubToken, projectNumber)
const projectId: string = projectInfo.projectId
const itemsAlreadyInProject: Set<string> = projectInfo.issueIds
console.log(`Project ${ projectId } has`, itemsAlreadyInProject)
const itemsToAdd: Set<string> = new Set(Array.from(itemsExpectedToBeInProject).filter((x) => !itemsAlreadyInProject.has(x)))
console.log(`Items to add: ${ itemsToAdd.size }`)
for (const item of itemsToAdd) {
await githubQuery(githubToken, `mutation {
addProjectV2ItemById(input: {projectId: "${ projectId }" contentId: "${ item }"}) {
item {
id
}
}
}`)
}
const itemsToRemove: Set<string> = new Set(Array.from(itemsAlreadyInProject).filter((x) => !itemsExpectedToBeInProject.has(x)))
console.log(`Items to remove: ${ itemsToRemove.size }`)
for (const item of itemsToRemove) {
await githubQuery(githubToken, `mutation {
deleteProjectV2Item(input: {projectId: "${ projectId }" itemId: "${ item }"}) {
deletedItemId
}
}`)
}
}
async function loadAllProjects(githubToken: string): Promise<Project[]> {
const projects: Project[] = []
let afterId: null | string = null
type Response = {
data: {
organization: {
projectsV2: {
nodes: Project[]
pageInfo: {
hasNextPage: boolean
endCursor: string
}
}
}
}
}
for (let i = 0; i < 100; ++i) {
const r: Response = (await githubQuery(githubToken, `
{
organization(login: "appsmithorg") {
projectsV2(first: 100${ afterId == null ? "" : `, after: "${ afterId }"` }) {
nodes {
title
number
readme
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
`))
console.log(r)
projects.push(...r.data.organization.projectsV2.nodes)
if (r.data.organization.projectsV2.pageInfo.hasNextPage) {
afterId = r.data.organization.projectsV2.pageInfo.endCursor
} else {
break
}
}
return projects
}
async function fetchOpenIssuesWithLabel(githubToken: string, repo: string, label: string): Promise<Set<string>> {
const ids: Set<string> = new Set
let afterId: null | string = null
type Response = {
data: {
repository: {
issues: {
nodes: {
id: string
}[]
pageInfo: {
hasNextPage: boolean
endCursor: string
}
}
}
}
}
for (let i = 0; i < 100; ++i) {
const r: Response = (await githubQuery(githubToken, `{
repository(name: "${ repo }", owner: "appsmithorg") {
issues(labels: "${ label }", states: OPEN, first: 100${ afterId == null ? "" : `, after: "${ afterId }"` }) {
nodes {
id
number
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`))
const issueNodes = r.data.repository.issues.nodes
console.log(issueNodes)
for (const issue of issueNodes) {
ids.add(issue.id)
}
if (r.data.repository.issues.pageInfo.hasNextPage) {
afterId = r.data.repository.issues.pageInfo.endCursor
} else {
break
}
}
return ids
}
async function fetchIssuesInProject(githubToken: string, projectNumber: number): Promise<{ projectId: string, issueIds: Set<string> }> {
const issueIds: Set<string> = new Set
let projectId: null | string = null
let afterId: null | string = null
type Response = {
data?: {
organization: {
projectV2: {
id: string
items: {
nodes: {
content: {
id: string
}
}[]
pageInfo: {
hasNextPage: boolean
endCursor: string
}
}
}
}
}
}
for (let i = 0; i < 100; ++i) {
const r: Response = (await githubQuery(githubToken, `
{
organization(login: "appsmithorg") {
projectV2(number: ${ projectNumber }) {
id
items(first: 100${ afterId == null ? "" : `, after: "${ afterId }"` }) {
nodes {
... on ProjectV2Item {
content {
... on DraftIssue {
id
}
... on Issue {
id
}
... on PullRequest {
id
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}
`))
if (r.data == null) {
console.error("No organization in response", r)
throw new Error("No organization")
}
const project = r.data.organization.projectV2
projectId = project.id
if (project.items.nodes == null) {
break
}
for (const item of project.items.nodes) {
issueIds.add(item.content.id)
}
if (project.items.pageInfo.hasNextPage) {
afterId = project.items.pageInfo.endCursor
} else {
break
}
}
if (projectId == null) {
throw new Error("No project found")
}
return {
projectId,
issueIds,
}
}