-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
196 lines (189 loc) · 6.15 KB
/
index.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
const core = require('@actions/core');
const github = require('@actions/github');
const { Octokit } = require('@octokit/rest');
const { _ } = require('lodash');
const action = require('./src/github/action');
const ctx = require('./src/github/context');
const {
noIssues,
minorIssues,
criticalHighIssues,
criticalHighIssuesFixed,
minorIssuesFixed,
} = require('./src/messages');
run();
async function run() {
try {
// Get github context data
const context = github.context;
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(context.payload, undefined, 2);
console.log(`The event payload: ${payload}`);
// Get GitHub Personal Access Token and user name
const ghPat = core.getInput('gh-pat', { required: true });
const actionUser = core.getInput('action-user', { required: true });
const octokit = new Octokit({ auth: ghPat });
const repoName = ctx.getRepoName(context);
const repoOwner = ctx.getRepoOwner(context);
const pullRequestHeadSha = ctx.getPullRequestHeadSha(context);
const pullRequestNumber = ctx.getPullRequestNumber(context);
const pullRequestTitle = ctx.getPullRequestTitle(context);
const pullRequestBody = ctx.getPullRequestBody(context);
const pullRequestCreator = ctx.getPullRequestCreator(context);
const repoDefaultBranch = ctx.getDefaultBranch(context);
const repoInfo = await action.getVulnerabilities(repoName, repoOwner, process.env.GITHUB_TOKEN || ghPat);
const repoPRs = repoInfo.pullRequests.nodes;
const [prInfo] = _.filter(repoPRs, (node) => node.number === pullRequestNumber);
const prCommits = prInfo.commits.nodes;
const repoVulnerabilityAlerts = _.filter(repoInfo.vulnerabilityAlerts.nodes, { state: 'OPEN' });
const criticalIssues = _.filter(repoVulnerabilityAlerts, (node) =>
node.securityVulnerability.severity.match(/CRITICAL|HIGH/),
);
const otherIssues = _.filter(
repoVulnerabilityAlerts,
(node) => !node.securityVulnerability.severity.match(/CRITICAL|HIGH/),
);
console.log(`Repo alerts: ${JSON.stringify(repoVulnerabilityAlerts, undefined, 2)}`);
// make 'security-monitor' status check required if it's not
const requiredStatusChecks = await octokit.rest.repos.getAllStatusCheckContexts({
owner: repoOwner,
repo: repoName,
branch: repoDefaultBranch,
});
if (!requiredStatusChecks.data.includes('security-monitor')) {
requiredStatusChecks.data.push('security-monitor');
await octokit.rest.repos.updateStatusCheckProtection({
owner: repoOwner,
repo: repoName,
branch: repoDefaultBranch,
contexts: requiredStatusChecks.data,
});
}
// check if PR already has a comment from the action.
const prComments = await octokit.rest.issues.listComments({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestNumber,
});
let actionComment;
if (prComments.data.length) {
actionComment = _.findLast(
prComments.data,
(comment) => comment.user.login === (process.env.GITHUB_ACTION_USER || actionUser),
);
}
// check vulnerability alerts
if (repoVulnerabilityAlerts.length) {
// repo has vulnerability alerts
console.log(
`${context.payload.repository.name} repo has ${repoVulnerabilityAlerts.length} vulnerability alert(s)`,
);
if (criticalIssues.length) {
const commitStatus = await action.findStatus(
criticalIssues,
prCommits,
pullRequestCreator,
pullRequestTitle,
pullRequestBody,
);
const prMessage =
commitStatus === 'failure'
? criticalHighIssues(
criticalIssues.length,
await action.issuesMessage(repoInfo, criticalIssues),
)
: criticalHighIssuesFixed;
if (!pullRequestCreator.match(/dependabot\[bot\]|oneflow/)) {
if (actionComment) {
await octokit.rest.issues.updateComment({
owner: repoOwner,
repo: repoName,
comment_id: actionComment.id,
body: `${prMessage}\n\n_Latest update timestamp: ${new Date().toISOString()}_`,
});
} else {
await octokit.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestNumber,
body: prMessage,
});
}
}
await octokit.rest.repos.createCommitStatus({
context: 'security-monitor',
owner: repoOwner,
repo: repoName,
sha: pullRequestHeadSha,
state: commitStatus,
});
} else {
if (!pullRequestCreator.match(/dependabot\[bot\]|oneflow/)) {
const commitStatus = await action.findStatus(
otherIssues,
prCommits,
pullRequestCreator,
pullRequestTitle,
pullRequestBody,
);
const prMessage =
commitStatus === 'failure'
? minorIssues(await action.issuesMessage(repoInfo, otherIssues))
: minorIssuesFixed;
if (actionComment) {
await octokit.rest.issues.updateComment({
owner: repoOwner,
repo: repoName,
comment_id: actionComment.id,
body: `${prMessage}\n\n_Latest update timestamp: ${new Date().toISOString()}_`,
});
} else {
await octokit.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestNumber,
body: prMessage,
});
}
}
await octokit.rest.repos.createCommitStatus({
context: 'security-monitor',
owner: repoOwner,
repo: repoName,
sha: pullRequestHeadSha,
state: 'success',
});
}
} else {
// repo doesn't have any vulnerability alerts
console.log(`${context.payload.repository.name} repo doesn't have any vulnerability alerts`);
if (actionComment) {
await octokit.rest.issues.updateComment({
owner: repoOwner,
repo: repoName,
comment_id: actionComment.id,
body: `${noIssues}\n\n_Latest update timestamp: ${new Date().toISOString()}_`,
});
} else {
await octokit.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: pullRequestNumber,
body: noIssues,
});
}
await octokit.rest.repos.createCommitStatus({
context: 'security-monitor',
owner: repoOwner,
repo: repoName,
sha: pullRequestHeadSha,
state: 'success',
});
}
} catch (error) {
core.setFailed(error.message);
}
}
module.exports = {
run,
};