-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix create issue script #2876
Merged
Merged
Fix create issue script #2876
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ca3596
update some vars for new github api
sofide 099213c
refactor create issue
sofide d3baf33
add docstring
sofide 6916e38
refactor to cache issues
sofide 1915095
improve issue body
sofide 9461867
improve readability in check_translation_is_pending
sofide 9938321
fixes in body issue
sofide 2783511
Merge branch '3.12' into fix_create_issue_script
sofide 4a79e4e
fix first issue label
sofide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,159 @@ | ||
# Use together with `pageviews.py` | ||
# 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 '{}' | ||
""" | ||
Run this script with one variable: | ||
- PO filename to create an issue for that file | ||
- or '--all' to create the issues for all untranslated files that doesn't have an open issue already | ||
- or '--one' to create the next one issue | ||
""" | ||
|
||
import os | ||
import sys | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
from github import Github | ||
from potodo.potodo import PoFileStats | ||
|
||
if len(sys.argv) != 2: | ||
print('Specify PO filename') | ||
sys.exit(1) | ||
PYTHON_VERSION = "3.13" | ||
PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE = 5 | ||
GOOD_FIRST_ISSUE_LABEL = "good first issue" | ||
ISSUE_LABELS = [PYTHON_VERSION] | ||
ISSUE_TITLE = 'Translate `{pofilename}`' | ||
ISSUE_BODY = '''This file is {translated_percent}% translated and needs to reach 100%. | ||
|
||
pofilename = sys.argv[1] | ||
pofile = PoFileStats(Path(pofilename)) | ||
The rendered version of this file will be available at https://docs.python.org/es/{python_version}/{urlfile} once translated. | ||
Meanwhile, the English version is shown. | ||
|
||
g = Github(os.environ.get('GITHUB_TOKEN')) | ||
Current stats for `{pofilename}`: | ||
|
||
repo = g.get_repo('python/python-docs-es') | ||
- Total entries: {pofile_entries} | ||
|
||
- Entries that need work: {pending_entries} - ({pending_percent}%) | ||
- Fuzzy: {pofile_fuzzy} | ||
- Untranslated: {pofile_untranslated} | ||
|
||
issues = repo.get_issues(state='all') | ||
for issue in issues: | ||
if pofilename in issue.title: | ||
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. | ||
|
||
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') | ||
sys.exit(1) | ||
Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''' | ||
|
||
msg = f'There is a similar issue already created at {issue.html_url}.\nDo you want to create it anyways? [y/N] ' | ||
answer = input(msg) | ||
if answer != 'y': | ||
sys.exit(1) | ||
|
||
if pofile.fuzzy == 0 and any([ | ||
pofile.translated_nb == pofile.po_file_size, | ||
pofile.untranslated_nb == 0, | ||
]): | ||
print(f'Skipping {pofilename}. The file is 100% translated already.') | ||
sys.exit(1) | ||
class IssueAlreadyExistingError(Exception): | ||
"""Issue already existing in GitHub""" | ||
|
||
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue | ||
title = f'Translate `{pofilename}`' | ||
urlfile = pofilename.replace('.po', '.html') | ||
issue = repo.create_issue( | ||
title=title, | ||
body=f'''This needs to reach 100% translated. | ||
|
||
The rendered version of this file will be available at https://docs.python.org/es/3.8/{urlfile} once translated. | ||
Meanwhile, the English version is shown. | ||
class PoFileAlreadyTranslated(Exception): | ||
"""Given PO file is already 100% translated""" | ||
|
||
Current stats for `{pofilename}`: | ||
|
||
- Fuzzy: {pofile.fuzzy_nb} | ||
- Percent translated: {pofile.percent_translated}% | ||
- Entries: {pofile.translated_nb} / {pofile.po_file_size} | ||
- Untranslated: {pofile.untranslated_nb} | ||
class GitHubIssueGenerator: | ||
def __init__(self): | ||
g = Github(os.environ.get('GITHUB_TOKEN')) | ||
self.repo = g.get_repo('python/python-docs-es') | ||
self._issues = None | ||
|
||
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. | ||
@property | ||
def issues(self): | ||
if self._issues is None: | ||
self._issues = self.repo.get_issues(state='open') | ||
|
||
return self._issues | ||
|
||
def check_issue_not_already_existing(self, pofilename): | ||
for issue in self.issues: | ||
if pofilename in issue.title: | ||
|
||
print(f'Skipping {pofilename}. There is a similar issue already created at {issue.html_url}') | ||
raise IssueAlreadyExistingError | ||
|
||
|
||
@staticmethod | ||
def check_translation_is_pending(pofile): | ||
no_fuzzy_translations = pofile.fuzzy == 0 | ||
translated_match_all_entries = pofile.translated == pofile.entries | ||
no_untranslated_entries_left = pofile.untranslated == 0 | ||
|
||
if no_fuzzy_translations and (translated_match_all_entries or no_untranslated_entries_left): | ||
print(f'Skipping {pofile.filename}. The file is 100% translated already.') | ||
raise PoFileAlreadyTranslated | ||
|
||
|
||
|
||
def issue_generator(self, pofilename): | ||
pofile = PoFileStats(Path(pofilename)) | ||
|
||
self.check_issue_not_already_existing(pofilename) | ||
self.check_translation_is_pending(pofile) | ||
|
||
pending_entries = pofile.fuzzy + pofile.untranslated | ||
|
||
if pending_entries <= PENDING_ENTRIES_FOR_GOOD_FIRST_ISSUE: | ||
labels = ISSUE_LABELS + [GOOD_FIRST_ISSUE_LABEL] | ||
else: | ||
labels = ISSUE_LABELS | ||
|
||
urlfile = pofilename.replace('.po', '.html') | ||
title = ISSUE_TITLE.format(pofilename=pofilename) | ||
body = ISSUE_BODY.format( | ||
translated_percent=pofile.percent_translated, | ||
python_version=PYTHON_VERSION, | ||
urlfile=urlfile, | ||
pofilename=pofilename, | ||
pofile_fuzzy=pofile.fuzzy, | ||
pending_percent=100 - pofile.percent_translated, | ||
pofile_entries=pofile.entries, | ||
pofile_untranslated=pofile.untranslated, | ||
pending_entries=pending_entries, | ||
) | ||
# https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.create_issue | ||
issue = self.repo.create_issue(title=title, body=body, labels=labels) | ||
|
||
return issue | ||
|
||
def create_issues(self, only_one=False): | ||
po_files = glob("**/*.po") | ||
existing_issue_counter = 0 | ||
already_translated_counter = 0 | ||
created_issues_counter = 0 | ||
|
||
print(f"TOTAL PO FILES: {len(po_files)}") | ||
|
||
for pofilename in po_files: | ||
try: | ||
issue = self.issue_generator(pofilename) | ||
created_issues_counter += 1 | ||
print(f'Issue "{issue.title}" created at {issue.html_url}') | ||
if only_one: | ||
break | ||
except IssueAlreadyExistingError: | ||
existing_issue_counter += 1 | ||
except PoFileAlreadyTranslated: | ||
already_translated_counter += 1 | ||
|
||
print("Stats:") | ||
print(f"- Existing issues: {existing_issue_counter}") | ||
print(f"- Already translated files: {already_translated_counter}") | ||
print(f"- Created issues: {created_issues_counter}") | ||
|
||
|
||
def main(): | ||
error_msg = "Specify PO filename or '--all' to create all the issues, or '--one' to create the next one issue" | ||
if len(sys.argv) != 2: | ||
raise Exception(error_msg) | ||
|
||
arg = sys.argv[1] | ||
|
||
gh = GitHubIssueGenerator() | ||
|
||
if arg == "--all": | ||
gh.create_issues() | ||
|
||
elif arg == "--one": | ||
gh.create_issues(only_one=True) | ||
|
||
else: | ||
try: | ||
gh.issue_generator(arg) | ||
except FileNotFoundError: | ||
raise Exception(error_msg) | ||
|
||
Remember to follow the steps in our [Contributing Guide](https://python-docs-es.readthedocs.io/page/CONTRIBUTING.html).''', | ||
) | ||
print(f'Issue "{title}" created at {issue.html_url}') | ||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Una idea que tenía era agregar un modo "dry run", pero queda para el futuro, necesitaría más cambios de los que hay hasta el momento
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
de acuerdo con la idea, y con que se haga a futuro 😅