|
| 1 | +// Parses the ISO date string and checks it's past the age window |
| 2 | +// Co-authored-by: GPT-4.1 (GitHub Copilot) |
| 3 | +function isOlderThanDaysAgo(dateString, daysAgo) { |
| 4 | + const parsedDate = new Date(dateString); |
| 5 | + const now = new Date(); |
| 6 | + const thresholdDate = new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000); |
| 7 | + return parsedDate < thresholdDate; |
| 8 | +} |
| 9 | + |
| 10 | +module.exports = async ({ github, context, discussionAnsweredDays }) => { |
| 11 | + const owner = context.repo.owner; |
| 12 | + const repo = context.repo.repo; |
| 13 | + |
| 14 | + let cursor = null; |
| 15 | + |
| 16 | + const query = `query ($cursor: String) { |
| 17 | + repository(owner: "${owner}", name: "${repo}") { |
| 18 | + discussions(after: $cursor, states: OPEN, answered: true, first: 50, orderBy: {field: CREATED_AT, direction: ASC}) { |
| 19 | + pageInfo { |
| 20 | + endCursor |
| 21 | + hasNextPage |
| 22 | + } |
| 23 | + edges { |
| 24 | + node { |
| 25 | + id |
| 26 | + answerChosenAt |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | +}`; |
| 32 | + |
| 33 | + while (true) { |
| 34 | + console.debug({ cursor }, 'Starting query'); |
| 35 | + const { repository } = await github.graphql(query, { cursor }); |
| 36 | + |
| 37 | + console.debug( |
| 38 | + `Found ${repository.discussions.edges.length} discussions in this page of data`, |
| 39 | + ); |
| 40 | + |
| 41 | + let numMutating = 0; |
| 42 | + let mutation = 'mutation {'; |
| 43 | + for (let i in resp.repository.discussions.edges) { |
| 44 | + let edge = resp.repository.discussions.edges[i]; |
| 45 | + if ( |
| 46 | + isOlderThanDaysAgo(edge.node.answerChosenAt, discussionAnsweredDays) |
| 47 | + ) { |
| 48 | + mutation += `m${i}: closeDiscussion(input: {discussionId: "${edge.node.id}"}) { |
| 49 | + clientMutationId |
| 50 | + }\n`; |
| 51 | + numMutating++; |
| 52 | + } |
| 53 | + } |
| 54 | + mutation += '}'; |
| 55 | + |
| 56 | + if (numMutating > 0) { |
| 57 | + console.debug(`Attempting the following mutation:\n${mutation}`); |
| 58 | + |
| 59 | + await github.graphql(mutation); |
| 60 | + |
| 61 | + console.log(`Closed ${numMutating} answered Discussions`); |
| 62 | + } else { |
| 63 | + console.debug( |
| 64 | + `Did not find any Discussions to close in this page of data`, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (!resp.repository.discussions.pageInfo.hasNextPage) { |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + cursor = resp.repository.discussions.pageInfo.endCursor; |
| 73 | + } |
| 74 | + |
| 75 | + return ''; |
| 76 | +}; |
0 commit comments