Skip to content

Commit 31985b4

Browse files
author
Ilya Radchenko
committed
Merge pull request #17 from oliversalzburg/feature/remove-request
Replace request with superagent
2 parents 2b8aac4 + eba0971 commit 31985b4

File tree

5 files changed

+361
-358
lines changed

5 files changed

+361
-358
lines changed

lib/api.js

Lines changed: 137 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
var request = require('request'),
2-
qs = require('querystring'),
3-
_ = require('lodash'),
4-
async = require('async'),
5-
superagent = require('superagent');
1+
var _ = require('lodash');
2+
var async = require('async');
3+
var superagent = require('superagent');
64

75
module.exports = {
8-
get: get,
9-
parseRepo: parseRepo,
10-
createHooks: createHooks,
11-
deleteHooks: deleteHooks,
12-
addDeployKey: addDeployKey,
13-
removeDeployKey: removeDeployKey
6+
get: get,
7+
parseRepo: parseRepo,
8+
createHooks: createHooks,
9+
deleteHooks: deleteHooks,
10+
addDeployKey: addDeployKey,
11+
removeDeployKey: removeDeployKey
1412
};
1513

1614
/**
@@ -20,145 +18,149 @@ module.exports = {
2018
* @param {Function} done A function to call, once a result is available.
2119
*/
2220
function get(config, uri, done) {
23-
// If the configuration has no GitLab API URL, bail.
24-
if (!config.api_url) {
25-
return done(new Error('API URL missing from GitLab provider configuration!'));
26-
}
27-
// Append a slash to the URL if there isn't one.
28-
var apiBase = config.api_url;
29-
if (!/\/$/.test(apiBase)) {
30-
apiBase += '/';
31-
}
32-
// Construct the complete request URL
33-
var url = apiBase + uri;
34-
var req = superagent.get(url).set('User-Agent', 'StriderCD (http://stridercd.com)');
35-
// Add our additional request parameters to the query part of the URL.
36-
req.query({
37-
private_token: config.api_key,
38-
per_page: 100
39-
});
40-
41-
req.end(function (res) {
42-
if (res.error) {
43-
return done(res.error, null);
44-
}
45-
if (!res.body) {
46-
return done();
47-
}
48-
done(null, res.body);
21+
// If the configuration has no GitLab API URL, bail.
22+
if (!config.api_url) {
23+
return done(new Error('API URL missing from GitLab provider configuration!'));
24+
}
25+
// Append a slash to the URL if there isn't one.
26+
var apiBase = config.api_url;
27+
if (!/\/$/.test(apiBase)) {
28+
apiBase += '/';
29+
}
30+
// Construct the complete request URL
31+
var url = apiBase + uri;
32+
superagent
33+
.get(url)
34+
.query({
35+
private_token: config.api_key,
36+
per_page: 100
37+
})
38+
.set('User-Agent', 'StriderCD (http://stridercd.com)')
39+
.end(function (error, response) {
40+
if (response.error) {
41+
return done(response.error, null);
42+
}
43+
if (!response.body) {
44+
return done();
45+
}
46+
done(null, response.body);
4947
});
5048
}
5149

52-
function parseRepo(repo){
53-
return {
54-
id: repo.id,
55-
name: repo.path_with_namespace,
56-
display_name: repo.path_with_namespace,
57-
display_url: repo.web_url,
58-
group: repo.namespace.path,
59-
private: !repo.public,
60-
config: {
61-
auth: { type: 'ssh' },
62-
scm: 'git',
63-
url: repo.ssh_url_to_repo,
64-
owner: repo.owner,
65-
repo: repo.web_url,
66-
pull_requests: 'none',
67-
whitelist: []
68-
}
50+
function parseRepo(repo) {
51+
return {
52+
id: repo.id,
53+
name: repo.path_with_namespace,
54+
display_name: repo.path_with_namespace,
55+
display_url: repo.web_url,
56+
group: repo.namespace.path,
57+
private: !repo.public,
58+
config: {
59+
auth: {type: 'ssh'},
60+
scm: 'git',
61+
url: repo.ssh_url_to_repo,
62+
owner: repo.owner,
63+
repo: repo.web_url,
64+
pull_requests: 'none',
65+
whitelist: []
6966
}
67+
}
7068
}
7169

72-
73-
function createHooks(config, repo_id, url, callback){
74-
var qpm = { private_token: config.api_key },
75-
endpoint_url = config.api_url + "/projects/" + repo_id + "/hooks?" + qs.stringify(qpm);
76-
77-
request.post({
78-
url: endpoint_url,
79-
body: {
80-
url: url,
81-
push_events: true
82-
},
83-
json: true
84-
}, function (err, response, body){
85-
if (err) return callback(err);
86-
if (response.status !== 201) return callback(response.status);
87-
88-
return callback(null, true)
70+
function createHooks(config, repo_id, url, callback) {
71+
var qpm = {private_token: config.api_key};
72+
var endpoint_url = config.api_url + '/projects/' + repo_id + '/hooks';
73+
74+
superagent
75+
.post(endpoint_url)
76+
.query(qpm)
77+
.send({
78+
url: url,
79+
push_events: true
80+
})
81+
.end(function (err, res) {
82+
if (err) return callback(err);
83+
if (res.status !== 201) return callback(res.status);
84+
85+
return callback(null, true);
8986
});
9087
}
9188

92-
9389
function deleteHooks(config, repo_id, url, callback) {
94-
var qpm = { private_token: config.api_key },
95-
endpoint_url = config.api_url + "/projects/" + repo_id + "/hooks";
96-
97-
// Fetch all webhooks from Gitlab
98-
request.get({
99-
url: endpoint_url + "?" + qs.stringify(qpm),
100-
json: true
101-
}, function(err, res){
102-
var deleted = false;
103-
104-
async.each(res.body, function(hook, cb){
105-
// Remove all webhooks matching url
106-
if (hook.url == url){
107-
var hook_url = endpoint_url +"/"+ hook.id + "?" + qs.stringify(qpm);
108-
109-
request.del(hook_url, function(err, res){
110-
deleted = true;
111-
cb(err);
112-
});
113-
} else cb();
114-
}, function(err){
115-
callback(err, deleted);
116-
});
90+
var qpm = {private_token: config.api_key};
91+
var endpoint_url = config.api_url + "/projects/" + repo_id + "/hooks";
92+
93+
// Fetch all webhooks from Gitlab
94+
superagent
95+
.get(endpoint_url)
96+
.query(qpm)
97+
.end(function (err, res) {
98+
var deleted = false;
99+
100+
async.each(res.body, function (hook, cb) {
101+
// Remove all webhooks matching url
102+
if (hook.url == url) {
103+
var hook_url = endpoint_url + "/" + hook.id;
104+
105+
superagent
106+
.del(hook_url)
107+
.query(qpm)
108+
.end(function (err, res) {
109+
deleted = true;
110+
cb(err);
111+
});
112+
} else cb();
113+
}, function (err) {
114+
callback(err, deleted);
115+
});
117116
});
118117
}
119118

120-
function addDeployKey(config, repo_id, title, key, callback){
121-
var qpm = { private_token: config.api_key },
122-
endpoint_url = config.api_url + "/projects/" + repo_id + "/keys?" + qs.stringify(qpm);
123-
124-
request.post({
125-
url: endpoint_url,
126-
body: {
127-
title: title,
128-
key: key
129-
},
130-
json: true
131-
}, function (err, response, body){
132-
if (err) return callback(err);
133-
if (response.status !== 201) return callback(response.status);
134-
135-
return callback(null, true)
119+
function addDeployKey(config, repo_id, title, key, callback) {
120+
var qpm = {private_token: config.api_key};
121+
var endpoint_url = config.api_url + '/projects/' + repo_id + '/keys';
122+
123+
superagent
124+
.post(endpoint_url)
125+
.query(qpm)
126+
.send({
127+
title: title,
128+
key: key
129+
})
130+
.end(function (err, res) {
131+
if (err) return callback(err);
132+
if (res.status !== 201) return callback(res.status);
133+
134+
return callback(null, true)
136135
});
137136
}
138137

139-
function removeDeployKey(config, repo_id, title, callback){
140-
var qpm = { private_token: config.api_key },
141-
endpoint_url = config.api_url + "/projects/" + repo_id + "/keys";
142-
143-
// Fetch all deploy keys from Gitlab
144-
request.get({
145-
url: endpoint_url + "?" + qs.stringify(qpm),
146-
json: true
147-
}, function(err, res){
148-
var deleted = false;
149-
150-
async.each(res.body, function(key, cb){
151-
// Remove all webhooks matching url
152-
if (key.title == title){
153-
var deploy_key_url = endpoint_url +"/"+ key.id + "?" + qs.stringify(qpm);
154-
155-
request.del(deploy_key_url, function(err, res){
156-
deleted = true;
157-
cb(err);
158-
});
159-
} else cb();
160-
}, function(err){
161-
callback(err, deleted);
162-
});
138+
function removeDeployKey(config, repo_id, title, callback) {
139+
var qpm = {private_token: config.api_key};
140+
var endpoint_url = config.api_url + '/projects/' + repo_id + '/keys';
141+
142+
// Fetch all deploy keys from Gitlab
143+
superagent
144+
.get(endpoint_url)
145+
.query(qpm)
146+
.end(function (err, res) {
147+
var deleted = false;
148+
149+
async.each(res.body, function (key, cb) {
150+
// Remove all webhooks matching url
151+
if (key.title == title) {
152+
var deploy_key_url = endpoint_url + '/' + key.id;
153+
154+
superagent
155+
.del(deploy_key_url)
156+
.query(qpm)
157+
.end(function (err, res) {
158+
deleted = true;
159+
cb(err);
160+
});
161+
} else cb();
162+
}, function (err) {
163+
callback(err, deleted);
164+
});
163165
});
164166
}

0 commit comments

Comments
 (0)