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