-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (149 loc) · 5 KB
/
script.js
File metadata and controls
166 lines (149 loc) · 5 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
import fetch from "node-fetch";
import csv from "csv-writer";
// Set the necessary authentication credentials
const username = "YOUR_USERNAME"; // the username of the user who created the app password
const workspace = "YOUR_WORKSPACE"; // the workspace where the repositories are located
const password = "YOUR_APP_PASSWORD"; // the app password used in your receptor
const auditStartDate = "0000-00-00"; // the date from which your audit was started in the format YYYY-MM-DD
const auditEndDate = "0000-00-00"; // the end date for you audit in the format YYYY-MM-DD
// get all repositories for a workspace
async function getRepositories() {
let repositories = [];
const url = `https://api.bitbucket.org/2.0/repositories/${workspace}`;
try {
const response = await fetch(url, {
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString(
"base64"
)}`,
},
});
if (response.ok) {
const data = await response.json();
repositories = data.values.map((repo) => repo.name);
} else {
console.log(`Error: ${response.status} - ${response.statusText}`);
}
} catch (error) {
console.log("Error:", error.message);
}
return repositories;
}
// get all pull requests ids for a repository
async function getAllPullRequests(repository) {
var auditStart = new Date(auditStartDate.concat(" 00:00:00"));
let isoStart = auditStart.toISOString();
var auditEnd = new Date(auditEndDate.concat(" 23:59:59"));
let isoEnd = auditEnd.toISOString();
let query = encodeURIComponent(
`(state = "merged") and created_on >= ${isoStart} and created_on < ${isoEnd}`
);
const pullRequestsUrl = `https://api.bitbucket.org/2.0/repositories/${workspace}/${repository}/pullrequests?q=${query}`;
let PRs = [];
try {
let allPullRequests = [];
let nextUrl = pullRequestsUrl;
while (nextUrl) {
const response = await fetch(nextUrl, {
headers: {
Authorization: `Basic ${Buffer.from(
`${username}:${password}`
).toString("base64")}`,
},
});
if (response.ok) {
const pullRequestsData = await response.json();
allPullRequests = allPullRequests.concat(pullRequestsData.values);
nextUrl = pullRequestsData.next;
console.log(
`Fetched ${allPullRequests.length} of ${pullRequestsData.size} pull requests for ${repository}`
);
} else {
console.log(`Error: ${response.status} - ${response.statusText}`);
break;
}
}
PRs = PRs.concat(allPullRequests);
} catch (error) {
console.log("Error:", error.message);
}
return PRs;
}
// get details for a single pull request
async function getPRDetails(repository, pullRequestId) {
const pullRequestDetailsUrl = `https://api.bitbucket.org/2.0/repositories/${workspace}/${repository}/pullrequests/${pullRequestId}`;
let id,
title,
date,
author,
approver = "No approver found";
try {
const response = await fetch(pullRequestDetailsUrl, {
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString(
"base64"
)}`,
},
});
if (response.ok) {
const pullRequestDetails = await response.json();
id = pullRequestDetails.id;
title = pullRequestDetails.title;
date = new Date(pullRequestDetails.created_on).toLocaleDateString();
author = pullRequestDetails.author.display_name;
for (let i = 0; i < pullRequestDetails.participants.length; i++) {
if (
pullRequestDetails.participants[i].role === "REVIEWER" &&
pullRequestDetails.participants[i].approved === true
) {
approver = pullRequestDetails.participants[i].user.display_name;
break;
}
}
} else {
console.log(`Error: ${response.status} - ${response.statusText}`);
}
} catch (error) {
console.log("Error:", error.message);
}
return {
id: id,
title: title,
date: date,
author: author,
approver: approver,
};
}
// generate csv
async function generateApproverCSV() {
let repos = await getRepositories();
let AllPRs = [];
for (let i = 0; i < repos.length; i++) {
let PRs = await getAllPullRequests(repos[i]);
let rows = PRs.map((pr) => {
return {
id: pr.id,
repository: repos[i],
title: pr.title,
date: new Date(pr.created_on).toLocaleDateString(),
author: pr.author.display_name,
link: pr.links.html.href,
};
});
AllPRs = AllPRs.concat(rows);
}
const csvWriter = csv.createObjectCsvWriter({
path: "pull_requests.csv",
header: [
{ id: "id", title: "PR ID" },
{ id: "repository", title: "Repository" },
{ id: "title", title: "Pull Request Title" },
{ id: "date", title: "Date" },
{ id: "author", title: "Author" },
{ id: "link", title: "Link" },
],
});
await csvWriter.writeRecords(AllPRs);
console.log('CSV file "pull_requests.csv" generated successfully.');
}
generateApproverCSV();