GitHub Actions Filter Button

Filter Kata Containers passed or non-required checks.

Fra 20.02.2024. Se den seneste versjonen.

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