Skip to content

Instantly share code, notes, and snippets.

@ishowta
Created April 14, 2022 19:23
Show Gist options
  • Save ishowta/7adbcb95264277c109a7613d6595bf10 to your computer and use it in GitHub Desktop.
Save ishowta/7adbcb95264277c109a7613d6595bf10 to your computer and use it in GitHub Desktop.
Move all issues in a project to another project
import "dotenv/config";
import { request, gql } from "graphql-request";
import axios from "axios";
const GITHUB_TOKEN = `xxx`;
const ORG_NAME = `abc`;
const DST_PROJECT_NUMBER = 1;
const SRC_PROJECT_NUMBER = 2;
const greq = async (q: string) => {
return request("https://api.github.com/graphql", q, undefined, {
Authorization: `bearer ${GITHUB_TOKEN}`,
});
};
const run = async () => {
const dstProject = await greq(gql`
query {
organization(login: "${ORG_NAME}") {
project(number: ${DST_PROJECT_NUMBER}) {
id
name
}
}
}
`);
const dstProjectID = dstProject.organization.project.id;
const srcProject = await greq(gql`
query {
organization(login: "${ORG_NAME}") {
project(number: ${SRC_PROJECT_NUMBER}) {
columns(first: 100) {
nodes {
name
databaseId
}
}
}
}
}
`);
console.log(srcProject);
const srcProjectCols = srcProject.organization.project.columns.nodes as {
name: string;
databaseId: string;
}[];
console.log(srcProjectCols);
let dstProjectCols: string[] = [];
for await (const col of srcProjectCols) {
const res = await greq(gql`
mutation {
addProjectColumn(
input: { projectId: "${dstProjectID}", name: "${col.name}" }
) {
columnEdge {
node {
id
name
}
}
}
}
`);
console.log(col.name, res.addProjectColumn.columnEdge.node.id);
dstProjectCols.push(res.addProjectColumn.columnEdge.node.id);
}
await Promise.all(
srcProjectCols.map(async (col, i) => {
const res = await axios.get(
`https://api.github.com/projects/columns/${col.databaseId}/cards`,
{
headers: {
Authorization: `bearer ${GITHUB_TOKEN}`,
},
params: {
per_page: 100,
},
}
);
const cards = res.data;
await Promise.all(
cards.map(async (card: any) => {
if (card.content_url == null) {
console.log("skip");
return;
}
const issue = await axios.get(card.content_url, {
headers: {
Authorization: `bearer ${GITHUB_TOKEN}`,
},
});
const issueId = issue.data.node_id as number;
console.log(col.databaseId, col.name, card.content_url, issueId);
const res2 = await greq(gql`
mutation {
addProjectCard(input: { projectColumnId: "${dstProjectCols[i]}", contentId: "${issueId}" }){
clientMutationId
}
}
`);
console.log(res2.data);
})
);
})
);
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment