-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (111 loc) · 4.55 KB
/
index.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
const { Octokit } = require("@octokit/core"); // for making HTTP requests to the GitHub API
const esprima = require('esprima'); // for parsing JavaScript code
require('dotenv').config(); // for getting the .env file to work
const username = 'erwilliams64'; // Replace with your GitHub username
const token = process.env.GITHUB_TOKEN; // Make sure the .env file has GITHUB_TOKEN set. The token is generated using this flow: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token
const octokit = new Octokit({
auth: token,
});
async function getAllReposWithCommits(username) {
try {
const response = await octokit.request('GET /search/commits', {
q: `author:${username}`,
headers: {
'Accept': 'application/vnd.github.cloak-preview'
}
});
const repoNames = new Set();
response.data.items.forEach(commit => {
repoNames.add(commit.repository.full_name); // Using full_name to get the 'owner/repo' format
});
const reposArray = Array.from(repoNames);
console.log('Repositories with Commits:', reposArray); // Log the list of repositories
return reposArray;
} catch (error) {
console.error("Error fetching repositories with commits:", error);
return [];
}
}
async function getCommitDiff(owner, repo, sha) {
try {
const response = await octokit.request('GET /repos/{owner}/{repo}/commits/{sha}', {
owner,
repo,
sha
});
return response.data.files; // This will contain the diff for each file in the commit
} catch (error) {
console.error(`Error fetching commit diff for ${sha} in repo ${repo}:`, error.response?.status, error.message);
return [];
}
}
async function getCommitsForRepo(owner, repo) {
console.log(`Fetching commits for repo: ${owner}/${repo}`);
try {
const response = await octokit.request('GET /repos/{owner}/{repo}/commits', {
owner,
repo
});
return response.data;
} catch (error) {
console.error(`Error fetching commits for repo ${repo}:`, error.response?.status, error.message);
return [];
}
}
function parseJavaScriptFunctions(content) {
try {
const ast = esprima.parseScript(content);
const functions = [];
esprima.tokenize(content, {}, (node) => {
if (node.type === 'FunctionDeclaration') {
functions.push(node.value);
}
});
return functions;
} catch (error) {
console.error("Error parsing JavaScript:", error);
return [];
}
}
// const octokit = new Octokit({
// auth: token
// })
// await octokit.request('GET /repos/{owner}/{repo}/commits', {
// owner: username,
// repo: "erwilliams64/js-skill-tree",
// headers: {
// 'X-GitHub-Api-Version': '2022-11-28'
// }
// })
async function main() {
const repos = await getAllReposWithCommits(username);
for (const repoFullName of repos) {
// Correctly extract the owner and repo from the full repo name
const [owner, repo] = repoFullName.split("/");
const commits = await getCommitsForRepo(owner, repo);
for (const commit of commits) {
const commitSha = commit.sha;
const commitDate = commit.commit.committer.date;
const commitDiff = await getCommitDiff(owner, repo, commitSha);
console.log(`Repo: ${repoFullName}, Commit SHA: ${commitSha}, Date: ${commitDate}`);
console.log(`Repo: ${repo}, Commit SHA: ${commitSha}, Date: ${commitDate}`);
console.log(commitDiff)
// // Fetching the actual JavaScript code for each commit is a complex task.
// // This requires either cloning the repository and checking out each commit,
// // or fetching individual files via the API.
// const commitCode = ""; // Replace this with the actual code fetched for the commit
// const functions = parseJavaScriptFunctions(commitCode);
// console.log(`In repo ${repo}, commit ${commit.sha} has functions:`, functions);
}
}
}
async function checkTokenScopes() {
try {
const response = await octokit.request('GET /user');
console.log('Token Scopes:', response.headers['x-oauth-scopes']);
} catch (error) {
console.error('Error:', error.message);
}
}
main();
checkTokenScopes();