Skip to content

Instantly share code, notes, and snippets.

@nhocki
Last active August 27, 2024 21:33
Show Gist options
  • Save nhocki/ea9fcb036d9c099b42d2629116b1a642 to your computer and use it in GitHub Desktop.
Save nhocki/ea9fcb036d9c099b42d2629116b1a642 to your computer and use it in GitHub Desktop.
Reorder GitHub checks in pull requests to make it easier to find what failed.

Reorder GitHub Checks

This small extension will reorder GitHub checks in a GitHub pull request and place the successful checks at the bottom of the list. This is useful when you have a lot of checks and you want to see the failed checks first.

How to install

  1. Clone this Gist: git clone https://gist.github.com/ea9fcb036d9c099b42d2629116b1a642.git reorder-github-checks

  2. Install the extension in Chrome:

    • Go to chrome://extensions/
    • Enable "Developer mode"
    • Click "Load unpacked"
    • Select the folder where you cloned the gist
function reorderChecks() {
console.log("Reordering GitHub PR checks...");
// Select all merge status items
const mergeStatusItems = document.querySelectorAll(
"div.merge-status-item:has(.merge-status-icon)"
);
// If no items found, exit the function
if (mergeStatusItems.length === 0) return;
// Convert NodeList to Array for easier manipulation
const itemsArray = Array.from(mergeStatusItems);
// Sort the array
itemsArray.sort((a, b) => {
const aSuccessful = a.querySelector(".octicon-check") !== null;
const bSuccessful = b.querySelector(".octicon-check") !== null;
// First, sort by status
if (aSuccessful !== bSuccessful) {
return aSuccessful ? 1 : -1;
}
const aName = a ? a.textContent.trim() : "";
const bName = b ? b.textContent.trim() : "";
return aName.localeCompare(bName);
});
// Get the parent container
const container = mergeStatusItems[0].parentNode;
// Remove all existing items
mergeStatusItems.forEach((item) => item.remove());
// Append sorted items back to the container
itemsArray.forEach((item) => container.appendChild(item));
console.log("GitHub PR checks reordered.");
}
function waitForChecksAndReorder() {
const branchActionItem = document.querySelector(".merge-status-list");
if (branchActionItem) {
// If the merge-status-list exists, wait a short time for its content to fully load
setTimeout(() => {
try {
reorderChecks();
} catch (error) {
console.error("Error while reordering checks:", error);
}
}, 1000);
} else {
// If the merge-status-list doesn't exist yet, check again after a short delay
setTimeout(waitForChecksAndReorder, 500);
}
}
// Create and start a MutationObserver to detect when the merge-status-list is added to the DOM
const observer = new MutationObserver((mutations, obs) => {
const branchActionItem = document.querySelector(".merge-status-list");
if (branchActionItem) {
waitForChecksAndReorder();
obs.disconnect(); // Stop observing once we've found the element
}
});
// Start observing the document with the configured parameters
observer.observe(document.body, { childList: true, subtree: true });
document.addEventListener("DOMContentLoaded", (event) => {
waitForChecksAndReorder();
observer.observe(document.body, { childList: true, subtree: true });
});
{
"manifest_version": 2,
"name": "GitHub PR Checks Reorderer",
"version": "1.1",
"description": "Reorders GitHub PR checks, sorting by status and name.",
"permissions": [
"https://github.com/*"
],
"content_scripts": [
{
"matches": [
"https://github.com/*"
],
"js": [
"content.js"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment