Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Lock discussion #2557

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions .github/workflows/lock-discussion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
101 changes: 101 additions & 0 deletions .github/workflows/lock-discussion/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//import fetch from "node-fetch";
import { graphql } from "@octokit/graphql"
//global.fetch = fetch;

const graphqlWithAuth = graphql.defaults({
headers: {
authorization: process.env.TOKEN,
},
});

async function lockDiscussions(ids) {
for (let discussionId of ids) {
try {
await graphqlWithAuth(
`
mutation lock ($id: ID!) {
lockLockable(input:{lockableId: $id}) {
clientMutationId
}
}
`,
{
id: discussionId,
}
);
} catch (e) {
console.log(e);
}
}
}

async function main () {
console.log("TOKEN: ", process.env.TOKEN);
const unlockedNodes = []
const oneDay = 1000 * 60 * 60 * 24; // one day in ms
const today = new Date();
let readDiscussions = true
let cursor = null
while (readDiscussions) {
const {repository} = await graphqlWithAuth(`
query discussionListQuery ($owner: String!, $repo: String!, $lastId: String) {
repository(owner:$owner, name:$repo) {
discussions(states:[OPEN], first:4, after: $lastId) {
totalCount
nodes {
id
answerChosenAt
createdAt
updatedAt
locked
title
category {
name
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
`, {
owner: "cloudscape-design",
repo: "components",
lastId: cursor,
});

repository.discussions.nodes.forEach(discussion => {
if (!discussion.locked) {
const isQA = discussion.category.name === 'Q&A'
const lockQA = !!discussion.answerChosenAt
&& (today - new Date(discussion.answerChosenAt).getTime()) / oneDay > 7

// there is no reliable way to detect if it was answered,
// so we give it 2 weeks
const lockGeneral = !!discussion.updatedAt
&& discussion.updatedAt !== discussion.createdAt
&& (today - new Date(discussion.updatedAt).getTime()) / oneDay > 14

const shouldBeLocked = isQA ? lockQA : lockGeneral;
if (shouldBeLocked) {
unlockedNodes.push(discussion.id)
}
}
})
const {endCursor, hasNextPage} = repository.discussions.pageInfo;
readDiscussions = hasNextPage
cursor = endCursor
}

console.log(`Discussions to lock: ${unlockedNodes.length}`);
if (unlockedNodes.length) {
await lockDiscussions(unlockedNodes);
}
console.log('Locking completed');
}

main();
18 changes: 18 additions & 0 deletions .github/workflows/lock-discussion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "lock-discussion",
"version": "1.0.0",
"description": "",
"main": "index.mjs",
"type": "module",
"scripts": {
"lock": "node index.mjs $TOKEN"
},
"repository": {
"type": "git",
"url": "https://github.com/cloudscape-design/actions.git"
},
"private": true,
"dependencies": {
"@octokit/graphql": "^7.0.2"
}
}
38 changes: 38 additions & 0 deletions .github/workflows/lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Lock threads


on:
schedule:
- cron: '0 0 * * 1'

workflow_dispatch:
pull_request:
branches:
- main

permissions:
discussions: write
contents: read

jobs:
lock-discussion:
runs-on: ubuntu-latest
steps:
- name: Checkout the PR branch
uses: actions/checkout@v4
with:
path: 'components'

- name: Setup lock-discussion project
run: |
echo "the PWD is : ${PWD}"
cp -r components/.github/workflows/lock-discussion lock-discussion
cd lock-discussion
npm i --force

- name: Run lock discussion
run: |
cd lock-discussion
npm run lock
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
Loading