Dependabot commands - github.com

Adds shortcut buttons to run dependabot commands in PRs where dependabot is the author

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Dependabot commands - github.com
// @icon        https://avatars.githubusercontent.com/u/27347476?s=200&v=4
// @namespace   Violentmonkey Scripts
// @match       https://github.com/*
// @grant       none
// @version     1.3
// @author      Dante Catalfamo <[email protected]>
// @license     BSD-3-Clause; https://opensource.org/licenses/BSD-3-Clause
// @description Adds shortcut buttons to run dependabot commands in PRs where dependabot is the author
// ==/UserScript==

const commands = ['rebase', 'recreate', 'merge', 'close'];

function submitComment(text) {
  const inputCommentElement = document.getElementById('new_comment_field');
  const submitCommentElement = document.querySelector('#partial-new-comment-form-actions button.btn-primary');

  inputCommentElement.value = text;
  submitCommentElement.disabled = false;
  submitCommentElement.click();
}

function dependabotCommand(command) {
  submitComment(`@dependabot ${command}`);
}

function createDependabotButton(command) {
  const div = document.createElement('div');
  div.classList = 'color-bg-secondary';

  const btn = document.createElement('button');
  btn.classList = 'btn';
  btn.type = 'button';
  btn.innerText = `@db ${command}`;
  btn.addEventListener('click', function() {
    dependabotCommand(command);
  });

  div.appendChild(btn);
  return div;
}

function addDependabotButton(command) {
  const buttonContainerElement = document.querySelector('#partial-new-comment-form-actions .d-flex');
  const div = createDependabotButton(command);

  buttonContainerElement.lastElementChild.before(div);
}

function addDependabotButtons() {
  commands.forEach(function(command) {
    addDependabotButton(command);
  });
}

function isDependabot() {
  return document.querySelector('.TimelineItem .author').innerText === 'dependabot';
}

function maybeAddDependabotButtons() {
  if (isDependabot()) {
    addDependabotButtons();
  }
}

document.addEventListener('pjax:complete', maybeAddDependabotButtons);
maybeAddDependabotButtons();