-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira.js
123 lines (103 loc) · 2.48 KB
/
jira.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
var request = require('request');
var async = require('async');
var prompt = require('prompt');
var args = {
host: process.argv[2],
issueIdOrKey: process.argv[3],
command: process.argv[4],
custom1: process.argv[5],
custom2: process.argv[6]
};
var username;
var password;
var request = request.defaults({jar: true})
function getServiceUrl(path) {
return "https://" + username + ":" + password + "@" + args.host + path;
}
function showResponse(error, response, body) {
if (!error) {
console.log('***** RESPONSE ******');
console.log(body);
console.log('*********************');
} else {
console.log(error);
}
}
function login(callback) {
var properties = [
{
name: 'username',
validator: /^[a-zA-Z\s\-]+$/,
warning: 'Username may be only letters, spaces, or dashes.'
},
{
name: 'password',
hidden: true
}
];
prompt.start();
prompt.get(properties, function (err, result) {
if (err)
{
console.log(err);
process.exit(code=1);
}
username = result.username;
password = result.password;
callback();
});
}
var updateFieldOnIssue = function(issueIdOrKey, fieldName, newValue) {
console.log('Updating issue ' + issueIdOrKey);
var payload = {};
payload.fields = {};
payload.fields[fieldName] = newValue;
request.put(getServiceUrl("/rest/api/2/issue/" + issueIdOrKey, true),
{
json: payload
},
function (error, response, body) {
if (!error && response.statusCode == 204) {
console.log('Successfully updated field.');
} else {
console.log(body);
console.log(error);
}
});
};
var getIssue = function(issueIdOrKey, field) {
var url = getServiceUrl("/rest/api/2/issue/" + issueIdOrKey);
request(url,
function(error, response, body) {
if (field) {
var issue = JSON.parse(body);
console.log(field + ": " + issue.fields.summary);
} else {
console.log(body);
}
}
);
console.log()
}
// *** SCRIPT EXECUTION STARTS HERE ** /
if (process.argv.indexOf('--validate-ssl-cert=false') >= 0) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
}
login(function() {
var issueIdOrKey = args.issueIdOrKey;
var action = args.command;
switch (action) {
case "get":
getIssue(issueIdOrKey);
break;
case "get-field":
getIssue(issueIdOrKey, args.custom1);
break;
case "update-field":
updateFieldOnIssue(issueIdOrKey, args.custom1, args.custom2);
break;
default:
console.log("Unrecognized command: " + args.command);
break;
}
});