-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.js
125 lines (99 loc) · 3.15 KB
/
github.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
var request = require('request');
var parseLinkHeader = require('parse-link-header');
var config = require('./config');
function GitHub(profile, accessToken) {
this.profile = profile;
this.accessToken = accessToken;
this.api = request.defaults({
baseUrl: config.apiEndpoint,
json: true,
headers: {
Authorization: 'token ' + accessToken,
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'request strider-github-auth'
}
});
}
GitHub.prototype.emails = function (callback) {
this.api.get('/user/emails', function (err, response, body) {
if (err) return callback(err);
if (response.statusCode !== 200) {
return callback(new Error('Unexpected ' + response.statusCode + ' status from GitHub API', {
path: '/user/emails',
statusCode: response.statusCode,
body: body
}));
}
return callback(null, body);
})
};
GitHub.prototype.belongsToOrganization = function (orgName, callback) {
var p = '/user/memberships/orgs/' + encodeURIComponent(orgName);
this.api.get(p, function (err, response, body) {
if (err) return callback(err);
if (response.statusCode === 200 && body.state === 'active') {
return callback(null, true, body.role === 'admin');
}
callback(null, false, false);
});
};
GitHub.prototype.belongsToTeam = function (teamId, callback) {
var p = '/teams/';
p += encodeURIComponent(teamId);
p += '/memberships/';
p += encodeURIComponent(this.profile._json.login);
this.api.get(p, function (err, response, body) {
if (err) return callback(err);
if (response.statusCode === 404) {
return callback(null, false);
}
if (response.statusCode !== 200) {
return callback(new Error('Unexpected ' + response.statusCode + ' status from GitHub API', {
path: p,
statusCode: response.statusCode,
body: body
}));
}
callback(null, body.state === 'active');
});
};
GitHub.prototype.findTeamWithName = function (orgName, teamName, callback) {
var p = '/orgs/';
p += encodeURIComponent(orgName);
p += '/teams';
var consumePage = function (u) {
var opts = { url: u };
if (/^http/.test(u)) {
opts.baseUrl = null;
}
this.api.get(opts, function (err, response, body) {
if (err) return callback(err);
if (response.statusCode === 403) {
// User does not belong to this organization.
return callback(null, null);
}
if (response.statusCode !== 200) {
return callback(new Error('Unexpected ' + response.statusCode + ' status from GitHub API', {
path: p,
statusCode: response.statusCode,
body: body
}));
}
for (var i = 0; i < body.length; i++) {
if (body[i].name === teamName) {
return callback(null, body[i].id);
}
}
var link = response.headers.link;
var parsed = link && parseLinkHeader(link);
if (!link || !parsed.next) {
return callback(new Error('Team name ' + teamName + ' not found', {
path: p
}));
}
consumePage(parsed.next.url);
});
}.bind(this);
consumePage(p);
};
exports.GitHub = GitHub;