-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.js
More file actions
154 lines (132 loc) · 4.78 KB
/
github.js
File metadata and controls
154 lines (132 loc) · 4.78 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
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
/*
* Connector to Redmine from Google Apps Scripts platform.
*
* Copyright (c) 2012 Emergya
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Author: Alejandro Leiva <aleiva@emergya.com>
*
*/
var GITHUB_API_URL = 'https://api.github.com';
// TODO: this should be obtained from a configuration dialog.
var API_ACCESS_KEY = 'YOUR_API_ACCESS_KEY_HERE!';
var linkexp=/<[^>]*>\s*(\s*;\s*[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)/g;
var paramexp=/[^\(\)<>@,;:"\/\[\]\?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*"))/g;
var GitHub = (function() {
// base_uri and api_key could be specified at initialization time
function GitHub(base_uri, api_key) {
this.base_uri = base_uri || GITHUB_API_URL;
this.api_key = api_key || API_ACCESS_KEY;
}
// Parse linkHeader whiich contains the 'next page uri' of a request in Github
var parseLinkHeader = function (value) {
var matches = value.match(linkexp);
var rels = new Object();
var titles = new Object();
for (i = 0; i < matches.length; i++) {
var split = matches[i].split('>');
var href = split[0].substring(1);
var ps = split[1];
var link = new Object();
link.href = href;
var s = ps.match(paramexp);
function unquote(value) {
if (value.charAt(0) == '"' && value.charAt(value.length - 1) == '"') return value.substring(1, value.length - 1);
return value;
}
for (j = 0; j < s.length; j++) {
var p = s[j];
var paramsplit = p.split('=');
var name = paramsplit[0];
link[name] = unquote(paramsplit[1]);
}
if (link.rel != undefined) {
rels[link.rel] = link;
}
if (link.title != undefined) {
titles[link.title] = link;
}
}
var linkheader = new Object();
linkheader.rels = rels;
linkheader.titles = titles;
return linkheader;
}
// Github paginates requests that return multiple items
// See http://developer.github.com/v3/#pagination
// This allows to retrieve all items of a multiple item request
GitHub.prototype.retrieveAll = function (api_call) {
var page = 0;
var per_page = 100;
// First request
var uri = this.base_uri + api_call + '?page=' + page + '&per_page=' + per_page + '&key=' + this.api_key;
try {
var response = UrlFetchApp.fetch(uri);
var content = Utilities.jsonParse(response.getContentText());
var headers = response.getHeaders();
} catch (err) {
return [];
}
if (!('Link' in headers)) {
// No more pages to retrieve
return content;
}
else {
// Get the next page uri from header
var link_headers = parseLinkHeader(headers['Link']);
var results = [];
do {
results = results.concat(content);
var uri = link_headers['rels'].next.href;
var response = UrlFetchApp.fetch(uri);
var content = Utilities.jsonParse(response.getContentText());
var headers = response.getHeaders();
var link_headers = parseLinkHeader(headers['Link']);
} while (link_headers['rels'].next)
results = results.concat(content);
return results;
}
};
// Repos API http://developer.github.com/v3/repos/
// /repos/:user/:repo/commits
GitHub.prototype.getReposCommits = function (user, project) {
var api_call = '/repos/' + user + '/' + project + '/commits';
return this.retrieveAll(api_call);
};
// /repos/:user/:repo/forks
GitHub.prototype.getReposForks = function (user, project) {
var api_call = '/repos/' + user + '/' + project + '/forks';
return this.retrieveAll(api_call);
};
// Orgs API http://developer.github.com/v3/orgs/
// /orgs/:org/members
GitHub.prototype.getOrgMembers = function (org) {
var api_call = '/orgs/' + org + '/members';
return this.retrieveAll(api_call);
};
// Users API http://developer.github.com/v3/users/
// /users/:user
GitHub.prototype.getUser = function (user) {
var api_call = '/users/' + user;
return this.retrieveAll(api_call);
};
// /users/:user/repos
GitHub.prototype.getUserRepos = function (user) {
var api_call = '/users/' + user + '/repos';
return this.retrieveAll(api_call);
};
return GitHub;
})();
function stub() {
gh = new GitHub(GITHUB_API_URL);
gh.getCommits('gloob', 'github-google_apps_scripts');
}