From 16169ca331531b100f1251ca4064c759b0fc8022 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 18 Jan 2024 11:46:12 -0800 Subject: [PATCH] Chore: Close outdated support / general discussions (#5443) --- .github/workflows/repo-maintenance.yml | 91 ++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/.github/workflows/repo-maintenance.yml b/.github/workflows/repo-maintenance.yml index 6a2a1713e..8cc876778 100644 --- a/.github/workflows/repo-maintenance.yml +++ b/.github/workflows/repo-maintenance.yml @@ -107,3 +107,94 @@ jobs: await sleep(1000) } + close-outdated-discussions: + name: 'Close Outdated Discussions' + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v7 + with: + script: | + function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + const CUTOFF_DAYS = 180; + const cutoff = new Date(); + cutoff.setDate(cutoff.getDate() - CUTOFF_DAYS); + + const query = `query( + $owner:String!, + $name:String!, + $supportCategory:ID!, + $generalCategory:ID!, + ) { + supportDiscussions: repository(owner:$owner, name:$name){ + discussions( + categoryId:$supportCategory, + last:50, + answered:false, + states:[OPEN], + ) { + nodes { + id, + number, + updatedAt + } + }, + }, + generalDiscussions: repository(owner:$owner, name:$name){ + discussions( + categoryId:$generalCategory, + last:50, + states:[OPEN], + ) { + nodes { + id, + number, + updatedAt + } + } + } + }`; + const variables = { + owner: context.repo.owner, + name: context.repo.repo, + supportCategory: "DIC_kwDOG1Zs184CBKWK", + generalCategory: "DIC_kwDOG1Zs184CBKWJ" + } + const result = await github.graphql(query, variables); + const combinedDiscussions = [ + ...result.supportDiscussions.discussions.nodes, + ...result.generalDiscussions.discussions.nodes, + ] + + console.log(`Checking ${combinedDiscussions.length} open discussions`); + + for (const discussion of combinedDiscussions) { + if (new Date(discussion.updatedAt) < cutoff) { + console.log(`Closing outdated discussion #${discussion.number} (${discussion.id}), last updated at ${discussion.updatedAt}`); + const addCommentMutation = `mutation($discussion:ID!, $body:String!) { + addDiscussionComment(input:{discussionId:$discussion, body:$body}) { + clientMutationId + } + }`; + const commentVariables = { + discussion: discussion.id, + body: 'This discussion has been automatically closed due to inactivity.', + } + await github.graphql(addCommentMutation, commentVariables); + + const closeDiscussionMutation = `mutation($discussion:ID!, $reason:DiscussionCloseReason!) { + closeDiscussion(input:{discussionId:$discussion, reason:$reason}) { + clientMutationId + } + }`; + const closeVariables = { + discussion: discussion.id, + reason: "OUTDATED", + } + await github.graphql(closeDiscussionMutation, closeVariables); + + await sleep(1000); + } + }