-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_info.js
84 lines (69 loc) · 1.52 KB
/
build_info.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
var Promise = require('bluebird');
var rp = require('request-promise');
var args = process.argv.slice(2);
if (!args[0]) {
throw new Error('Build ID not passed.');
}
else if (!args[1]) {
throw new Error('Access token not passed.');
}
var accessToken = args[1];
/**
* Get Build data.
*
* @param buildId
* The build ID.
*
* @returns {*}
*/
var getBuild = function(buildId) {
var backendUrl = process.env.BACKEND_URL;
var options = {
url: backendUrl + '/api/builds/' + buildId,
qs: {
access_token: accessToken,
fields: 'id,git_branch,repository'
}
};
return rp.get(options);
};
/**
* Get Repository data.
*
* @param repoId
* The repository ID.
*
* @returns {*}
*/
var getRepository = function(repoId) {
var backendUrl = process.env.BACKEND_URL;
var options = {
url: backendUrl + '/api/repositories/' + repoId,
qs: {
access_token: accessToken,
fields: 'id,label',
ssh_key: true
}
};
return rp.get(options);
};
var output = {};
getBuild(args[0])
.then(function(response) {
// Build data.
var data = JSON.parse(response).data[0];
var repoId = data.repository;
output.branch = data.git_branch;
return getRepository(repoId);
})
.then(function(response) {
// Repository data.
var data = JSON.parse(response).data[0];
var repoInfo = data.label.split('/');
output.owner = repoInfo[0];
output.repo = repoInfo[1];
process.stdout.write(JSON.stringify(output));
})
.catch(function(err) {
console.log(err);
});