Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangshangjin committed Jan 18, 2021
1 parent 1f8d5ba commit ac17aec
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 84 deletions.
23 changes: 8 additions & 15 deletions .github/workflows/generate_readme.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
name: Generate GitBlog README

on:
workflow_dispatch:
issues:
types: [opened, edited]
issue_comment:
types: [created, edited]
types: [opened, edited, deleted]
push:
branches:
- master
paths:
- main.py

env:
GITHUB_NAME: simplefeel
GITHUB_EMAIL: [email protected]

jobs:
sync:
name: Generate README
Expand Down Expand Up @@ -45,18 +38,18 @@ jobs:
pip install -r requirements.txt
if: steps.pip-cache.outputs.cache-hit != 'true'

- name: Generate new md # if you fork or clone this repo, please delete the curl line unless you know why
- name: Generate new md
run: |
echo ${{github.repository}}
source venv/bin/activate
python main.py ${{ secrets.Z_S_J }} ${{ github.repository }} --issue_numbe '${{ github.event.issue.number }}'
curl -H "Content-Type:application/json" -X POST -d '{"inputs": {}, "ref":"main"}' https://api.github.com/repos/yihong0618/2021/actions/workflows/4756004/dispatches -H "Authorization: token ${{ secrets.Z_S_J }}"
python main.py ${{ secrets.Z_S_J }} ${{github.repository}}
- name: Push README
uses: github-actions-x/[email protected]
with:
github-token: ${{ secrets.Z_S_J }}
commit-message: "Refresh README AND BACK UP"
files: README.md BACKUP/
commit-message: "Refresh README"
files: README.md
rebase: "true"
name: ${{ env.GITHUB_NAME }}
email: ${{ env.GITHUB_EMAIL }}
name: simplefeel
email: [email protected]
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
nohup.out
.git-credentials
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Manuel Martínez-Almeida

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
71 changes: 14 additions & 57 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# -*- coding: utf-8 -*-
import argparse
import os

from github import Github
from github.Issue import Issue
import argparse

MD_HEAD = """## Gitblog
My personal blog using issues and GitHub Actions
My personal blog using issues and GitHub Action
"""

BACKUP_DIR = "BACKUP"
ANCHOR_NUMBER = 5
TOP_ISSUES_LABELS = ["Top"]
TODO_ISSUES_LABELS = ["TODO"]
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

Expand All @@ -33,28 +34,22 @@ def login(token):
def get_repo(user: Github, repo: str):
return user.get_repo(repo)


def parse_TODO(issue):
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,
)
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()]

Expand All @@ -76,7 +71,7 @@ def add_md_todo(repo, md, me):
md.write("## TODO\n")
for issue in todo_issues:
if isMe(issue, me):
todo_title, todo_list = parse_TODO(issue)
todo_title, todo_list = parseTODO(issue)
md.write("TODO list from " + todo_title + "\n")
for t in todo_list:
md.write(t + "\n")
Expand Down Expand Up @@ -145,22 +140,7 @@ def add_md_label(repo, md, me):
md.write("\n")


def get_to_generate_issues(repo, dir_name, issue_number=None):
md_files = os.listdir(dir_name)
generated_issues_numbers = [
int(i.split("_")[0]) for i in md_files if i.split("_")[0].isdigit()
]
to_generate_issues = [
i
for i in list(repo.get_issues())
if int(i.number) not in generated_issues_numbers
]
if issue_number:
to_generate_issues.append(repo.get_issue(int(issue_number)))
return to_generate_issues


def main(token, repo_name, issue_number=None, dir_name=BACKUP_DIR):
def main(token, repo_name):
user = login(token)
me = get_me(user)
repo = get_repo(user, repo_name)
Expand All @@ -169,33 +149,10 @@ def main(token, repo_name, issue_number=None, dir_name=BACKUP_DIR):
for func in [add_md_top, add_md_recent, add_md_label, add_md_todo]:
func(repo, "README.md", me)

to_generate_issues = get_to_generate_issues(repo, dir_name, issue_number)

# save md files to backup folder
for issue in to_generate_issues:
save_issue(issue, me, dir_name)


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)
if issue.comments:
for c in issue.get_comments():
if isMe(c, me):
f.write("\n\n---\n\n")
f.write(c.body)


if __name__ == "__main__":
if not os.path.exists(BACKUP_DIR):
os.mkdir(BACKUP_DIR)
parser = argparse.ArgumentParser()
parser.add_argument("github_token", help="github_token")
parser.add_argument("repo_name", help="repo_name")
parser.add_argument("--issue_number", help="issue_number", default=None, required=False)
options = parser.parse_args()
main(options.github_token, options.repo_name, options.issue_number)
main(options.github_token, options.repo_name)
11 changes: 0 additions & 11 deletions package.json

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PyGithub
PyGithub

0 comments on commit ac17aec

Please sign in to comment.