-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_commits.js
More file actions
74 lines (61 loc) · 2.45 KB
/
fetch_commits.js
File metadata and controls
74 lines (61 loc) · 2.45 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
const fs = require('fs');
const fetch = require('node-fetch');
const path = require('path');
// Environment variables
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const REPO_OWNER = 'PatLittle';
const REPO_NAME = 'GC-Ref-Data-Tracker';
const BRANCH_NAME = 'main';
const OUTPUT_FILE = 'docs/commits.json'; // Location to store the fetched commit data
// GitHub API URL for fetching commits
const GITHUB_API_URL = `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/commits?sha=${BRANCH_NAME}&per_page=500`;
// Fetch commit details (including diffs) for a given commit SHA
async function fetchCommitDetails(commitSha) {
const commitApiUrl = `https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/commits/${commitSha}`;
const response = await fetch(commitApiUrl, {
headers: {
'Authorization': `token ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (!response.ok) {
throw new Error(`Failed to fetch commit details for ${commitSha}: ${response.statusText}`);
}
const commitData = await response.json();
return commitData;
}
// Fetch commits and their diffs
async function fetchCommits() {
try {
const response = await fetch(GITHUB_API_URL, {
headers: {
'Authorization': `token ${GITHUB_TOKEN}`,
'Accept': 'application/vnd.github.v3+json'
}
});
if (!response.ok) {
throw new Error(`GitHub API responded with ${response.status}: ${response.statusText}`);
}
const commits = await response.json();
console.log(`Fetched ${commits.length} commits.`);
// Array to hold the fetched commits with diffs
const detailedCommits = [];
// Fetch commit details (including diffs) for each commit
for (const commit of commits) {
const commitSha = commit.sha;
console.log(`Fetching details for commit ${commitSha}`);
const commitDetails = await fetchCommitDetails(commitSha);
// Add the commit details (including diffs) to the detailedCommits array
detailedCommits.push(commitDetails);
}
// Write the detailed commits (with diffs) to a JSON file
fs.mkdirSync(path.dirname(OUTPUT_FILE), { recursive: true });
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(detailedCommits, null, 2));
console.log(`Commit data with diffs saved to ${OUTPUT_FILE}`);
} catch (error) {
console.error('Error fetching commits:', error);
process.exit(1); // Exit with failure
}
}
// Run the fetchCommits function
fetchCommits();