-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubAPI.js
156 lines (138 loc) · 6.39 KB
/
githubAPI.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
const { Octokit } = require("@octokit/core");
const fileHandler = require("./middleware/fileHandler")
const PAGINATION_CONTROL_INDICATOR = 'rel="next", <https';
const FILENAME_PREFIX_REPOLIST = 'Gihtub_Repo_Lists_'; // Outout format will be FILENAME_PREFIX_REPOLIST+{ogranization}.csv
/**
*
* @param {string} organization
* @param {string} sinceTime
* @param {string} githubAPIToken
* @returns Arraylist of Github Repo Name
*/
async function fetchActiveGithubRepoLists(organization,sinceTime,githubAPIToken){
const octokit = new Octokit({ auth: githubAPIToken });
let activeGithubArrayList = [];
let activeRepoData = [];
let columns = {
RepoName: 'Repo Name',
LastUpdatedAt: 'Last Updated At',
LastPushedAt: 'Last Pushed At',
CreatedAt : 'Created At'
};
let fileName = FILENAME_PREFIX_REPOLIST+organization+".csv"
let cursor = "";
let rel = "next"; // check whether the search reaches the end
let page = 1;
let testGithubRepoHooks = "/orgs/"+organization+"/repos?per_page=100&sort=pushed&direction=desc"
// Pagination Control
while (rel!=='prev'){
let githubRepoListURI = testGithubRepoHooks;
if(cursor!==""){
githubRepoListURI = githubRepoListURI +cursor;
}
console.log("Current URI: "+githubRepoListURI);
await new Promise(resolve => setTimeout(resolve, 1000)); // Set a Timeout before next Execution to avoid rate limit issue
let githubRepoListRes= await octokit.request("GET "+githubRepoListURI);
let githubRepoListJSONObject= JSON.parse(JSON.stringify(githubRepoListRes))
let linkResHeader = githubRepoListJSONObject.headers.link;
if(linkResHeader == undefined)
{
rel = 'prev' ; // If there is not cursor or next page, skip the next loop
}
else{
if(linkResHeader.indexOf(PAGINATION_CONTROL_INDICATOR)>=0){
rel = 'next'
page = page+1
cursor="&page="+page // This is the pagination control;
}
else{
rel = 'prev'
}
}
let githubListJSONArray = githubRepoListJSONObject.data;
console.log("Current Github Repo List length :"+githubListJSONArray.length)
for (let githubRepo of githubRepoListJSONObject.data) {
if(githubRepo.name){
// let updatedCommit_Date = githubRepo.updated_at;
activeRepoData.push([githubRepo.name,githubRepo.updated_at,githubRepo.pushed_at,githubRepo.created_at])
// The following if statement is removed before last_update_time is not the same with the last commit time
// if(updatedCommit_Date && updatedCommit_Date>sinceTime){
activeGithubArrayList.push(githubRepo.name);
// }
}
}
}
fileHandler.writeToCSVFile(fileName,columns,activeRepoData);
console.log("Total NO. of Active Repos List : "+activeGithubArrayList.length);
return activeGithubArrayList;
}
/**
* @description This function is to get a hashMap with commiter as key and last commit time as value
* @param {string} organization
* @param {string} repoName
* @param {string} sinceTime
* @param {string} githubAPIToken
* @returns list
*/
async function getCommitterFromRepo(organization,repoName,sinceTime,githubAPIToken){
const octokit = new Octokit({ auth: githubAPIToken });
let committerHashMap = new Map(); //The format will be [{'Committer_ID':{'total_commit':number_of_commit, 'last_commit_time',}}]
let baseGetCommitURI = "/repos/"+organization+"/"+repoName+"/commits?since="+sinceTime+"&per_page=100";
let cursor = "";
let rel = "next"; //check whether the search reaches the end
let count = 0;
let page = 1;
while (rel!=='prev'){
let getCommitURI = baseGetCommitURI;
if(cursor!==""){
getCommitURI = baseGetCommitURI +cursor;
}
await new Promise(resolve => setTimeout(resolve, 1000)); // Set a TimeOut before next Executtion
let githubCommitRes = "";
try{
console.log("Current URI request to pull the commits "+getCommitURI);
githubCommitRes= await octokit.request("GET "+getCommitURI);
}catch (e){
console.log("Exception Occured when making a request to "+getCommitURI);
break;
}
let githubCommitsJSONObject= JSON.parse(JSON.stringify(githubCommitRes))
let linkResHeader = githubCommitsJSONObject.headers.link;
if(linkResHeader == undefined)
{
rel = 'prev' ; // If there is not cursor or next page, just not going to start next loop
}
else{
if(linkResHeader.indexOf(PAGINATION_CONTROL_INDICATOR)>=0){
rel = 'next'
page = page+1
cursor="&page="+page // This is the pagination control;
}
else{
rel = 'prev'
}
}
if(githubCommitsJSONObject.data || githubCommitsJSONObject.data.length >= 1)
for(let commitEntry of githubCommitsJSONObject.data){
let lastcommitDate = commitEntry.commit.author.date;
let commiterID = "";
if(commitEntry.author!=null){
commiterID= commitEntry.author.login;
}else{
commiterID= commitEntry.commit.author.name;
}
if(committerHashMap.has(commiterID) == false){ // Since the commit is sorted as desc, the first commit added is the latest one
committerHashMap.set(commiterID, JSON.parse("{\"total_commits\":1,\"last_commit_date\":\""+lastcommitDate+"\"}"));
}else{
//Update the total commit numbers if a committer has already been added.
let totalCommits = committerHashMap.get(commiterID).total_commits;
totalCommits++;
committerHashMap.set(commiterID,JSON.parse("{\"total_commits\":"+totalCommits+",\"last_commit_date\":\""+lastcommitDate+"\"}"))
}
count++;
}
}
console.log("Total Commits Since "+sinceTime + " for repo " + repoName+ " is "+count + " since "+sinceTime);
return committerHashMap;
}
module.exports = {getCommitterFromRepo,fetchActiveGithubRepoLists}