GitLab一键合并按钮

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

当前为 2022-05-30 提交的版本,查看 最新版本

  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.90
  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. const text = "Are you sure to merge all?"
  33. if (confirm(text) == true) {
  34. console.log("Start merging all...");
  35. mergeAll();
  36. }
  37. });
  38. })();
  39.  
  40. /**
  41. * Iterate all merge requests on current page and perform merge action for each one.
  42. */
  43. function mergeAll() {
  44. // iterate merge request list
  45. let elements = $('li.merge-request .issuable-info .issuable-reference');
  46. if (elements.length) {
  47. elements.each(function () {
  48. const mergeRequestReference = $(this).text();
  49. if (mergeRequestReference.length) {
  50. const projectNamespace = mergeRequestReference.split('!')[0].trim().replace('/', '%2F');
  51. const mergeRequestID = mergeRequestReference.split('!')[1];
  52. console.log(projectNamespace, mergeRequestID);
  53. mergeProject(projectNamespace, mergeRequestID);
  54. }
  55. });
  56. }
  57. }
  58.  
  59. /**
  60. * Get project ID and perform merge action.
  61. *
  62. * @param {string} projectNamespace The project name with namespace.
  63. * @param {number} mergeRequestID The merge request ID.
  64. */
  65. function mergeProject(projectNamespace, mergeRequestID) {
  66. GM.xmlHttpRequest({
  67. method: "GET",
  68. url: `${BASE_URL}/api/v4/projects/${projectNamespace}`,
  69. headers: {
  70. "Authorization": `Bearer ${ACCESS_TOKEN}`
  71. },
  72. onload: function (response) {
  73. if (response.status == SUCCESS) {
  74. const projectInfo = JSON.parse(response.responseText);
  75. console.log(`project ID: ${projectInfo.id}; merge request ID: ${mergeRequestID}`);
  76. performMergeAction(projectInfo.id, mergeRequestID);
  77. }
  78. }
  79. });
  80. }
  81.  
  82. /**
  83. * Perform actual merge action for specific project.
  84. *
  85. * @param {number} projectID The project ID.
  86. * @param {number} mergeRequestID The merge request ID.
  87. */
  88. function performMergeAction(projectID, mergeRequestID) {
  89. GM.xmlHttpRequest({
  90. method: "PUT",
  91. url: `${BASE_URL}/api/v4/projects/${projectID}/merge_requests/${mergeRequestID}/merge`,
  92. headers: {
  93. "Authorization": `Bearer ${ACCESS_TOKEN}`
  94. },
  95. onload: function (response) {
  96. if (response.status == SUCCESS) {
  97. console.log("Merged successfully!");
  98. }
  99. }
  100. });
  101. }