def from_pull_request(cls, pr: PullRequest) \ -> Union['UpdateRequest', IgnoredRequest]: """ Construct from a Github pull request. :param pr: The pull request. :return: The constructed update request. """ if pr.is_merged(): return IgnoredRequest.merged if pr.changed_files != 1: return IgnoredRequest.invalid # system file after merge file = pr.get_files()[0] # construct download url of the original file orig_file = cls.FILE_URL.format(repo=pr.base.repo.full_name, commit=pr.base.sha, file=file.filename) # retrieve the original and patched files resp = requests.get(orig_file) if not resp.ok: raise ReconstructionError('Unable to retrieve the original file: ' '%d %s' % (resp.status_code, resp.reason)) original = resp.text resp = requests.get(file.raw_url) if not resp.ok: raise ReconstructionError('Unable to retrieve the original file: ' '%d %s' % (resp.status_code, resp.reason)) patched = resp.text # read xml and reconstruct system update object update = None try: adapter = oec.Adapter() update = data_compare(adapter.read_system(io.StringIO(original)), adapter.read_system(io.StringIO(patched))) if update is None: logging.debug('No changes detected on ' + pr.number) return IgnoredRequest.invalid except Exception as e: logging.debug(e) return IgnoredRequest.invalid message, reference = UpdateRequest._parse_description(pr.body) return UpdateRequest(update, title=pr.title, message=message, reference=reference, pullreq_num=pr.number, pullreq_url=pr.html_url, branch=pr.head.label, rejected=pr.state == "closed")
def _pr_from_github_object(github_pr: GithubPullRequest) -> PullRequest: return PullRequest( title=github_pr.title, id=github_pr.number, status=PRStatus.merged if github_pr.is_merged() else PRStatus[github_pr.state], url=github_pr.html_url, description=github_pr.body, author=github_pr.user.name, source_branch=github_pr.head.ref, target_branch=github_pr.base.ref, created=github_pr.created_at, )