8000 refactor create issue · python/python-docs-es@099213c · GitHub
[go: up one dir, main page]

Skip to content

Commit 099213c

Browse files
committed
refactor create issue
1 parent 6ca3596 commit 099213c

File tree

1 file changed

+110
-39
lines changed

1 file changed

+110
-39
lines changed

scripts/create_issue.py

Lines changed: 110 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,132 @@
33

44
import os
55
import sys
6+
from glob import glob
67
from pathlib import Path
78

89
from github import Github
910
from potodo.potodo import PoFileStats
1011

11-
if len(sys.argv) != 2:
12-
print('Specify PO filename')
13-
sys.exit(1)
14-
15-
pofilename = sys.argv[1]
16-
pofile = PoFileStats(Path(pofilename))
1712

1813
g = Github(os.environ.get('GITHUB_TOKEN'))
19-
2014
repo = g.get_repo('python/python-docs-es')
2115

16+
PYTHON_VERSION = "3.13"
17+
ISSUE_LABELS = [PYTHON_VERSION, "good first issue"]
18+
ISSUE_TITLE = 'Translate `{pofilename}`'
19+
ISSUE_BODY = '''This needs to reach 100% translated.
2220
23-
issues = repo.get_issues(state='all')
24-
for issue in issues:
25-
if pofilename in issue.title:
21+
The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated.
22+
Meanwhile, the English version is shown.
2623
27-
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
28-
sys.exit(1)
24+
Current stats for `{pofilename}`:
2925
30-
msg = f'There is a similar issue already created at {issue.html_url}.\nDo you want to create it anyways? [y/N] '
31-
answer = input(msg)
32-
if answer != 'y':
33-
sys.exit(1)
26+
- Fuzzy: {pofile_fuzzy}
27+
- Percent translated: {pofile_percent_translated}%
28+
- Entries: {pofile_entries}
29+
- Untranslated: {pofile_untranslated}
3430
35-
if pofile.fuzzy == 0 and any([
36-
pofile.translated == pofile.entries,
37-
pofile.untranslated == 0,
38-
]):
39-
print(f'Skipping {pofilename}. The file is 100% translated already.')
40-
sys.exit(1)
31+
Please, comment here if you want this file to be assigned to you and a member will assign it to you as soon as possible, so you can start working on it.
4132
42-
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue
43-
title = f'Translate `{pofilename}`'
44-
urlfile = pofilename.replace('.po', '.html')
45-
issue = repo.create_issue(
46-
title=title,
47-
body=f'''This needs to reach 100% translated.
33+
Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).'''
4834

49-
The rendered version of this file will be available at https://docs.python.org/es/3.8/{urlfile} once translated.
50-
Meanwhile, the English version is shown.
5135

52-
Current stats for `{pofilename}`:
36+
class IssueAlreadyExistingError(Exception):
37+
"""Issue already existing in GitHub"""
5338

54-
- Fuzzy: {pofile.fuzzy}
55-
- Percent translated: {pofile.percent_translated}%
56-
- Entries: {pofile.entries}
57-
- Untranslated: {pofile.untranslated}
5839

59-
Please, comment here if you want this file to be assigned to you and a member will assign it to you as soon as possible, so you can start working on it.
40+
class PoFileAlreadyTranslated(Exception):
41+
"""Given PO file is already 100% translated"""
42+
43+
44+
45+
def check_issue_not_already_existing(pofilename):
46+
issues = repo.get_issues(state='open')
47+
for issue in issues:
48+
if pofilename in issue.title:
49+
50+
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
51+
raise IssueAlreadyExistingError()
52+
53+
54+
def check_translation_is_pending(pofile):
55+
if pofile.fuzzy == 0 and any([
56+
pofile.translated == pofile.entries,
57+
pofile.untranslated == 0,
58+
]):
59+
print(f'Skipping {pofile.filename}. The file is 100% translated already.')
60+
raise PoFileAlreadyTranslated()
61+
62+
63+
64+
def issue_generator(pofilename):
65+
pofile = PoFileStats(Path(pofilename))
66+
67+
check_issue_not_already_existing(pofilename)
68+
check_translation_is_pending(pofile)
69+
70+
urlfile = pofilename.replace('.po', '.html')
71+
title = ISSUE_TITLE.format(pofilename=pofilename)
72+
body = ISSUE_BODY.format(
73+
python_version=PYTHON_VERSION,
74+
urlfile=urlfile,
75+
pofilename=pofilename,
76+
pofile_fuzzy=pofile.fuzzy,
77+
pofile_percent_translated=pofile.percent_translated,
78+
pofile_entries=pofile.entries,
79+
pofile_untranslated=pofile.untranslated,
80+
)
81+
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue
82+
issue = repo.create_issue(title=title, body=body, labels=ISSUE_LABELS)
83+
84+
return issue
85+
86+
def create_issues(only_one=False):
87+
po_files = glob("**/*.po")
88+
existing_issue_counter = 0
89+
already_translated_counter = 0
90+
created_issues_counter = 0
91+
92+
print(f"TOTAL PO FILES: {len(po_files)}")
93+
94+
for pofilename in po_files:
95+
try:
96+
issue = issue_generator(pofilename)
97+
created_issues_counter += 1
98+
print(f'Issue "{issue.title}" created at {issue.html_url}')
99+
if only_one:
100+
break
101+
except IssueAlreadyExistingError:
102+
existing_issue_counter += 1
103+
except PoFileAlreadyTranslated:
104+
already_translated_counter += 1
105+
106+
print("Stats:")
107+
print(f"- Existing issues: {existing_issue_counter}")
108+
print(f"- Already translated files: {already_translated_counter}")
109+
print(f"- Created issues: {created_issues_counter}")
110+
111+
112+
113+
114+
def main():
115+
error_msg = "Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue"
116+
if len(sys.argv) != 2:
117+
raise Exception(error_msg)
118+
119+
arg = sys.argv[1]
120+
121+
if arg == "--all":
122+
create_issues()
123+
124+
elif arg == "--one":
125+
create_issues(only_one=True)
126+
127+
else:
128+
try:
129+
issue_generator(arg)
130+
except FileNotFoundError:
131+
raise Exception(error_msg)
60132

61-
Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''',
62-
)
63-
print(f'Issue "{title}" created at {issue.html_url}')
133+
if __name__ == "__main__":
134+
main()

0 commit comments

Comments
 (0)
0