def __init__(
        self,
        owner,
        repo,
        pr=None,
        branch=None,
        token=None,
        url=None,
        commit=None,
        ignore_paths=None,
        prefix=None,
        autofix=False,
        set_status=False,
    ):
        """
        GitHubInterface lets us post messages to GitHub.

        owner and repo are the repository owner/organization and repo name respectively.

        pr is the ID number of the pull request. branch is the branch name. either pr OR branch
        must be populated.

        token is your GitHub API token.

        url is the base URL of your GitHub instance, such as https://github.com

        commit is the commit hash we're running against

        ignore_paths are paths to ignore comments from
        """
        self.start = datetime.datetime.now()
        self.github = None
        self.stopped_early = False
        self.autofixed = False
        self.prefix = prefix
        self.autofix = autofix
        self.ignore_paths = set(ignore_paths or [])
        self.token = token
        self.set_status = set_status
        url = url or "https://github.com"
        print("url={}".format(url))
        self.netloc = urlparse(url).netloc.strip()
        print("urlparse={}".format(urlparse(url)))
        if not url or url == "https://github.com":
            self.github = github3.GitHub(token=token)
        else:
            self.github = github3.GitHubEnterprise(url, token=token)

        try:
            self.github_user = self.github.me().as_dict()
        except (TypeError, AttributeError):
            # github.py == 0.9.6
            self.github_user = self.github.user().to_json()

        self.username = ""
        self.email = ""
        try:
            self.username = self.github_user["login"]
            for email in self.github.emails():
                try:
                    email_obj = email.as_dict()
                except (TypeError, AttributeError):
                    # github.py == 0.9.6
                    email_obj = email.to_json()
                if email_obj["primary"]:
                    self.email = email_obj["email"]
        except Exception:  # NOQA
            traceback.print_exc()

        self.owner = owner
        self.repo = repo

        self.github_repo = self.github.repository(self.owner, self.repo)
        print("Branch: {0}".format(branch))
        self.branch = branch
        self.pull_request_number = None
        if branch and not pr:
            for github_repo in [self.github_repo, self.github_repo.parent]:
                if pr:
                    break

                if not github_repo:
                    continue

                try:
                    # github.py == 0.9.6
                    pulls = github_repo.iter_pulls()
                except AttributeError:
                    pulls = github_repo.pull_requests()

                for pull_request in pulls:
                    print(
                        "Branch: {} - Pull Request Head Ref: {}".format(
                            branch, pull_request.head.ref
                        )
                    )
                    if pull_request.head.ref == branch:
                        pr = pull_request.number
                        self.github_repo = github_repo
                        break

        self.owner = self.github_repo.owner
        self.repo = self.github_repo.name

        # TODO: support non-PR runs
        try:
            pr = int(pr)
        except (ValueError, TypeError):
            print("{0} is not a valid pull request ID".format(pr))
            self.github = None
            return

        print("PR ID: {0}".format(pr))
        self.pull_request_number = pr
        self.pull_request = self.github.pull_request(self.owner, self.repo, pr)
        self.target_sha = self.pull_request.base.sha
        self.target_branch = self.pull_request.base.ref
        self.sha = self.pull_request.head.sha
        self.branch = self.pull_request.head.ref
        try:
            # github.py == 0.9.6
            try:
                git.fetch(self.pull_request.base.to_json()["repo"]["clone_url"])
            except subprocess.CalledProcessError:
                git.fetch(self.pull_request.base.to_json()["repo"]["ssh_url"])
        except AttributeError:
            # latest github.py
            try:
                git.fetch(self.pull_request.base.repository.as_dict()["clone_url"])
            except subprocess.CalledProcessError:
                git.fetch(self.pull_request.base.repository.as_dict()["ssh_url"])

        print("Target SHA: {0}".format(self.target_sha))
        print("Target Branch: {0}".format(self.target_branch))
        print("Head SHA: {0}".format(self.sha))
        print("Head Branch: {0}".format(self.branch))
        self.last_sha = commit or git.current_sha()
        print("Last SHA: {0}".format(self.last_sha))
        self.diff = git.diff(self.target_sha, self.last_sha)
        self.patch = unidiff.PatchSet(self.diff.split("\n"))
        self.review_comments = list(self.pull_request.review_comments())
        self.last_update = time.time()
        self.messages_in_files = dict()
        self.filenames = set()

        try:
            try:
                pr_files = self.pull_request.files()
            except AttributeError:
                # github.py == 0.9.6
                pr_files = self.pull_request.iter_files()
            self.filenames = set(
                os.path.relpath(pr_file.filename).replace("\\", "/").strip()
                for pr_file in pr_files
            )
            print("Files in PR: {}".format(self.filenames))
        except Exception:
            traceback.print_exc()
