Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 79 additions & 8 deletions src/mergify.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ function postCommand(command) {
button.click();
}

function togglePrListFilter() {
debug("Applying PR list filter");
const url = new URL(window.location.href);
// url.pathname = url.pathname.replace(/\/pulls$/, "/issues");
const oldQuery = url.searchParams.get("q")?.trim() || "";
const components = new Set(oldQuery.split(" "));
components.add("is:pr");
if (!components.has("is:closed") && !components.has("is:open")) {
components.add("is:open");
}
if (components.has("-author:app/mergify")) {
components.delete("-author:app/mergify");
} else {
components.add("-author:app/mergify");
}
debug("New query components:", components);
url.searchParams.set("q", Array.from(components).join(" "));
window.location.href = url.href;
}

function isPullRequestOpen() {
const opened = document.querySelector("span[data-status=pullOpened]");
const draft = document.querySelector("span[data-status=draft]");
Expand Down Expand Up @@ -355,12 +375,43 @@ function buildMergifySectionForTimelineActions() {
return container1;
}

function buildPRListMergifyFilterButton() {
const container = document.createElement("button");
container.setAttribute(
"title",
"Filter out pull requests managed by Mergify",
);
container.type = "button";
container.id = "mergify-pr-list-filter";
container.className = "mr-1 border rounded-md";
const params = new URLSearchParams(window.location.search);
if (params.get("q")?.includes("-author:app/mergify")) {
container.style.backgroundColor =
"var(--button-default-bgColor-active)";
} else {
container.style.backgroundColor = "var(--button-default-bgColor-rest)";
}
const logo = parseSvg(getLogoSvg());
logo.setAttribute("width", "22");
logo.setAttribute("height", "22");
logo.style.verticalAlign = "top";
container.appendChild(logo);
container.onclick = togglePrListFilter;
return container;
}

function isGitHubPullRequestPage() {
const url = new URL(window.location.href);
const parts = url.pathname.split("/");
return parts.length >= 5 && parts[3] === "pull";
}

function isGitHubPullRequestListingPage() {
const url = new URL(window.location.href);
const path = url.pathname;
return path.split("/").length === 4 && !!path.match(/\/pulls$/);
}

function findTimelineActions() {
const mergeBoxDiv = document.querySelector(
"div[class=discussion-timeline-actions]",
Expand All @@ -370,15 +421,13 @@ function findTimelineActions() {
}
}

async function _tryInject() {
if (!isGitHubPullRequestPage()) {
debug("Not a pull request page");
return;
}
function findNewPrButton() {
return document.querySelector('[data-hotkey="c"]');
}

const isMergifySectionInjected = document.querySelector("#mergify");
if (isMergifySectionInjected) {
debug("Mergify section is already injected");
async function _tryInject() {
if (!isGitHubPullRequestPage() && !isGitHubPullRequestListingPage()) {
debug("Not a pull request related page");
return;
}

Expand All @@ -388,6 +437,28 @@ async function _tryInject() {
return;
}

if (isGitHubPullRequestListingPage()) {
const existingBtn = document.querySelector("#mergify-pr-list-filter");
if (existingBtn) {
debug("Mergify PR list filter button is already injected");
return;
}
debug("Injecting Mergify PR list filter button");
const btn = buildPRListMergifyFilterButton();
const newPrButton = findNewPrButton();
if (newPrButton) {
const parent = newPrButton.parentElement;
parent.parentElement.insertBefore(btn, parent);
}
return;
}

const isMergifySectionInjected = document.querySelector("#mergify");
if (isMergifySectionInjected) {
debug("Mergify section is already injected");
return;
}

let detailSection = findTimelineActions();
if (detailSection) {
// New merge box
Expand Down
Loading