Pull request

Add extra copy buttons into pull request pages.

Mint 2019.10.05.. Lásd a legutóbbi verzió

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name Pull request
// @namespace GitHub Scripts
// @description Add extra copy buttons into pull request pages.
// @icon 
// @run-at document-start
// @grant none
// @version 1.0
// ==/UserScript==

function appendFetchBranchName() {
    if (!document.getElementById('pr_branch_name') && !document.getElementById('pr_reset_fetch')) {
        var remoteBranchEl = document.getElementsByClassName('TableObject-item--primary')[0];
        var cpSpanEl = remoteBranchEl.lastElementChild.cloneNode(true);
        cpSpanEl.setAttribute('id', 'pr_branch_name');
        var cpResetSpanEl = remoteBranchEl.lastElementChild.cloneNode(true);
        cpSpanEl.setAttribute('id', 'pr_reset_fetch');
        var cpEl = cpSpanEl.firstElementChild;
        var remoteBranchName = cpEl.getAttribute('value');
        var branchName = remoteBranchName.split(':')[1];
        var pullNumber = document.getElementsByClassName('gh-header-number')[0].textContent.substr(1);
        var pullName = 'pull/' + pullNumber + '/head:' + branchName;
        cpEl.setAttribute('value', branchName);

        var branchNameEl = document.createElement('span');
        branchNameEl.innerText = branchName;
        branchNameEl.setAttribute('class', 'commit-ref');

        var remoteRefEl = document.getElementsByClassName('base-ref')[0];
        var remoteUrl = '[email protected]:' + remoteRefEl.firstElementChild.getAttribute('href').substr(1) + '.git ';
        var gitCommands = 'git stash && git checkout develop';
        gitCommands += ' && git fetch ' + remoteUrl + pullName;
        gitCommands += ' && git checkout ' + branchName;
        gitCommands += ' && git reset --hard HEAD~1';
        gitCommands += ' && git pull ' + remoteUrl + pullName;
        cpResetSpanEl.firstElementChild.setAttribute('value', gitCommands);

        var resetFetchEl = document.createElement('span');
        resetFetchEl.innerText = 'Fetch and pull PR\'s branch commands';
        resetFetchEl.setAttribute('class', 'commit-ref');

        remoteBranchEl.appendChild(document.createElement('br'));
        remoteBranchEl.appendChild(branchNameEl);
        remoteBranchEl.appendChild(cpSpanEl);
        remoteBranchEl.appendChild(document.createTextNode('\u00A0\u00A0\u00A0'));
        remoteBranchEl.appendChild(resetFetchEl);
        remoteBranchEl.appendChild(cpResetSpanEl);
    }
}

document.addEventListener('DOMContentLoaded', function () {
    appendFetchBranchName();
    var targetNode = document.getElementById('js-repo-pjax-container');
    var config = { attributes: true, childList: true, subtree: true };
    var callback = function (mutations) {
        var count = 0;
        for (var i = 0; i < mutations.length; i++) {
            var mutation = mutations[i];
            if (!count && mutation.type === 'attributes' && mutation.attributeName === 'tabindex') {
                count++;
                appendFetchBranchName();
            }
        }
    };
    var observer = new MutationObserver(callback);
    observer.observe(targetNode, config);
});