GitLab一键合并按钮

在GitLab仪表盘界面添加一个合并所有请求的按钮

  1. // ==UserScript==
  2. // @name Merge All Button for GitLab
  3. // @name:zh-CN GitLab一键合并按钮
  4. // @namespace https://greasyfork.org/users/692574
  5. // @version 0.0.92
  6. // @description Add a button to GitLab dashboard page to merge all merge requests
  7. // @description:zh-CN 在GitLab仪表盘界面添加一个合并所有请求的按钮
  8. // @author Chase Choi
  9. // @license MIT
  10. // @match https://gitlab.com/dashboard/merge_requests*
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js
  12. // @grant GM.xmlHttpRequest
  13. // ==/UserScript==
  14.  
  15. const SUCCESS = 200;
  16. const BASE_URL = window.location.origin;
  17. const ACCESS_TOKEN = '<YOUR_ACCESS_TOKEN>';
  18.  
  19. (function() {
  20. 'use strict';
  21. // Update DOM
  22. const mergeAllElement = `
  23. <div class="gl-ml-3">
  24. <a class="btn gl-button btn-confirm merge-all-btn">Merge All</a>
  25. </div>
  26. `;
  27. $(`${mergeAllElement}`).insertAfter($('.filter-dropdown-container'));
  28. $('.merge-all-btn').css('background-color', '#3C824E');
  29.  
  30. // add event listener
  31. $('.merge-all-btn').click(function() {
  32. if (ACCESS_TOKEN == '<YOUR_ACCESS_TOKEN>') {
  33. alert("Empty access token!");
  34. return;
  35. }
  36. const text = "Are you sure to merge all?"
  37. if (confirm(text) == true) {
  38. console.log("Start merging all...");
  39. mergeAll();
  40. }
  41. });
  42. })();
  43.  
  44. /**
  45. * Iterate all merge requests on current page and perform merge action for each one.
  46. */
  47. function mergeAll() {
  48. // iterate merge request list
  49. let elements = $('li.merge-request .issuable-info .issuable-reference');
  50. if (elements.length) {
  51. elements.each(function () {
  52. const mergeRequestReference = $(this).text();
  53. if (mergeRequestReference.length) {
  54. const projectNamespace = mergeRequestReference.split('!')[0].trim().replaceAll('/', '%2F');
  55. const mergeRequestID = mergeRequestReference.split('!')[1];
  56. console.log(projectNamespace, mergeRequestID);
  57. mergeProject(projectNamespace, mergeRequestID);
  58. }
  59. });
  60. }
  61. }
  62.  
  63. /**
  64. * Get project ID and perform merge action.
  65. *
  66. * @param {string} projectNamespace The project name with namespace.
  67. * @param {number} mergeRequestID The merge request ID.
  68. */
  69. function mergeProject(projectNamespace, mergeRequestID) {
  70. GM.xmlHttpRequest({
  71. method: "GET",
  72. url: `${BASE_URL}/api/v4/projects/${projectNamespace}`,
  73. headers: {
  74. "Authorization": `Bearer ${ACCESS_TOKEN}`
  75. },
  76. onload: function (response) {
  77. if (response.status == SUCCESS) {
  78. const projectInfo = JSON.parse(response.responseText);
  79. console.log(`project ID: ${projectInfo.id}; merge request ID: ${mergeRequestID}`);
  80. performMergeAction(projectInfo.id, mergeRequestID);
  81. }
  82. }
  83. });
  84. }
  85.  
  86. /**
  87. * Perform actual merge action for specific project.
  88. *
  89. * @param {number} projectID The project ID.
  90. * @param {number} mergeRequestID The merge request ID.
  91. */
  92. function performMergeAction(projectID, mergeRequestID) {
  93. GM.xmlHttpRequest({
  94. method: "PUT",
  95. url: `${BASE_URL}/api/v4/projects/${projectID}/merge_requests/${mergeRequestID}/merge`,
  96. headers: {
  97. "Authorization": `Bearer ${ACCESS_TOKEN}`
  98. },
  99. onload: function (response) {
  100. if (response.status == SUCCESS) {
  101. console.log("Merged successfully!");
  102. }
  103. }
  104. });
  105. }