Skip to content

Instantly share code, notes, and snippets.

@aomarks
Created January 20, 2022 03:36
Show Gist options
  • Save aomarks/452c081267d180962c9434b5444ac8cb to your computer and use it in GitHub Desktop.
Save aomarks/452c081267d180962c9434b5444ac8cb to your computer and use it in GitHub Desktop.
// This script finds all open issues in the Lit org and adds
// them to the Lit project board, if they are not already there.
import { graphql } from "@octokit/graphql";
const graphqlWithAuth = graphql.defaults({
headers: {
// This PAT must have repo and admin:org scopes
authorization: `token <INSERT PERSONAL ACCESS TOKEN HERE>`,
},
});
const projectIdResult = await graphqlWithAuth(`
{
organization(login: "lit"){
projectNext(number: 4) {
id
}
}
}
`);
const projectId = projectIdResult.organization.projectNext.id;
const issueUrls = new Set();
const issueUrlDatabaseIds = new Map();
const issueUrlsInProject = new Set();
const repos = [
"lit",
"lit.dev",
"bundles",
"lit-element-starter-js",
"lit-element-starter-ts",
"lit-element",
];
{
for (const repo of repos) {
let cursor = null;
let more = true;
while (more) {
const r = await graphqlWithAuth(`
{
repository(owner: "lit", name: "${repo}") {
issues(states: OPEN, first: 100, after:${
cursor ? `"${cursor}"` : "null"
}) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
number
}
}
}
}
}`);
for (const issue of r.repository.issues.edges) {
const url = `https://github.com/lit/${repo}/issues/${issue.node.number}`;
issueUrls.add(url);
issueUrlDatabaseIds.set(url, issue.node.id);
}
console.log(`Loaded ${issueUrls.size} open repo issues`);
const pageInfo = r.repository.issues.pageInfo;
more = pageInfo.hasNextPage;
cursor = pageInfo.endCursor;
}
}
}
{
let cursor = null;
let more = true;
while (more) {
const r = await graphqlWithAuth(`
{
node(id: "${projectId}") {
... on ProjectNext {
items(first: 100, after:${cursor ? `"${cursor}"` : "null"}) {
pageInfo {
hasNextPage
endCursor
}
nodes{
content {
...on Issue {
number
repository {
nameWithOwner
}
}
}
}
}
}
}
}`);
for (const issue of r.node.items.nodes) {
const orgRepo = issue.content?.repository?.nameWithOwner;
if (!orgRepo) {
continue;
}
const url = `https://github.com/${orgRepo}/issues/${issue.content.number}`;
issueUrlsInProject.add(url);
}
const pageInfo = r.node.items.pageInfo;
more = pageInfo.hasNextPage;
cursor = pageInfo.endCursor;
console.log(`Loaded ${issueUrlsInProject.size} project issues`);
}
}
let numAdded = 0;
for (const url of issueUrls) {
if (issueUrlsInProject.has(url)) {
continue;
}
const issueId = issueUrlDatabaseIds.get(url);
console.log(`Adding ${url} to the project`);
await graphqlWithAuth(`
mutation {
addProjectNextItem(input: {projectId: "${projectId}", contentId: "${issueId}"}) {
projectNextItem {
id
}
}
}
`);
numAdded++;
}
console.log(`Done! Added ${numAdded} issues to the project.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment