From fefb8100654831c28eb0df2135190ce64e9b6a4c Mon Sep 17 00:00:00 2001 From: Brice Vandeputte Date: Fri, 31 Jan 2025 09:09:35 +0100 Subject: [PATCH] Fix #13 keep 17_7 compatibility, augment readme --- .github/CONTRIBUTING.md | 12 +++ .gitignore | 3 + README.md | 3 + enhance-merge-requests.js | 192 +++++++++++++++++++++++++------------- 4 files changed, 147 insertions(+), 63 deletions(-) create mode 100644 .github/CONTRIBUTING.md diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..d357058 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,12 @@ +## How to debug/run test locally + +Clone this repository locally and then tell to google Chrome browser to use your local extension source code. + +1) (fork &) clone github repository & update the code (ex. switch `debugMode` to `true`) +2) Open google chrome [chrome://extensions/](chrome://extensions/) +3) remove official extension and switch to activate `developper mode` (toggle button upper right corner) +4) then `Load unpackaged extension` : you need to **select repository directory** + +Then you are ok, you can go to a gitlab MR list to show your updates. + +On source update, repeat step 4) only. \ No newline at end of file diff --git a/.gitignore b/.gitignore index e43b0f9..c709e93 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store + +## IntelliJ +.idea \ No newline at end of file diff --git a/README.md b/README.md index d588b67..2ef4c6e 100755 --- a/README.md +++ b/README.md @@ -8,3 +8,6 @@ https://chrome.google.com/webstore/detail/gitlab-unresolved-threads/dhmmedpdnmhm Pull requests are welcome :) + +## How to contribute ? +cf. [Contributing](./.github/CONTRIBUTING.md) \ No newline at end of file diff --git a/enhance-merge-requests.js b/enhance-merge-requests.js index 295329e..f8abc78 100755 --- a/enhance-merge-requests.js +++ b/enhance-merge-requests.js @@ -1,69 +1,135 @@ -if (/^https:\/\/gitlab\.*.*\/merge_requests\/?(\?.*)?$/.test(window.location.href)) { - function createThreadsBadge(element, color, resolved, resolvable) { +//~ commons - means compatible with 17_7 and 17_9 +const createThreadsBadge = (element, color, resolved, resolvable) => { const li = $("
  • ").addClass("issuable-comments d-none d-sm-flex").prependTo(element); $("") - .addClass("badge color-label") - .css("background-color", color) - .css("color", "#333333") - .text(`${resolved}/${resolvable} threads resolved`) - .prependTo(li); - } - - function run() { - $(".issue").each(function () { - const anchor = $(this).find(".issue-title a")[0]; - const metaList = $(this).find(".issuable-meta ul, ul.controls")[0]; - - if (!anchor || !metaList) return; - - $.ajax({ - url: `${anchor.href}/discussions.json`, - success: function (result) { - let resolvable = 0; - let resolved = 0; - result.forEach((item) => { - if (item.resolvable) resolvable++; - if (item.resolved) resolved++; - }); - - if (resolvable > resolved) { - createThreadsBadge(metaList, "#ffd3d3", resolved, resolvable); - } else if (resolved === resolvable && resolvable > 0) { - createThreadsBadge(metaList, "#8fc7a6", resolved, resolvable); - } - }, - }); + .addClass("badge color-label") + .css("background-color", color) + .css("color", "#333333") + .text(`${resolved}/${resolvable} threads resolved`) + .prependTo(li); +} +const isCurrentPageAGitlabMRList = () => /^https:\/\/gitlab\.*.*\/merge_requests\/?(\?.*)?$/.test(window.location.href) + +//~ commons end. + +//~ before 17.7.x gitlab version +const gitlab_17_7_detect = () => { + return ($(".merge-request").length > 0); +} + +const gitlab_17_7_run = () => { + $(".merge-request").each(function () { + const anchor = $(this).find(".merge-request-title-text a")[0]; + const metaList = $(this).find(".issuable-meta ul, ul.controls")[0]; + + $.ajax({ + url: `${anchor.href}/discussions.json`, + success: function (result) { + let resolvable = 0; + let resolved = 0; + result.forEach(item => { + if (item.resolvable) resolvable++; + if (item.resolved) resolved++; + }); + + if (resolvable > resolved) { + createThreadsBadge(metaList, "#ffd3d3", resolved, resolvable); + } else if (resolved === resolvable && resolvable > 0) { + createThreadsBadge(metaList, "#8fc7a6", resolved, resolvable); + } + } + }); }); - } - - try { - if (typeof MutationObserver !== "undefined") { - const targetNode = document.querySelector(".content-list"); - - if (targetNode) { - const observer = new MutationObserver(run); - - const config = { - childList: true, // Observe additions/removals of child nodes - attributes: false, // Observe attribute changes - subtree: false, // Observe changes in all descendants - characterData: false, // Observe text content changes - }; - - observer.observe(targetNode, config); - - console.log("MutationObserver is now watching .content-list"); - } else { - throw new Error(".content-list element not found!"); - } - } else { - throw new Error("MutationObserver is not supported in this environment."); +} +//~ before 17.7.x end. + +//~ after 17.9.x gitlab version +const gitlab_17_9_latest_detect = () => { + return ($(".issue").length > 0); +} +const gitlab_17_9_latest_main = () => { + function run() { + $(".issue").each(function () { + const anchor = $(this).find(".issue-title a")[0]; + const metaList = $(this).find(".issuable-meta ul, ul.controls")[0]; + + if (!anchor || !metaList) return; + + $.ajax({ + url: `${anchor.href}/discussions.json`, + success: function (result) { + let resolvable = 0; + let resolved = 0; + result.forEach((item) => { + if (item.resolvable) resolvable++; + if (item.resolved) resolved++; + }); + + if (resolvable > resolved) { + createThreadsBadge(metaList, "#ffd3d3", resolved, resolvable); + } else if (resolved === resolvable && resolvable > 0) { + createThreadsBadge(metaList, "#8fc7a6", resolved, resolvable); + } + }, + }); + }); } - } catch (error) { - console.error("MutationObserver setup failed:", error.message); - setTimeout(() => { - run(); - }, 3000); - } + try { + if (typeof MutationObserver !== "undefined") { + const targetNode = document.querySelector(".content-list"); + + if (targetNode) { + const observer = new MutationObserver(run); + + const config = { + childList: true, // Observe additions/removals of child nodes + attributes: false, // Observe attribute changes + subtree: false, // Observe changes in all descendants + characterData: false, // Observe text content changes + }; + + observer.observe(targetNode, config); + + console.log("MutationObserver is now watching .content-list"); + } else { + throw new Error(".content-list element not found!"); + } + } else { + throw new Error("MutationObserver is not supported in this environment."); + } + } catch (error) { + console.error("MutationObserver setup failed:", error.message); + + setTimeout(() => { + run(); + }, 3000); + } +} +//~ after 17.9.x end. + + +//~ main entry point +const extension_main = (debugMode = false) => { + if (!isCurrentPageAGitlabMRList()) { + return;// avoid processing for out-of-scope pages + } + if (gitlab_17_7_detect()) { + debugMode && console.log("gitlab-unresolved-threads 17_7"); + gitlab_17_7_run() + return; + } + + if (gitlab_17_9_latest_detect()) { + debugMode && console.log("gitlab-unresolved-threads 17_9"); + gitlab_17_9_latest_main(); + return; + } + + console.warn("gitlab-unresolved-threads extension is not compatible with current gitlab version." + + "If you're using a recent gitlab version, you can open a github issue."); + } + +const debugMode = false; +extension_main(debugMode);