Confirm GitLab Merge Request Creator

Confirm GitLab merge request when creator differs from the current user.

  1. // ==UserScript==
  2. // @name Confirm GitLab Merge Request Creator
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description Confirm GitLab merge request when creator differs from the current user.
  6. // @author Ben L
  7. // @match https://gitlab.covergenius.biz/*/merge_requests/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=covergenius.biz
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. "use strict";
  15.  
  16. let hasConfirmed = false;
  17.  
  18. const handleMergeButtonClick = (event) => {
  19. if (hasConfirmed) {
  20. return;
  21. }
  22.  
  23. const currentUserId = window.gon?.current_user_id;
  24. const creatorElement = document.querySelector("[data-user-id]");
  25.  
  26. if (!creatorElement) {
  27. return;
  28. }
  29.  
  30. const creatorUserId = parseInt(
  31. creatorElement.getAttribute("data-user-id"),
  32. 10
  33. );
  34.  
  35. if (currentUserId !== creatorUserId) {
  36. event.stopImmediatePropagation();
  37. const confirmed = confirm(
  38. "The creator of this MR is not you. Do you want to proceed with the merge?"
  39. );
  40.  
  41. if (!confirmed) {
  42. event.preventDefault();
  43. } else {
  44. hasConfirmed = true;
  45. event.target.click();
  46. }
  47. }
  48. };
  49.  
  50. const observer = new MutationObserver(() => {
  51. const mergeButton = document.querySelector(
  52. ".accept-merge-request.btn-confirm"
  53. );
  54.  
  55. if (mergeButton) {
  56. mergeButton.addEventListener("click", handleMergeButtonClick, true);
  57. observer.disconnect();
  58. }
  59. });
  60.  
  61. observer.observe(document.body, { childList: true, subtree: true });
  62. })();