Last active
September 16, 2023 03:48
-
-
Save turadg/a6871cafba99d6b3fc281f407cd64e55 to your computer and use it in GitHub Desktop.
Focus the Checks view on the selected result type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub Select PR checks | |
// @version 0.1.0 | |
// @description A userscript that selects PR checks | |
// @license MIT | |
// @author Turadg Aleahmad | |
// @namespace https://github.com/Mottie | |
// @include https://github.com/* | |
// @run-at document-idle | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM_addStyle | |
// @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=878498 | |
// @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=877686 | |
// @icon https://github.githubassets.com/pinned-octocat.svg | |
// ==/UserScript== | |
/* global $ $$ on off debounce */ | |
(() => { | |
"use strict"; | |
GM_addStyle(` | |
.status-meta .is-selected { font-weight: bold; } | |
`); | |
const defaultTypes = { | |
cancelled: { | |
icon: "octicon-stop", | |
isHidden: false | |
}, | |
errored: { | |
icon: "octicon-x", | |
text: "error", | |
isHidden: false | |
}, | |
expected: { | |
icon: "octicon-dot-fill", | |
text: "expected", | |
isHidden: false, | |
}, | |
failing: { | |
icon: "octicon-x", | |
text: "failing", | |
isHidden: false | |
}, | |
skipped: { | |
icon: "octicon-skip", | |
text: "skipped", | |
isHidden: false | |
}, | |
// Found in https://github.com/ruby/ruby/pull/3845 | |
neutral: { | |
icon: "octicon-square-fill", | |
isHidden: false | |
}, | |
pending: { | |
icon: "octicon-dot-fill", | |
text: "pending", | |
isHidden: false, | |
}, | |
progress: { | |
icon: "octicon-dot-fill", | |
text: "in progress", | |
isHidden: false | |
}, | |
queued: { | |
icon: "octicon-dot-fill", | |
text: "queued", | |
isHidden: false | |
}, | |
successful: { | |
icon: "octicon-check", | |
isHidden: false | |
} | |
}; | |
const types = Object.assign({}, defaultTypes, GM_getValue("ghfc", {})); | |
const metaRegexp = /(?:,|\s*and)\s*/; | |
const typeKeys = Object.keys(types); | |
let selectedType = undefined; | |
const setVisibility = block => { | |
$$(".merge-status-item", block).forEach(row => { | |
const svgClass = $(".merge-status-icon .octicon", row)?.getAttribute("class") || "---"; | |
const rowType = typeKeys.find(key => { | |
const type = types[key]; | |
const matchingIcon = svgClass.includes(types[key].icon); | |
// only a few icons are unique | |
if (matchingIcon && type.text) { | |
return (row.textContent || "").toLowerCase().includes(type.text); | |
} | |
return matchingIcon; | |
}); | |
const isHidden = selectedType ? rowType !== selectedType : false; | |
if (rowType && row.classList.contains("d-none") !== isHidden) { | |
row.classList.toggle("d-none", isHidden); | |
} | |
}); | |
}; | |
const toggleVisibility = event => { | |
const target = event.target; | |
if (target.classList.contains("ghsc")) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
const type = target.dataset.type; | |
if (selectedType === type) { | |
selectedType = undefined; | |
} else { | |
selectedType = type; | |
} | |
const isSelected = selectedType && selectedType === type; | |
const previousSelection = $('.ghsc.is-selected'); | |
if (previousSelection ) { | |
previousSelection.classList.remove('is-selected'); | |
} | |
if (previousSelection != target) { | |
target.className = `ghsc ${isSelected ? "is-selected" : ""}`; | |
} | |
GM_setValue("ghsc", types); | |
setVisibility(); | |
} | |
}; | |
const createLinks = block => { | |
const meta = $(".status-meta", block); | |
if (!$("a", meta)) { | |
const links = meta.textContent.trim().split(metaRegexp).filter(t => t); | |
// Don't add clickable links if only 1 type is visible | |
if (links.length <= 1) return; | |
const last = links.length - 2; | |
// ## cancelled, ## queed, ## failing, ## in progress, ## neutral, and ## | |
// successful checks | |
meta.innerHTML = links.reduce((html, text, index) => { | |
const separator = index === last ? " and " : index > last ? "" : ", "; | |
const isType = typeKeys.find(type => text.includes(type)); | |
if (isType) { | |
const isSelected = selectedType === isType; | |
html.push(` | |
<a | |
href="#" | |
data-type="${isType}" | |
class="ghsc ${isSelected ? "is-selected" : ""}" | |
>${text}</a>${separator}`); | |
} | |
return html; | |
}, []).join(""); | |
} | |
}; | |
// At least 3 branch-action-item blocks: reviewers, checks & merge state | |
const getChecksBlock = () => $$(".branch-action-item").find(block => | |
$(".statuses-toggle-opened", block)?.textContent?.includes("checks") | |
); | |
function init() { | |
if ( | |
location.pathname.includes("/pull") && | |
$(".mergeability-details .status-meta") | |
) { | |
const block = getChecksBlock(); | |
if (block) { | |
createLinks(block); | |
off(block, "click", toggleVisibility); | |
on(block, "click", toggleVisibility); | |
setVisibility(block); | |
} | |
} | |
} | |
on(document, "ghmo:updatable ghmo:container", debounce(init)); | |
init(); | |
})(); |
Author
turadg
commented
Sep 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment