def _copy(self, ctxt, branch_name): """Copy the PR to a branch. Returns a tuple of strings (state, reason). """ # NOTE(sileht): Ensure branch exists first escaped_branch_name = parse.quote(branch_name, safe="") try: ctxt.client.item(f"branches/{escaped_branch_name}") except http.HTTPError as e: if not e.response: raise detail = "%s to branch `%s` failed: " % ( self.KIND.capitalize(), branch_name, ) if e.response.status_code >= 500: state = None else: state = "failure" detail += e.response.json()["message"] return state, detail # NOTE(sileht) does the duplicate have already been done ? new_pull = self.get_existing_duplicate_pull(ctxt, branch_name) # No, then do it if not new_pull: try: new_pull = duplicate_pull.duplicate( ctxt, branch_name, self.config["label_conflicts"], self.config["ignore_conflicts"], self.KIND, ) except duplicate_pull.DuplicateFailed as e: return ( "failure", f"Backport to branch `{branch_name}` failed\n{e.reason}", ) # NOTE(sileht): We relook again in case of concurrent duplicate # are done because of two events received too closely if not new_pull: new_pull = self.get_existing_duplicate_pull(ctxt, branch_name) if new_pull: return ( "success", f"[#{new_pull['number']} {new_pull['title']}]({new_pull['html_url']}) " f"has been created for branch `{branch_name}`", ) return ( "failure", f"{self.KIND.capitalize()} to branch `{branch_name}` failed", )
def _copy(self, pull, branch_name): """Copy the PR to a branch. Returns a tuple of strings (state, reason). """ try: branch = pull.g_pull.base.repo.get_branch( parse.quote(branch_name, safe="")) except github.GithubException as e: if e.status >= 500: state = None else: state = "failure" detail = "%s to branch `%s` failed: " % ( self.KIND.capitalize(), branch_name, ) detail += e.data["message"] return state, detail # NOTE(sileht) does the duplicate have already been done ? new_pull = self.get_existing_duplicate_pull(pull, branch) # No, then do it if not new_pull: try: new_pull = duplicate_pull.duplicate( pull, branch, self.config["label_conflicts"], self.config["ignore_conflicts"], self.KIND, ) except duplicate_pull.DuplicateFailed as e: return ( "failure", f"Backport to branch `{branch_name}` failed\n{e.reason}", ) # NOTE(sileht): We relook again in case of concurrent duplicate # are done because of two events received too closely if not new_pull: new_pull = self.get_existing_duplicate_pull(pull, branch) if new_pull: return ( "success", "[#%d %s](%s) has been created for branch `%s`" % ( new_pull.number, new_pull.title, new_pull.html_url, branch_name, ), ) return ( "failure", "%s to branch `%s` failed" % (self.KIND.capitalize(), branch_name), )
def run( self, installation_id, installation_token, event_type, data, pull, missing_conditions, ): branches = self.config["branches"] if self.config["regexes"]: regexes = list(map(re.compile, self.config["regexes"])) branches.extend( (branch.name for branch in pull.g_pull.base.repo.get_branches() if any(map(lambda regex: regex.match(branch.name), regexes)))) state = "success" detail = "The following pull requests have been created: " for branch_name in branches: try: branch = pull.g_pull.base.repo.get_branch( parse.quote(branch_name, safe="")) except github.GithubException as e: # pragma: no cover LOG.error( "%s: fail to get branch", self.KIND, pull_request=pull, error=e.data["message"], ) state = "failure" detail += "\n* %s to branch `%s` has failed" % (self.KIND, branch_name) if e.status == 404: detail += ": the branch does not exists" continue # NOTE(sileht) does the duplicate have already been done ? new_pull = self.get_existing_duplicate_pull(pull, branch) # No, then do it if not new_pull: new_pull = duplicate_pull.duplicate(pull, branch, installation_token, self.KIND) # NOTE(sileht): We relook again in case of concurrent duplicate # are done because of two events received too closely if not new_pull: new_pull = self.get_existing_duplicate_pull(pull, branch) if new_pull: detail += "\n* [#%d %s](%s)" % ( new_pull.number, new_pull.title, new_pull.html_url, ) else: # pragma: no cover state = "failure" detail += "\n* %s to branch `%s` has failed" % (branch_name, self.KIND) return state, self.SUCCESS_MESSAGE, detail