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.

当前为 2017-02-21 提交的版本,查看 最新版本

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