Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store backup in another branch #253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions .github/workflows/generate_readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,51 @@ on:
env:
GITHUB_NAME: yihong0618
GITHUB_EMAIL: [email protected]
BACKUP_BRANCH: backup

jobs:
sync:
name: Generate README
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
uses: actions/checkout@v3
with:
python-version: 3.6
fetch-depth: 0
path: main

- name: Configure pip cache
uses: actions/cache@v1
id: pip-cache
- name: Checkout Blog
uses: actions/checkout@v3
with:
path: venv
key: pip-1-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
pip-
ref: ${{ env.BACKUP_BRANCH }}
path: back

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
if: steps.pip-cache.outputs.cache-hit != 'true'
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: 3.9
cache: 'pip'

- name: Generate new md
- name: Get pip env
run: |
source venv/bin/activate
python main.py ${{ secrets.G_T }} ${{ github.repository }} --issue_number '${{ github.event.issue.number }}'
python -m pip install --upgrade pip
pip install -r main/requirements.txt
- name: Generate new md # if you fork or clone this repo, please delete the curl line unless you know why
run: cd back && python ../main/main.py ${{ secrets.G_T }} ${{ github.repository }} --issue_number '${{ github.event.issue.number }}'

- name: Push README
run: |
cp back/README.md main/README.md
cp back/feed.xml main/feed.xml
cd main
git config --local user.email "${{ env.GITHUB_EMAIL }}"
git config --local user.name "${{ env.GITHUB_NAME }}"
git add BACKUP/*.md
git commit -a -m 'update new blog' || echo "nothing to commit"
git push || echo "nothing to push"

git add README.md feed.xml
git commit --amend --no-edit
git push --force
cd ../back
git config --local user.email "${{ env.GITHUB_EMAIL }}"
git config --local user.name "${{ env.GITHUB_NAME }}"
git add .
git commit -m "update new blog"
git push
Comment on lines +52 to +65
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥这么复杂。。。

80 changes: 44 additions & 36 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
from feedgen.feed import FeedGenerator
from lxml.etree import CDATA

MD_HEAD = """## Gitblog
My personal blog using issues and GitHub Actions (随意转载,无需署名)
[RSS Feed](https://raw.githubusercontent.com/{repo_name}/master/feed.xml)
MD_HEAD = """# Gitblog
My personal blog using issues and GitHub Actions(随意转载,无需署名)
Subscribe to [RSS Feed](https://raw.githubusercontent.com/{repo_name}/master/feed.xml)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 repo_name 是变量?

"""

MD_FOOT = """
---
Thanks for [yihong0618](https://github/com/yihong0618/gitblog)
Comment on lines +16 to +18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句可以不要哈哈,不过没关系,我自己来改也行

"""

BACKUP_DIR = "BACKUP"
Expand All @@ -20,8 +25,8 @@
FRIENDS_LABELS = ["Friends"]
IGNORE_LABELS = FRIENDS_LABELS + TOP_ISSUES_LABELS + TODO_ISSUES_LABELS

FRIENDS_TABLE_HEAD = "| Name | Link | Desc | \n | ---- | ---- | ---- |\n"
FRIENDS_TABLE_TEMPLATE = "| {name} | {link} | {desc} |\n"
FRIENDS_TABLE_HEAD = "| 名字 | 链接 | 描述 |\n| --- | --- | --- |\n"
FRIENDS_TABLE_TEMPLATE = "| {name} | <{link}> | {desc} |\n"
FRIENDS_INFO_DICT = {
"名字": "",
"链接": "",
Expand Down Expand Up @@ -90,13 +95,13 @@ def get_repo(user: Github, repo: str):

def parse_TODO(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] ")]
todo_undone = [l.strip() for l in body if l.lstrip().startswith("- [ ] ")]
todo_done = [l.strip() for l in body if l.lstrip().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",
f"[{issue.title}]({issue.html_url}) --{len(todo_done)} jobs done --{len(todo_undone)} jobs to do",
todo_done + todo_undone,
)

Expand All @@ -119,39 +124,40 @@ def get_issues_from_label(repo, label):

def add_issue_info(issue, md):
time = format_time(issue.created_at)
md.write(f"- [{issue.title}]({issue.html_url})--{time}\n")
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")
md.write("\n## TODO\n")
for issue in todo_issues:
if is_me(issue, me):
todo_title, todo_list = parse_TODO(issue)
md.write("TODO list from " + todo_title + "\n")
for t in todo_list:
md.write(t + "\n")
# new line
md.write("\n")
if todo_list:
md.write("\nTODO list from " + todo_title + "\n\n")
for t in todo_list:
md.write(t + "\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")
md.write("\n## 置顶文章\n\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 Label 不是有么?不加这个也行吧?

for issue in top_issues:
if is_me(issue, me):
add_issue_info(issue, md)


def add_md_firends(repo, md, me):
s = FRIENDS_TABLE_HEAD
friends_issues = list(repo.get_issues(labels=FRIENDS_LABELS))
friends_issues = repo.get_issues(labels=FRIENDS_LABELS)
if not FRIENDS_LABELS or not friends_issues:
return
Comment on lines +158 to +160
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

for issue in friends_issues:
for comment in issue.get_comments():
if is_hearted_by_me(comment, me):
Expand All @@ -161,16 +167,19 @@ def add_md_firends(repo, md, me):
print(str(e))
pass
with open(md, "a+", encoding="utf-8") as md:
md.write("## 友情链接\n")
md.write("\n## 友情链接\n\n")
md.write(s)
md.write("\n> 通过向以下 issues 评论的形式,将您的博客加入友链列表\n> \n")
for issue in friends_issues:
md.write(f"> [{issue.title}]({issue.html_url})\n> \n")
Comment on lines +170 to +174
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个很棒,谢谢



def add_md_recent(repo, md, me, limit=5):
count = 0
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")
md.write("\n## 最近更新\n\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

for issue in repo.get_issues():
if is_me(issue, me):
add_issue_info(issue, md)
Expand All @@ -185,38 +194,35 @@ def add_md_header(md, repo_name):
with open(md, "w", encoding="utf-8") as md:
md.write(MD_HEAD.format(repo_name=repo_name))

def add_md_footer(md):
with open(md, "a+", encoding="utf-8") as md:
md.write(MD_FOOT)

def add_md_label(repo, md, me):
labels = get_repo_labels(repo)

# sort lables by description info if it exists, otherwise sort by name,
# for example, we can let the description start with a number (1#Java, 2#Docker, 3#K8s, etc.)
labels = sorted(labels, key=lambda x: (x.description is None, x.description == "", x.description, x.name))

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 IGNORE_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)
Comment on lines 210 to 211
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得我之前的方式比较好,用 label 当 title


i = 0
for issue in issues:
if not issue:
continue
if is_me(issue, me):
if i == 0:
md.write("\n## " + label.name + "\n\n")
if i == ANCHOR_NUMBER:
md.write("<details><summary>显示更多</summary>\n")
md.write("\n")
md.write("\n<details><summary>显示更多</summary>\n\n")
add_issue_info(issue, md)
i += 1
if i > ANCHOR_NUMBER:
md.write("</details>\n")
md.write("\n")
md.write("\n</details>\n")


def get_to_generate_issues(repo, dir_name, issue_number=None):
Expand Down Expand Up @@ -269,6 +275,7 @@ def main(token, repo_name, issue_number=None, dir_name=BACKUP_DIR):
add_md_header("README.md", repo_name)
for func in [add_md_firends, add_md_top, add_md_recent, add_md_label, add_md_todo]:
func(repo, "README.md", me)
add_md_footer("README.md")

generate_rss_feed(repo, "feed.xml", me)
to_generate_issues = get_to_generate_issues(repo, dir_name, issue_number)
Expand All @@ -282,14 +289,15 @@ def save_issue(issue, me, dir_name=BACKUP_DIR):
md_name = os.path.join(
dir_name, f"{issue.number}_{issue.title.replace(' ', '.')}.md"
)
with open(md_name, "w") as f:
f.write(f"# [{issue.title}]({issue.html_url})\n\n")
f.write(issue.body)
with open(md_name, "w", encoding="utf-8") as f:
f.write(f"# [{issue.title}]({issue.html_url})\n")
if issue.body:
f.write(f"\n{issue.body}\n")
if issue.comments:
for c in issue.get_comments():
if is_me(c, me):
f.write("\n\n---\n\n")
f.write(c.body)
f.write("\n---\n\n> [{c.user.login}]({c.user.html_url})\n\n")
f.write(c.body)
f.write("\n")


if __name__ == "__main__":
Expand Down