-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHubTools.js
345 lines (303 loc) · 9.03 KB
/
GitHubTools.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// get the month day year from the github date strings and return a Date object
function extractDate(d) {
// catch already existing date objects
if ( typeof d != 'string') {
return d
}
var year=parseInt(d.slice(0,4),10)
var mon=parseInt(d.slice(5,7),10)-1 //I don't understand this...
var day=parseInt(d.slice(8,10),10)
return new Date(year,mon,day);
}
function checkDate() {
var str='2016-12-09T15:27:41Z'
var d=extractDate(str)
//Logger.log(str+ ' :::::: '+d)
}
function getIssues() {
var issues=[]
var milestones=milestoneList()
var releases=releaseList()
for ( var r in releases) {
m = releases[r]
var myTok=auth()
issues[m]=[]
//Logger.log(m);
//Logger.log(milestones[m]);
var data=[]
for ( var page =1 ; page<100; page++ ) {
var urlIssues="https://api.github.com/repos/cms-sw/cmssw/issues?state=open&milestone="+milestones[m]+"&per_page=100&page="+page;
var dataT = getJSONFromURL(urlIssues,myTok);
data=data.concat(dataT)
if ( dataT.length != 100 ) break
}
for ( var key in data ) {
var pullRequest = data[key]['pull_request'];
var prNum=0;
for ( var key2 in pullRequest ) {
if ( key2 == 'url' ) {
prNum=pullRequest[key2]
}
}
//Logger.log('PR is '+prNum)
if (prNum == 0) {
Logger.log("Nonsense prNum "+pullRequest)
continue;
}
var sp=prNum.split('/');
prNum=sp[sp.length-1];
tIssue={}
tIssue['prNum']=prNum
tIssue['title']=data[key]['title']
//Logger.log('Title is '+tIssue['title'])
tIssue['date']=data[key]['created_at']
//Logger.log('Date is '+tIssue['date'])
tIssue['labels']=[]
tIssue['user']=data[key]['user']['login']
var labels = data[key]['labels']
for ( var label in labels) {
tIssue['labels'].push(labels[label]['name']);
}
issues[m].push(tIssue)
//Logger.log('Labels are '+issues[m][prNum]['labels'])
//for ( var label in issues[m][prNum]['labels'] ) {
// Logger.log(issues[m][prNum]['labels'][label])
//}
}
}
return issues;
}
function getNewReleases(orpDate) {
var myTok=auth()
var data=[]
for ( var page =1 ; page<100; page++ ) {
relUrl='https://api.github.com/repos/cms-sw/cmssw/releases?per_page=100&page='+page;
var t = getJSONFromURL(relUrl,myTok);
var len=0;
for ( var i in t ) {
data[len+100*(page-1)]=t[i];
len=len+1;
}
if ( len != 100 ) break;
}
var len=0;
for ( var i in data) {
len=len+1;
}
//Logger.log('Found releases '+len)
newReleases=[]
for (var rel in data) {
var rDate=extractDate(data[rel]['published_at'])
if ( days_between(rDate,orpDate) > -2 ) {
newReleases.push( [data[rel]['tag_name'], rDate] )
}
}
//for ( var rel in newReleases ) {
// Logger.log(newReleases[rel])
//}
return newReleases;
}
function getGitHubDetails(lastORPDate) {
var cache = CacheService.getScriptCache();
//get the list of releases made since the last orp
//returned as tuples of name, date made
var releasesStr = null//cache.get('releaseInfo')
var releases=null
if ( releasesStr == null ) {
//Logger.log('refrething releases list')
releases=getNewReleases(lastORPDate)
//for ( var r in releases ) {
// Logger.log('Found a release ',r)
//}
Logger.log(JSON.stringify(releases))
cache.put('releaseInfo',JSON.stringify(releases),6*3600)
}
else{
Logger.log('Using cashed release information')
Logger.log(releasesStr)
releases=JSON.parse(releasesStr)
//for ( var r in releases ) {
// Logger.log('Found a release ',r)
//}
}
//get the list of all issues pending in github
var issuesStr= null//cache.get('issuesInfo')
var issues=null
if ( issuesStr == null ) {
//Logger.log('refreshing issues list')
issues=getIssues()
issueList=[]
for ( var i in issues) {
cache.put('issuesInfo'+i,JSON.stringify(issues[i]),6*3600)
//Logger.log(JSON.stringify(issues[i]).length)
//Logger.log(JSON.stringify(issues[i]))
issueList.push(i)
}
cache.put('issuesInfo',JSON.stringify(issueList),6*3600)
}
else{
//Logger.log('Using cached issue information')
// Logger.log(issuesStr)
issuesList=JSON.parse(issuesStr)
issues=[]
for ( var i in issuesList) {
var tStr=cache.get('issuesInfo'+issuesList[i])
//Logger.log('Found milestone '+issuesList[i])
//Logger.log(tStr)
issues[issuesList[i]]=JSON.parse(tStr)
var c=0
for ( var it in issues[issuesList[i]]) {
c=c+1
}
//Logger.log('Found '+c+' issues in '+issuesList[i])
}
}
return [releases,issues]
}
//to make the needed library
// - create a separate project (File->New->Project)
// - impcement these two functions with your tokens returned as strings (return 'lbhaadfafe')
// - In the new project, File->Manage versions to create a new version
// - In this project, Resources->Libraries->AddLibrary with the project key
function auth() {
var authHeader = 'token ' + MyGitHubAuth.auth();
var options = {
headers: {'Authorization' : authHeader}
}
return options;
}
function authBett() {
var authHeader = 'token ' + MyGitHubAuth.authBett();
var options = {
headers: {'Authorization' : authHeader}
}
return options;
}
function getRelCache() {
var cache = CacheService.getScriptCache();
var releasesStr = cache.get('releaseInfo')
Logger.log(releasesStr)
releases=JSON.parse(releasesStr)
Logger.log(releases)
Logger.log(releases.length)
for ( var r in releases ) {
Logger.log('Found a release ',r)
}
}
function checkType() {
var myDate = new Date().toString(),
myRealDate = new Date();
if ( typeof myDate == 'string' ) { Logger.log('good string') }
if ( typeof myRealDate == 'string' ) { Logger.log('good date') }
}
function getPullRequestInfo(pr) {
var myTok=auth()
var urlIssues="https://api.github.com/repos/cms-sw/cmssw/pulls/"+pr;
var dataT = getJSONFromURL(urlIssues,myTok);
return dataT;
}
function checkMyTok() {
var myTok=auth()
var url="https://api.github.com/rate_limit";
var dataT = getJSONFromURL(url,myTok);
Logger.log(dataT)
}
function getPullRequestComments(pr) {
var myTok=auth()
var page=1
var data=[]
while ( 1 ) {
var urlIssues="https://api.github.com/repos/cms-sw/cmssw/issues/"+pr+"/comments?page="+page+"&per_page=100"
var dataT = getJSONFromURL(urlIssues,myTok);
data=data.concat(dataT)
if ( dataT.length !=100) break
Logger.log("Number of comments "+data.length)
page=page+1
}
//for ( var i in data) {
// Logger.log("New comment")
// Logger.log(data[i]['body'])
//}
return data
}
function getPullRequestCommits(pr) {
var myTok=auth()
var urlIssues="https://api.github.com/repos/cms-sw/cmssw/pulls/"+pr+"/commits"
var data = getJSONFromURL(urlIssues,myTok);
//Logger.log(data.length)
return data
}
function getPullRequestFiles(pr) {
var myTok=auth()
var urlIssues="https://api.github.com/repos/cms-sw/cmssw/pulls/"+pr+"/files"
var data = getJSONFromURL(urlIssues,myTok);
return data
}
function closeIssue(pr) {
var myTok=authBett()
var options = {
'method' : "post",
'contentType': 'application/json',
'payload' : JSON.stringify({'state' : "closed"}),
'headers' : myTok['headers']
};
var response = UrlFetchApp.fetch("https://api.github.com/repos/cms-sw/cmssw/issues/"+pr, options);
var json = response.getContentText();
Logger.log(json)
}
function addComment(pr,comment) {
var myTok=authBett()
var options = {
'method' : "post",
'contentType': 'application/json',
'payload' : JSON.stringify({'body' : comment}),
'headers' : myTok['headers']
};
Logger.log(options)
var response = UrlFetchApp.fetch("https://api.github.com/repos/cms-sw/cmssw/issues/"+pr+"/comments", options);
var json = response.getContentText();
Logger.log(json)
}
function makePullRequest(orig,dest,title, comment) {
Logger.log(orig)
Logger.log(dest
)
Logger.log(title)
Logger.log(comment)
var body = {
"title": title,
"body": comment,
"head": orig,
"base": dest
}
var myTok=authBett()
var options = {
'method' : "post",
'contentType': 'application/json',
'payload' : JSON.stringify(body),
'headers' : myTok['headers']
};
Logger.log(options)
var response = UrlFetchApp.fetch("https://api.github.com/repos/cms-sw/cmssw/pulls", options);
var json = response.getContentText();
Logger.log(json)
}
function createFile(repo,filePathAndName,branch,contents) {
var body = {
"path": filePathAndName,
"message": "Creating new file",
"content": contents,
"branch": branch
}
var myTok=authBett()
var options = {
'method' : "put",
'payload' : body,
'headers' : myTok['headers']
};
var response = UrlFetchApp.fetch("https://api.github.com/repos/"+repo+"/contents", options);
var json = response.getContentText();
}
function testCreateFile() {
createFile("davidlange6/testing","test.1","master","Hi there\n")
}