-
Notifications
You must be signed in to change notification settings - Fork 0
/
stash.js
103 lines (84 loc) · 2.66 KB
/
stash.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
'use strict';
var assert = require('chai').assert;
var request = require('superagent');
require('superagent-as-promised')(request);
function verifyConfig(config) {
assert.isObject(config);
assert.equal(config.name, StashService.prototype.serviceKey);
assert.isObject(config.options);
var optionKeys = Object.keys(config.options);
assert.equal(optionKeys.length, 1);
var optionKey = optionKeys[0];
if (optionKey === 'user') {
assert.isString(config.options[optionKey].user);
assert.isString(config.options[optionKey].repository);
} else if (optionKey === 'project') {
assert.isString(config.options[optionKey].project);
assert.isString(config.options[optionKey].repository);
} else {
assert(false, 'Expected stash repo type to be a user or a project');
}
}
function StashService(options) {
assert.isObject(options);
assert.isString(options.url);
assert.isString(options.user);
assert.isString(options.password);
this._serviceConfig = options;
}
StashService.prototype = {
serviceKey: 'stash',
setBuildStatus: function(config, options) {
verifyConfig(config);
assert.isObject(options);
assert.isString(options.sha);
assert.include(['pending', 'success', 'failure'], options.status);
var state;
switch (options.status) {
case 'pending':
state = 'INPROGRESS';
break;
case 'success':
state = 'SUCCESSFUL';
break;
case 'failure':
state = 'FAILED';
break;
}
return request
.post(this._serviceConfig.url + '/rest/build-status/1.0/commits/' + options.sha)
.auth(this._serviceConfig.user, this._serviceConfig.password)
.send({
state: state,
key: 'CI - Visual',
name: 'CI - Visual',
url: 'http://google.com'
});
},
addComment: function(config, options) {
verifyConfig(config);
assert.isObject(options);
assert.isString(options.sha);
assert.isString(options.comment);
var projectKey;
var repository;
if (config.options.user) {
projectKey = '~' + config.options.user.user;
repository = config.options.user.repository;
} else {
projectKey = config.options.project.project;
repository = config.options.project.repository;
}
var url = '/rest/api/1.0/projects/' + projectKey + '/repos/' + repository + '/commits/' + options.sha + '/comments';
return request
.post(this._serviceConfig.url + url)
.auth(this._serviceConfig.user, this._serviceConfig.password)
.send({
text: options.comment
});
}
};
if (process.env.NODE_ENV === 'test') {
StashService.prototype.verifyConfig = verifyConfig;
}
module.exports = StashService;