-
Notifications
You must be signed in to change notification settings - Fork 0
/
jiraApi.js
218 lines (190 loc) · 6.65 KB
/
jiraApi.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const Client = require('node-rest-client').Client;
const fs = require('fs');
const config = require('./config');
const client = new Client();
let sessionCookie = LoadSession(); // Load session cookie once when module is loaded
// Function to log in to Jira and obtain session cookie
function LoginJira(username, password) {
return new Promise((resolve, reject) => {
const args = {
headers: {
"Content-Type": "application/json"
},
data: {
"username": username,
"password": password
}
};
client.post(`${config.JIRA_URL}/rest/auth/1/session`, args, (data, response) => {
if (response.statusCode === 200) {
const session = data.session;
sessionCookie = session.name + '=' + session.value; // Update sessionCookie variable
SaveSession(sessionCookie);
resolve(sessionCookie);
} else {
reject(new Error('Invalid username or password.'));
}
});
});
}
// Function to validate the session cookie
function ValidateJiraSession() {
return new Promise((resolve) => {
const args = {
headers: {
cookie: sessionCookie // Use sessionCookie directly
}
};
client.get(`${config.JIRA_URL}/rest/api/latest/myself`, args, (data, response) => {
if (response.statusCode === 200) {
resolve(true); // Session cookie is valid
} else {
resolve(false); // Session cookie is invalid
}
});
});
}
// Function to get Jira user information
function GetJiraUser() {
return new Promise((resolve, reject) => {
const args = {
headers: {
cookie: sessionCookie // Use sessionCookie directly
}
};
client.get(`${config.JIRA_URL}/rest/api/latest/myself`, args, (data, response) => {
if (response.statusCode === 200) {
resolve(data); // Return user information
} else {
reject(new Error('Failed to retrieve user information. Session cookie might be invalid.'));
}
});
});
}
// Function to save session cookie to file
function SaveSession(sessionCookie) {
const cookiePath = 'session-cookie.json';
fs.writeFileSync(cookiePath, JSON.stringify(sessionCookie));
}
// Function to load session cookie from file
function LoadSession() {
const cookiePath = 'session-cookie.json';
if (fs.existsSync(cookiePath)) {
const cookieData = fs.readFileSync(cookiePath, 'utf8');
return JSON.parse(cookieData);
}
return null;
}
function CreateIssueInJira(issueData) {
const args = {
headers: {
cookie: sessionCookie,
"Content-Type": "application/json"
},
data: issueData
};
config.debug(issueData);
return new Promise((resolve, reject) => {
client.post(`${config.JIRA_URL}/rest/api/latest/issue`, args, (data, response) => {
if (response.statusCode === 201) {
config.debug('Issue created:', data.key);
resolve(data);
} else {
reject({ message: 'Failed to create issue', errors: data.errors });
}
});
});
}
// Function to create a link between two issues
function LinkIssuesInJira(issueKey1, issueKey2, linkType) {
return new Promise((resolve, reject) => {
const args = {
headers: {
cookie: sessionCookie,
"Content-Type": "application/json"
},
data: {
type: {
name: linkType // Link type, e.g., "Blocks", "Relates To", etc.
},
inwardIssue: {
key: issueKey1 // Issue key of the inward issue
},
outwardIssue: {
key: issueKey2 // Issue key of the outward issue
}
}
};
client.post(`${config.JIRA_URL}/rest/api/latest/issueLink`, args, (data, response) => {
if (response.statusCode === 201) {
console.log('Issues linked successfully');
resolve(data);
} else {
console.error('Failed to link issues:', response.statusCode, response.statusMessage);
console.error('Response body:', data);
reject(`Failed to link issues: ${response.statusCode} ${response.statusMessage}`);
}
});
});
}
/**
* async function linkRelatedIssues(issueKey1, issueKey2, linkType) {
try {
await linkIssues(issueKey1, issueKey2, linkType);
console.log('Issues linked successfully');
} catch (error) {
console.error('Error linking issues:', error.message);
}
}
*/
// Function to retrieve issue details
function GetIssue(issueKey) {
return new Promise((resolve, reject) => {
client.get(`${config.JIRA_URL}/rest/api/2/issue/${issueKey}`, {
headers: {
cookie: sessionCookie
}
}, (data, response) => {
if (response.statusCode === 200) {
resolve(data);
} else {
reject(`Failed to retrieve issue details: ${response.statusCode} ${response.statusMessage}`);
}
});
});
}
// Function to retrieve issue links
async function GetIssueLinks(issueKey) {
const issueDetails = await GetIssue(issueKey);
const linkedIssues = [];
// Function to recursively retrieve linked issues
async function GetLinkedIssues(issueKey, parentIssueType, parentSummary) {
const issueDetails = await GetIssue(issueKey);
const issueType = issueDetails.fields.issuetype.name;
const summary = issueDetails.fields.summary;
linkedIssues.push({
issueType: parentIssueType,
issueKey,
summary
});
// Retrieve and process linked issues recursively
if (issueDetails.fields.issuelinks) {
for (const link of issueDetails.fields.issuelinks) {
let linkedIssueKey = link.outwardIssue ? link.outwardIssue.key : link.inwardIssue.key;
await GetLinkedIssues(linkedIssueKey, issueType, summary);
}
}
}
// Start recursion with the main issue
await GetLinkedIssues(issueKey, issueDetails.fields.issuetype.name, issueDetails.fields.summary);
return linkedIssues;
}
module.exports = {
LoginJira,
ValidateJiraSession,
GetJiraUser,
CreateIssueInJira,
SaveSession,
LoadSession,
GetIssue
};