Skip to content

Commit df96dad

Browse files
authored
actions를 위한 코드 추가 (#19)
* actions를 위한 코드 추가 * 불필요한 공백 라인 제거 * requirements 추가
1 parent d904391 commit df96dad

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

script/update_original.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import os
2+
import sys
3+
import requests
4+
import urllib.request
5+
from bs4 import BeautifulSoup
6+
from github import Github, InputGitAuthor
7+
8+
MASTER_BRANCH = "master"
9+
MASTER_REF = "refs/heads/master"
10+
ORIGINAL_MARKDOWN_PATH = "Original.md"
11+
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
12+
REPO_NAME = "Yosseulsin-JOB/Google-Python-Style-Guide-kor"
13+
COMMIT_ID_CLASSNAME = ".text-small.text-mono.link-gray"
14+
MARDOWN_URL = "https://github.com/google/styleguide/blob/gh-pages/pyguide.md"
15+
RAW_MARKDOWN_URL = "https://raw.githubusercontent.com/google/styleguide/gh-pages/pyguide.md"
16+
17+
def create_ref_name(commit):
18+
return 'refs/heads/original/' + commit['label']
19+
20+
def create_branch_name(commit):
21+
return 'original/' + commit['label']
22+
23+
def create_commit_title(commit):
24+
return 'merge ' + commit['label'] + ' 🛰'
25+
26+
def create_pr_title(commit):
27+
return '['+commit['label']+'] 변역 요청 💬'
28+
29+
def create_pr_body():
30+
return '''
31+
새로운 번역이 왔습니다. 번역해주세요! 💤
32+
'''
33+
34+
# 항상 루트에서 python ./script/update_original.py 로 실행해야 루트에 있는 Original.md 파일로 대체됩니다.
35+
def download_from_google_style_guide_original():
36+
urllib.request.urlretrieve(RAW_MARKDOWN_URL, ORIGINAL_MARKDOWN_PATH)
37+
print("저장완료")
38+
39+
# commit 이름과 링크를 가져옵니다.
40+
def get_commit_from_google_style_guide_original():
41+
markdown = requests.get(MARDOWN_URL).text
42+
soup = BeautifulSoup(markdown, 'html.parser')
43+
commit = soup.select(COMMIT_ID_CLASSNAME)[0]
44+
commit_label = commit.text
45+
commit_url = 'https://github.com' + commit.attrs['href']
46+
47+
print(commit_label, commit_url)
48+
49+
return { "label":commit_label,'url': commit_url }
50+
51+
def get_repo():
52+
return Github(ACCESS_TOKEN).get_repo(REPO_NAME)
53+
54+
def create_branch(repo, commit):
55+
ref = create_ref_name(commit)
56+
branch = repo.get_branch(branch=MASTER_BRANCH)
57+
58+
try:
59+
repo.create_git_ref(ref=ref, sha=branch.commit.sha)
60+
except:
61+
return False
62+
return True
63+
64+
def create_commit(repo, commit):
65+
branch = create_branch_name(commit)
66+
title = create_commit_title(commit)
67+
text = open(ORIGINAL_MARKDOWN_PATH, "r").read()
68+
contents = repo.get_contents(ORIGINAL_MARKDOWN_PATH, ref=MASTER_REF)
69+
70+
repo.update_file(contents.path, title, text, contents.sha, branch=branch)
71+
72+
def create_pull_request(repo, commit):
73+
body = create_pr_body()
74+
head = create_branch_name(commit)
75+
title = create_pr_title(commit)
76+
77+
repo.create_pull(title=title, body=body, head=head, base=MASTER_BRANCH)
78+
79+
80+
if __name__ == "__main__":
81+
82+
# 스타일 가이드를 최신 버전을 가져옵니다.
83+
download_from_google_style_guide_original()
84+
85+
# 최신 버전의 commit id와 주소를 가져옵니다.
86+
commit = get_commit_from_google_style_guide_original()
87+
88+
# 스타일 가이드 한글판 레포를 가져옵니다.
89+
repo = get_repo()
90+
91+
# 브랜치를 생성합니다.
92+
branch = create_branch(repo, commit)
93+
94+
# 브랜치를 생성할 수 없는 상태라면 이미 생성된 것으로 더이상의 작업을 진행하지 않습니다.
95+
if branch is False:
96+
print('이미 생성된 작업 요청과 동일한 Commit 입니다. 🚨')
97+
sys.exit(0)
98+
99+
# 업데이트 된 파일을 commit합니다.
100+
create_commit(repo, commit)
101+
102+
# Pull Request를 생성합니다.
103+
create_pull_request(repo, commit)
104+
105+
print("요청 완료했습니다.")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
beautifulsoup4==4.7.1
2+
requests==2.22.0
3+
PyGithub==1.51

0 commit comments

Comments
 (0)