-
Notifications
You must be signed in to change notification settings - Fork 0
/
createIssues.js
190 lines (171 loc) · 5.22 KB
/
createIssues.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
/* Importing the libraries that we need to use in our code. */
const request = require('request-promise')
const fs = require('fs-extra')
const glob = require('glob')
const { sleep } = require('./utils')
const config = require('./config')
const createMessage = require('./createMessage')
const processImages = require('./processImages')
const api = `${config.target.baseUrl}/${config.target.org}/${config.target.repo}`
if (!fs.pathExistsSync(`./${config.source.repo}/state.json`)) {
console.log('Creating state file')
fs.writeFileSync(`./${config.source.repo}/state.json`, '{}')
}
/* This is setting up the headers for the request. */
const headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'node.js'
}
if (config.target.token) {
headers['Authorization'] = `token ${config.target.token}`
}
/**
* It reads the state.json file, increments the issue number, and writes the state.json file back to
* disk
* @param issue - The issue object from the GitHub API
*/
const bumpIssueCount = (issue) => {
const state = JSON.parse(fs.readFileSync(`./${config.source.repo}/state.json`))
state.issue = issue.number
fs.writeFileSync(`./${config.source.repo}/state.json`, JSON.stringify(state, null, ' '))
}
/**
* It checks if an issue exists in the repository
* @param issue - the issue object that we're going to create
* @returns A boolean value
*/
const isIssueMade = async (issue) => {
const url = `${api}/issues/${issue.number}`
let exists = true
try {
await request({
method: 'GET',
headers,
url,
json: true,
})
} catch (error) {
exists = false
}
return exists
}
/**
* It creates an issue on GitHub
* @param issue - The issue object from the JSON file
*/
const createIssue = async (issue) => {
console.log(`Creating issue: ${issue.number}`)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
await request({
method: 'POST',
headers,
url: `${api}/issues`,
body: {
title: issue.title,
body: `${createMessage(issue)}\n\n${await processImages(issue.body)}`,
},
json: true,
})
.then(response => {
bumpIssueCount(issue)
return response
})
.catch(async err => {
console.log('Error creating issue:', err);
await delay(10 * 60 * 1000); // Delay for 10 minutes
console.log('Retrying...');
await createIssue(issue);
})
}
//isPullRequestMade is the same function as isIssueMade
//In the future, we can refactor this to be one function
/**
* It checks if a pull request exists for a given issue
* @param issue - The issue object that we're checking to see if a pull request has been made for.
* @returns A boolean value
*/
const isPullRequestMade = async (issue) => {
const url = `${api}/issues/${issue.number}`
let exists = true
try {
await request({
method: 'GET',
headers,
url,
json: true,
})
} catch (error) {
exists = false
}
return exists
}
/**
* It creates a pull request on the target repository
* @param pull - The pull request object from the source repo
*/
const createPull = async (pull) => {
console.log(`Creating pull: ${pull.number}`)
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const body =
{
title: pull.title,
repo: `${config.target.repo}`,
body: `${createMessage(pull)}\n\n${await processImages(pull.body)}`,
head: pull.base.sha === pull.head.sha ? 'refs/heads/master' : `pr${pull.number}head`,
base: `pr${pull.number}base`,
maintainer_can_modify: true,
}
await request({
method: 'POST',
headers,
url: `${api}/pulls`,
body,
json: true,
})
.then(response => {
bumpIssueCount(pull)
return response
})
.catch(async (err) => {
console.log('Error creating pull:', err);
await delay(10 * 60 * 500); // Delay for 5 minutes
console.log('Retrying...');
await createPull(pull);
})
}
const main = async () => {
const issues = glob.sync(`${config.source.repo}/issues/issue-+([0-9]).json`)
.map(file => JSON.parse(fs.readFileSync(file)))
.sort((a, b) => a.number - b.number)
const state = JSON.parse(await fs.readFile(`./${config.source.repo}/state.json`))
for (let issue of issues) {
if (issue.number <= (state.issue || 0)) {
// we already processed this issue
console.log(`Skipping ${issue.number}. Already processed`)
}
else if (issue.html_url.includes('pull')) {
/* This is checking if the issue is a pull request. If it is, it creates a pull request on the
target repository. */
await createPull(issue)
await sleep(1000)
let pullExists = await isPullRequestMade(issue)
while (!pullExists) {
console.log(`Waiting for issue ${issue.number} to exist`)
await sleep(1000)
pullExists = await isPullRequestMade(issue)
}
} else {
/* Checking if the issue is a pull request. If it is, it creates a pull request on the
target repository. */
await createIssue(issue)
await sleep(1000)
let issueExists = await isIssueMade(issue)
while (!issueExists) {
console.log(`Waiting for pull ${issue.number} to exist`)
await sleep(1000)
issueExists = await isIssueMade(issue)
}
}
}
}
main().catch(console.error)