From ecb0b7c857e9b675f7cf7dbb66e918bfe6d43320 Mon Sep 17 00:00:00 2001 From: CookiePieWw Date: Thu, 22 Feb 2024 20:14:52 +0800 Subject: [PATCH] feat: actions for toc of discussions --- .github/scripts/toc.py | 52 ++++++++++++++++++++++++++++++++++++++ .github/workflows/toc.yaml | 29 +++++++++++++++++++++ README.md | 2 ++ 3 files changed, 83 insertions(+) create mode 100644 .github/scripts/toc.py create mode 100644 .github/workflows/toc.yaml diff --git a/.github/scripts/toc.py b/.github/scripts/toc.py new file mode 100644 index 0000000..45f20f2 --- /dev/null +++ b/.github/scripts/toc.py @@ -0,0 +1,52 @@ +import os +import requests + +# GitHub GraphQL API endpoint +graphql_url = 'https://api.github.com/graphql' + +# Get necessary information from the environment +username = os.environ['GITHUB_REPOSITORY'].split("/")[0] +# repo is like USERNAME/REPO_NAME +repo = os.environ['GITHUB_REPOSITORY'].split("/")[1] + +# GraphQL query to get the title of the latest discussion +graphql_query = """ +query { + repository(owner: "%(username)s", name: "%(repo)s") { + discussions(first: 1, orderBy: { field: CREATED_AT, direction: DESC }) { + nodes { + title + number + } + } + } +} +""" % {"username": username, "repo": repo} + +# Fetch discussion details +headers = { + "Authorization": f"Bearer {os.environ['GH_TOKEN']}", + 'Content-Type': 'application/json', +} + +# Make the GraphQL request +response = requests.post(graphql_url, headers=headers, json={'query': graphql_query}) +discussion_data = response.json() + +# Check for a successful response +if response.status_code == 200: + data = response.json() + discussion_title = data['data']['repository']['discussions']['nodes'][0]['title'] + discussion_number = data['data']['repository']['discussions']['nodes'][0]['number'] + discussion_url = f"https://github.com/{username}/{repo}/discussions/{discussion_number}" + # Update README.md + readme_path = "README.md" + with open(readme_path, "a") as readme_file: + readme_file.write(f"\n- [{discussion_title}]({discussion_url})\n") + # Commit and push changes + os.system("git add README.md") + os.system("git commit -m \"Update README with latest discussion title\"") + os.system("git push origin master") +else: + print(f"GraphQL request failed with status code {response.status_code}: {response.text}") + exit(-1) diff --git a/.github/workflows/toc.yaml b/.github/workflows/toc.yaml new file mode 100644 index 0000000..b6bc689 --- /dev/null +++ b/.github/workflows/toc.yaml @@ -0,0 +1,29 @@ +name: toc of discussions +on: + discussion: + types: + - created + +jobs: + update-readme: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: "3.x" + + - name: Install Dependencies + run: | + pip install requests + + - name: Update README + run: python .github/scripts/toc.py + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} + GITHUB_REPOSITORY: ${{ github.repository }} diff --git a/README.md b/README.md index daef52b..c6c34e6 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,5 @@ 对 [skyleaworlder](https://github.com/skyleaworlder) 的 [paper-reading](https://github.com/skyleaworlder/paper-reading) 的拙劣模仿。 阅读的内容并不仅限于论文。Blog posts, talks 和问答网站里的回答都行。 + +## TOC