-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] ocabot migration issue maintainer
New ocabot command for maintaining migration issues: ``/ocabot migration``, followed by the module name, performing the following: * Look for an issue in that repository with the name "Migration to version ``{version}``", where ``{version}`` is the name of the target branch. * Add or edit a line in that issue, linking the module to the pull request (PR) and the author of it. * TODO: When the PR is merged, the line gets ticked. * Put the milestone corresponding to the target branch in the PR. Co-authored-by: Pedro M. Baeza <pedro.baeza@tecnativa.com>
- Loading branch information
1 parent
9e7221d
commit 872e3b8
Showing
9 changed files
with
3,351 additions
and
1 deletion.
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Copyright 2019 Tecnativa - Pedro M. Baeza | ||
# Copyright 2021 Tecnativa - Víctor Martínez | ||
# Distributed under the MIT License (http://opensource.org/licenses/MIT). | ||
|
||
import re | ||
|
||
from .. import github | ||
from ..config import switchable | ||
from ..manifest import user_can_push | ||
from ..process import check_call | ||
from ..queue import getLogger, task | ||
|
||
_logger = getLogger(__name__) | ||
|
||
|
||
def _create_or_find_branch_milestone(repo, branch): | ||
for milestone in repo.milestones(): | ||
if milestone.title == branch: | ||
return milestone | ||
return repo.create_milestone(branch) | ||
|
||
|
||
def _find_issue(repo, milestone, target_branch): | ||
issue_title = f"Migration to version {target_branch}" | ||
issue = False | ||
for i in repo.issues(milestone=milestone.number): | ||
if i.title == issue_title: | ||
issue = i | ||
break | ||
return issue | ||
|
||
|
||
def _set_lines_issue(gh_pr, issue, module): | ||
lines = [] | ||
added = False | ||
module_list = False | ||
new_line = f"- [ ] {module} - By @{gh_pr.user.login} - #{gh_pr.number}" | ||
for line in issue.body.split("\n"): | ||
if line.startswith(f"- [ ] {module}"): | ||
lines.append(new_line) | ||
continue | ||
elif not added: | ||
splits = re.split(r"- \[ \] ([0-9a-zA-Z_]*)", line) | ||
if len(splits) >= 2: | ||
# Flag for detecting if we have passed already module list | ||
module_list = True | ||
line_module = splits[1] | ||
if line_module > module: | ||
lines.append(new_line) | ||
added = True | ||
elif module_list: | ||
lines.append(new_line) | ||
added = True | ||
lines.append(line) | ||
return lines | ||
|
||
|
||
@task() | ||
@switchable("migration_issue_bot") | ||
def migration_issue_start(org, repo, pr, username, module=None, dry_run=False): | ||
with github.login() as gh: | ||
gh_pr = gh.pull_request(org, repo, pr) | ||
target_branch = gh_pr.base.ref | ||
pr_branch = f"tmp-pr-{pr}" | ||
try: | ||
with github.temporary_clone(org, repo, target_branch) as clone_dir: | ||
# Create merge bot branch from PR and rebase it on target branch | ||
# This only serves for checking permissions | ||
check_call( | ||
["git", "fetch", "origin", f"pull/{pr}/head:{pr_branch}"], | ||
cwd=clone_dir, | ||
) | ||
check_call(["git", "checkout", pr_branch], cwd=clone_dir) | ||
if not user_can_push(gh, org, repo, username, clone_dir, target_branch): | ||
github.gh_call( | ||
gh_pr.create_comment, | ||
f"Sorry @{username} you are not allowed to mark the addon to" | ||
f"be migrated.\n\n" | ||
f"To do so you must either have push permissions on " | ||
f"the repository, or be a declared maintainer of all " | ||
f"modified addons.\n\n" | ||
f"If you wish to adopt an addon and become it's " | ||
f"[maintainer]" | ||
f"(https://odoo-community.org/page/maintainer-role), " | ||
f"open a pull request to add " | ||
f"your GitHub login to the `maintainers` key of its " | ||
f"manifest.", | ||
) | ||
return | ||
# Assign milestone to PR | ||
milestone = _create_or_find_branch_milestone(repo, target_branch) | ||
gh_pr.issue().edit(milestone=milestone.number) | ||
# Find issue | ||
issue = _find_issue(repo, milestone, target_branch) | ||
if not issue: | ||
issue_title = f"Migration to version {target_branch}" | ||
github.gh_call( | ||
gh_pr.create_comment, | ||
f"There's no issue in this repo with the title '{issue_title}' " | ||
f"and the milestone {target_branch}, so not possible to add " | ||
f"the comment.", | ||
) | ||
return | ||
# Change issue to add the PR in the module list | ||
lines = _set_lines_issue(gh_pr, issue, module) | ||
issue.edit(body="\n".join(lines)) | ||
except Exception as e: | ||
github.gh_call( | ||
gh_pr.create_comment, | ||
f"@{username} The migration issue commenter process could not " | ||
f"start, because of exception {e}.", | ||
) | ||
raise |
Oops, something went wrong.