-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: actions for toc of discussions
- Loading branch information
1 parent
96c4716
commit ecb0b7c
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters