Skip to content

Instantly share code, notes, and snippets.

@mabry1985
Created June 21, 2023 20:58
Show Gist options
  • Save mabry1985/7cf5cec8d5913948aeda070f51ecfe4d to your computer and use it in GitHub Desktop.
Save mabry1985/7cf5cec8d5913948aeda070f51ecfe4d to your computer and use it in GitHub Desktop.
// Name: Search Open PRs
// Description: Search open PRs in a repo
import "@johnlindquist/kit";
const fetch = await npm("node-fetch");
const variables = {
owner: "knapsack-labs",
repoName: "app-monorepo",
};
let token = await env("GITHUB_AUTH_TOKEN", {
hint: `Grab a key from <a href="https://github.com/settings/tokens">here</a>`,
});
const query = `
query getPrs($owner: String!, $repoName: String!) {
repository(owner: $owner, name: $repoName) {
pullRequests(last: 100, states: OPEN) {
nodes {
body
createdAt
mergeable
number
state
title
updatedAt
url
author {
avatarUrl
login
}
}
}
}
}
`;
async function getPrs() {
return fetch("https://api.github.com/graphql", {
headers: {
authorization: `bearer ${token}`,
},
method: "POST",
body: JSON.stringify({ query, variables }),
})
.then((res) => res.json())
.catch((err) => {
console.log(err);
exit();
});
}
const prs = await getPrs();
const openPRs = prs.data.repository.pullRequests.nodes;
const sortedPrs = openPRs.sort(
(a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt)
);
const pr = await arg(
{
placeholder: `Select a PR to view`,
},
sortedPrs.map((pr) => {
return {
name: `${pr.number} - ${pr.title}`,
preview: () =>
`<div class="p-2">
<h2>${pr.number} - ${pr.title}</h2>
<hr class="mb-4"/>
<p>Ready to Merge: ${pr.mergeable === "MERGEABLE" ? "✅" : "⛔"}</p>
<p class="my-4">${md(pr.body)}</p>
<span class="flex flex-row">
<p>Author: ${pr.author.login}</p>
<img class="w-5 h-5 ml-2" src="${pr.author.avatarUrl}" />
</span>
</div>`,
value: pr.number,
};
})
);
const prInfo = sortedPrs.find((p) => p.number === pr);
browse(prInfo.url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment