Gitlab share

Allows to easily share links to issues and MRs

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @license     MIT
// @name        Gitlab share
// @namespace   https://gitlab.ilab.zone/
// @description Allows to easily share links to issues and MRs
// @match       https://gitlab.ilab.zone/*
// @version     1.0.0
// @author      rwysocki
// @require     https://cdn.jsdelivr.net/combine/npm/@violentmonkey/dom@2,npm/@violentmonkey/[email protected]
// @grant       GM_addStyle
// ==/UserScript==

(function () {
'use strict';

var styles = {"toast":"toast-module_toast__HTmbd","error":"toast-module_error__cc4Ok"};
var stylesheet=".toast-module_toast__HTmbd{left:unset;right:10px;top:10px;transform:unset}.toast-module_error__cc4Ok{background-color:red;color:#fff}";

const copy = async ({
  fullTitle,
  url
}) => {
  await navigator.clipboard.write([new ClipboardItem({
    [`text/plain`]: new Blob([fullTitle], {
      type: `text/plain`
    }),
    [`text/html`]: new Blob([`<a href="${url}">${fullTitle}</a>`], {
      type: `text/html`
    })
  })]);
  VM.showToast('Link copied!', {
    style: stylesheet,
    className: styles.toast
  });
};
const getBoardIssueLink = () => {
  const selectedCard = document.querySelector("[data-testid='board_card'].gl-bg-blue-50");
  const issueNumber = selectedCard.querySelector('.board-card-footer').innerText.split('\n')[1];
  const title = selectedCard.querySelector('.board-card-title').textContent.trim();
  return {
    fullTitle: `[${issueNumber}] ${title}`,
    url: `https://gitlab.ilab.zone/ihlc/teams/mvp-teams/external-innovators/team-cmre/mcm-trumpet/-/issues/${issueNumber.substring(1)}`
  };
};
const getMergeRequestLink = () => {
  const title = document.querySelector(`[data-qa-selector='title_content']`).textContent.trim();
  return {
    fullTitle: `MR: ${title}`,
    url: window.location.href
  };
};
const getIssueLink = () => {
  const issueNumber = document.querySelector(`[data-qa-selector='breadcrumb_current_link']`).textContent.trim();
  const title = document.querySelector(`[data-qa-selector='title_content']`).textContent.trim();
  return {
    fullTitle: `[${issueNumber}] ${title}`,
    url: `https://gitlab.ilab.zone/ihlc/teams/mvp-teams/external-innovators/team-cmre/mcm-trumpet/-/issues/${issueNumber.substring(1)}`
  };
};
function ShareIssueButton({
  linkProvider
}) {
  const handleClick = async () => {
    try {
      const linkParams = linkProvider();
      await copy(linkParams);
    } catch (e) {
      VM.showToast('Something went wrong', {
        style: stylesheet,
        className: [styles.toast, styles.error].join(' ')
      });
      console.error(e);
    }
  };
  return VM.h("button", {
    type: "button",
    class: "btn hide-collapsed btn-default btn-sm gl-button",
    size: "small",
    "data-testid": 'share',
    onclick: handleClick
  }, VM.h("span", {
    class: "gl-button-text"
  }, "Share"));
}
VM.observe(document.body, () => {
  if (window.location.pathname.includes('/-/boards/')) {
    const toDoList = document.querySelector('[data-testid="sidebar-todo"]');
    if (toDoList !== null && !hasShareButton(toDoList)) {
      toDoList.appendChild(VM.m(VM.h(ShareIssueButton, {
        linkProvider: getBoardIssueLink
      })));
    }
  }
  if (window.location.pathname.includes('/-/issues/')) {
    const toDoList = document.querySelector('.issuable-sidebar-header');
    if (toDoList !== null && !hasShareButton(toDoList)) {
      toDoList.appendChild(VM.m(VM.h(ShareIssueButton, {
        linkProvider: getIssueLink
      })));
    }
  }
  if (window.location.pathname.includes('/-/merge_requests/')) {
    const toDoList = document.querySelector('[data-testid="sidebar-todo"]');
    if (toDoList !== null && !hasShareButton(toDoList)) {
      toDoList.appendChild(VM.m(VM.h(ShareIssueButton, {
        linkProvider: getMergeRequestLink
      })));
    }
  }
});
const hasShareButton = el => {
  const shareButton = el.querySelector('[data-testid="share"]');
  return shareButton !== null;
};

})();