NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript==
// @namespace https://openuserjs.org/users/yuanoook
// @name Gitlab MR Auto Checker
// @description Showing the current basic and recommended format for a User script.
// @copyright 2020, yuanoook (https://openuserjs.org/users/yuanoook)
// @license MIT
// @version 1.0.1
// @include https://git*
// @grant none
// ==/UserScript==
// ==OpenUserJS==
// @author yuanoook
// ==/OpenUserJS==
// ==Configuration==
// localStorage.defaultReviewerId
// localStorage.defaultReviewerName
// localStorage.jiraHost
// ==/Configuration==
const triggerEvent = (target, name) => {
if (typeof target[name] === 'function') {
target[name]();
return;
}
const oldFashion = 'createEvent' in document;
const event = oldFashion ? document.createEvent('Event') : new Event(name, {
bubbles: true,
cancelable: true,
});
if (oldFashion) {
event.initEvent(name, true, true);
}
target.dispatchEvent(event);
};
const isMRReady = () => Boolean(window.merge_request_title && window.merge_request_title.value);
const getSearchArray = () => {
return decodeURIComponent(location.search || '').split(/[\=\&\?]/);
};
const getSourceBranch = () => {
const searchArray = getSearchArray();
const sourceBranchKeyIndex = searchArray.indexOf('merge_request[source_branch]');
const sourceBranchValueIndex = sourceBranchKeyIndex + 1;
return searchArray[sourceBranchValueIndex];
};
const getIssueNumber = () => {
const sourceBranch = getSourceBranch();
const matchRes = (sourceBranch || '').match(/[A-Z]+-\d+/);
if (matchRes) {
return matchRes[0];
}
};
const updateDescription = () => {
const issueNumber = getIssueNumber();
if (!issueNumber) {
return;
}
const issueLink = `${localStorage.jiraHost}${issueNumber}`;
window.merge_request_description.value = issueLink + '\n\n' + merge_request_description.value;
};
const updateAssignee = () => {
const assigneeInput = document.querySelector('[name="merge_request[assignee_ids][]"');
assigneeInput.value = localStorage.defaultReviewerId;
const assigneeText = assigneeInput.parentElement.querySelector('.dropdown .dropdown-toggle-text');
assigneeText.innerText = localStorage.defaultReviewerName;
};
const checkMergeOptions = () => {
const removeSourceBranchCheckbox = document.querySelector('[for="merge_request_force_remove_source_branch"]');
const squashCheckbox = document.querySelector('[for="merge_request_squash"]');
triggerEvent(removeSourceBranchCheckbox, 'click');
triggerEvent(squashCheckbox, 'click');
};
const prepareForMR = () => {
updateDescription();
updateAssignee();
checkMergeOptions();
};
const 等待 = (时间 = 1) => new Promise(释放 => setTimeout(释放, 时间 * 1000));
const startPrepareForMR = () => {
if (!isMRReady()) {
setTimeout(startPrepareForMR, 300);
} else {
prepareForMR();
}
};
(async () => {
await 等待(1);
startPrepareForMR();
})();