GitHub Actions Filter Button

Filter Kata Containers passed or non-required checks.

Od 22.02.2024.. Pogledajte najnovija verzija.

  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 filterButton;
  15.  
  16. function onFilterButtonClicked() {
  17. var checks = document.querySelectorAll('div.merge-status-list.hide-closed-list.js-updatable-content-preserve-scroll-position > div');
  18. if (checks.length == 0) {
  19. console.log('stop hiding checks: they aren\'t ready')
  20. return;
  21. }
  22.  
  23. var hidden = (filteredItems === 0);
  24. // todo!
  25. if (!hidden) {
  26. filteredItems = 0
  27. }
  28. checks.forEach(function(check) {
  29. console.debug('check item', check);
  30. if (hidden) {
  31. var statusElement = check.querySelector('div:nth-child(3)');
  32. if (!statusElement) {
  33. console.debug(statusElement, 'check status not found');
  34. return;
  35. }
  36. var detailsElement = check.querySelector('div:nth-child(4)');
  37. if (!detailsElement) {
  38. console.debug(detailsElement, 'check details not found');
  39. return;
  40. }
  41.  
  42. var statusText = statusElement.textContent;
  43. var requiredText = detailsElement.textContent;
  44. // 'Successful in' for GHA, 'Build finished' for Jenkins
  45. var successful = statusText.includes('Successful in') || statusText.includes('Build finished');
  46. var required = requiredText.includes('Required');
  47.  
  48. if (successful || !required) {
  49. console.debug('check item is hidden: successful: ' + successful + ', required: ' + required)
  50. check.classList.add('hidden-check');
  51. }
  52. filteredItems += 1;
  53. } else {
  54. check.classList.remove('hidden-check');
  55. }
  56. });
  57. }
  58.  
  59. function insertFilterButton() {
  60. var body = document.querySelector('body');
  61. var bodyFirstChild = document.querySelector('body div:nth-child(1)');
  62.  
  63. filterButton = document.createElement('button');
  64. filterButton.type = 'button';
  65. filterButton.textContent = 'Filter Checks';
  66. filterButton.classList.add('gha-filter-button');
  67. filterButton.classList.add('hidden-check');
  68. filterButton.addEventListener('click', onFilterButtonClicked);
  69.  
  70. body.insertBefore(filterButton, bodyFirstChild);
  71. }
  72.  
  73. function insertHiddenCheckCssStyle() {
  74. var styleElement = document.createElement('style');
  75. styleElement.type = 'text/css';
  76. 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;}');
  77. styleElement.appendChild(cssRule);
  78. document.head.appendChild(styleElement);
  79. }
  80.  
  81. function updateFilterButton(href) {
  82. const regex = /github\.com\/kata-containers\/kata-containers\/pull\/\d+(#pullrequestreview-\d+)?(#discussion_r\d+)?(#issuecomment-\d+)?$/;
  83. if (regex.test(href)) {
  84. console.debug('show filter button', href);
  85. filterButton.classList.remove('hidden-check');
  86. } else {
  87. console.debug('hide filter button', href);
  88. filterButton.classList.add('hidden-check');
  89. }
  90. }
  91.  
  92. function listenUrlChanged(callback) {
  93. var _wr = function (type) {
  94. var orig = history[type];
  95. return function () {
  96. var rv = orig.apply(this, arguments);
  97. var e = new Event(type);
  98. e.arguments = arguments;
  99. window.dispatchEvent(e);
  100. return rv;
  101. };
  102. };
  103. history.pushState = _wr('pushState');
  104.  
  105. window.addEventListener('pushState', function (e) {
  106. console.debug('pushState: url changed', window.location.href);
  107. callback();
  108. });
  109. window.addEventListener('popstate', function (e) {
  110. console.debug('popstate: url changed', window.location.href);
  111. callback();
  112. });
  113. window.addEventListener('hashchange', function (e) {
  114. console.debug('hashchange: url changed', window.location.href);
  115. callback();
  116. });
  117. }
  118.  
  119. (function() {
  120. 'use strict';
  121.  
  122. insertHiddenCheckCssStyle();
  123. insertFilterButton();
  124.  
  125. updateFilterButton(window.location.href);
  126. listenUrlChanged(function() {
  127. updateFilterButton(window.location.href);
  128. filteredItems = 0;
  129. });
  130. })();