-
Notifications
You must be signed in to change notification settings - Fork 819
Expand file tree
/
Copy pathcheck-dependabot.ts
More file actions
171 lines (146 loc) · 4.8 KB
/
check-dependabot.ts
File metadata and controls
171 lines (146 loc) · 4.8 KB
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
#!/usr/bin/env ts-node
/**
* Check Dependabot Alerts
*
* This script fetches and displays Dependabot security alerts for the repository.
* Requires GitHub CLI (gh) to be installed and authenticated.
*
* Usage:
* npx ts-node scripts/check-dependabot.ts
*/
import { execSync } from 'child_process';
interface DependabotAlert {
number: number;
state: string;
dependency: {
package: {
name: string;
ecosystem: string;
};
};
security_advisory: {
severity: string;
summary: string;
description: string;
ghsa_id: string;
cve_id?: string;
};
security_vulnerability: {
vulnerable_version_range: string;
first_patched_version?: {
identifier: string;
};
};
url: string;
html_url: string;
created_at: string;
updated_at: string;
}
const checkGhInstalled = (): boolean => {
try {
execSync('gh --version', { stdio: 'ignore' });
return true;
} catch {
return false;
}
};
const checkGhAuth = (): boolean => {
try {
execSync('gh auth status', { stdio: 'ignore' });
return true;
} catch {
return false;
}
};
const fetchDependabotAlerts = (): DependabotAlert[] => {
try {
const output = execSync('gh api repos/aws-amplify/amplify-cli/dependabot/alerts --paginate', {
encoding: 'utf8',
maxBuffer: 10 * 1024 * 1024,
});
return JSON.parse(output);
} catch (error: any) {
console.error('❌ Failed to fetch Dependabot alerts:', error.message);
return [];
}
};
const groupAlertsBySeverity = (alerts: DependabotAlert[]): Map<string, DependabotAlert[]> => {
const grouped = new Map<string, DependabotAlert[]>();
const severityOrder = ['critical', 'high', 'medium', 'low'];
for (const severity of severityOrder) {
grouped.set(severity, []);
}
for (const alert of alerts) {
const severity = alert.security_advisory.severity.toLowerCase();
if (grouped.has(severity)) {
grouped.get(severity)!.push(alert);
}
}
return grouped;
};
const printAlertSummary = (alerts: DependabotAlert[]): void => {
const openAlerts = alerts.filter((a) => a.state === 'open');
const grouped = groupAlertsBySeverity(openAlerts);
console.log('\n=== Dependabot Alert Summary ===\n');
console.log(`Total Open Alerts: ${openAlerts.length}`);
console.log(`Total Closed Alerts: ${alerts.filter((a) => a.state !== 'open').length}\n`);
console.log('By Severity:');
for (const [severity, severityAlerts] of grouped) {
if (severityAlerts.length > 0) {
const icon = severity === 'critical' ? '🔴' : severity === 'high' ? '🟠' : severity === 'medium' ? '🟡' : '🟢';
console.log(` ${icon} ${severity.toUpperCase()}: ${severityAlerts.length}`);
}
}
console.log('\n=== Alert Details ===\n');
for (const [severity, severityAlerts] of grouped) {
if (severityAlerts.length === 0) continue;
const icon = severity === 'critical' ? '🔴' : severity === 'high' ? '🟠' : severity === 'medium' ? '🟡' : '🟢';
console.log(`\n${icon} ${severity.toUpperCase()} Severity (${severityAlerts.length} alerts):\n`);
for (const alert of severityAlerts) {
console.log(`Alert #${alert.number}: ${alert.security_advisory.summary}`);
console.log(` Package: ${alert.dependency.package.name} (${alert.dependency.package.ecosystem})`);
console.log(` Vulnerable: ${alert.security_vulnerability.vulnerable_version_range}`);
if (alert.security_vulnerability.first_patched_version) {
console.log(` Patched: ${alert.security_vulnerability.first_patched_version.identifier}`);
} else {
console.log(` Patched: No patch available yet`);
}
if (alert.security_advisory.cve_id) {
console.log(` CVE: ${alert.security_advisory.cve_id}`);
}
console.log(` GHSA: ${alert.security_advisory.ghsa_id}`);
console.log(` URL: ${alert.html_url}`);
console.log('');
}
}
if (openAlerts.length === 0) {
console.log('✅ No open Dependabot alerts found!\n');
}
};
const main = (): void => {
console.log('🔍 Checking Dependabot alerts...\n');
// Check prerequisites
if (!checkGhInstalled()) {
console.error('❌ GitHub CLI (gh) is not installed.');
console.error('\nInstall instructions:');
console.error(' macOS: brew install gh');
console.error(' Windows: winget install GitHub.cli');
console.error(' Linux: https://github.com/cli/cli#installation');
process.exit(1);
}
if (!checkGhAuth()) {
console.error('❌ GitHub CLI is not authenticated.');
console.error('\nRun: gh auth login');
process.exit(1);
}
// Fetch and display alerts
const alerts = fetchDependabotAlerts();
if (alerts.length === 0) {
console.log('✅ No Dependabot alerts found (or failed to fetch).\n');
return;
}
printAlertSummary(alerts);
};
if (require.main === module) {
main();
}