-
Notifications
You must be signed in to change notification settings - Fork 2
/
codeqlAlerts.js
165 lines (151 loc) · 4.67 KB
/
codeqlAlerts.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
#!/usr/bin/env node
"use strict";
require("dotenv").config();
const _ = require("lodash");
const moment = require("moment");
const owner = "Sparkpost";
const Promise = require("bluebird");
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
userAgent: "secrets v1.2.3",
// Set GitHub Auth Token in environment variable
});
const [, , ...args] = process.argv;
// .number, .created_at, .url, .html_url, .state, .dismissed_by.login, .dismissed_at, .dismissed_reason, .rule.id, .rule.severity, .rule.description, .tool.name, .most_recent_instance.classifications[]]
function getCodeAlerts(repos) {
const codeAlerts = [];
return Promise.each(repos, ({ name, org }) => {
const sortedAlerts = {};
const summary = {};
return octokit
.paginate(octokit.codeScanning.listAlertsForRepo, {
owner: org,
repo: name,
})
.then((alerts) => {
const filteredAlerts = filterCodeAlerts(alerts);
filteredAlerts.forEach((alert) => {
var rule = alert.rule.description;
if (!sortedAlerts[rule]) {
sortedAlerts[rule] = { count: 1 };
sortedAlerts[rule]["createdAt"] = alert.created_at;
sortedAlerts[rule]["severity"] = alert.rule.severity;
} else {
sortedAlerts[rule]["count"] += 1;
}
if (alert.rule.severity === "error") {
summary.error = (summary.error || 0) + 1;
}
if (alert.rule.severity === "warning") {
summary.warning = (summary.warning || 0) + 1;
}
});
return sortedAlerts;
})
.then((sortedAlerts) => {
const blocks = Object.keys(sortedAlerts).map((alert) =>
buildBlocks("code", alert, sortedAlerts[alert])
);
codeAlerts.push({ repo: name, summary, blocks });
})
.catch((error) => {
// if it's a 403, that means code scanning was not enabled on this repo.
// See https://docs.github.com/rest/reference/code-scanning#list-code-scanning-alerts-for-a-repository
if (error.status !== 403) {
console.error(error);
}
});
}).then(() => codeAlerts);
}
function filterCodeAlerts(alerts) {
return alerts.filter((alert) => {
return (
alert.rule.severity !== "note" &&
alert.most_recent_instance.classifications !== "test" &&
alert.most_recent_instance.state === "open" &&
moment(alert.created_at).add(14, "days") < moment()
);
});
}
// .number, .html_url, .state, .secret_type, .secret, .resolution, .resolved_by, .resolved_at]
function getSecretAlerts(repos) {
const repoAlerts = Promise.map(repos, ({ name, org }) => {
const sortedAlerts = {};
const summary = {};
return octokit
.paginate(octokit.secretScanning.listAlertsForRepo, {
owner: org,
repo: name,
})
.then((alerts) => {
alerts.forEach((alert) => {
if (alert.state === "open") {
const type = alert.secret_type;
if (!sortedAlerts[type]) {
sortedAlerts[type] = { count: 1 };
sortedAlerts[type]["createdAt"] = alert.created_at;
} else {
sortedAlerts[type]["count"] += 1;
}
summary["secret"] = (summary["secret"] || 0) + 1;
}
});
return sortedAlerts;
})
.then((sortedAlerts) => {
const blocks = [];
const alerts = Object.keys(sortedAlerts);
alerts.forEach((alert) => {
blocks.push(buildBlocks("secret", alert, sortedAlerts[alert]));
});
return { repo: name, summary, blocks };
})
.catch((err) => {
if (err.status === 404) {
// secret alerts does not support public repos
return;
}
});
}).catch((err) => {
throw new Error(
`Could not retrieve vulnerability alerts - status code ${err.status}`
);
});
return repoAlerts.filter(function (alert) {
return alert != null;
});
}
function buildBlocks(alertType, name, { count, createdAt, severity }) {
const secretBlock = {
type: "section",
fields: [
{
type: "mrkdwn",
text: `*${name}* x ${count}`,
},
{
type: "mrkdwn",
text: `*Created on* ${createdAt}`,
},
],
};
const codeBlock = {
type: "section",
fields: [
{
type: "mrkdwn",
text: `*${name}* x ${count} \n*Severity Level*: (${severity})`,
},
{
type: "mrkdwn",
text: `*Created on*\n${createdAt}`,
},
],
};
return alertType === "secret" ? secretBlock : codeBlock;
}
module.exports = {
getCodeAlerts,
getSecretAlerts,
};