-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgit-issues-blog.py
164 lines (137 loc) · 5.42 KB
/
git-issues-blog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# Author: Sep0lkit
# Github: https://github.com/Sep0lkit/git-issues-blo
import os
import re
import json
import pathlib
import requests
import subprocess
import urllib.parse
from github import Github
from github import GithubException
from github import UnknownObjectException
GITHUB_API = "https://api.github.com"
GITHUB_ACTION_NAME = os.environ['GITHUB_ACTION']
# Get environment variables
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
GITHUB_REPO = os.environ['GITHUB_REPOSITORY']
GITHUB_USER = GITHUB_REPO.split('/')[0]
GITHUB_BRANCH = os.getenv('GITHUB_BRANCH', 'master')
POSTS_PATH = os.getenv('POSTS_PATH', 'posts')
POST_INDEX_FILE = os.getenv('POST_INDEX_FILE', '_index')
# Global variables
POSTS = []
CHANGED = []
# github object
g = Github(GITHUB_TOKEN)
repo = g.get_repo(GITHUB_REPO)
# local dictionary
dictionary = {}
index = pathlib.Path(POST_INDEX_FILE)
if index.exists():
try:
with open(POST_INDEX_FILE, encoding='utf-8', mode = 'r') as f:
dictionary = json.load(f)
lastcommit = dictionary['__commit__']
command = "git diff --name-only -z " + lastcommit
changed = subprocess.check_output(['git', 'diff', '--name-only', '-z', lastcommit])
for x in changed.split(b'\x00'):
if x.decode('utf-8'):
CHANGED.append(x.decode('utf-8'))
f.close()
except Exception as e:
print('%s load error: %s' % (POST_INDEX_FILE, e))
exit(-1)
p = pathlib.Path(POSTS_PATH)
for f in p.rglob('*.md'):
if len(CHANGED) != 0:
if (f.as_posix() in CHANGED):
print("post %s need update" % f.as_posix())
POSTS.append(f)
else:
POSTS.append(f)
print("posts need update: ")
print(POSTS)
header = pathlib.Path('_tpl/post-header.md')
if header.exists():
issue_header = header.read_text()
else:
issue_header = ""
footer = pathlib.Path('_tpl/post-footer.md')
if footer.exists():
issue_footer = footer.read_text()
else:
issue_footer = "\n\nPowered by [Git-Issues-Blog](https://github.com/marketplace/actions/git-issues-blog)"
# issues tpl variables function
def parse_issue_tpl(content, user, path):
#GITHUB_POSTS_USER
content = re.sub(r'{{\s?GITHUB_POSTS_USER\s?}}', user , content, flags=re.M)
#GITHUB_POSTS_FILENAME
postname = path.rsplit('/',1)[1]
content = re.sub(r'{{\s?GITHUB_POSTS_NAME\s?}}', postname , content, flags=re.M)
#GITHUB_POSTS_URL
url = "https://github.com/{}/blob/{}/{}".format(GITHUB_REPO, GITHUB_BRANCH, urllib.parse.quote(path))
content = re.sub(r'{{\s?GITHUB_POSTS_URL\s?}}', url , content, flags=re.M)
return content
for p in POSTS:
# issue content templat)e
with open(p, encoding='utf-8', mode = 'r') as f:
issue_body = f.read()
f.close()
# relative link to raw.github link
re_format = "![\\1](https://raw.githubusercontent.com/{}/{}/{}/\\2)".format(GITHUB_REPO, GITHUB_BRANCH, p.parent.as_posix())
issue_body_with_giturl = re.sub(r'!\[(.*)\]\((?!http)(.*)\)', re_format, issue_body, flags = re.M)
# template variables in header and footer
issue_header_with_tpl = parse_issue_tpl(issue_header, GITHUB_USER, p.as_posix())
issue_footer_with_tpl = parse_issue_tpl(issue_footer, GITHUB_USER, p.as_posix())
issue_content = issue_header_with_tpl + issue_body_with_giturl + issue_footer_with_tpl
# check file exist issue or not by title(POSTS_PATH)
pstr = p.as_posix()
if pstr in dictionary:
# get issue info
issue_number = dictionary[pstr]
try:
issue = repo.get_issue(number=issue_number)
issue_url = issue.html_url
issue_title = issue.title
# content
payload = {}
payload['title'] = issue_title
payload['body'] = issue_content
# github edit issue api
header = {'Authorization': 'token %s' % GITHUB_TOKEN}
url = GITHUB_API + "/repos/" + GITHUB_REPO + "/issues/" + str(issue_number)
r = requests.patch(url, headers=header, data=json.dumps(payload))
if r.status_code == 200:
print("issue update successful: %s" % issue_number)
else:
print("issue update failed: %s" % issue_number)
exit(-1)
except GithubException as e:
print('get issues: %s error, skip for next' % issue_number)
else:
#creat issue
title = p.name
title = re.sub('.md$', '', title, flags=re.IGNORECASE)
issue = repo.create_issue(title, issue_content)
print("issue create successfule: %s" % issue.number)
dictionary[pstr] = issue.number
commit = os.environ['GITHUB_SHA']
print("update posts to commit id: %s" % commit)
if re.search("^\w{40}$", commit):
dictionary['__commit__'] = commit
else:
print('last commit id got error')
exit(-1)
#write posts index and commit step
try:
repo.get_contents(POST_INDEX_FILE,ref=GITHUB_BRANCH)
except UnknownObjectException:
repo.create_file(POST_INDEX_FILE, "add post index: _index", "{}", branch=GITHUB_BRANCH)
#update POST_INDEX_FILE
post_index_file = repo.get_contents(POST_INDEX_FILE,ref=GITHUB_BRANCH)
post_index_content = json.dumps(dictionary, ensure_ascii=False)
post_index_msg = "rebuild posts index from: " + GITHUB_ACTION_NAME
repo.update_file(post_index_file.path, post_index_msg, post_index_content, post_index_file.sha, branch=GITHUB_BRANCH)
print("posts update successful")