forked from CrispyConductor/sinopia-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-client.js
169 lines (157 loc) · 4.3 KB
/
gitlab-client.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
var request = require('request');
var async = require('async');
function GitlabClient(url, options) {
this.url = url + '/api/v3/';
this.options = options;
}
GitlabClient.prototype.auth = function(username, password, cb) {
var isEmail = username.indexOf('@') !== -1;
var params = {
url: this.url + 'session',
method: 'POST',
json: {
login: isEmail ? undefined : username,
email: isEmail ? username : undefined,
password: password
},
ca: this.options.caFile
};
request(params, function(error, response, body) {
if(error) return cb(error);
if(response.statusCode < 200 || response.statusCode >= 300) return cb('Invalid status code ' + response.statusCode);
cb(null, body);
});
};
GitlabClient.prototype.token = function (token, cb) {
var params = {
url: this.url + 'user',
method: 'GET',
qs: {
private_token: token,
},
ca: this.options.caFile
};
request(params, function (error, response, user) {
if (error) return cb(error);
if (response.statusCode < 200 || response.statusCode >= 300) return cb('Invalid status code ' + response.statusCode);
cb(null, user);
});
};
GitlabClient.prototype.paginate = function(params, cb) {
params.qs.per_page = 50;
params.qs.page = 1;
var results = [];
var hasMore = true;
async.whilst(function() {
return hasMore;
}, function(next) {
request(params, function(error, response, body) {
if(error) return cb(error);
if(response.statusCode < 200 || response.statusCode >= 300) return next('Invalid status code ' + response.statusCode);
body = JSON.parse(body);
Array.prototype.push.apply(results, body);
delete params.qs;
if (!response.headers.link) {
hasMore = false;
} else {
var linkParts = response.headers.link.split(/ *, */g);
var linkNext;
linkParts.forEach(function(part) {
var partParts = part.split(/ *; */g);
var rel, link;
partParts.forEach(function(partPart) {
var matches;
matches = /^rel="([a-z]+)"$/.exec(partPart);
if (matches) {
rel = matches[1];
}
matches = /^<(.*)>$/.exec(partPart);
if (matches) {
link = matches[1];
}
});
if (rel === 'next' && link) {
linkNext = link;
}
});
if (linkNext) {
params.url = linkNext;
} else {
hasMore = false;
}
}
next();
});
}, function(error) {
if(error) return cb(error);
cb(null, results);
});
};
GitlabClient.prototype.listUsers = function(search, privateToken, cb) {
this.paginate({
url: this.url + 'users',
qs: {
private_token: privateToken,
search: search
},
ca: this.options.caFile
}, cb);
};
GitlabClient.prototype.listAllProjects = function(search, privateToken, cb) {
this.paginate({
url: this.url + 'projects/all',
qs: {
private_token: privateToken,
search: search
},
ca: this.options.caFile
}, cb);
};
GitlabClient.prototype.getProject = function(id, privateToken, cb) {
request({
url: this.url + 'projects/' + encodeURIComponent(id),
qs: {
private_token: privateToken
},
ca: this.options.caFile
}, function(error, response, body) {
if(error) return cb(error);
if(response.statusCode == 404) return cb(null, null);
if(response.statusCode < 200 || response.statusCode >= 300) return cb('Invalid status code ' + response.statusCode);
cb(null, JSON.parse(body));
});
};
GitlabClient.prototype.listProjects = function(search, privateToken, cb) {
this.paginate({
url: this.url + 'projects',
qs: {
private_token: privateToken,
search: search
},
ca: this.options.caFile
}, cb);
};
GitlabClient.prototype.getProjectTeamMember = function(projectId, userId, privateToken, cb) {
request({
url: this.url + 'projects/' + encodeURIComponent(projectId) + '/members/' + encodeURIComponent(userId),
qs: {
private_token: privateToken
},
ca: this.options.caFile
}, function(error, response, body) {
if(error) return cb(error);
if(response.statusCode == 404) return cb(null, null);
if(response.statusCode < 200 || response.statusCode >= 300) return cb('Invalid status code ' + response.statusCode);
cb(null, JSON.parse(body));
});
};
GitlabClient.prototype.listGroupMembers = function(groupId, privateToken, cb) {
this.paginate({
url: this.url + 'groups/' + groupId + '/members',
qs: {
private_token: privateToken
},
ca: this.options.caFile
}, cb);
};
module.exports = GitlabClient;