GitHub - Pull Request - Add Atlantis buttons

Add Atlantis buttons to the footer of your Pull Request

当前为 2024-10-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub - Pull Request - Add Atlantis buttons
  3. // @namespace http://hear.com
  4. // @version 0.1
  5. // @description Add Atlantis buttons to the footer of your Pull Request
  6. // @author You
  7. // @match https://github.com/Audibene-GMBH/*/pull/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @tag productivity
  10. // @grant none
  11. // ==/UserScript==
  12. (() => {
  13. 'use strict';
  14.  
  15. let interval
  16.  
  17. const addElements = () => {
  18. var ctas = document.querySelector('#partial-new-comment-form-actions')
  19.  
  20. document.body.querySelectorAll('div[x-data-n8="true"]')?.forEach((el) => ctas.removeChild(el))
  21.  
  22. var container = document.createElement('div')
  23.  
  24. container.setAttribute('x-data-n8', true)
  25. container.classList.add(...'d-flex'.split(' '))
  26. ctas.prepend(container)
  27.  
  28. const toggle = createToggle()
  29.  
  30. toggle.addEventListener('change', (e) => {
  31. const actionText = toggle.checked ? 'apply' : 'plan'
  32.  
  33. if (!document.querySelector('#new_comment_field').value) {
  34. return
  35. }
  36.  
  37. document.querySelector('#new_comment_field').value = document.querySelector('#new_comment_field').value.replace(/(apply|plan)/gmi, actionText)
  38.  
  39. setTimeout(() => Array.from(document.querySelectorAll('.btn-primary.btn')).find(btn => btn.innerText === 'Comment')?.removeAttribute('disabled'), 50)
  40. })
  41.  
  42. var addButton = (name, env, rightPad = 0) => {
  43. var newButton = document.createElement('button')
  44.  
  45. newButton.setAttribute('x-data-n8', true)
  46. newButton.setAttribute('x-data-n8-name', name)
  47. newButton.classList.add(...'btn js-quick-submit-alternative js-comment-and-button'.split(' '))
  48. newButton.style.marginRight = rightPad ? `${rightPad}px` : undefined
  49. newButton.type = 'button'
  50. container.parentNode.parentNode.disabled = true
  51. container.append(newButton)
  52. setTimeout(() => { newButton.innerText = `${name}` }, 0)
  53. newButton.addEventListener('click', (e) => {
  54. e.preventDefault()
  55. newButton.setAttribute('disabled', true)
  56. document.querySelector('#new_comment_field').value = `atlantis ${toggle.checked ? 'apply' : 'plan'} -p ${env}`
  57. setTimeout(() => { newButton.removeAttribute('disabled') }, 1_000)
  58. Array.from(document.querySelectorAll('.btn-primary.btn')).find(btn => btn.innerText === 'Comment')?.removeAttribute('disabled')
  59. })
  60. }
  61.  
  62. document.querySelector('#new_comment_field').addEventListener('keyup', () => {
  63. [...container.children].forEach((el) => {
  64. el.innerText = el.getAttribute('x-data-n8-name')
  65. })
  66. })
  67.  
  68. addButton('TST', 'infrastructure-eu-central-1-testing', 5)
  69. addButton('STG', 'infrastructure-eu-central-1-staging', 5)
  70. addButton('PRD [NA]', 'infrastructure-us-east-1-production', 5)
  71. addButton('PRD [EU]', 'infrastructure-eu-central-1-production', 5)
  72.  
  73. container.append(toggle)
  74.  
  75. clearInterval(interval);
  76. }
  77.  
  78. interval = setInterval(addElements, 500)
  79. })()
  80.  
  81. function createToggle(handler) {
  82. const toggle = document.createElement('input')
  83. const toggleClassName = `toggle-n8`
  84. const styleId = 'toggle-n8'
  85.  
  86. if (!document.querySelector(`style#${styleId}`)) {
  87. const style = document.createElement('style')
  88.  
  89. style.id = styleId
  90.  
  91. style.textContent = `
  92. .${toggleClassName} {
  93. appearance: none;
  94. background-color: #707070;
  95. border-radius: 1.5em;
  96. border: none;
  97. box-sizing: content-box;
  98. cursor: pointer;
  99. display: inline-block;
  100. height: 2em;
  101. overflow: hidden;
  102. padding: 0.2em;
  103. position: relative;
  104. transition: background ease 0.3s;
  105. width: 6em;
  106.  
  107. &:before {
  108. content: "apply plan";
  109. display: block;
  110. position: absolute;
  111. z-index: 2;
  112. width: 2em;
  113. height: 2em;
  114. font-family: system-ui;
  115. font-size: 1em;
  116. line-height: 2em;
  117. font-weight: 500;
  118. text-transform: uppercase;
  119. text-indent: -3.5em;
  120. word-spacing: 2.55em;
  121. text-shadow: -1px -1px rgba(0,0,0,0.15);
  122. white-space: nowrap;
  123. background: #fff;
  124. color: #fff;
  125. border-radius: 1.5em;
  126. transition: transform cubic-bezier(0.3, 1.5, 0.7, 1) 0.3s;
  127. }
  128.  
  129. &:checked {
  130. background-color: red;
  131. }
  132.  
  133. &:checked:before {
  134. transform: translateX(4em);
  135. }
  136. }
  137. `
  138.  
  139. document.body.append(style)
  140. }
  141.  
  142. toggle.type = 'checkbox'
  143. toggle.classList.add(toggleClassName)
  144.  
  145. return toggle
  146. }