-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink-issues.js
92 lines (79 loc) · 2.44 KB
/
link-issues.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
import 'dotenv/config'
import { LinearClient } from '@linear/sdk'
import { Gitlab } from '@gitbeaker/node'
const linearClient = new LinearClient({
apiKey: process.env.LINEAR_TOKEN,
})
const gitlab = new Gitlab({
token: process.env.GITLAB_TOKEN,
})
const projectId = process.env.GITLAB_PROJECT_ID
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function main() {
const getInput = () =>
new Promise((resolve) => {
var stdin = process.openStdin()
var data = ''
stdin.on('data', function (chunk) {
data += chunk
})
stdin.on('end', function () {
resolve(JSON.parse(data))
})
})
const input = await getInput()
for (const issueID of Object.keys(input)) {
do {
await delay(500)
try {
const issue = await linearClient.issue(issueID)
const issueRelations = await issue.relations()
while (issueRelations.pageInfo.hasNextPage) {
await issueRelations.fetchNext()
}
for (const relation of issueRelations.nodes) {
console.error('iterate', relation.id)
do {
await delay(500)
try {
const relatedIssue = await relation.relatedIssue
const glIssueIid = input[issue.id]
const glRelatedIssueIid = input[relatedIssue.id]
if ((glIssueIid, glRelatedIssueIid)) {
await gitlab.Issues.link(
projectId,
glIssueIid,
projectId,
glRelatedIssueIid,
{
link_type:
relation.type === 'blocks' ? 'blocks' : undefined,
}
)
}
} catch (error) {
if (
error.description === 'Issue(s) already assigned' ||
error.description.includes('cannot be related to itself')
) {
console.error('relation', issue.identifier, error.description)
break
} else {
console.error('relation', issue.identifier, error)
await delay(3000)
continue // go again
}
}
break
} while (true)
}
} catch (error) {
console.error('issue', issueID, error)
await delay(3000)
continue // go again
}
break
} while (true)
}
}
await main()