/
bamboo.js
130 lines (100 loc) · 2.45 KB
/
bamboo.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
//bamboo.js
//
var request = require('request');
// constructor
function Bamboo(urlbase, username, password) {
this.urlbase = urlbase;
this.username = username;
this.password = password;
}
// public methods
Bamboo.prototype.getAuthCredentials=getAuthCredentials;
Bamboo.prototype.getProjects=getProjects;
Bamboo.prototype.getProject=todo;
Bamboo.prototype.getPlans=todo;
Bamboo.prototype.getPlan=todo;
// private attributes
var prefix = "/rest/api/latest";
// private methods
function todo() {}
function getProjects(projectKey) {
var projects = null;
var resource = "project";
var format = "json";
if (projectKey!=undefined) {
resource += "/" + projectKey;
}
if (format == "json") {
resource += ".json";
}
var url = this.urlbase + prefix + "/" + resource;
console.log("url: " + url);
var headers = { "Authorization" : "Basic " + base64encode(this.username + ":" + this.password) };
console.log("headers: \n%j", headers);
request.get(
{
url : url,
headers : headers
},
handleResponse
);
return projects;
}
function handleResponse(error, response, body) {
console.log('callback from bamboo.projects');
if (error) {
console.log("ERROR");
console.log(error);
}
if (response) {
console.log("RESPONSE");
//console.log(response);
}
if (body) {
console.log("BODY");
console.log(body);
}
}
// function get(url, query_string, params) {
// console.log();
// console.log("url: " + url);
// var credentials = base64encode(this.username + ":" + this.password);
// console.log("credentials: " + credentials);
// request.get(
// {
// headers : { "Authorization" : "Basic " + credentials },
// url : url
// },
// function (error, response, body) {
// console.log('in request callback');
// if (error) {
// console.log("ERROR");
// console.log(error);
// }
// if (response) {
// console.log("RESPONSE");
// console.log(response);
// }
// if (body) {
// console.log("BODY");
// console.log(body);
// }
// }
// );
// }
function getAuthCredentials(username, password) {
if (username) { this.username = username; }
if (password) { this.password = password; }
if (this.username == undefined || this.password == undefined) {
return null;
}
return base64encode(this.username + ":" + this.password);
}
function base64encode(text) {
return new Buffer(text).toString('base64');
}
function base64decode(text) {
return new Buffer(text, 'base64').toString('ascii');
}
// export
module.exports = Bamboo;