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