NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Gitlab merge request chat webhook button // @namespace http://tampermonkey.net/ // @version 0.2 // @author JombY // @license Apache-2.0 // @include /^https:\/\/gitlab.*\/merge_requests\/[0-9]+$/ // @icon https://www.google.com/s2/favicons?domain=gitlab.com // @grant none // ==/UserScript== /* jshint esversion: 11 */ const projectSpaceTokenJson = { "key" : "key", "PROJECTCODETOLOOKFOR": { "space": "webhook-space", "token": "webhook-token" }, "PROJECTCODETOLOOKFOR2": { "space": "webhook-space2", "token": "webhook-token2" } }; (async function () { const actionsContainer = document.querySelector('.detail-page-header-actions'); if (actionsContainer) { actionsContainer.insertBefore(createSendMessageButton(), actionsContainer.firstChild); } })(); function createSendMessageButton() { const button = document.createElement('button'); button.innerText = 'Send message to chat'; button.classList.add('gl-button', 'btn', 'btn-default', 'float-left'); button.addEventListener('click', sendMessage); return button; } function sendMessage() { const id = document.URL.match('merge_requests/(\\d+)')[1]; const user = document.querySelector('.author').textContent; const title = document.querySelector('.page-title').textContent.trim(); const status = document.querySelector('.issuable-status-badge').textContent.trim(); const target = document.querySelector('.detail-page-description').childNodes[9].textContent.trim(); const text = `*${user}* ${toVerb(status)} merge request <${document.URL}| !${id} *${title}*> into \`${target}\``; const projectId = title.trim().replace("Draft: ", "").split("-")[0]; const projectWebHookUrl = getProjectWebHookUrl(projectId); fetch(projectWebHookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=UTF-8' }, body: JSON.stringify({ "text": text }) }).then(console.log); } function toVerb(status) { if (status === "Open") return "opened"; if (status === "Merged") return "merged"; if (status === "Closed") return "closed"; } function getProjectWebHookUrl(projectId) { const key = projectSpaceTokenJson.key; const space = projectSpaceTokenJson[projectId]?.space; const token = projectSpaceTokenJson[projectId]?.token; if (space && token) { return `https://chat.googleapis.com/v1/spaces/${space}/messages?key=${key}&token=${token}`; } else { console.error(`Unable to find webhook details for project ${projectId}`); return ''; } }