Skip to content

Instantly share code, notes, and snippets.

@pencil
Last active December 6, 2021 22:35
Show Gist options
  • Save pencil/51460c59d4c21e10e2d33f1a7208db04 to your computer and use it in GitHub Desktop.
Save pencil/51460c59d4c21e10e2d33f1a7208db04 to your computer and use it in GitHub Desktop.
GitHub: Set merge message to PR title and description
// ==UserScript==
// @name GitHub: Set merge message to PR title and description
// @namespace https://github.com/pencil
// @version 0.6
// @description Set the default GitHub merge title and description to the one of the PR itself.
// @author Nils Caspar
// @match https://github.com/*
// @icon https://github.githubassets.com/favicons/favicon.png
// @grant none
// @downloadURL https://gist.githubusercontent.com/pencil/51460c59d4c21e10e2d33f1a7208db04/raw/script.js
// @updateURL https://gist.githubusercontent.com/pencil/51460c59d4c21e10e2d33f1a7208db04/raw/script.js
// @homepageURL https://greasyfork.org/en/scripts/426735-github-set-merge-message-to-pr-title-and-description
// @supportURL https://greasyfork.org/en/scripts/426735-github-set-merge-message-to-pr-title-and-description/feedback
// ==/UserScript==
(function() {
'use strict';
const $ = (str, el) => (el || document).querySelector(str),
$$ = (str, el) => [...(el || document).querySelectorAll(str)];
const githubApp = $('.application-main');
githubApp.addEventListener('click', (event) => {
const target = event.target;
if(target.getAttribute('data-details-container') != '.js-merge-pr') {
return;
}
console.info('updating merge message and title...');
const mergeTitleField = $('#merge_title_field'),
mergeDescriptionField = $('#merge_message_field'),
prTitleContainer = $('.gh-header-title .js-issue-title'),
prNumberContainer = $('.gh-header-title .color-fg-muted'),
prDescriptionContainer = $('form.js-comment-update textarea');
if(!prNumberContainer || !prNumberContainer.innerText) {
console.info('could not find PR number');
return;
}
if(!prTitleContainer || !prTitleContainer.innerText) {
console.info('could not find PR title');
return;
}
if(!mergeTitleField) {
console.info('could not find merge title');
return;
}
mergeTitleField.value = prTitleContainer.innerText.trim() + ' (' + prNumberContainer.innerText.trim() + ')';
if(!prDescriptionContainer || !prDescriptionContainer.innerText) {
console.info('could not find PR description');
return;
}
if(!mergeDescriptionField) {
console.info('could not find merge description');
return;
}
mergeDescriptionField.value = prDescriptionContainer.value;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment