Skip to content

Commit

Permalink
feat: actions for toc of discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePieWw committed Feb 22, 2024
1 parent 96c4716 commit ecb0b7c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/scripts/toc.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions .github/workflows/toc.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
[skyleaworlder](https://github.com/skyleaworlder)[paper-reading](https://github.com/skyleaworlder/paper-reading) 的拙劣模仿。

阅读的内容并不仅限于论文。Blog posts, talks 和问答网站里的回答都行。

## TOC

0 comments on commit ecb0b7c

Please sign in to comment.