11var request = require ( 'request' ) ,
22 qs = require ( 'querystring' ) ,
33 _ = require ( 'lodash' ) ,
4- async = require ( 'async' ) ;
4+ async = require ( 'async' ) ,
5+ superagent = require ( 'superagent' ) ;
56
67module . exports = {
78 get : get ,
@@ -12,23 +13,39 @@ module.exports = {
1213 removeDeployKey : removeDeployKey
1314} ;
1415
16+ /**
17+ * Retrieve a file from a GitLab repository, using the GitLab API.
18+ * @param {Object } config The configuration of the GitLab provider plugin.
19+ * @param {String } uri The URI of the file to retrieve.
20+ * @param {Function } done A function to call, once a result is available.
21+ */
22+ 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+ } ) ;
1540
16- function get ( config , uri , callback ) {
17- var qpm = {
18- private_token : config . api_key ,
19- per_page : 100
20- } ,
21- glue = ( ~ uri . indexOf ( '?' ) ) ? "&" : "?" ,
22- url = config . api_url + '/' + uri + glue + qs . stringify ( qpm ) ;
23-
24- request . get ( {
25- url : url
26- } , function ( err , res ) {
27- if ( err ) {
28- return callback ( err ) ;
41+ req . end ( function ( res ) {
42+ if ( res . error ) {
43+ return done ( res . error , null ) ;
44+ }
45+ if ( ! res . body ) {
46+ return done ( ) ;
2947 }
30-
31- callback ( undefined , res . body ) ;
48+ done ( null , res . body ) ;
3249 } ) ;
3350}
3451
0 commit comments