]> src.twobees.de Git - tampermonkeyscripts.git/blob - AzureDevOpsCommentEnhancer.user.js
0.15: fix links to work items found in commitmessages (e.g. #1235)
[tampermonkeyscripts.git] / AzureDevOpsCommentEnhancer.user.js
1 // ==UserScript==
2 // @name         Fix ADS checkin comments in discussion and history of workitems
3 // @version      0.15
4 // @author       Tobias Sachs
5 //  ... in @match replace "ads" with the url of you Azure DevOps Server
6 // @match        https://ads/*
7 // @updateURL    https://src.twobees.de/?p=tampermonkeyscripts.git;a=blob_plain;f=AzureDevOpsCommentEnhancer.user.js;hb=HEAD
8 // @downloadURL  https://src.twobees.de/?p=tampermonkeyscripts.git;a=blob_plain;f=AzureDevOpsCommentEnhancer.user.js;hb=HEAD
9 // @grant        none
10 // @description
11 // ==/UserScript==
12
13 // 0.15: fix links to work items found in Commitmessages (e.g. #1235)
14 // 0.14: also check html property of comments
15 // 0.13: Allow to manually insert changeset comments, which where not associated with the item during checking.
16 //       Just copy changecomment into the commentsection and prefix with "Associated with changeset CHANGESET_NUMBER:"
17 // 0.12: also fix "Resolved with changeset" comments
18 // 0.11: fix regexp for later changesets attached.
19 // 0.10: #Bugnumber to links, highlight comments for customors in checkins
20 // 0.09: updateq download/update URLs
21 // 0.08: fix typregexp for later changesets attachedos/formatting
22 // 0.07: fix work itlsem tampering
23 // 0.06: Add link to Changeset in diff view
24
25 /* jshint esversion:6 */
26 (function() {
27     'use strict';
28     let timerId;
29
30     let fixWorkitems = () => {
31         let found = document.getElementsByClassName("comment-content");
32         fixCommentContents(found);
33
34         found = document.getElementsByClassName("history-item-comment");
35         fixCommentContents(found);
36
37         console.debug("observe...");
38     };
39
40     let checkRegex = /^.*(Associated|Resolved).*[:.]/;
41     let fixCommentContents = (items) => {
42         if (items === null || items === undefined || items.length === 0) {
43             return;
44         }
45         console.info("fixing '" + items.length +"' comments.");
46         for (var i = 0; i < items.length; i++) {
47             let el = items[i];
48             let html = el.innerHTML;
49
50
51             if (    checkRegex.test(html)
52                 || (el.textContext && checkRegex.test(el.textContext))
53                ) {
54                 html = html.replace(/((Associated|Resolved) with changeset )(\d*)([:.])/,
55                     "<b>$1<a href='/HeBa/Entwicklung/_versionControl/changeset/$3'>$3</a></b>$4<br />");
56                 html = html.replace(/#(\d+)/g, "<a href='/HeBa/Entwicklung/_workitems/edit/$1'>#$1</a>");
57                 html = html.replace(/\n/gi, "<br />");
58                 html = html.replace(/(@@CUST.*)/s, "<br><span style=\"color: green; font-style:italic;\">$1</span>");
59                 el.innerHTML = html;
60             }
61         }
62     };
63
64     let fixVersionControl = () => {
65         let elToFix;
66         let found = document.getElementsByClassName("changeset-version")[0];
67         if (!found) {
68             return;
69         }
70
71         // if opened from email notification, it is the first span in div "changeset-version"
72         elToFix = found.querySelector("span");
73
74         if (!elToFix) {
75             return;
76         }
77
78         elToFix.innerHTML = elToFix.innerHTML
79             .replace(/(Changeset )(\d+)/,
80                 "$1<a href='/HeBa/Entwicklung/_versionControl/changeset/$2'>$2</a>");
81     };
82
83     let fixit = () => {
84         if (timerId) {
85             console.debug("fixit timerreset...");
86             clearTimeout(timerId);
87         }
88
89         observer.disconnect();
90
91         timerId = setTimeout(function() {
92             timerId = undefined;
93
94             let url = window.location.href;
95
96             if (url.includes("/_versionControl")) {
97                 fixVersionControl();
98             }
99             else {
100                 // if (url.includes("/_workitems")){
101                 // does not work since workitems are often shown in
102                 // dialogs on random pages
103                 fixWorkitems();
104             }
105
106             // keep watching for changes
107             observer.observe(document, { subtree: true, childList: true, characterData: true });
108         }, 300);
109     };
110
111     const observer = new MutationObserver(function() {
112         console.debug('observer was triggered...');
113         fixit();
114     });
115
116     fixit();
117
118 })();
119