-
Notifications
You must be signed in to change notification settings - Fork 18
/
github.js
275 lines (252 loc) · 11.4 KB
/
github.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
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
const core = require('@actions/core')
const github = require('@actions/github')
const sastreport = require('../report/sastreport')
const osareport = require('../report/osareport')
const inputs = require("./inputs")
const utils = require('../utils/utils')
const envs = process.env
const HTTP_STATUS_OK = 200
const HTTP_STATUS_CREATED = 201
const GITHUB_STATE_OPEN = "open"
const GITHUB_STATE_CLOSED = "closed"
const GITHUB_EVENT_PUSH = "push"
const GITHUB_EVENT_PULL_REQUEST = "pull_request"
function getToken() {
let token = ""
let createGithubIssues = inputs.getBoolean(inputs.CX_GITHUB_ISSUES, false)
if (createGithubIssues && createGithubIssues != "false") {
token = inputs.getString(inputs.CX_GITHUB_TOKEN, false, "", true)
} else {
core.info('Issues will not be created since cxGithubIssues was not provided or set to false')
}
return token
}
async function createIssues(cxAction) {
let token = getToken()
if (token) {
let repository = envs.GITHUB_REPOSITORY
let commitSha = envs.GITHUB_SHA
let workspace = envs.GITHUB_WORKSPACE
let event = envs.GITHUB_EVENT_NAME
let githubLabels = inputs.getArray(inputs.CX_GITHUB_LABELS, false)
githubLabels.push("checkmarx")
let githubAssignees = inputs.getArray(inputs.CX_GITHUB_ASSIGNEES, false)
let githubMilestone = inputs.getInt(inputs.CX_GITHUB_MILESTONE, false)
if (githubMilestone == -1) {
githubMilestone = null
}
const repoSplit = repository.split("/")
const owner = repoSplit[0]
const repo = repoSplit[1]
core.info("Getting Octokit...")
const octokit = github.getOctokit(token)
if (octokit) {
if (cxAction == utils.SCAN) {
let xmlPath = sastreport.getXmlReportPath(workspace)
let issues = sastreport.getIssuesFromXml(xmlPath, repository, commitSha)
if (issues) {
let repositoryIssues = await getIssues(owner, repo, octokit)
let resolvedIssues = 0
let reopenedIssues = 0
let recurrentIssues = 0
let newIssues = 0
for (let i = 0; i < issues.length; i++) {
let issue = issues[i]
const title = sastreport.getTitle(issue)
const body = sastreport.getBody(issue)
let issueGithubLabels = sastreport.getLabels(githubLabels, issue)
let state = GITHUB_STATE_OPEN
if (issue.resultState == sastreport.NOT_EXPLOITABLE) {
state = GITHUB_STATE_CLOSED
}
let issueExists = false
for (let j = 0; j < repositoryIssues.length; j++) {
let repositoryIssue = repositoryIssues[j]
const titleRepositoryIssue = repositoryIssue.title
if (titleRepositoryIssue == title) {
issueExists = true
if (state != repositoryIssue.state) {
if (state == GITHUB_STATE_OPEN && repositoryIssue.state == GITHUB_STATE_CLOSED) {
reopenedIssues++
} else if (state == GITHUB_STATE_CLOSED && repositoryIssue.state == GITHUB_STATE_OPEN) {
resolvedIssues++
} else {
recurrentIssues++
}
} else {
recurrentIssues++
}
await updateIssue(owner, repo, octokit, body, issueGithubLabels, githubAssignees, githubMilestone, repositoryIssue, state)
break
}
}
if (!issueExists) {
newIssues++
let issueId = await createIssue(owner, repo, octokit, title, body, issueGithubLabels, githubAssignees, githubMilestone, i, state)
/*for (let j = 0; j < issue.resultNodes.length; j++) {
let node = issue.resultNodes[j]
let commentBody = "**#" + issueId + " - " + issue.resultSeverity + " - " + issue.queryName + " - " + j + " Node** - " + node.name
await createCommitComment(owner, repo, octokit, commitSha, commentBody, node.relativefileName, node.line)
}*/
issueGithubLabels = []
}
}
let summary = sastreport.getSummary(issues, newIssues, recurrentIssues, resolvedIssues, reopenedIssues)
await createCommitComment(owner, repo, octokit, commitSha, summary, null, null)
if (event == GITHUB_EVENT_PULL_REQUEST) {
const pull_number = parseInt(envs.GITHUB_REF.replace("/merge", "").replace("refs/pull/", ""))
core.info("\nUpdating Pull Request #" + pull_number + " for " + owner + "/" + repo)
const pullRequestCommented = await octokit.issues.createComment({ owner: owner, repo: repo, body: summary, issue_number: pull_number})
if (pullRequestCommented.status == HTTP_STATUS_CREATED) {
core.info("\nUpdated Pull Request #" + pull_number+ " for " + owner + "/" + repo)
} else {
core.info("\nFailed to Update Pull Request #" + pull_number+ " for " + owner + "/" + repo)
}
}
}
} else if (cxAction == utils.OSA_SCAN) {
//TODO
core.info("Github Issues was not implemented for cxAction: " + cxAction)
} else {
core.info("Github Issues was not implemented for cxAction: " + cxAction)
}
} else {
core.info("Unable to authenticate to octokit. Please provide a proper GITHUB_TOKEN")
}
} else {
core.info('No issues will be created')
}
}
async function getIssues(owner, repo, octokit) {
core.info("\nGetting Issues from " + owner + "/" + repo)
let issues = []
for (let i = 0; i < 1000000; i++) {//TODO Find a better way to get total number of pages for issues
let res = await octokit.issues.listForRepo({
owner: owner,
repo: repo,
state: "all",
page: i
})
if (res.status == HTTP_STATUS_OK) {
issues = issues.concat(res.data)
if (res.data.length < 30) {
break
}
} else {
core.info("Cannot retrieve issues page " + i + " from " + owner + "/" + repo)
return issues
}
}
return issues
}
async function updateIssue(owner, repo, octokit, body, githubLabels, githubAssignees, githubMilestone, repositoryIssue, state) {
core.info("\nUpdating ticket #" + repositoryIssue.number + " for " + owner + "/" + repo)
let uniqueLabels = githubLabels
for (let i = 0; i < repositoryIssue.labels.length; i++) {
const label = repositoryIssue.labels[i].name
if (!uniqueLabels.includes(label)) {
uniqueLabels.push(label)
}
}
let uniqueAssignees = githubAssignees
for (let i = 0; i < repositoryIssue.assignees.length; i++) {
const assignee = repositoryIssue.assignees[i].login
if (!uniqueAssignees.includes(assignee)) {
uniqueAssignees.push(assignee)
}
}
const issueUpdated = await octokit.issues.update({
owner: owner,
repo: repo,
issue_number: repositoryIssue.number,
title: repositoryIssue.title,
body: repositoryIssue.body,
state: state,
milestone: githubMilestone,
labels: uniqueLabels,
assignees: uniqueAssignees
})
if (issueUpdated.status == HTTP_STATUS_OK) {
const issueCommented = await octokit.issues.createComment({
owner: owner,
repo: repo,
issue_number: repositoryIssue.number,
body: body
})
if (issueCommented.status == HTTP_STATUS_CREATED) {
core.info("New Comment was Created for Issue #" + repositoryIssue.number + " for " + owner + "/" + repo)
return issueCommented.data
} else {
core.info("Cannot Create Comment for issue #" + repositoryIssue.number + " from " + owner + "/" + repo)
return issueCommented.data
}
} else {
core.info("Cannot update issue #" + repositoryIssue.number + " from " + owner + "/" + repo)
return issueUpdated.data
}
}
async function createIssue(owner, repo, octokit, title, body, githubLabels, githubAssignees, githubMilestone, id, state) {
core.info("\nCreating ticket #" + id + " for " + owner + "/" + repo)
let issueCreated = await octokit.issues.create({
owner: owner,
repo: repo,
title: title,
body: body,
assignees: githubAssignees,
labels: githubLabels,
milestone: githubMilestone
})
if (issueCreated.status == HTTP_STATUS_CREATED) {
const issueId = issueCreated.data.number
const issueUrl = issueCreated.data.html_url
core.info("Ticket #" + issueId + " was Created for " + owner + "/" + repo)
core.info(issueUrl)
const issueUpdated = await octokit.issues.update({
owner: owner,
repo: repo,
issue_number: issueId,
title: title,
body: body,
state: state,
milestone: githubMilestone,
labels: githubLabels,
assignees: githubAssignees
})
if (issueUpdated.status == HTTP_STATUS_OK) {
core.info("Update State of Issue #" + issueId + " from " + owner + "/" + repo)
return issueId
} else {
core.info("Cannot update issue #" + issueId + " from " + owner + "/" + repo)
return issueId
}
} else {
core.info("Ticket #" + id + " failed to be Created for " + owner + "/" + repo)
return false
}
}
async function createCommitComment(owner, repo, octokit, commitSha, body, path, position) {
core.info("\nCreating Comment with Checkmarx Summary for Commit #" + commitSha + " for " + owner + "/" + repo)
const commitCommentCreated = await octokit.repos.createCommitComment({
owner: owner,
repo: repo,
commit_sha: commitSha,
body: body,
path: path,
position: position
})
if (commitCommentCreated.status == HTTP_STATUS_CREATED) {
const commentUrl = commitCommentCreated.data.html_url
core.info("New Comment was Created for Commit #" + commitSha + " for " + owner + "/" + repo)
core.info(commentUrl)
return true
} else {
core.info("New Comment failed to be created for Commit #" + commitSha + " for " + owner + "/" + repo)
return false
}
}
module.exports = {
createIssues: createIssues,
createIssue: createIssue,
updateIssue: updateIssue,
createCommitComment: createCommitComment
}