def get_current_branch_pr(self, git_project: GitProject) -> PullRequest: """ If the current branch is assoctiated with a PR, get it, otherwise return None """ current_branch = self.get_current_branch() pr_re = re.compile(r"^pr/(\d+)$") m = pr_re.match(current_branch) if m: pr_id = int(m.group(1)) pr = git_project.get_pr(pr_id) return pr # FIXME: could this logic be in ogr? input: branch + repo, output: pr for pr in git_project.get_pr_list(): if (pr.source_branch == current_branch and pr.author == git_project.service.user.get_username()): return pr
def update_or_create_dist_git_pr( self, project: GitProject, pr_id: int, pr_url: str, top_commit: str, title: str, source_ref: str, pagure_fork_token: str, pagure_package_token: str, ) -> None: # Sadly, pagure does not support editing initial comments of a PR via the API # https://pagure.io/pagure/issue/4111 # Short-term solution: keep adding comments # and get updated info about sg PR ID and commit desc for pr in project.get_pr_list(): sg_pr_id_match = project.search_in_pr( pr_id=pr.id, filter_regex=DG_PR_COMMENT_KEY_SG_PR + r":\s*(\d+)", reverse=True, description=True, ) if not sg_pr_id_match: logger.debug(f"No automation comment found in dist-git PR: {pr.id}.") continue sg_pr_id = sg_pr_id_match[1] if sg_pr_id_match[1] != str(pr_id): logger.debug( f"Dist-git PR `{pr.id}` does not match " f"source-git PR `{pr_id}`." ) continue commit_match = project.search_in_pr( pr_id=pr.id, filter_regex=DG_PR_COMMENT_KEY_SG_COMMIT + r":\s*(\d+)", reverse=True, description=True, ) if not commit_match: logger.debug( f"Dist-git PR `{pr.id}` does not contain top-commit of the " f"source-git PR `{pr_id}`." ) continue logger.debug(f"Adding a new comment with update to existing PR.") msg = ( f"New changes were pushed to the upstream pull request\n\n" f"[{DG_PR_COMMENT_KEY_SG_PR}: {pr_id}]({pr_url})\n" f"{DG_PR_COMMENT_KEY_SG_COMMIT}: {top_commit}" ) # FIXME: consider storing the data above as a git note of the top commit project.change_token(pagure_package_token) project.pr_comment(pr.id, msg) logger.info(f"new comment added on PR {pr.id} ({pr.url})") break else: logger.debug(f"Matching dist-git PR not found => creating a new one.") msg = ( f"This pull request contains changes from upstream " f"and is meant to integrate them into Fedora\n\n" f"[{DG_PR_COMMENT_KEY_SG_PR}: {pr_id}]({pr_url})\n" f"{DG_PR_COMMENT_KEY_SG_COMMIT}: {top_commit}" ) # This pagure call requires token from the package's FORK project_fork = project.get_fork() project_fork.change_token(pagure_fork_token) dist_git_pr_id = project_fork.pr_create( title=f"[source-git] {title}", body=msg, source_branch=source_ref, target_branch="master", ).id logger.info(f"PR created: {dist_git_pr_id}")