Skip to content

Commit 30a3564

Browse files
authored
Fix create issue script (#2876)
Algunas mejoras para el script que genera issues. Lo probé en mi fork y me generó por ejemplo este issue: sofide#2
1 parent 05975a4 commit 30a3564

File tree

1 file changed

+137
-41
lines changed

1 file changed

+137
-41
lines changed

scripts/create_issue.py

Lines changed: 137 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,159 @@
1-
# Use together with `pageviews.py`
2-
# python scripts/pageviews.py | head -n 150 | grep -v whats | cut -d ' ' -f 2 | sed 's/\.html/\.po/g' | xargs -I '{}' python scripts/create_issue.py '{}'
1+
"""
2+
Run this script with one variable:
3+
- PO filename to create an issue for that file
4+
- or '--all' to create the issues for all untranslated files that doesn't have an open issue already
5+
- or '--one' to create the next one issue
6+
"""
37

48
import os
59
import sys
10+
from glob import glob
611
from pathlib import Path
712

813
from github import Github
914
from potodo.potodo import PoFileStats
1015

11-
if len(sys.argv) != 2:
12-
print('Specify PO filename')
13-
sys.exit(1)
16+
PYTHON_VERSION = "3.13"
17+
PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE = 5
18+
GOOD_FIRST_ISSUE_LABEL = "good first issue"
19+
ISSUE_LABELS = [PYTHON_VERSION]
20+
ISSUE_TITLE = 'Translate `{pofilename}`'
21+
ISSUE_BODY = '''This file is {translated_percent}% translated and needs to reach 100%.
1422
15-
pofilename = sys.argv[1]
16-
pofile = PoFileStats(Path(pofilename))
23+
The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated.
24+
Meanwhile, the English version is shown.
1725
18-
g = Github(os.environ.get('GITHUB_TOKEN'))
26+
Current stats for `{pofilename}`:
1927
20-
repo = g.get_repo('python/python-docs-es')
28+
- Total entries: {pofile_entries}
2129
30+
- Entries that need work: {pending_entries} - ({pending_percent}%)
31+
- Fuzzy: {pofile_fuzzy}
32+
- Untranslated: {pofile_untranslated}
2233
23-
issues = repo.get_issues(state='all')
24-
for issue in issues:
25-
if pofilename in issue.title:
34+
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.
2635
27-
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
28-
sys.exit(1)
36+
Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).'''
2937

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)
3438

35-
if pofile.fuzzy == 0 and any([
36-
pofile.translated_nb == pofile.po_file_size,
37-
pofile.untranslated_nb == 0,
38-
]):
39-
print(f'Skipping {pofilename}. The file is 100% translated already.')
40-
sys.exit(1)
39+
class IssueAlreadyExistingError(Exception):
40+
"""Issue already existing in GitHub"""
4141

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.
4842

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.
43+
class PoFileAlreadyTranslated(Exception):
44+
"""Given PO file is already 100% translated"""
5145

52-
Current stats for `{pofilename}`:
5346

54-
- Fuzzy: {pofile.fuzzy_nb}
55-
- Percent translated: {pofile.percent_translated}%
56-
- Entries: {pofile.translated_nb} / {pofile.po_file_size}
57-
- Untranslated: {pofile.untranslated_nb}
47+
class GitHubIssueGenerator:
48+
def __init__(self):
49+
g = Github(os.environ.get('GITHUB_TOKEN'))
50+
self.repo = g.get_repo('python/python-docs-es')
51+
self._issues = None
5852

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.
53+
@property
54+
def issues(self):
55+
if self._issues is None:
56+
self._issues = self.repo.get_issues(state='open')
57+
58+
return self._issues
59+
60+
def check_issue_not_already_existing(self, pofilename):
61+
for issue in self.issues:
62+
if pofilename in issue.title:
63+
64+
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}')
65+
raise IssueAlreadyExistingError
66+
67+
68+
@staticmethod
69+
def check_translation_is_pending(pofile):
70+
no_fuzzy_translations = pofile.fuzzy == 0
71+
translated_match_all_entries = pofile.translated == pofile.entries
72+
no_untranslated_entries_left = pofile.untranslated == 0
73+
74+
if no_fuzzy_translations and (translated_match_all_entries or no_untranslated_entries_left):
75+
print(f'Skipping {pofile.filename}. The file is 100% translated already.')
76+
raise PoFileAlreadyTranslated
77+
78+
79+
80+
def issue_generator(self, pofilename):
81+
pofile = PoFileStats(Path(pofilename))
82+
83+
self.check_issue_not_already_existing(pofilename)
84+
self.check_translation_is_pending(pofile)
85+
86+
pending_entries = pofile.fuzzy + pofile.untranslated
87+
88+
if pending_entries <= PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE:
89+
labels = ISSUE_LABELS + [GOOD_FIRST_ISSUE_LABEL]
90+
else:
91+
labels = ISSUE_LABELS
92+
93+
urlfile = pofilename.replace('.po', '.html')
94+
title = ISSUE_TITLE.format(pofilename=pofilename)
95+
body = ISSUE_BODY.format(
96+
translated_percent=pofile.percent_translated,
97+
python_version=PYTHON_VERSION,
98+
urlfile=urlfile,
99+
pofilename=pofilename,
100+
pofile_fuzzy=pofile.fuzzy,
101+
pending_percent=100 - pofile.percent_translated,
102+
pofile_entries=pofile.entries,
103+
pofile_untranslated=pofile.untranslated,
104+
pending_entries=pending_entries,
105+
)
106+
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue
107+
issue = self.repo.create_issue(title=title, body=body, labels=labels)
108+
109+
return issue
110+
111+
def create_issues(self, only_one=False):
112+
po_files = glob("**/*.po")
113+
existing_issue_counter = 0
114+
already_translated_counter = 0
115+
created_issues_counter = 0
116+
117+
print(f"TOTAL PO FILES: {len(po_files)}")
118+
119+
for pofilename in po_files:
120+
try:
121+
issue = self.issue_generator(pofilename)
122+
created_issues_counter += 1
123+
print(f'Issue "{issue.title}" created at {issue.html_url}')
124+
if only_one:
125+
break
126+
except IssueAlreadyExistingError:
127+
existing_issue_counter += 1
128+
except PoFileAlreadyTranslated:
129+
already_translated_counter += 1
130+
131+
print("Stats:")
132+
print(f"- Existing issues: {existing_issue_counter}")
133+
print(f"- Already translated files: {already_translated_counter}")
134+
print(f"- Created issues: {created_issues_counter}")
135+
136+
137+
def main():
138+
error_msg = "Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue"
139+
if len(sys.argv) != 2:
140+
raise Exception(error_msg)
141+
142+
arg = sys.argv[1]
143+
144+
gh = GitHubIssueGenerator()
145+
146+
if arg == "--all":
147+
gh.create_issues()
148+
149+
elif arg == "--one":
150+
gh.create_issues(only_one=True)
151+
152+
else:
153+
try:
154+
gh.issue_generator(arg)
155+
except FileNotFoundError:
156+
raise Exception(error_msg)
60157

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}')
158+
if __name__ == "__main__":
159+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy