# -*- coding: utf-8 -*-
from github import Github
from github.Issue import Issue
import argparse
MD_HEAD = """## Gitblog
My personal blog using issues and GitHub Action
"""
ANCHOR_NUMBER = 5
TOP_ISSUES_LABELS = [
"Top",
]
TODO_ISSUES_LABELS = [
"TODO",
]
def get_me(user):
return user.get_user().login
def isMe(issue, me):
return issue.user.login == me
def format_time(time):
return str(time)[:10]
def login(token):
return Github(token)
def get_repo(user: Github, repo: str):
return user.get_repo(repo)
def parseTODO(issue):
body = issue.body.splitlines()
todo_undone = [l for l in body if l.startswith("- [ ] ")]
todo_done = [l for l in body if l.startswith("- [x] ")]
# just add info all done
if not todo_undone:
return f"[{issue.title}]({issue.html_url}) all done", []
return f"[{issue.title}]({issue.html_url})--{len(todo_undone)} jobs to do--{len(todo_done)} jobs done", todo_done + todo_undone
def get_top_issues(repo):
return repo.get_issues(labels=TOP_ISSUES_LABELS)
def get_todo_issues(repo):
return repo.get_issues(labels=TODO_ISSUES_LABELS)
def get_repo_labels(repo):
return [l for l in repo.get_labels()]
def get_issues_from_label(repo, label):
return repo.get_issues(labels=(label,))
def add_issue_info(issue, md):
time = format_time(issue.created_at)
md.write(f"- [{issue.title}]({issue.html_url})--{time}\n")
def add_md_todo(repo, md, me):
todo_issues = list(get_todo_issues(repo))
if not TODO_ISSUES_LABELS or not todo_issues:
return
with open(md, "a+", encoding="utf-8") as md:
md.write("## TODO\n")
for issue in todo_issues:
if isMe(issue, me):
todo_title, todo_list = parseTODO(issue)
md.write("TODO list from " + todo_title + "\n")
for t in todo_list:
md.write(t + "\n")
# new line
md.write("\n")
def add_md_top(repo, md, me):
top_issues = list(get_top_issues(repo))
if not TOP_ISSUES_LABELS or not top_issues:
return
with open(md, "a+", encoding="utf-8") as md:
md.write("## 置顶æç« \n")
for issue in top_issues:
if isMe(issue, me):
add_issue_info(issue, md)
def add_md_recent(repo, md, me):
new_five_issues = repo.get_issues()[:5]
with open(md, "a+", encoding="utf-8") as md:
# one the issue that only one issue and delete (pyGitHub raise an exception)
try:
md.write("## æè¿æ´æ°\n")
for issue in new_five_issues:
if isMe(issue, me):
add_issue_info(issue, md)
except:
return
def add_md_header(md):
with open(md, "w", encoding="utf-8") as md:
md.write(MD_HEAD)
def add_md_label(repo, md, me):
labels = get_repo_labels(repo)
with open(md, "a+", encoding="utf-8") as md:
for label in labels:
# we don't need add top label again
if label.name in TOP_ISSUES_LABELS:
continue
# we don't need add todo label again
if label.name in TODO_ISSUES_LABELS:
continue
issues = get_issues_from_label(repo, label)
if issues.totalCount:
md.write("## " + label.name + "\n")
issues = sorted(issues, key=lambda x: x.created_at, reverse=True)
i = 0
for issue in issues:
if not issue:
continue
if isMe(issue, me):
if i == ANCHOR_NUMBER:
md.write("æ¾ç¤ºæ´å¤
\n")
md.write("\n")
add_issue_info(issue, md)
i += 1
if i > ANCHOR_NUMBER:
md.write("