GitHub Actions Filter Button

Filter GitHub Actions which is not passed and quired

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

  1. // ==UserScript==
  2. // @name GitHub Actions Filter Button
  3. // @namespace http://www.nxw.name
  4. // @version 2024-02-08
  5. // @description Filter GitHub Actions which is not passed and quired
  6. // @author Xuewei Niu
  7. // @match https://github.com/kata-containers/kata-containers/pull/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @grant none
  10. // @license Apache-2.0
  11. // ==/UserScript==
  12. var hidden = false;
  13. var loaded = false;
  14. function filterButtonOnClick() {
  15. hidden = !hidden;
  16. var checks = document.querySelectorAll('#partial-pull-merging div.merge-status-list.hide-closed-list.js-updatable-content-preserve-scroll-position > div');
  17. checks.forEach(function(check) {
  18. if (hidden) {
  19. var statusElement = check.querySelector('div:nth-child(3)');
  20. if (!statusElement) {
  21. // console.log(check, 'check status not found');
  22. return;
  23. }
  24. var detailsElement = check.querySelector('div:nth-child(4)');
  25. if (!detailsElement) {
  26. return;
  27. }
  28. var successful = statusElement.textContent.includes('Successful in') || statusElement.textContent.includes('Build finished');
  29. var required = detailsElement.textContent.includes('Required');
  30. if (successful || !required) {
  31. check.classList.add('hidden-check');
  32. }
  33. } else {
  34. check.classList.remove('hidden-check');
  35. }
  36. });
  37. }
  38. function insertFilter() {
  39. var hideAllChecks = document.querySelector('#partial-pull-merging div.branch-action-item.js-details-container.Details.open button');
  40. if (!hideAllChecks) {
  41. console.log('Failed to find filter button container');
  42. return;
  43. }
  44. loaded = true;
  45. var filterButton = document.createElement('button');
  46. filterButton.type = 'button';
  47. filterButton.textContent = 'Filter Not Passed and Required Checks';
  48. filterButton.addEventListener('click', filterButtonOnClick);
  49. hideAllChecks.parentNode.insertBefore(filterButton, filterButton.nextSibling);
  50. }
  51. function insertHiddenCheckCssStyle() {
  52. var styleElement = document.createElement('style');
  53. styleElement.type = 'text/css';
  54. var cssRule = document.createTextNode('.hidden-check { display: none !important }');
  55. styleElement.appendChild(cssRule);
  56. document.head.appendChild(styleElement);
  57. }
  58. function loopWithDelay() {
  59. var count = 0;
  60. function iterate() {
  61. if (loaded) {
  62. return;
  63. }
  64. // retry 60 times (1 min)
  65. if (count == 60) {
  66. return;
  67. }
  68. console.log("Iteration: " + count);
  69. count++;
  70. insertFilter();
  71. setTimeout(iterate, 1000);
  72. }
  73. iterate();
  74. }
  75. (function() {
  76. 'use strict';
  77. var stateElem = document.querySelector('#partial-discussion-header .State');
  78. if (stateElem) {
  79. var title = stateElem.getAttribute('title');
  80. if (title !== 'Status: Open' && title !== 'Status: Draft') {
  81. console.log('GitHub Actions Filter Button: Exit due to the status of PR is not "open".');
  82. return;
  83. }
  84. } else {
  85. console.log('GitHub Actions Filter Button: Can\'t determine PR\'s status.');
  86. return;
  87. }
  88. insertHiddenCheckCssStyle();
  89. loopWithDelay();
  90. })();