gitlab项目 提交合并请求时,为了防止出现错误,写个脚本,提示自己,源分支-目标分支自动选取和识别。
需要在@match里配置
https://xxx.com/username_xxx/project_xxx/merge_requests/new
// ==UserScript==
// @name GIT 合并请求时,自动识别分支
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://xxx.com/username_xxx/project_xxx/merge_requests/new
// @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
// @license GPL License
// @grant GM_download
// @grant GM_openInTab
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant unsafeWindow
// @grant GM_setClipboard
// @grant GM_getResourceURL
// @grant GM_getResourceText
// @run-at document-end // 当DOMContentLoaded事件被触发时或者之后注入
// ==/UserScript==
(function() {
'use strict';
if(!/merge_requests\/new/.test(location.href)){return}
let lastSelectBranch = localStorage.getItem('lastSelectBranch') || 'feature/2.5.0';
let selectGroup = $('.js-merge-request-new-compare .col-lg-6 .merge-request-select:last-child');
// 自动选择对应分支
selectGroup.eq(0).find('.dropdown-menu-toggle').click();
let firstTimer = setInterval(()=>{
let liList = selectGroup.eq(0).find('.dropdown-content ul li a');
if(liList.length>0){
liList.eq(0).click()
clearInterval(firstTimer);
liList.each((index,item)=>{
if($(item).attr('data-ref') === lastSelectBranch){
item.click();
}
})
selectGroup.eq(1).find('.dropdown-menu-toggle').click();
let secTimer = setInterval(()=>{
let liList = selectGroup.eq(1).find('.dropdown-content ul li a');
if(liList.length>0){
liList.eq(0).click()
clearInterval(secTimer);
liList.each((index,item)=>{
if($(item).attr('data-ref') === lastSelectBranch){
item.click();
}
})
}
},20)
}
},20)
// 监听下拉单,如果左右分支不一致,进行强提醒
let left = '';
let right = '';
selectGroup.eq(0).find('.dropdown-content').click(function(e){
if(e.target){
left= e.target.dataset.ref;
diffLeftRight()
}
})
selectGroup.eq(1).find('.dropdown-content').click(function(e){
if(e.target){
right= e.target.dataset.ref;
diffLeftRight()
}
})
function diffLeftRight(){
if(left!==right){
if($('#btn-warning-txt').length===0){
$('.mr-compare-btn').after('<input type="button" value="左右分支不一样,注意检查" id="btn-warning-txt" class="btn btn-danger mr-compare-btn" style="margin-left:1rem;transition-duration:.2s;transform:scale(1);transition-property: all;">');
}else{
$('#btn-warning-txt').css({
transform:'scale(1.1)'
});
setTimeout(function(){
$('#btn-warning-txt').css({
transform:'scale(1)'
})
},400)
}
}else{
$('#btn-warning-txt').remove();
localStorage.setItem('lastSelectBranch',right)
}
}
// Your code here...
})();