diff --git a/.github/workflows/archive-old-cards.yaml b/.github/workflows/archive-old-cards.yaml new file mode 100644 index 000000000..4d004d742 --- /dev/null +++ b/.github/workflows/archive-old-cards.yaml @@ -0,0 +1,103 @@ +--- +on: + workflow_dispatch: + schedule: + - cron: "0 5 * * *" + +name: Archive old cards +jobs: + archive-old-cards: + name: Move closed issues to a "done" or "closed" column + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const owner = context.repo.owner; + const repo = context.repo.repo; + + const maxOperations = 150; + + // fetch all projects + const projects = await github.rest.projects.listForRepo({ owner, repo }); + console.log(`got ${projects.data.length} projects`); + + // projects.map(fetch all columns) + const columns = await Promise.all( + projects.data.map(async project => { + const resp = await github.rest.projects.listColumns({ + project_id: project.id, + }); + + const doneColumnForProject = resp.data.filter(column => + column.name == "Closed" || + column.name == "Done" || + column.name == "Successful" + )[0]; + + console.log(project.name, doneColumnForProject.name, doneColumnForProject.id); + + return resp.data.map(column => { + column.projectName = project.name; + + // attach the "done" column for the project to each column in + // the project so we can move cards to it + column.doneColumnForProject = doneColumnForProject + + return column; + }); + }) + ); + + const allColumns = columns.flat(); + console.log(`got ${allColumns.length} columns`); + + // columns.map(fetch all cards) + const cards = await Promise.all( + allColumns.map(async column => { + const cards = await github.rest.projects.listCards({ + column_id: column.id, + per_page: maxOperations, + }); + return cards.data.map(card => { + card.projectName = column.projectName; + card.columnName = column.name; + card.doneColumnForProject = column.doneColumnForProject; + return card; + }); + }) + ); + + const allCards = cards.flat(); + console.log(`got ${allCards.length} cards`); + console.log(`processing ${Math.min(maxOperations, allCards.length)} cards`); + + + // for each card, fetch linked issue. If the issue is closed, move the card to the done column. + let moveCounter = 0; + for (const card of allCards.slice(0,maxOperations)) { + const issue = (await github.request(card.content_url)).data; + + console.log(card.id, issue.number, issue.state, card.columnName); + + if (issue.state === "open") { + console.log(`SKIP open: ${issue.number} in ${card.projectName} | ${card.columnName} | ${issue.title} | ${issue.html_url}`); + continue; + } + if (card.columnName === card.doneColumnForProject.name) { + console.log(`SKIP already in done column: ${issue.number} in ${card.projectName} | ${card.columnName} | ${issue.title} | ${issue.html_url}`); + continue; + } + + console.log(`MOVE card for closed issue ${issue.number} in ${card.projectName} | ${card.columnName} --> ${card.doneColumnForProject.name} | ${issue.title} | ${issue.html_url}`); + await github.rest.projects.moveCard({ + position: "top", + card_id: card.id, + column_id: card.doneColumnForProject.id + }); + + moveCounter += 1; + } + + console.log(`Moved ${moveCounter}/${allCards.length} cards`);