Dependabot commands - github.com

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

As of 2022-01-11. See the latest version.

  1. // ==UserScript==
  2. // @name Dependabot commands - github.com
  3. // @icon https://avatars.githubusercontent.com/u/27347476?s=200&v=4
  4. // @namespace Violentmonkey Scripts
  5. // @match https://github.com/*
  6. // @grant none
  7. // @version 1.3
  8. // @author Dante Catalfamo <dante@lambda.cx>
  9. // @license BSD-3-Clause; https://opensource.org/licenses/BSD-3-Clause
  10. // @description Adds shortcut buttons to run dependabot commands in PRs where dependabot is the author
  11. // ==/UserScript==
  12.  
  13. const commands = ['rebase', 'recreate', 'merge', 'close'];
  14.  
  15. function submitComment(text) {
  16. const inputCommentElement = document.getElementById('new_comment_field');
  17. const submitCommentElement = document.querySelector('#partial-new-comment-form-actions button.btn-primary');
  18.  
  19. inputCommentElement.value = text;
  20. submitCommentElement.disabled = false;
  21. submitCommentElement.click();
  22. }
  23.  
  24. function dependabotCommand(command) {
  25. submitComment(`@dependabot ${command}`);
  26. }
  27.  
  28. function createDependabotButton(command) {
  29. const div = document.createElement('div');
  30. div.classList = 'color-bg-secondary';
  31.  
  32. const btn = document.createElement('button');
  33. btn.classList = 'btn';
  34. btn.type = 'button';
  35. btn.innerText = `@db ${command}`;
  36. btn.addEventListener('click', function() {
  37. dependabotCommand(command);
  38. });
  39.  
  40. div.appendChild(btn);
  41. return div;
  42. }
  43.  
  44. function addDependabotButton(command) {
  45. const buttonContainerElement = document.querySelector('#partial-new-comment-form-actions .d-flex');
  46. const div = createDependabotButton(command);
  47.  
  48. buttonContainerElement.lastElementChild.before(div);
  49. }
  50.  
  51. function addDependabotButtons() {
  52. commands.forEach(function(command) {
  53. addDependabotButton(command);
  54. });
  55. }
  56.  
  57. function isDependabot() {
  58. return document.querySelector('.TimelineItem .author').innerText === 'dependabot';
  59. }
  60.  
  61. function maybeAddDependabotButtons() {
  62. if (isDependabot()) {
  63. addDependabotButtons();
  64. }
  65. }
  66.  
  67. document.addEventListener('pjax:complete', maybeAddDependabotButtons);
  68. maybeAddDependabotButtons();