def gitEnterpriseConnect(args): try: # If given use the authentication token if 'giteos2_authtoken' in args: ghe = github3.GitHubEnterprise(url=args['giteos2_url'], token=args['giteos2_authtoken'], verify=args['giteos2_certs']) else: ghe = github3.GitHubEnterprise(url=args['giteos2_url'], username=args['giteos2_username'], password=args['giteos2_password'], verify=args['giteos2_certs']) # Log into the organization used by this operation. ghe.organization(args['giteos2_organization']) except: print 'Unable to login with given credentials.' print 'Github Enterprise url = "{}"'.format( args['giteos2_url']) if 'giteos2_authtoken' in args: print 'Github Enterprise authentication token = "{}"'.format( args['giteos2_authtoken']) else: print 'Github Enterprise user name = "{}"'.format( args['giteos2_username']) print 'Github Enterprise password = "******"'.format( args['giteos2_password']) print 'Github Enterprise organization = "{}"'.format( args['giteos2_organization']) exit(1) return ghe
def __init__(self, owner, repo, pr=None, branch=None, token=None, url=None, commit=None, ignore_paths=None): """ 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.github = None self.ignore_paths = set(ignore_paths or []) if not url or url == 'https://github.com': self.github = github3.GitHub(token=token) else: self.github = github3.GitHubEnterprise(url, token=token) self.owner = owner self.repo = repo print('Branch: {0}'.format(branch)) if branch and not pr: github_repo = self.github.repository(self.owner, self.repo) for pull_request in github_repo.iter_pulls(): if pull_request.to_json()['head']['ref'] == branch: pr = pull_request.to_json()['number'] break # 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.pr = pr self.pull_request = self.github.pull_request(owner, repo, pr) self.commits = self.pr_commits(self.pull_request) self.last_sha = commit or git.current_sha() print('Last SHA: {0}'.format(self.last_sha)) self.first_sha = self.commits[0].sha self.parent_sha = git.parent_sha(self.first_sha) self.diff = git.diff(self.parent_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()
def setUp(self): self.session = self.create_session_mock() self.instance = github3.GitHubEnterprise(self.enterprise_url) self.instance.session = self.session # Proxy the build_url method to the class so it can build the URL and # we can assert things about the call that will be attempted to the # internet self.instance._build_url = self.build_url self.after_setup()
def create_github_client(self, project): """Create a github3 client per repo/installation.""" token = self._get_installation_key(project=project) if not token: LOGGER.warning( "Could not find an authentication token for '%s'. Do you " "have access to this repository?", project, ) return gh = github3.GitHubEnterprise(self.base_url) gh.login(token=token) return gh
def login(github_enterprise_url: Optional[str] = None) -> github3.GitHub: if github_enterprise_url: verify = get_verify_tls_setting(auth_system="github_enterprise") gh_api = github3.GitHubEnterprise(url=github_enterprise_url, verify=verify) token = ensure_token(github_client=gh_api, auth_system="github_enterprise") else: gh_api = github3.GitHub() token = ensure_token(github_client=gh_api, auth_system="github") gh_api.login(token=token) ui.info_2("Successfully logged in on GitHub") return gh_api
def __init__(self, user, password, token, session, enterprise_url=None): super(GitHubBackend, self).__init__('github') if token: kwargs = {'token': token} else: kwargs = {'username': user, 'password': password} if enterprise_url: self.gh = github3.GitHubEnterprise(enterprise_url, **kwargs) else: self.gh = github3.login(**kwargs) self.USERS_CACHE = {} self.session = session
def get_client(config): """ Factory for the Github client """ if 'GITHUB_OAUTH_TOKEN' not in config: raise KeyError('Missing GITHUB_OAUTH_TOKEN in application config. ' 'Update your settings.py file.') session = get_session(config.get('GITHUB_CLIENT_RETRY_OPTIONS', None)) if config.get('GITHUB_URL', GITHUB_BASE_URL) != GITHUB_BASE_URL: client = github3.GitHubEnterprise(config['GITHUB_URL'], token=config['GITHUB_OAUTH_TOKEN'], session=session) else: client = github3.GitHub(token=config['GITHUB_OAUTH_TOKEN'], session=session) return client
def get_auth_token(cls, login, password, prompt=None): import platform if cls.fqdn != GITHUB_COM_FQDN: gh = github3.GitHubEnterprise() else: gh = github3.GitHub() gh.login(login, password, two_factor_callback=lambda: prompt('2FA code> ')) try: auth = gh.authorize(login, password, scopes=[ 'repo', 'delete_repo', 'gist' ], note='git-repo2 token used on {}'.format(platform.node()), note_url='https://github.com/guyzmo/git-repo') return auth.token except github3.models.GitHubError as err: if len(err.args) > 0 and 422 == err.args[0].status_code: raise ResourceExistsError("A token already exist for this machine on your github account.") else: raise err
def _get_github(self): try: import github3 except ImportError: raise Exception(""" ERROR: github3.py not installed! Please install via pip install boundary-layer[github] and try again.""") if self.github_url: return github3.GitHubEnterprise(url=self.github_url, username=self.github_username, password=self.github_password, token=self.github_token) return github3.GitHub(username=self.github_username, password=self.github_password, token=self.github_token)
def _createGithubClient(self): if self.server != 'github.com': url = 'https://%s/' % self.server if not self.verify_ssl: # disabling ssl verification is evil so emit a warning self.log.warning("SSL verification disabled for " "GitHub Enterprise") github = github3.GitHubEnterprise(url, verify=self.verify_ssl) else: github = github3.GitHub() # anything going through requests to http/s goes through cache github.session.mount('http://', self.cache_adapter) github.session.mount('https://', self.cache_adapter) # Add properties to store project and user for logging later github._zuul_project = None github._zuul_user_id = None return github
def __init__(self, owner, repo, pr, token, url=None): self.github = None # TODO: support non-PR runs try: pr = int(pr) except (ValueError, TypeError): return if not url or url == 'https://github.com': self.github = github3.GitHub(token=token) else: self.github = github3.GitHubEnterprise(url, token=token) self.owner = owner self.repo = repo self.pr = pr self.pull_request = self.github.pull_request(owner, repo, pr) self.commits = self.pr_commits(self.pull_request) self.last_sha = self.commits[-1].sha self.first_sha = self.commits[0].sha self.parent_sha = git.parent_sha(self.first_sha) self.diff = git.diff(self.parent_sha, self.last_sha)
def connect(self): if self.fqdn != GITHUB_COM_FQDN: # upgrade self.gh from a GitHub object to a GitHubEnterprise object gh = github3.GitHubEnterprise(RepositoryService.build_url(self)) self.gh._session.base_url = gh._session.base_url gh._session = self.gh._session self.gh = gh # propagate ssl certificate parameter self.gh._session.verify = self.session_certificate or not self.session_insecure try: self.gh.login(token=self._privatekey) self.username = self.gh.user().login except github3.models.GitHubError as err: if 401 == err.code: if not self._privatekey: raise ConnectionError('Could not connect to Github. ' 'Please configure .gitconfig ' 'with your github private key.') from err else: raise ConnectionError('Could not connect to Github. ' 'Check your configuration and try again.') from err
def __init__( self, owner, repo, pr=None, branch=None, token=None, url=None, commit=None, ignore_paths=None, prefix=None, ): """ 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.github = None self.stopped_early = False self.prefix = prefix self.ignore_paths = set(ignore_paths or []) if not url or url == "https://github.com": self.github = github3.GitHub(token=token) else: self.github = github3.GitHubEnterprise(url, token=token) self.owner = owner self.repo = repo self.github_repo = self.github.repository(self.owner, self.repo) all_commits = self.repo_commits(self.github_repo) self.master_sha = all_commits[0].sha print("Master SHA: {0}".format(self.master_sha)) print("Branch: {0}".format(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.label 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)) self.commits = self.pr_commits(self.pull_request) self.last_sha = commit or git.current_sha() print("Last SHA: {0}".format(self.last_sha)) self.first_sha = self.commits[0].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()
def __init__(self, enterprise, repo_name, project_path, **kwargs): provider = 'oss.navercorp' if enterprise else 'github' self.__url = 'https://{0}.com'.format(provider) url_parsed = urlparse(self.__url) self.__api_url = self.__url + '/api/v3/' if enterprise \ else url_parsed.scheme + '://api.{0}/'.format(url_parsed.netloc) self.__enterprise = enterprise self.__token = kwargs.get('token') self.__org_name = kwargs.get('org_name') self.__repo_name = repo_name self.__project_path = project_path self.__cur_dir = os.path.abspath(os.curdir) # if token passed by parameter write to file after confirming token # else read file and confirming that token # if token is invalid raise InvalidTokenError if self.__token: self.confirm_token(self.__token) # After confirming token and writing file mode = 'w' else: if not os.path.exists(self.token_file_name): self.__token = str(input('Please input token: ')) self.confirm_token(self.__token) mode = 'w' else: mode = 'r' with open(self.token_file_name, mode, encoding='utf-8') as token_file: if mode == 'w': token_file.write(str(self.__token)) else: self.__token = self.confirm_token(token_file.read()) self.__session = github3.GitHub(token=self.__token) if not enterprise \ else github3.GitHubEnterprise(self.__url, token=self.__token) self.__username = self.__session.user().login self.owner_name = self.__org_name \ if self.__org_name else self.__username self.__repo = self.__session.repository(owner=self.owner_name, repository=self.__repo_name) if not self.__repo: if not self.__org_name: self.__repo = self.__session.create_repo(self.__repo_name) else: org = self.__session.organization(login=self.__org_name) self.__repo = org.create_repo(self.__repo_name) self.__repo_name = self.__repo.name self.header_basis['authorization'] += self.token self.basic_header = self.header_basis.copy() self.issue_header = self.header_basis.copy() self.repo_header = self.header_basis.copy() self.basic_header['Accept'] = 'application/vnd.github.v3+json' self.issue_header['Accept'] = 'application/vnd.github.golden-' \ 'comet-preview' self.repo_header['Accept'] = 'application/vnd.github.barred-' \ 'rock-preview' self.basis_repo_url = '{0}repos/{1}/{2}'.format( self.api_url, self.owner_name, self.repo_name) self.import_repo_url = self.basis_repo_url + '/import' self.import_issue_url = self.import_repo_url + '/issues' self.issues = self.read_issue_json() self.downloads = self.read_downloads()
def setUp(self): super(TestUnsecureGitHubEnterprise, self).setUp() self.g = github3.GitHubEnterprise('https://github.example.com:8080/', verify=False)
def login(self): host = self.config('host') if host and host not in ('https://api.github.com', 'api.github.com'): if not host.startswith(('http://', 'https://')): host = 'https://' + host self.gh = github3.GitHubEnterprise(url=host) else: self.gh = github3.GitHub() user = self.config('user') if not user: user = raw_input("GitHub user: "******"""Callback for github3.py's 2FA support.""" return raw_input("Two-Factor Authentication Code: ").strip() password = getpass.getpass("GitHub password: "******"GitSpindle on %s" % socket.gethostname(), "http://seveas.github.com/git-spindle") except github3.GitHubError: type, exc = sys.exc_info()[:2] if hasattr(exc, 'response'): response = exc.response if response.status_code == 422: for error in response.json()['errors']: if error['resource'] == 'OauthAccess' and error[ 'code'] == 'already_exists': err("An OAuth token for this host already exists, please delete it on https://github.com/settings/applications" ) raise if auth is None: err("Authentication failed") token = auth.token self.config('token', token) self.config('auth_id', auth.id) print( "A GitHub authentication token is now cached in ~/.gitspindle - do not share this file" ) print( "To revoke access, visit https://github.com/settings/applications" ) if not user or not token: err("No user or token specified") self.gh.login(username=user, token=token) try: self.me = self.gh.user() self.my_login = self.me.login except github3.GitHubError: # Token obsolete self.config('token', None) self.login()
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 setUp(self): super(TestGitHubEnterprise, self).setUp() self.g = github3.GitHubEnterprise('https://github.example.com/')
import argparse import time import feedparser from copy import copy from sys import stderr gh_user = os.getenv('GH_USER', None) gh_pass = os.getenv('GH_PWD', None) gh_token = os.getenv('GH_TOKEN', None) gh_url = os.getenv('GH_URL', None) if gh_url is None: gh = github.GitHub(username=gh_user, password=gh_pass, token=gh_token) else: gh = github.GitHubEnterprise(url=gh_url, username=gh_user, password=gh_pass, token=gh_token) def search_wrapper(gen): # print(gen) while True: gen_back = copy(gen) try: # print(gen_back) yield next(gen) except StopIteration: return except github.exceptions.ForbiddenError as e: search_rate_limit = gh.rate_limit()['resources']['search'] # limit_remaining = search_rate_limit['remaining']
def get_client(self): return github3.GitHubEnterprise(self.enterprise_url)