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