-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_pr_data.js
80 lines (71 loc) · 2.7 KB
/
fetch_pr_data.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
(async function () {
chrome.storage.local.get(["githubToken"], async (result) => {
const token = result.githubToken;
const prUrlMatch = window.location.href.match(/github\.com\/(.+?)\/(.+?)\/pull\/(\d+)/);
if (!prUrlMatch) {
alert("Not a valid PR page.");
return;
}
const [_, owner, repo, prNumber] = prUrlMatch;
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`;
const filesUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/files`;
const commitsUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/commits`;
const reviewCommentsUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/comments`;
const prReviewsUrl = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/reviews`;
async function fetchAllPages(url, token) {
let page = 1;
const perPage = 100;
let results = [];
let hasMore = true;
while (hasMore) {
const pagedUrl = `${url}?per_page=${perPage}&page=${page}`;
const response = await fetch(pagedUrl, {
headers: { Authorization: `token ${token}` }
});
const data = await response.json();
results = results.concat(data);
hasMore = data.length === perPage;
page++;
}
return results;
}
try {
const [
prRes,
filesData,
commitsData,
reviewCommentsData,
prReviewsData,
] = await Promise.all([
fetch(apiUrl, { headers: { Authorization: `token ${token}` } }).then(res => res.json()),
fetchAllPages(filesUrl, token),
fetchAllPages(commitsUrl, token),
fetchAllPages(reviewCommentsUrl, token),
fetchAllPages(prReviewsUrl, token),
]);
const fullPRData = {
...prRes,
files: filesData,
commits: commitsData,
review_comments: reviewCommentsData,
pr_reviews: prReviewsData,
};
const blob = new Blob([JSON.stringify(fullPRData, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `PR-${prNumber}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
// Notify the content script that the download is complete
chrome.runtime.sendMessage({ type: "downloadComplete" });
} catch (error) {
console.error("Error fetching PR data:", error);
alert("Failed to fetch PR data.");
// Notify the content script that the download is complete
chrome.runtime.sendMessage({ type: "downloadComplete" });
}
});
})();