forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-issue-create.js
237 lines (224 loc) · 7.71 KB
/
github-issue-create.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
229
230
231
232
233
234
235
236
237
// Copyright 2017 TODO Group. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
const Result = require('../lib/result')
const InternalHelpers = require('./helpers/github-issue-create-helpers')
// eslint-disable-next-line no-unused-vars
const { Octokit } = require('@octokit/rest')
let targetOrg = ''
let targetRepository = ''
/**
* Create a Github Issue on the targeted repository specifically for this broken rule.
*
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories
* @param {object} options The rule configuration
* @param {string[]} targets The files to modify (will be overridden by options if present)
* @param {boolean} dryRun If true, repolinter will report suggested fixes, but will make no disk modifications.
* @returns {Promise<Result>} The fix result
*/
async function createGithubIssue(fs, options, targets, dryRun = false) {
try {
try {
await prepareWorkingEnvironment(dryRun)
} catch (error) {
return new Result(error.message, [], false)
}
// Create Labels
const labels = options.issueLabels
labels.push(options.bypassLabel)
await InternalHelpers.ensureAddedGithubLabels(
labels,
targetOrg,
targetRepository,
this.Octokit
)
options.issueLabels = options.issueLabels.filter(
label => label !== options.bypassLabel
)
// Find issues created by Repolinter
const issues = await InternalHelpers.findExistingRepolinterIssues(
options,
targetOrg,
targetRepository,
this.Octokit
)
// If there are no issues, create one.
// If there are issues, we loop through them and handle each each on it's own
if (issues === null || issues === undefined) {
// Issue should include the broken rule, a message in the body and a label.
const createdIssue = await createIssueOnGithub(options)
// We are done here, we created a new issue.
return new Result(
`No Open/Closed issues were found for this rule - Created new Github Issue with issue number - ${createdIssue.data.number}`,
[],
true
)
}
const openIssues = issues.filter(issue => issue.state === 'open')
for (let i = 0; i < openIssues.length; i++) {
const issue = openIssues[i]
// Issue is open, check body and find what rules have been broken.
// If the rule that has been broken, is already listed in the issue body/title, do nothing
const ruleIdentifier = InternalHelpers.retrieveRuleIdentifier(issue.body)
if (ruleIdentifier === options.uniqueRuleId) {
if (InternalHelpers.hasBypassLabelBeenApplied(options, issue.labels)) {
// Bypass label has been seen for this issue, we can ignore it.
return new Result(
`Rule fix failed as Github Issue ${issue.number} has bypass label.`,
[],
true
)
} else {
return new Result(
`No Github Issue Created - Issue already exists with correct unique identifier`,
[],
true
)
}
}
}
const closedIssues = issues.filter(issue => issue.state === 'closed')
for (let i = 0; i < closedIssues.length; i++) {
const issue = closedIssues[i]
const ruleIdentifier = InternalHelpers.retrieveRuleIdentifier(issue.body)
if (ruleIdentifier === options.uniqueRuleId) {
// This means that there is regression, we should update the issue with new body and comment on it.
if (InternalHelpers.hasBypassLabelBeenApplied(options, issue.labels)) {
// Bypass label has been seen for this issue, we can ignore it.
return new Result(
`Rule fix failed as Github Issue ${issue.number} has bypass label.`,
[],
true
)
} else {
await updateIssueOnGithub(options, issue.number)
await commentOnGithubIssue(options, issue.number)
return new Result(
`Github Issue ${issue.number} re-opened as there seems to be regression!`,
[],
true
)
}
} else {
console.log(
'Issue: ' + issue.number + ' - No matching rule identifier was found'
)
}
}
// There are open/closed issues from Repolinter, but non of them are for this ruleset
// Issue should include the broken rule, a message in the body and a label.
const newIssue = await createIssueOnGithub(options)
return new Result(
`Github Issue ${newIssue.data.number} Created!`,
targets,
true
)
} catch (e) {
console.error(e)
}
}
/**
* Create an issue on Github with labels and all on the target repository.
*
* @param {object} options The rule configuration.
* @returns {object} Returns issue after adding it via the Github API.
*/
async function createIssueOnGithub(options) {
try {
const issueBodyWithId = options.issueBody.concat(
`\n Unique rule set ID: ${options.uniqueRuleId}`
)
return await this.Octokit.request('POST /repos/{owner}/{repo}/issues', {
owner: targetOrg,
repo: targetRepository,
title: options.issueTitle,
body: issueBodyWithId,
labels: options.issueLabels
})
} catch (e) {
console.error(e)
}
}
/**
* Update specific issue on Github.
*
* @param {object} options The rule configuration.
* @param {string} issueNumber The number of the issue we should update.
* @returns {object} Returns issue after updating it via the Github API.
*/
async function updateIssueOnGithub(options, issueNumber) {
try {
const issueBodyWithId = options.issueBody.concat(
`\n Unique rule set ID: ${options.uniqueRuleId}`
)
return await this.Octokit.request(
'PATCH /repos/{owner}/{repo}/issues/{issue_number}',
{
owner: targetOrg,
repo: targetRepository,
issue_number: issueNumber,
title: options.issueTitle,
body: issueBodyWithId,
labels: options.issueLabels,
state: 'open'
}
)
} catch (e) {
console.error(e)
}
}
/**
* Comment on a specific issue on Github.
*
* @param {object} options The rule configuration.
* @param {string} issueNumber The number of the issue we should update.
* @returns {object} Returns issue after commenting on it via the Github API.
*/
async function commentOnGithubIssue(options, issueNumber) {
try {
return await this.Octokit.request(
'POST /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
owner: targetOrg,
repo: targetRepository,
issue_number: issueNumber,
body: options.commentBody
}
)
} catch (e) {
console.error(e)
}
}
/**
* Prepare our working environment.
* Check if environment variables are set.
* @param {string} dryRun Enable this if you want to test without configuring environment variables
* Set constants like targetOrg and targetRepository and initialize OctoKit.
*
*/
async function prepareWorkingEnvironment(dryRun) {
if (!dryRun) {
const targetRepoEnv = process.env.TARGET_REPO
const authTokenEnv = process.env.GITHUB_TOKEN
if (authTokenEnv === undefined || targetRepoEnv === undefined) {
throw new Error(
'Could not perform fix due to missing/invalid environment variables! Please set TARGET_REPO and GITHUB_TOKEN environment variables.'
)
}
targetOrg = targetRepoEnv.split('/')[0]
targetRepository = targetRepoEnv.split('/')[1]
this.Octokit = new Octokit({
auth: authTokenEnv,
baseUrl: 'https://api.github.com',
owner: targetOrg,
repo: targetRepository
})
} else {
targetOrg = 'test'
targetRepository = 'tester-repo'
this.Octokit = new Octokit({
auth: 'fake',
baseUrl: 'https://api.github.com'
})
}
}
module.exports = createGithubIssue