floodmeadows / Jira ready for review message

// ==UserScript==
// @name         Jira ready for review message
// @description  Lets you copy a message ready for pasting into Slack when a story is ready for review or prioritisation
// @namespace    https://openuserjs.org/users/floodmeadows
// @author       floodmeadows
// @copyright    2021, floodmeadows (https://openuserjs.org/users/floodmeadows)
// @license      MIT
// @version      0.4
// @include      https://jira.*.uk/browse/*
// @updateURL    https://openuserjs.org/meta/floodmeadows/Jira_ready_for_review_message.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/floodmeadows/Jira_ready_for_review_message.user.js
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

const currentUrl = new URL(document.URL);
const jiraBaseUrl = currentUrl.protocol + '//' + currentUrl.host;
const issueKey = document.getElementById("key-val").childNodes[0].nodeValue;
const issueName = document.getElementById("summary-val").childNodes[0].nodeValue;
const issueUrl = `${jiraBaseUrl}/browse/${issueKey}`;
const slackEmojiForLgtm = ":lgtm:";

(function() {
    'use strict';

    addButtonReadyForReview();
    addButtonReadyForPrioritisation();
})();


function copyReadyForReviewMessage() {
  const text = `@android @ios @mobile-services @mobile-qa Story ready for review:
> ${issueName}
> (${issueUrl})

All OK? Just respond with :lgtm: and your points estimates.
Comments or questions? Add them in a thread >>>
If it's worth a refinement call, just let me know :) Thanks`

  const html = `@android @ios @mobile-services @mobile-qa Story ready for review:<br />
<a href="${issueUrl}">'${issueName}' (${issueKey})</a><br />
<br />
All OK? Just respond with ${slackEmojiForLgtm} and your points estimates.<br />
Comments or questions? Add them in a thread >>><br />
If it's worth a refinement call, just let me know :simple-smile: :thanks:`

  copyTextAndHtmlToClipboard(text, html)
}

function copyReadyForPrioritisationMessage() {
  const text = `@mobile-prioritisers this is now Ready For Dev:
> ${issueName}
> (${issueUrl})`

  const html = `@mobile-prioritisers this is now :ready-4-dev: :<br />
  <a href="${issueUrl}">'${issueName}' (${issueKey})</a>`

  copyTextAndHtmlToClipboard(text, html)
}

function addButtonReadyForReview() {
    const buttonLabel = "Copy review message"
    const targetElement = document.getElementById('opsbar-opsbar-admin')
    const eventHandler = copyReadyForReviewMessage
    const buttonCssClasses = "aui-button toolbar-trigger issueaction-workflow-transition"

    addButton(buttonLabel, targetElement, eventHandler, buttonCssClasses)
}

function addButtonReadyForPrioritisation() {
    const buttonLabel = "Copy prioritisation message"
    const targetElement = document.getElementById('opsbar-opsbar-admin')
    const eventHandler = copyReadyForPrioritisationMessage
    const buttonCssClasses = "aui-button toolbar-trigger issueaction-workflow-transition"

    addButton(buttonLabel, targetElement, eventHandler, buttonCssClasses)
}

function addButton(text, parentElement, clickEventFunctionName, cssClass) {
  console.log("addButton called")
  addButtonBefore(text, parentElement, null, clickEventFunctionName, cssClass)
}

function addButtonBefore(text, parentElement, beforeElement, clickEventFunctionName, cssClass) {
  console.log("addButtonBefore called")
  const btn = document.createElement("a")
  const textNode = document.createTextNode(text)
  btn.appendChild(textNode)
  btn.setAttribute("href","#")
  btn.addEventListener("click", clickEventFunctionName)
  btn.setAttribute("class", cssClass)
  parentElement.insertBefore(btn, beforeElement)
}

function copyTextAndHtmlToClipboard(text, html) {
  console.log("copyTextAndHtmlToClipboard() called.")

  const blobText = new Blob([text], { type: "text/plain" })
  const blobHtml = new Blob([html], { type: "text/html" })
  const data = new ClipboardItem({
    "text/plain": blobText,
    "text/html": blobHtml
  })

  navigator.clipboard
    .write([data])
    .then(
      (response) => {
        console.log("Success writing to the clipboard.")
        console.log(new Array(text, html))
      },
      (response) => {
        console.log("Error writing to the clipboard: " + response.text)
      }
    )
}