Jira Deployment Helper

Adds Jira Code Review button to Subversion Commits tab, highlights database changes in the comments or description that have "dbo." in them or say "ERRORFIXED" or "ERRORIGNORED", highlights tasks in list view and dashboard gadgets based on original time estimate and due date.

目前為 2015-12-01 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Jira Deployment Helper
  3. // @namespace http://www.mapyourshow.com/
  4. // @description Adds Jira Code Review button to Subversion Commits tab, highlights database changes in the comments or description that have "dbo." in them or say "ERRORFIXED" or "ERRORIGNORED", highlights tasks in list view and dashboard gadgets based on original time estimate and due date.
  5. // @include */secure/dashboard*
  6. // @include */browse/*
  7. // @include */issues/*
  8. // @grant GM_log
  9. // @version 1.3.1
  10. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  11. // ==/UserScript==
  12.  
  13. function scriptLoader(callback) {
  14. var script = document.createElement("script");
  15. script.textContent = "(" + callback.toString() + ")(window.AJS.$);";
  16. document.body.appendChild(script);
  17. }
  18.  
  19. mainCode = function($) {
  20. var version;
  21.  
  22. // This is for the main Dashboard Gadgets
  23. $(document).on('DOMNodeInserted', 'div.gadget', function(e) {
  24. var $elem = $(e.target);
  25.  
  26. $elem.on('DOMNodeInserted', function(e) {
  27. if (typeof $('div.gadget-inline') !== 'undefined') {
  28. initJiraPlugin($('div.gadget-inline').contents().find('table'));
  29. } else {
  30. initJiraPlugin($('iframe').contents().find('table'));
  31. }
  32. });
  33. });
  34.  
  35. // These are for the Issues page
  36. JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e, context, reason) {
  37. initJiraPlugin($('table#issuetable'));
  38. });
  39.  
  40. if (typeof JIRA.ViewIssueTabs !== 'undefined') {
  41. JIRA.ViewIssueTabs.onTabReady(function() {
  42. initJiraPlugin($('table#issuetable'));
  43. });
  44. }
  45.  
  46. function initJiraPlugin(issueTable) {
  47. version = parseFloat($('meta[name=ajs-version-number]').attr('content'));
  48.  
  49. // This colorizes the tasks in list view based on Original Estimate and Due Date
  50. colorTasks(issueTable, version);
  51.  
  52. // This highlights all instances of database names with "dbo." in them
  53. highlightDB(version);
  54.  
  55. // This highlights all instances of "ERRORFIXED"
  56. highlightErrorNotes(version);
  57.  
  58. // Clears duplicated highlight tags when editing a comment/description with a highlighted term in it
  59. clearInvalidHighlightTags(version);
  60.  
  61. // This adds the Code Review button in the Subversion tab
  62. addCodeReviewButton(version);
  63. }
  64.  
  65. function Change(label, url) {
  66. this.label = label;
  67. var s = url.split(url.indexOf('#') > 0 ? '#' : '?r=');
  68. this.ref = s[0];
  69. this.revision = s[1];
  70. this.commits = 1;
  71. this.pref = new RegExp('/[a-zA-Z-_]+/', '');
  72.  
  73. this.asA = function() {
  74. return this.label;
  75. };
  76.  
  77. this.setMaxRevision = function(revision) {
  78. if (revision > this.revision)
  79. this.revision = revision;
  80. this.commits++;
  81. };
  82.  
  83. this.samePrefix = function(prefix) {
  84. return getPrefix() == prefix;
  85. };
  86.  
  87. this.getPrefix = function() {
  88. return this.label.match(this.pref)[0];
  89. };
  90. };
  91.  
  92. function createReviewView() {
  93. var index = 0;
  94. var changes = [];
  95. var mapChanges = [];
  96. //select dev with id issueContent
  97. //select div with id issue_actions_container in it
  98. //select tables in it
  99. //find rows with text 'Files Changed'
  100. //select next sibling tr (in terminology of jquery it is 'next adjacent selector'
  101. //select td in it
  102. //select a in it
  103. $('div#issue_actions_container table tr:contains("Files Changed") + tr td').each(function() {
  104. var lines = $(this).text().split('\n');
  105. $.each(lines, function() {
  106. if (this.trim().substr(0, 7) == '/trunk/' || this.trim().substr(0, 10) == '/branches/') {
  107. var change = new Change(this, this);
  108. if (mapChanges[change.label]) {
  109. mapChanges[change.label].setMaxRevision(change.revision);
  110. } else {
  111. changes[index++] = change;
  112. mapChanges[change.label] = change;
  113. }
  114. }
  115. });
  116. });
  117. changes.sort(function(a, b) {
  118. return (b.label.toLowerCase() < a.label.toLowerCase()) - (a.label.toLowerCase() < b.label.toLowerCase());
  119. });
  120. var res = '<div class="issuePanelContainer" id="issue_actions_container"><table cellpadding="2" cellspacing="0" border="0" width="100%">';
  121. res += '<tbody><tr><td bgcolor="#f0f0f0"><b>Commits</b></td><td bgcolor="#f0f0f0"><b>Changed File</b></td></tr>';
  122. var prefix;
  123. var sep = false;
  124. for (var i = 0; i < index; i++) {
  125. sep = (prefix && (prefix != changes[i].getPrefix()));
  126. res = res + '<tr><td' + style(sep) + '>' + changes[i].commits + '</td><td' + style(sep) + '>' + changes[i].asA() + '</td></tr>';
  127. prefix = changes[i].getPrefix();
  128. }
  129. return res + '</tbody></table></div>';
  130. };
  131.  
  132. function style(sep) {
  133. return sep ? ' style="border-top: solid #BBB 1px;"' : '';
  134. };
  135.  
  136. function highlightDB(version) {
  137. if ($('#description-val').length) {
  138. var $descriptionDiv = $('#description-val');
  139. } else if ($('#issue-description').length) {
  140. var $descriptionDiv = $('#issue-description');
  141. }
  142. if ($descriptionDiv) {
  143. var result = $descriptionDiv.html();
  144. var regex = new RegExp('[\\w.*_]*dbo\\.[\\w]*','ig');
  145. $descriptionDiv.html(result.replace(regex, '<span style="background-color:yellow;">$&</span>'));
  146. }
  147.  
  148. $('.action-body').each(function() {
  149. result = $(this).html();
  150. regex = new RegExp('[\\w.*_]*dbo\\.[\\w]*','ig');
  151. $(this).html(result.replace(regex, '<span style="background-color:yellow;">$&</span>'));
  152. });
  153. }
  154.  
  155. function highlightErrorNotes(version) {
  156. if ($('#description-val').length) {
  157. var $descriptionDiv = $('#description-val');
  158. } else if ($('#issue-description').length) {
  159. var $descriptionDiv = $('#issue-description');
  160. }
  161. if ($descriptionDiv) {
  162. var result = $descriptionDiv.html();
  163.  
  164. var regex = new RegExp('ERRORFIXED','g');
  165. $descriptionDiv.html(result.replace(regex, '<span style="background-color:#ffff00;">$&</span>'));
  166.  
  167. var regex = new RegExp('ERRORIGNORED','g');
  168. $descriptionDiv.html(result.replace(regex, '<span style="background-color:#90ee90;">$&</span>'));
  169. }
  170.  
  171. $('.action-body').each(function() {
  172. result = $(this).html();
  173.  
  174. regex = new RegExp('ERRORFIXED','g');
  175. $(this).html(result.replace(regex, '<span style="background-color:#ffff00;">$&</span>'));
  176.  
  177. regex = new RegExp('ERRORIGNORED','g');
  178. $(this).html(result.replace(regex, '<span style="background-color:#90ee90;">$&</span>'));
  179. });
  180. }
  181.  
  182. function clearInvalidHighlightTags(version) {
  183. if ($('#description-val').length) {
  184. var $descriptionDiv = $('#description-val');
  185. } else if ($('#issue-description').length) {
  186. var $descriptionDiv = $('#issue-description');
  187. }
  188. if ($descriptionDiv) {
  189. $descriptionDiv.html($descriptionDiv.html().replace('&lt;span style="background-color:yellow;"&gt;', ''));
  190. $descriptionDiv.html($descriptionDiv.html().replace('&lt;/span&gt;', ''));
  191. }
  192.  
  193. $('.action-body').each(function() {
  194. $(this).html($(this).html().replace('&lt;span style="background-color:yellow;"&gt;', ''));
  195. $(this).html($(this).html().replace('&lt;/span&gt;', ''));
  196. });
  197. }
  198.  
  199. function addCodeReviewButton(version) {
  200. if (!$('#review_button_div').length) {
  201. if (version >= 6) {
  202. if ($('li#subversion-commits-tabpanel').hasClass('active') == 1) { //where the Subversion Commits is but not inside a tag
  203. $('div#issue_actions_container:first').prepend('<div id="review_button_div" style="width: 100%;margin-bottom:10px;"><button id="review_button">Code Review</button></div>');
  204. var view = createReviewView();
  205. var revView = false;
  206. $('#review_button').click(function() {
  207. var oldView = $('table', 'div#issue_actions_container').detach();
  208. $('div#review_button_div').after(view);
  209. $('#review_button').text(revView ? 'Code Review' : 'All Commits');
  210. view = oldView;
  211. revView = !revView;
  212. });
  213. }
  214. } else {
  215. if ($('li#subversion-commits-tabpanel').hasClass('active') == 1) { //where the Subversion Commits is but not inside a tag
  216. $('ul#issue-tabs').closest('div').after('<div id="review_button_div" style="width: 100%;margin-bottom:10px;"><button id="review_button">Code Review</button></div>');
  217. var view = createReviewView();
  218. var revView = false;
  219. $('#review_button').click(function() {
  220. var oldView = $('div#issue_actions_container').detach();
  221. $('div#review_button_div').after(view);
  222. $('#review_button').text(revView ? 'Code Review' : 'All Commits');
  223. view = oldView;
  224. revView = !revView;
  225. });
  226. }
  227. }
  228. }
  229. }
  230.  
  231. function colorTasks(issueTable, version) {
  232. if (issueTable.length && (issueTable.find('td.timeoriginalestimate').length || issueTable.find('td.aggregatetimeoriginalestimate').length) && issueTable.find('td.duedate').length) {
  233. var $issueTable = issueTable,
  234. $issueTableRows = issueTable.find('tr:gt(0)'); // skip the header row
  235.  
  236. $issueTableRows.each(function(index) {
  237. var $originalEstimate = ($.trim($('td.timeoriginalestimate', this).html()) == '' ? $.trim($('td.aggregatetimeoriginalestimate', this).html()) : $.trim($('td.timeoriginalestimate', this).html())),
  238. $dueDate = (typeof($('td.duedate', this).html()) == 'string' ? $('td.duedate', this).html() : ''),
  239. $originalEstimateDays,
  240. $originalEstimateMinutes,
  241. $numberDays,
  242. thisdate,
  243. today;
  244.  
  245. // Get total estimated minutes
  246. $originalEstimateMinutes = ($originalEstimate.match(/\d+(?= week)/ig) == null ? 0 : parseInt($originalEstimate.match(/\d+(?= week)/ig)) * 2400)
  247. + ($originalEstimate.match(/\d+(?= day)/ig) == null ? 0 : parseInt($originalEstimate.match(/\d+(?= day)/ig)) * 480)
  248. + ($originalEstimate.match(/\d+(?= hour)/ig) == null ? 0 : parseInt($originalEstimate.match(/\d+(?= hour)/ig)) * 60)
  249. + ($originalEstimate.match(/\d+(?= minute)/ig) == null ? 0 : parseInt($originalEstimate.match(/\d+(?= minute)/ig)));
  250.  
  251. // Now let's account for weekends
  252. $numberDays = Math.ceil($originalEstimateMinutes / 480);
  253.  
  254. thisdate = new Date();
  255. today = thisdate.getDay();
  256.  
  257. switch (true) {
  258. case ($numberDays == 0):
  259. if (today == 6) {
  260. $originalEstimateMinutes += 480;
  261. } else if (today == 5) {
  262. $originalEstimateMinutes += 960;
  263. }
  264. break;
  265. case ($numberDays == 1):
  266. if (today == 6) {
  267. $originalEstimateMinutes += 480;
  268. } else if (today == 5) {
  269. $originalEstimateMinutes += 960;
  270. }
  271. break;
  272. case ($numberDays == 2):
  273. if (today == 6) {
  274. $originalEstimateMinutes += 480;
  275. } else if (today >= 4) {
  276. $originalEstimateMinutes += 960;
  277. }
  278. break;
  279. case ($numberDays == 3):
  280. if (today == 6) {
  281. $originalEstimateMinutes += 480;
  282. } else if (today >= 3) {
  283. $originalEstimateMinutes += 960;
  284. }
  285. break;
  286. case ($numberDays == 4):
  287. if (today == 6) {
  288. $originalEstimateMinutes += 480;
  289. } else if (today >= 2) {
  290. $originalEstimateMinutes += 960;
  291. }
  292. break;
  293. case ($numberDays == 5):
  294. if (today == 6) {
  295. $originalEstimateMinutes += 480;
  296. } else if (today >= 1) {
  297. $originalEstimateMinutes += 960;
  298. }
  299. break;
  300. case ($numberDays >= 6 && $numberDays < 11):
  301. if (today == 6) {
  302. $originalEstimateMinutes += 1440;
  303. } else if (today >= 5) {
  304. $originalEstimateMinutes += 1920;
  305. } else if (today == 0) {
  306. $originalEstimateMinutes += 960;
  307. }
  308. break;
  309. case ($numberDays >= 11 && $numberDays < 16):
  310. if (today == 6) {
  311. $originalEstimateMinutes += 2400;
  312. } else if (today >= 5) {
  313. $originalEstimateMinutes += 2880;
  314. } else if (today == 0) {
  315. $originalEstimateMinutes += 1920;
  316. }
  317. break;
  318. case ($numberDays >= 16 && $numberDays < 21):
  319. if (today == 6) {
  320. $originalEstimateMinutes += 3360;
  321. } else if (today >= 5) {
  322. $originalEstimateMinutes += 3840;
  323. } else if (today == 0) {
  324. $originalEstimateMinutes += 2880;
  325. }
  326. break;
  327. case ($numberDays >= 21 && $numberDays < 26):
  328. if (today == 6) {
  329. $originalEstimateMinutes += 4350;
  330. } else if (today >= 5) {
  331. $originalEstimateMinutes += 4800;
  332. } else if (today == 0) {
  333. $originalEstimateMinutes += 3840;
  334. }
  335. break;
  336. default:
  337. break;
  338. }
  339.  
  340. // The number of total days estimated, including weekends
  341. $originalEstimateDays = Math.ceil($originalEstimateMinutes / 480);
  342. $daysTilDueDate = Math.ceil(dateDiff($dueDate));
  343.  
  344. if ($originalEstimate.trim() != '') { // If an Original Estimate was given
  345. if (($daysTilDueDate - $originalEstimateDays) < 1) {
  346. $(this).css('background-color','#ff9999'); // Red
  347. } else if (($daysTilDueDate - $originalEstimateDays) <= 2) {
  348. $(this).css('background-color','#fed080'); // Orange
  349. } else if (($daysTilDueDate - $originalEstimateDays) <= 4) {
  350. $(this).css('background-color','#fffeab'); // Yellow
  351. }
  352. } else {
  353. if (($daysTilDueDate - $originalEstimateDays) < 1) {
  354. $(this).css('background-color','#ff9999'); // Red
  355. } else if (($daysTilDueDate - $originalEstimateDays) == 1) {
  356. $(this).css('background-color','#fed080'); // Orange
  357. } else if (($daysTilDueDate - $originalEstimateDays) == 2) {
  358. $(this).css('background-color','#fffeab'); // Yellow
  359. }
  360. }
  361. });
  362. } else if ($('.issue-list').length) {
  363. // Do nothing for now
  364. }
  365. }
  366.  
  367. function dateDiff(dueDate) {
  368. var dueDate = dueDate.replace('Jan', '0')
  369. .replace('Feb', '1')
  370. .replace('Mar', '2')
  371. .replace('Apr', '3')
  372. .replace('May', '4')
  373. .replace('Jun', '5')
  374. .replace('Jul', '6')
  375. .replace('Aug', '7')
  376. .replace('Sep', '8')
  377. .replace('Oct', '9')
  378. .replace('Nov', '10')
  379. .replace('Dec', '11');
  380. var arrDueDate = dueDate.split('/');
  381. var dt1 = new Date();
  382. var dt2 = new Date('20' + arrDueDate[2], arrDueDate[1], arrDueDate[0]);
  383. var one_day = 1000 * 60 * 60 * 24;
  384.  
  385. return (parseInt(dt2.getTime() - dt1.getTime()) / (one_day));
  386. }
  387. }
  388.  
  389. scriptLoader(mainCode);