def __init__ ( self , owner , repo , pr = None , branch = None , token = None , url = None , commit = None , ignore_paths = None , prefix = None , autofix = False , set_status = False , ) : self . start = datetime . datetime . now ( ) self . github = None self . stopped_early = False self . autofixed = False self . prefix = prefix self . autofix = autofix self . ignore_paths = set ( ignore_paths or [ ] ) self . token = token self . set_status = set_status url = url or "https://github.com" print ( "url={}" . format ( url ) ) self . netloc = urlparse ( url ) . netloc . strip ( ) print ( "urlparse={}" . format ( urlparse ( url ) ) ) if not url or url == "https://github.com" : self . github = github3 . GitHub ( token = token ) else : self . github = github3 . GitHubEnterprise ( url , token = token ) try : self . github_user = self . github . me ( ) . as_dict ( ) except ( TypeError , AttributeError ) : self . github_user = self . github . user ( ) . to_json ( ) self . username = "" self . email = "" try : self . username = self . github_user [ "login" ] for email in self . github . emails ( ) : try : email_obj = email . as_dict ( ) except ( TypeError , AttributeError ) : email_obj = email . to_json ( ) if email_obj [ "primary" ] : self . email = email_obj [ "email" ] except Exception : traceback . print_exc ( ) self . owner = owner self . repo = repo self . github_repo = self . github . repository ( self . owner , self . repo ) print ( "Branch: {0}" . format ( branch ) ) self . branch = branch self . pull_request_number = None if branch and not pr : for github_repo in [ self . github_repo , self . github_repo . parent ] : if pr : break if not github_repo : continue try : pulls = github_repo . iter_pulls ( ) except AttributeError : pulls = github_repo . pull_requests ( ) for pull_request in pulls : print ( "Branch: {} - Pull Request Head Ref: {}" . format ( branch , pull_request . head . ref ) ) if pull_request . head . ref == branch : pr = pull_request . number self . github_repo = github_repo break self . owner = self . github_repo . owner self . repo = self . github_repo . name try : pr = int ( pr ) except ( ValueError , TypeError ) : print ( "{0} is not a valid pull request ID" . format ( pr ) ) self . github = None return print ( "PR ID: {0}" . format ( pr ) ) self . pull_request_number = pr self . pull_request = self . github . pull_request ( self . owner , self . repo , pr ) self . target_sha = self . pull_request . base . sha self . target_branch = self . pull_request . base . ref self . sha = self . pull_request . head . sha self . branch = self . pull_request . head . ref try : try : git . fetch ( self . pull_request . base . to_json ( ) [ "repo" ] [ "clone_url" ] ) except subprocess . CalledProcessError : git . fetch ( self . pull_request . base . to_json ( ) [ "repo" ] [ "ssh_url" ] ) except AttributeError : try : git . fetch ( self . pull_request . base . repository . as_dict ( ) [ "clone_url" ] ) except subprocess . CalledProcessError : git . fetch ( self . pull_request . base . repository . as_dict ( ) [ "ssh_url" ] ) print ( "Target SHA: {0}" . format ( self . target_sha ) ) print ( "Target Branch: {0}" . format ( self . target_branch ) ) print ( "Head SHA: {0}" . format ( self . sha ) ) print ( "Head Branch: {0}" . format ( self . branch ) ) self . last_sha = commit or git . current_sha ( ) print ( "Last SHA: {0}" . format ( self . last_sha ) ) self . diff = git . diff ( self . target_sha , self . last_sha ) self . patch = unidiff . PatchSet ( self . diff . split ( "\n" ) ) self . review_comments = list ( self . pull_request . review_comments ( ) ) self . last_update = time . time ( ) self . messages_in_files = dict ( ) self . filenames = set ( ) try : try : pr_files = self . pull_request . files ( ) except AttributeError : pr_files = self . pull_request . iter_files ( ) self . filenames = set ( os . path . relpath ( pr_file . filename ) . replace ( "\\" , "/" ) . strip ( ) for pr_file in pr_files ) print ( "Files in PR: {}" . format ( self . filenames ) ) except Exception : traceback . print_exc ( )
GitHubInterface lets us post messages to GitHub.

        owner and repo are the repository owner/organization and repo name respectively.

        pr is the ID number of the pull request. branch is the branch name. either pr OR branch
        must be populated.

        token is your GitHub API token.

        url is the base URL of your GitHub instance, such as https://github.com

        commit is the commit hash we're running against

        ignore_paths are paths to ignore comments from