GitHub Actions Filter Button

Filter Kata Containers passed or non-required checks.

Від 23.02.2024. Дивіться остання версія.

  1. // ==UserScript==
  2. // @name GitHub Actions Filter Button
  3. // @namespace http://www.nxw.name
  4. // @version 1.0.9
  5. // @description Filter Kata Containers passed or non-required checks.
  6. // @author Xuewei Niu
  7. // @match *://github.com/kata-containers/kata-containers*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @grant none
  10. // @license Apache-2.0
  11. // ==/UserScript==
  12.  
  13. var filteredItems = 0;
  14. var checkCount = 0;
  15. var filterButton;
  16.  
  17. function onFilterButtonClicked() {
  18. var checks = document.querySelectorAll('div.merge-status-list.hide-closed-list.js-updatable-content-preserve-scroll-position > div');
  19. if (checks.length == 0) {
  20. console.log('stop hiding checks: they aren\'t ready')
  21. return;
  22. }
  23.  
  24. if (checkCount === 0) {
  25. checkCount = checks.length;
  26. }
  27.  
  28. var hidden = (filteredItems === 0);
  29. if (!hidden) {
  30. // GitHub updates periodly to restore the hidden checks.
  31. // We need to update `hidden` to fit the above issue.
  32. if (checkCount === checks.length) {
  33. console.log('checks were updated, hide checks again');
  34. hidden = true;
  35. }
  36. filteredItems = 0
  37. }
  38. checks.forEach(function(check) {
  39. if (hidden) {
  40. // There is no github icon in the case of non-gha,
  41. // so the indexes of status element and details element
  42. // are required to calculate dynamically.
  43. var elemCount = check.childElementCount;
  44. var statusElement = check.querySelector('div:nth-child('+ (elemCount-1) +')');
  45. if (!statusElement) {
  46. console.error(check, 'check status not found');
  47. return;
  48. }
  49. var detailsElement = check.querySelector('div:nth-child('+ elemCount +')');
  50. if (!detailsElement) {
  51. console.error(check, 'check details not found');
  52. return;
  53. }
  54.  
  55. var statusText = statusElement.textContent;
  56. var requiredText = detailsElement.textContent;
  57. // 'Successful in' for GHA, 'Build finished' for Jenkins
  58. var successful = statusText.includes('Successful in') || statusText.includes('Build finished');
  59. var required = requiredText.includes('Required');
  60.  
  61. if (successful || !required) {
  62. console.debug('check item is hidden: successful: ' + successful + ', required: ' + required, check);
  63. check.classList.add('hidden-check');
  64. } else {
  65. console.info('check item isn\'t hidden: successful: ' + successful + ', required: ' + required, check);
  66. }
  67. filteredItems += 1;
  68. } else {
  69. check.classList.remove('hidden-check');
  70. }
  71. });
  72. }
  73.  
  74. function insertFilterButton() {
  75. var body = document.querySelector('body');
  76. var bodyFirstChild = document.querySelector('body div:nth-child(1)');
  77.  
  78. filterButton = document.createElement('button');
  79. filterButton.type = 'button';
  80. filterButton.textContent = 'Filter Checks';
  81. filterButton.classList.add('gha-filter-button');
  82. filterButton.classList.add('hidden-check');
  83. filterButton.addEventListener('click', onFilterButtonClicked);
  84.  
  85. body.insertBefore(filterButton, bodyFirstChild);
  86. }
  87.  
  88. function insertHiddenCheckCssStyle() {
  89. var styleElement = document.createElement('style');
  90. styleElement.type = 'text/css';
  91. var cssRule = document.createTextNode('.hidden-check { display: none !important }\n.gha-filter-button { position: fixed; bottom: 100px; right: 20px; display: block; z-index: 10000; background-color: #218bff; color: #fff; padding: 5px 16px; border: none; border-radius: 6px;}');
  92. styleElement.appendChild(cssRule);
  93. document.head.appendChild(styleElement);
  94. }
  95.  
  96. function updateFilterButton(href) {
  97. const regex = /github\.com\/kata-containers\/kata-containers\/pull\/\d+(#pullrequestreview-\d+)?(#discussion_r\d+)?(#issuecomment-\d+)?$/;
  98. if (regex.test(href)) {
  99. console.debug('show filter button', href);
  100. filterButton.classList.remove('hidden-check');
  101. } else {
  102. console.debug('hide filter button', href);
  103. filterButton.classList.add('hidden-check');
  104. }
  105. }
  106.  
  107. function listenUrlChanged(callback) {
  108. var _wr = function (type) {
  109. var orig = history[type];
  110. return function () {
  111. var rv = orig.apply(this, arguments);
  112. var e = new Event(type);
  113. e.arguments = arguments;
  114. window.dispatchEvent(e);
  115. return rv;
  116. };
  117. };
  118. history.pushState = _wr('pushState');
  119.  
  120. window.addEventListener('pushState', function (e) {
  121. console.debug('pushState: url changed', window.location.href);
  122. callback();
  123. });
  124. window.addEventListener('popstate', function (e) {
  125. console.debug('popstate: url changed', window.location.href);
  126. callback();
  127. });
  128. window.addEventListener('hashchange', function (e) {
  129. console.debug('hashchange: url changed', window.location.href);
  130. callback();
  131. });
  132. }
  133.  
  134. (function() {
  135. 'use strict';
  136.  
  137. insertHiddenCheckCssStyle();
  138. insertFilterButton();
  139.  
  140. updateFilterButton(window.location.href);
  141. listenUrlChanged(function() {
  142. updateFilterButton(window.location.href);
  143. filteredItems = 0;
  144. });
  145. })();