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