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.

当前为 2016-02-03 提交的版本,查看 最新版本

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