-
Notifications
You must be signed in to change notification settings - Fork 2
/
dependabotAlerts.js
131 lines (123 loc) · 3.61 KB
/
dependabotAlerts.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
"use strict";
const _ = require("lodash");
const Promise = require("bluebird");
const token = process.env.GITHUB_TOKEN;
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({
auth: token,
// Set GitHub Auth Token in environment variable
});
function getAlerts(repos) {
return Promise.map(repos, async ({ org, name }) => {
const blocks = [];
const summary = {};
const alerts = await getVulnerabilities(org, name);
const criticalAlerts = alerts.filter(
(alert) =>
alert.severity === "critical" && !alert.dismissed && !alert.fixed
);
const highAlerts = alerts.filter(
(alert) => alert.severity === "high" && !alert.dismissed && !alert.fixed
);
const mediumAlerts = alerts.filter(
(alert) =>
alert.severity === "moderate" && !alert.dismissed && !alert.fixed
);
const lowAlerts = alerts.filter(
(alert) => alert.severity === "low" && !alert.dismissed && !alert.fixed
);
// Dependabot calls these "moderate", but SparkPost categorizes these as "medium"
mediumAlerts.forEach((mediumAlert) => (mediumAlert.severity = "medium"));
if (criticalAlerts.length > 0) {
criticalAlerts.forEach((alert) => {
blocks.push(buildBlocks(alert));
});
summary["critical"] = criticalAlerts.length;
}
if (highAlerts.length > 0) {
highAlerts.forEach((alert) => {
blocks.push(buildBlocks(alert));
});
summary["high"] = highAlerts.length;
}
if (mediumAlerts.length > 0) {
mediumAlerts.forEach((alert) => {
blocks.push(buildBlocks(alert));
});
summary["medium"] = mediumAlerts.length;
}
if (lowAlerts.length > 0) {
lowAlerts.forEach((alert) => {
blocks.push(buildBlocks(alert));
});
summary["low"] = lowAlerts.length;
}
return { repo: name, summary, blocks };
});
}
const getVulnerabilities = async (owner, repo) => {
const query = getVulnerabilityAlertQuery(owner, repo);
const results = await octokit.graphql(query);
const alerts = _.map(results.repository.vulnerabilityAlerts.edges, "node");
return _.map(alerts, (alert) => {
return {
id: alert.id,
createdAt: alert.createdAt,
severity: _.lowerCase(alert.securityVulnerability.severity),
description: alert.securityAdvisory.description,
packageName: alert.securityVulnerability.package.name,
dismissed: !!alert.dismissReason,
fixed: !!alert.fixReason,
};
});
};
const getVulnerabilityAlertQuery = (owner, repo, limit = 50) => {
return `query {
repository(owner:"${owner}", name:"${repo}") {
vulnerabilityAlerts(last:${limit}) {
edges {
node {
id
createdAt
vulnerableRequirements
vulnerableManifestFilename
dismissReason
fixReason
securityAdvisory {
description
ghsaId
id
origin
permalink
summary
}
securityVulnerability {
severity
package { name }
firstPatchedVersion { identifier }
}
}
}
}
}
}`;
};
function buildBlocks(alert) {
return {
type: "section",
block_id: `section-${alert.id}`,
fields: [
{
type: "mrkdwn",
text: `*Package (Severity Level)*\n${alert.packageName} (${alert.severity})`,
},
{
type: "mrkdwn",
text: `*Created on*\n${alert.createdAt}`,
},
],
};
}
module.exports = {
getAlerts,
};