Skip to content

Instantly share code, notes, and snippets.

@statico
Created March 25, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statico/df07093aaa273f7935fe63158957788d to your computer and use it in GitHub Desktop.
Save statico/df07093aaa273f7935fe63158957788d to your computer and use it in GitHub Desktop.
const { graphql } = require("@octokit/graphql")
const REPO_OWNER = "xxxxxx"
const REPO_NAME = "xxxxxx"
const PROJECT_NUMBER = 2
const COLUMN_TO_SORT = /To Do/
const api = graphql.defaults({
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
})
const main = async () => {
console.log("Fetching project data...")
const data = await api(`
query {
repository(owner:"${REPO_OWNER}", name:"${REPO_NAME}") {
project(number:${PROJECT_NUMBER}) {
columns(first:10) {
nodes {
id
name
cards(first:100) {
nodes {
id
content {
...cardFields
...labelFields
...milestoneFields
}
}
}
}
}
}
}
}
fragment cardFields on Issue {
title
}
fragment milestoneFields on Issue {
milestone {
title
}
}
fragment labelFields on Labelable {
labels(first:10) {
nodes {
name
}
}
}
`)
const cols = data.repository.project.columns.nodes
const col = cols.find((c) => COLUMN_TO_SORT.test(c.name))
const cards = []
let last = null
for (const card of col.cards.nodes) {
const obj = {
title: card.content.title,
id: card.id,
level1: 50,
level2: 50,
level3: 50,
}
if (last) obj.lastId = last.id
const milestone = card.content.milestone?.title ?? null
if (milestone === "V2.0") obj.level1 = 10
if (milestone === "V2.1") obj.level1 = 20
if (milestone === "V2.2") obj.level1 = 30
if (milestone == null) obj.level1 = 99
for (const { name: rawName } of card.content.labels.nodes) {
const name = String(rawName).replace(/\W/g, "").toLowerCase() // Remove emoji
if (name === "epic") obj.level1 = 1
if (name === "P0") obj.level2 = 1
if (name === "P1") obj.level2 = 2
if (name === "P2") obj.level2 = 3
if (name === "P3") obj.level2 = 4
if (name === "P4") obj.level2 = 5
if (name === "goodfirstissue") obj.level3 = 1
if (name === "bug") obj.level3 = 2
if (name === "needsmoredetail") obj.level3 = 99
}
cards.push(obj)
last = obj
}
cards.sort(
(a, b) => a.level1 - b.level1 || a.level2 - b.level2 || a.level3 - b.level3
)
last = null
for (const card of cards) {
const lastId = last ? last.id : null
// TODO: GraphQL is broken :(
// if (lastId !== card.lastId) {
if (last) {
console.log("Moving", card.id, "after", lastId)
await api(`
mutation {
moveProjectCard(input:{
columnId: "${col.id}",
cardId: "${card.id}",
afterCardId: "${lastId}",
}) { clientMutationId }
}
`)
}
last = card
}
}
main().catch((err) => {
console.log(err?.stack ?? err)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment