lock discussions automatically (#5106)

This commit is contained in:
Alessandro Ros 2025-10-17 15:18:28 +02:00 committed by GitHub
parent 26b6be02db
commit fe3adf9b78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

105
.github/workflows/discussion_lock.yml vendored Normal file
View file

@ -0,0 +1,105 @@
name: discussion_lock
on:
schedule:
- cron: '40 16 * * *'
workflow_dispatch:
jobs:
discussion_lock:
runs-on: ubuntu-22.04
steps:
- uses: actions/github-script@v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { repo: { owner, repo } } = context;
const now = new Date();
const twoYearsAgo = new Date(now.getTime() - 1000*60*60*24*365*2);
const query = `
query($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
discussions(first: 100, after: $cursor, filterBy: {locked: false}) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
number
title
closed
closedAt
updatedAt
category {
isAnswerable
}
answer {
id
}
}
}
}
}
`;
let hasNextPage = true;
let cursor = null;
while (hasNextPage) {
const result = await github.graphql(query, {
owner,
repo,
cursor
});
const discussions = result.repository.discussions;
hasNextPage = discussions.pageInfo.hasNextPage;
cursor = discussions.pageInfo.endCursor;
for (const discussion of discussions.nodes) {
const lastUpdateDate = new Date(discussion.updatedAt);
const closedDate = discussion.closedAt ? new Date(discussion.closedAt) : null;
const relevantDate = closedDate || lastUpdateDate;
if (relevantDate > twoYearsAgo) {
continue;
}
const addCommentMutation = `
mutation($discussionId: ID!, $body: String!) {
addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {
comment {
id
}
}
}
`;
await github.graphql(addCommentMutation, {
discussionId: discussion.id,
body: `This discussion is being locked automatically because the last update was more than 2 years ago.\n`+
`\n`+
`**Do not use the content of this discussion as reference since it's probably outdated!**\n`+
`\n`+
`The official documentation is the only place in which you can find up-to-date answers.\n`,
});
const lockMutation = `
mutation($discussionId: ID!) {
lockLockable(input: {lockableId: $discussionId}) {
lockedRecord {
locked
}
}
}
`;
await github.graphql(lockMutation, {
discussionId: discussion.id
});
process.exit(0);
}
}