-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjira.js
168 lines (142 loc) · 4.92 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
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
//Creates an Issue in JIRA
function createIssue(json, image, ycdesk){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function() {
if (xhr.readyState === 4){ //if complete
if(xhr.status === 201){ //check if "OK" (200
var result = JSON.parse(xhr.responseText);
addAttachment(image, result.key);
if(ycdesk != "")
createLink(result.key, ycdesk);
} else {
console.log("Error", xhr.responseText);
}
}
}
xhr.open("POST", "https://yexttest.atlassian.net/rest/api/2/issue/");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(json);
}
//Used to log Project and Issue types that can be used to create an Issue
function printMeta(){
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://yexttest.atlassian.net/rest/api/2/issue/createmeta", false);
xhr.onreadystatechange=function() {
if (xhr.readyState === 4){ //if complete
if(xhr.status === 200){ //check if "OK" (200)
var result = JSON.parse(xhr.responseText);
//code for logging projects in createmeta
var projects = result.projects
for(var x = 0; x < projects.length; x++)
{
console.log("PROJECT:", projects[x].name, projects[x].id, projects[x].key);
if(projects[x].id == 11300) //Consulting
{
var types = projects[x].issuetypes
//code for logging issue types
for(var i = 0; i < types.length; i++)
{
console.log(types[i].name, types[i].id);
}
}
}
} else {
console.log("Error", xhr.responseText);
}
}
}
xhr.send();
}
//Adds screenshot named "Screenshot.jpeg" to attachment of issue key
function addAttachment(image, key){
var xhr = new XMLHttpRequest();
var formData = new FormData();
formData.append("file", image, "Screenshot.jpeg");
xhr.open("POST", "https://yexttest.atlassian.net/rest/api/2/issue/"+key+"/attachments/", false);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("X-Atlassian-Token", "no-check");
xhr.onreadystatechange=function() {
if (xhr.readyState === 4){ //if complete
if(xhr.status === 200){ //check if "OK" (200)
var result = JSON.parse(xhr.responseText);
var jiraURL = "https://yexttest.atlassian.net/browse/" + key
chrome.tabs.create({ url: jiraURL });
}
else {
console.log("Error", xhr.responseText);
}
}
}
xhr.send(formData);
}
//Retrieve information about issue, used to test the add attachment function
function getIssue(key){
genericRequest("https://yexttest.atlassian.net/rest/api/2/issue/"+key+"/")
}
function createLink(ownIssue, outIssue){
var json = {
"type": {
"name": "Relates",
},
"inwardIssue": {
"key": ownIssue,
},
"outwardIssue": {
"key": outIssue,
}
} ;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function() {
if (xhr.readyState === 4){ //if complete
if(xhr.status === 201){ //check if "OK" (201)
} else {
console.log("Error", xhr.responseText);
}
}
}
xhr.open("POST", "https://yexttest.atlassian.net/rest/api/2/issueLink/");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(json));
}
function genericRequest(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.onreadystatechange=function() {
if (xhr.readyState === 4){ //if complete
if(xhr.status === 200){ //check if "OK" (200)
var result = JSON.parse(xhr.responseText);
console.log(result)
}
else {
console.log("Error", xhr.responseText);
}
}
}
xhr.send();
}
/**
* Convert a base64 string in a Blob according to the data and contentType.
*
* @param b64Data {String} Pure base64 string without contentType
* @param contentType {String} the content type of the file i.e (image/jpeg - image/png - text/plain)
* @param sliceSize {Int} SliceSize to process the byteCharacters
* @see http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript
* @return Blob
*/
function b64toBlob(b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
var byteCharacters = atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}