Skip to content

Commit

Permalink
feat: add display of opened prs and issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kowyo committed Nov 21, 2024
1 parent cddb5f0 commit fae9366
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/course.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "scripts/**"
- ".github/workflows/course.yaml"
schedule:
- cron: "0 16 * * *"
- cron: "0 0 * * *"
workflow_dispatch:

concurrency:
Expand Down Expand Up @@ -335,6 +335,12 @@ jobs:
run: |
python scripts/gen_news.py
- name: Fetch opened PRs and issues
env:
TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
run: |
python scripts/fetch_opened_prs_and_issues.py
- name: Wrap badges
run: |
for file in $(find content/docs -name "*.md")
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ hugo.linux
# Temporary lock file while building
/.hugo_build.lock

# # Ignore Mac system files
# Ignore Mac system files
*.DS_Store

.idea/*
*/.idea/

# Ignore .env
scripts/.env
134 changes: 134 additions & 0 deletions scripts/fetch_opened_prs_and_issues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import subprocess
import json
import os
# from dotenv import load_dotenv

# load_dotenv()

def run_gh_command(command, pat=None):
"""
Run a GitHub CLI command with optional Personal Access Token.
:param command: List of command arguments
:param pat: Personal Access Token (optional)
:return: Parsed JSON output
"""
try:
# If PAT is provided, set it as an environment variable
if pat:
env = os.environ.copy()
env['GH_TOKEN'] = pat
else:
env = None

result = subprocess.run(['gh'] + command,
capture_output=True,
text=True,
check=True,
env=env)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error running GitHub CLI command: {e}")
print(f"Error output: {e.stderr}")
return []

def get_org_issues(org_name, pat=None):
"""
Retrieve open issues for the specified organization.
:param org_name: GitHub organization name
:param pat: Personal Access Token (optional)
:return: List of open issues
"""
command = [
'search', 'issues',
'--json', 'title,url,repository,createdAt,author,labels',
'--state', 'open',
'--owner', org_name,
]
return run_gh_command(command, pat)

def get_org_pull_requests(org_name, pat=None):
"""
Retrieve open pull requests for the specified organization.
:param org_name: GitHub organization name
:param pat: Personal Access Token (optional)
:return: List of open pull requests
"""
command = [
'search', 'prs',
'--json', 'title,url,repository,createdAt,author,labels',
'--state', 'open',
'--owner', org_name,
]
return run_gh_command(command, pat)

def fetch_opened_prs_and_issues(org_name, pat=None):
"""
Generate a markdown report of open issues and pull requests.
:param org_name: GitHub organization name
:param pat: Personal Access Token (optional)
"""
# Get issues and PRs
issues = get_org_issues(org_name, pat)
prs = get_org_pull_requests(org_name, pat)

# Generate markdown report
with open(f'content/news/daily.md', 'r+') as f:
content = f.read()
f.seek(0)
# Keep content before "## 议题" and overwrite the rest
if "## Issues" in content:
f.write(content.split("## 待解决的 Issues")[0])
else:
f.write(content)
f.write("## 待解决的 Issues\n")
f.truncate()
if not issues:
f.write("暂无待解决的 Issues\n\n")
else:
for issue in issues:
if issue['repository']['name'] != 'hoa-moe':
f.write(f"### [{issue['title']}]({issue['url']})\n")
f.write(f"- **仓库**: {issue['repository']['name']}\n")
f.write(f"- **创建于**: {issue['createdAt']}\n")
f.write(f"- **作者**: {issue['author']['login']}\n")

# Labels (if any)
if issue['labels']:
label_list = ', '.join([label['name'] for label in issue['labels']])
f.write(f"- **标签**: {label_list}\n")

f.write("\n")

# Pull Requests section
f.write("## 待合并的 Pull Requests\n")
if not prs:
f.write("暂无打开的 Pull Requests\n\n")
else:
for pr in prs:
if pr['repository']['name'] != 'hoa-moe':
f.write(f"### [{pr['title']}]({pr['url']})\n")
f.write(f"- **仓库**: {pr['repository']['name']}\n")
f.write(f"- **创建于**: {pr['createdAt']}\n")
f.write(f"- **作者**: {pr['author']['login']}\n")

# Labels (if any)
if pr['labels']:
label_list = ', '.join([label['name'] for label in pr['labels']])
f.write(f"- **标签**: {label_list}\n")

f.write("\n")

print(f"Report updated: content/news/daily.md")

def main():
org_name = 'HITSZ-OpenAuto'
pat = os.getenv('TOKEN')

fetch_opened_prs_and_issues(org_name, pat)

if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions scripts/gen_news.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,17 @@ def create_markdown_report(filtered_commits, org_course_name, news_type):
"""Create the markdown report from filtered commits."""
filtered_commits.sort(key=lambda a: a['date'], reverse=True)
markdown_report = ""
markdown_report += f'## 更新内容\n\n'
prev_date = None

for commit in filtered_commits:
if commit["author"] == "github-actions":
if commit["author"] == "github-actions" or commit["author"] == "actions-user":
continue

date = commit['date']
title = org_course_name.get(commit['repo_name'], commit['repo_name'])
if prev_date != date.date():
markdown_report += f'#### {chinese_weekday(date)}({date.month}.{date.day})\n\n'
markdown_report += f'### {chinese_weekday(date)}({date.month}.{date.day})\n\n'
prev_date = date.date()

markdown_report += f'- {commit["author"]} 在 [{title}](https://github.com/{ORG_NAME}/{commit["repo_name"]}) 中提交了信息: {commit["message"].splitlines()[0]}\n\n'
Expand Down

0 comments on commit fae9366

Please sign in to comment.