-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetOrgSecretAlerts.js
158 lines (143 loc) · 5.29 KB
/
getOrgSecretAlerts.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
// getOrgSecretAlerts.js
import 'dotenv/config';
import { Octokit } from "octokit";
import { createObjectCsvWriter as createCsvWriter } from 'csv-writer';
// Initialize Octokit with authentication
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
});
// Initialize CSV writer
const csvWriter = createCsvWriter({
path: 'output.csv',
header: [
{ id: 'secret_type', title: 'SECRET_TYPE' },
{ id: 'location_type', title: 'LOCATION_TYPE' },
{ id: 'path', title: 'PATH' },
{ id: 'url', title: 'URL' }
]
});
/**
* Extracts repository information from a commit URL.
* @param {string} commitUrl - The commit URL.
* @returns {Object} An object containing orgName and repoName.
*/
const extractRepoInfoFromCommitUrl = (commitUrl) => {
const urlParts = commitUrl.split('/');
return {
orgName: urlParts[4],
repoName: urlParts[5]
};
};
/**
* Fetches and processes organization secret alerts.
*/
async function getOrgSecretAlerts() {
try {
const alerts = await octokit.paginate('GET /orgs/{org}/secret-scanning/alerts', {
org: process.env.GITHUB_ORG,
per_page: 100,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
console.log(`Found ${alerts.length} alerts`);
const records = [];
let counter = 1; // Initialize counter
for (let i = 0; i < alerts.length; i++) {
const alert = alerts[i];
console.log(`Processing alert ${i + 1} of ${alerts.length}`);
if (!alert.locations_url) {
console.log(`Alert ${alert.number} is missing locations_url`);
continue;
}
let locations;
try {
locations = await octokit.paginate(`GET ${alert.locations_url}`, {
headers: {
'X-GitHub-Api-Version': '2022-11-28'
},
per_page: 100
});
} catch (error) {
console.error(`Failed to fetch locations for alert ${alert.number}:`, error);
continue;
}
console.log(`Locations: ${JSON.stringify(locations, null, 2)}`);
locations.forEach(location => {
let url;
let path;
if (location.details.path) {
const commitUrl = location.details.commit_url || location.details.page_url;
const { orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl);
url = `https://github.com/${orgName}/${repoName}/commit/${location.details.commit_sha}`;
path = `${repoName}/${location.details.path}`;
} else {
let commitUrl;
let orgName;
let repoName;
switch (location.type) {
case 'commit':
case 'wiki_commit':
// No URL or path assignment needed
break;
case 'issue_title':
commitUrl = location.details.issue_title_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'issue_body':
commitUrl = location.details.issue_body_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'issue_comment':
commitUrl = location.details.issue_comment_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'pull_request_title':
commitUrl = location.details.pull_request_title_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'pull_request_body':
commitUrl = location.details.pull_request_body_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'pull_request_comment':
commitUrl = location.details.pull_request_comment_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'pull_request_review':
commitUrl = location.details.pull_request_review_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
case 'pull_request_review_comment':
commitUrl = location.details.pull_request_review_comment_url;
({ orgName, repoName } = extractRepoInfoFromCommitUrl(commitUrl));
path = `${orgName}/${repoName}`;
break;
default:
console.log(`Unknown type: ${location.type}`);
return;
}
}
records.push({
secret_type: `${alert.secret_type}_${counter}`, // Append counter to secret_type
location_type: location.type, // Add location type
path: path, // Add path
url: url // Add URL
});
});
counter++; // Increment counter for each alert object
}
await csvWriter.writeRecords(records);
console.log('CSV file written successfully');
} catch (error) {
console.error('Failed to fetch organization secret alerts:', error);
}
}
getOrgSecretAlerts();