-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
129 lines (102 loc) · 3.69 KB
/
main.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
var OAuth = require('oauth').OAuth;
var fs = require('fs');
var url = require('url');
var basicRequest = require('request');
var request = function(options, callback){
var urlParts = url.parse(options.uri);
if (urlParts.auth){
executeBasic.apply(this, arguments);
} else {
executeOAuth.apply(this, arguments);
}
};
request.oauthConfig = {
jiraUrl : undefined,
consumerPrivateKeyFile : undefined,
oauthAccessTokenSecret : undefined,
consumerKey : undefined,
oauthAccessToken : undefined,
callbackUrl : undefined
};
function executeBasic(){
basicRequest.apply(this, arguments);
}
function executeOAuth(options, callback){
var requestBody,
requestCallback = function(error, data, resp){
if (error==null){
data = JSON.parse(data);
}
callback && callback(error, resp, data);
},
contentType = "application/json",
config = request.oauthConfig;
var consumer = mayBeBuildConsumer();
if (options.body){
if( typeof options.body != "string" ) {
requestBody = object2body(options.body);
} else {
requestBody = options.body;
}
}
switch(options.method){
case "GET":{
consumer.get(options.uri,
config.oauthAccessToken,
config.oauthAccessTokenSecret,
contentType,
requestCallback
);
break;
}
case "POST":{
consumer.post(options.uri,
config.oauthAccessToken,
config.oauthAccessTokenSecret,
requestBody,
contentType,
requestCallback
);
break;
}
case "PUT":{
consumer.put(options.uri,
config.oauthAccessToken,
config.oauthAccessTokenSecret,
requestBody,
contentType,
requestCallback
);
break;
}
case "DELETE":{
consumer.delete(options.uri,
config.oauthAccessToken,
config.oauthAccessTokenSecret,
requestCallback
);
break;
}
}
}
function object2body(params){
return JSON.stringify(params);
}
function mayBeBuildConsumer(){
var config = request.oauthConfig, privateKeyData;
privateKeyData = fs.readFileSync(config.consumerPrivateKeyFile, "utf8");
if (request.consumer === undefined){
request.consumer = new OAuth(config.jiraUrl + "/plugins/servlet/oauth/request-token",
config.jiraUrl +"/plugins/servlet/oauth/access-token",
config.consumerKey,
"",
"1.0",
config.callbackUrl,
"RSA-SHA1",
null,
privateKeyData
);
}
return request.consumer;
}
module.exports = request;