-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagn.jenkins.js
111 lines (104 loc) · 3.31 KB
/
agn.jenkins.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
/*
* agent for jenkins
*/
var Agent = require('./agn.prototype.js'),
util = require('../util'),
conf = require('../conf')['jenkins'];
var JenkinsAgent = exports = module.exports = function (data) {
var s = this;
if (data) {
util.obj.deepCopy(data, s);
s.cookie = new Agent.Cookie(data.cookie);
} else {
s.cookie = new Agent.Cookie();
}
};
JenkinsAgent.conf = conf;
(function (Prototype) {
JenkinsAgent.prototype = new Prototype();
for (var name in Prototype) {
if (!Prototype.hasOwnProperty(name)) continue;
JenkinsAgent[name] = Prototype[name];
}
})(Agent);
JenkinsAgent.prototype.login = function (auth, callback) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
auth = null;
}
var s = this;
if (!auth) auth = conf.auth;
var form = {
j_username:auth.j_username,
j_password:auth.j_password
}
var base = conf.url.base || '';
s.post(base + 'j_acegi_security_check',function (res, body, $) {
if (res.statusCode == 301) {
console.log('location', res.headers && res.headers.location);
console.log('res.headers', res.headers);
}
if (res.statusCode == '411') {
console.error('failed to login');
callback.call(s, false);
return;
}
callback && callback.call(s, true);
}).form(form);
};
JenkinsAgent.prototype.getViews = function (callback) {
var s = this;
var url = conf.url.base;
var base = (function (url) {
return [url.protocol, url.host].join('\/\/');
})(require('url').parse(url))
s.get(url, function (res, body, $) {
try {
var data = [];
$('#main-panel table#viewList').find('tr').eq(1).find('td')
.each(function () {
var a = $(this).find('a');
var name = a.text();
if (name) {
data.push({
name:name,
link:base + a.attr('href')
});
}
});
callback && callback.call(s, true, data);
} catch (e) {
console.error(e);
callback && callback.call(s, false);
}
});
};
JenkinsAgent.prototype.getWhether = function (url, callback) {
var s = this;
var base = (function (url) {
return [url.protocol, url.host].join('\/\/');
})(require('url').parse(url))
s.get(url, function (res, body, $) {
try {
var data = [];
$('#main-panel table#projectstatus').find('tr').each(function (i) {
if (i === 0) return;
var tr = $(this),
td = tr.find('td');
var a = tr.find('.model-link').eq(0);
var name = a.text();
if (name) {
data.push({
name:name,
link:conf.url.base + a.attr('href'),
img:base + td.eq(1).find('img').attr('src')
});
}
});
callback && callback.call(s, true, data);
} catch (e) {
console.error(e);
callback && callback.call(s, false);
}
});
};