GitHub Actions Filter Button

Filter Kata Containers passed or non-required checks.

As of 2024-02-20. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub Actions Filter Button
  3. // @namespace http://www.nxw.name
  4. // @version 1.0.4
  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 hidden = false;
  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. hidden = !hidden;
  24. checks.forEach(function(check) {
  25. console.debug('check item', check);
  26. if (hidden) {
  27. var statusElement = check.querySelector('div:nth-child(3)');
  28. if (!statusElement) {
  29. console.debug(statusElement, 'check status not found');
  30. return;
  31. }
  32. var detailsElement = check.querySelector('div:nth-child(4)');
  33. if (!detailsElement) {
  34. console.debug(detailsElement, 'check details not found');
  35. return;
  36. }
  37.  
  38. var statusText = statusElement.textContent;
  39. var requiredText = detailsElement.textContent;
  40. // 'Successful in' for GHA, 'Build finished' for Jenkins
  41. var successful = statusText.includes('Successful in') || statusText.includes('Build finished');
  42. var required = requiredText.includes('Required');
  43.  
  44. if (successful || !required) {
  45. console.debug('check item is hidden: successful: ' + successful + ', required: ' + required)
  46. check.classList.add('hidden-check');
  47. }
  48. } else {
  49. check.classList.remove('hidden-check');
  50. }
  51. });
  52. }
  53.  
  54. function insertFilterButton() {
  55. var body = document.querySelector('body');
  56. var bodyFirstChild = document.querySelector('body div:nth-child(1)');
  57.  
  58. filterButton = document.createElement('button');
  59. filterButton.type = 'button';
  60. filterButton.textContent = 'Filter Checks';
  61. filterButton.classList.add('gha-filter-button');
  62. filterButton.classList.add('hidden-check');
  63. filterButton.addEventListener('click', onFilterButtonClicked);
  64.  
  65. body.insertBefore(filterButton, bodyFirstChild);
  66. }
  67.  
  68. function insertHiddenCheckCssStyle() {
  69. var styleElement = document.createElement('style');
  70. styleElement.type = 'text/css';
  71. 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;}');
  72. styleElement.appendChild(cssRule);
  73. document.head.appendChild(styleElement);
  74. }
  75.  
  76. function updateFilterButton(href) {
  77. const regex = /github\.com\/kata-containers\/kata-containers\/pull\/\d+$/;
  78. if (regex.test(href)) {
  79. console.log('show filter button', href);
  80. filterButton.classList.remove('hidden-check');
  81. } else {
  82. console.log('hide filter button', href);
  83. filterButton.classList.add('hidden-check');
  84. }
  85. }
  86.  
  87. function listenUrlChanged() {
  88. var _wr = function (type) {
  89. var orig = history[type];
  90. return function () {
  91. var rv = orig.apply(this, arguments);
  92. var e = new Event(type);
  93. e.arguments = arguments;
  94. window.dispatchEvent(e);
  95. return rv;
  96. };
  97. };
  98. history.pushState = _wr('pushState');
  99. window.addEventListener('pushState', function (e) {
  100. console.debug('Url changed', window.location.href);
  101. updateFilterButton(window.location.href);
  102. });
  103. }
  104.  
  105. (function() {
  106. 'use strict';
  107.  
  108. insertHiddenCheckCssStyle();
  109. insertFilterButton();
  110. updateFilterButton(window.location.href);
  111. listenUrlChanged();
  112. })();