|
| 1 | +# Copyright 2019 Tecnativa - Pedro M. Baeza |
| 2 | +# Copyright 2021 Tecnativa - Víctor Martínez |
| 3 | +# Distributed under the MIT License (http://opensource.org/licenses/MIT). |
| 4 | + |
| 5 | +import re |
| 6 | + |
| 7 | +from .. import github |
| 8 | +from ..config import switchable |
| 9 | +from ..manifest import user_can_push |
| 10 | +from ..process import check_call |
| 11 | +from ..queue import getLogger, task |
| 12 | + |
| 13 | +_logger = getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def _create_or_find_branch_milestone(repo, branch): |
| 17 | + for milestone in repo.milestones(): |
| 18 | + if milestone.title == branch: |
| 19 | + return milestone |
| 20 | + return repo.create_milestone(branch) |
| 21 | + |
| 22 | + |
| 23 | +def _find_issue(repo, milestone, target_branch): |
| 24 | + issue_title = f"Migration to version {target_branch}" |
| 25 | + issue = False |
| 26 | + for i in repo.issues(milestone=milestone.number): |
| 27 | + if i.title == issue_title: |
| 28 | + issue = i |
| 29 | + break |
| 30 | + return issue |
| 31 | + |
| 32 | + |
| 33 | +def _set_lines_issue(gh_pr, issue, module): |
| 34 | + lines = [] |
| 35 | + added = False |
| 36 | + module_list = False |
| 37 | + new_line = f"- [ ] {module} - By @{gh_pr.user.login} - #{gh_pr.number}" |
| 38 | + for line in issue.body.split("\n"): |
| 39 | + if line.startswith(f"- [ ] {module}"): |
| 40 | + lines.append(new_line) |
| 41 | + continue |
| 42 | + elif not added: |
| 43 | + splits = re.split(r"- \[ \] ([0-9a-zA-Z_]*)", line) |
| 44 | + if len(splits) >= 2: |
| 45 | + # Flag for detecting if we have passed already module list |
| 46 | + module_list = True |
| 47 | + line_module = splits[1] |
| 48 | + if line_module > module: |
| 49 | + lines.append(new_line) |
| 50 | + added = True |
| 51 | + elif module_list: |
| 52 | + lines.append(new_line) |
| 53 | + added = True |
| 54 | + lines.append(line) |
| 55 | + return lines |
| 56 | + |
| 57 | + |
| 58 | +@task() |
| 59 | +@switchable("migration_issue_bot") |
| 60 | +def migration_issue_start(org, repo, pr, username, module=None, dry_run=False): |
| 61 | + with github.login() as gh: |
| 62 | + gh_pr = gh.pull_request(org, repo, pr) |
| 63 | + target_branch = gh_pr.base.ref |
| 64 | + pr_branch = f"tmp-pr-{pr}" |
| 65 | + try: |
| 66 | + with github.temporary_clone(org, repo, target_branch) as clone_dir: |
| 67 | + # Create merge bot branch from PR and rebase it on target branch |
| 68 | + # This only serves for checking permissions |
| 69 | + check_call( |
| 70 | + ["git", "fetch", "origin", f"pull/{pr}/head:{pr_branch}"], |
| 71 | + cwd=clone_dir, |
| 72 | + ) |
| 73 | + check_call(["git", "checkout", pr_branch], cwd=clone_dir) |
| 74 | + if not user_can_push(gh, org, repo, username, clone_dir, target_branch): |
| 75 | + github.gh_call( |
| 76 | + gh_pr.create_comment, |
| 77 | + f"Sorry @{username} you are not allowed to mark the addon to" |
| 78 | + f"be migrated.\n\n" |
| 79 | + f"To do so you must either have push permissions on " |
| 80 | + f"the repository, or be a declared maintainer of all " |
| 81 | + f"modified addons.\n\n" |
| 82 | + f"If you wish to adopt an addon and become it's " |
| 83 | + f"[maintainer]" |
| 84 | + f"(https://odoo-community.org/page/maintainer-role), " |
| 85 | + f"open a pull request to add " |
| 86 | + f"your GitHub login to the `maintainers` key of its " |
| 87 | + f"manifest.", |
| 88 | + ) |
| 89 | + return |
| 90 | + # Assign milestone to PR |
| 91 | + milestone = _create_or_find_branch_milestone(repo, target_branch) |
| 92 | + gh_pr.issue().edit(milestone=milestone.number) |
| 93 | + # Find issue |
| 94 | + issue = _find_issue(repo, milestone, target_branch) |
| 95 | + if not issue: |
| 96 | + issue_title = f"Migration to version {target_branch}" |
| 97 | + github.gh_call( |
| 98 | + gh_pr.create_comment, |
| 99 | + f"There's no issue in this repo with the title '{issue_title}' " |
| 100 | + f"and the milestone {target_branch}, so not possible to add " |
| 101 | + f"the comment.", |
| 102 | + ) |
| 103 | + return |
| 104 | + # Change issue to add the PR in the module list |
| 105 | + lines = _set_lines_issue(gh_pr, issue, module) |
| 106 | + issue.edit(body="\n".join(lines)) |
| 107 | + except Exception as e: |
| 108 | + github.gh_call( |
| 109 | + gh_pr.create_comment, |
| 110 | + f"@{username} The migration issue commenter process could not " |
| 111 | + f"start, because of exception {e}.", |
| 112 | + ) |
| 113 | + raise |
0 commit comments