forked from fewle/Ticketeer-Integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ticketeer.user.js
147 lines (107 loc) · 4.09 KB
/
Ticketeer.user.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
// ==UserScript==
// @name Ticketeer Integration
// @namespace my.Home
// @version 0.2
// @description Creates a ticketeer ticket
// @author Erik Österholm
// @grant none
// @include https://access.istone.se*
// ==/UserScript==
var myTimeout;
var apiKey = localStorage.getItem('ticketeerApiKey');
var userName = localStorage.getItem('ticketeerUser');
var passWord = localStorage.getItem('ticketeerPassword');
AJS.$(document).ready(function() {
if (apiKey === "")
{
return;
}
checkDOMChange();
});
function checkDOMChange()
{
ticketeerCreate();
myTimeout = setTimeout(function(){ checkDOMChange(); }, 500);
}
function ticketeerCreate()
{
if (AJS.$("#createTicket").length > 0 || AJS.$("#summary-val").length == 0)
{
return;
}
if (AJS.$("#customStyle").length == 0)
{
AJS.$(document.body).append('<style id="customStyle">#BoardList{height:30px;margin-left:5px;} #StatusMessage{padding:5px;background-color:#56B62E;color:black;font-weight:bold;display:inline-block;margin-right:5px;border-radius:4px;border:1px solid transparent;border-color:#69BB47;} </style>');
}
var createTicketButton = AJS.$('<button id="createTicket" class="aui-button aui-button-primary aui-style">Skapa Ticketeer</button>');
createTicketButton.insertAfter('#summary-val');
setBoards(apiKey, userName, passWord);
AJS.$('#createTicket').on("click", function (e) {
e.preventDefault();
console.log("button clicked");
var selected = AJS.$("#BoardList").val();
if (selected != "0")
{
var title = "";
AJS.$.each(AJS.$('.aui-page-header-main .issue-link'), function () {
title += AJS.$(this).text() + ' ';
});
if (AJS.$('#type-val').text().indexOf("Change request") > -1) {
title = 'CR ' + title;
}
title += AJS.$('#summary-val').text();
createTicket(apiKey, userName, passWord, title, selected, AJS.$('#description-val').text())
}
});
}
function createTicket(apikey, user, pass, title, boardId, desc)
{
console.log("Creating ticket with title: " + title + " and boardid: " + boardId + " desc:" + desc);
AJS.$.ajax ( {
type: 'POST',
url: 'https://www.ticketeer.se/api/create_task',
dataType: 'json',
data: { key: apikey, board_id: boardId, task_title: title, task_description: desc},
beforeSend: function (xhr){
xhr.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass));
},
success: function (data) {
if (data.result && data.result == "success")
{
showMessage("Ticket skapad med ID: " + data.objectCreated.id);
}
}
} );
}
function showMessage(messge)
{
AJS.$('<div id="StatusMessage">' + messge + '</div>').delay(5000).fadeOut(300).insertAfter("#summary-val");
}
function setBoards(apikey, user, pass)
{
AJS.$.ajax ( {
type: 'POST',
url: 'https://www.ticketeer.se/api/get_boards',
dataType: 'json',
data: { key: apikey},
beforeSend: function (xhr){
xhr.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass));
},
success: function (data) {
if (data)
{
if (data.result)
{
showMessage(data.result);
return;
}
var boardDropdown = AJS.$('<select id="BoardList"></select>');
AJS.$(boardDropdown).append('<option value="0">Välj board att skapa i</option>')
for (var i = 0; i < data.length; i++) {
AJS.$(boardDropdown).append('<option value="' + data[i].id + '">' + data[i].name + '</option>')
}
boardDropdown.insertAfter('#createTicket');
}
}
} );
}