Example #1
0
def get_session():
    """Fetch and/or load API authorization token for GITHUB."""
    ensure_config_dir()
    credential_file = os.path.join(CONFIG_DIR, 'github_auth')
    if os.path.isfile(credential_file):
        with open(credential_file) as fd:
            token = fd.readline().strip()
        gh = GitHub(token=token)
        try:  # Test connection before starting
            gh.is_starred('github', 'gitignore')
            return gh
        except GitHubError as exc:
            raise_unexpected(exc.code)
            sys.stderr.write('Invalid saved credential file.\n')

    from getpass import getpass
    from github3 import authorize

    user = prompt('GITHUB Username')
    try:
        auth = authorize(
            user,
            getpass('Password for {0}: '.format(user)),
            'repo',
            'Farcy Code Reviewer',
            two_factor_callback=lambda: prompt('Two factor token'))
    except GitHubError as exc:
        raise_unexpected(exc.code)
        raise FarcyException(exc.message)

    with open(credential_file, 'w') as fd:
        fd.write('{0}\n{1}\n'.format(auth.token, auth.id))
    return GitHub(token=auth.token)
Example #2
0
 def _client(self, auth, api_url):
     client = GitHub()
     if auth.token.exists():
         client = GitHub(token=auth.token())
     elif auth.username.exists() and auth.password.exists():
         client = GitHub(username=auth.username(), password=auth.password())
     # TODO: hacky - might want to just open up a PR to upstream?
     if api_url is not None:
         client._github_url = api_url
         client.session.base_url = api_url
     return client
Example #3
0
    def __init__(self, private_key, app_id):
        """ Connect to github and create a Github client and a client specific
        to the Github app installation.

        Parameters
        ----------
        private_key : str
            The private key of the Github App.
        app_id : int
            The unique id of the Github App.
        """
        self.private_key = private_key
        self.owner = REPO_OWNER
        self.repo = REPO_NAME

        # Client for communicating with GitHub API
        self.git_client = GitHub()
        self.git_client.login_as_app(private_key_pem=str.encode(private_key),
                                     app_id=app_id)
        # Information about app installation associated with repo
        self.noisepage_repo_client = self.git_client.app_installation_for_repository(
            self.owner, self.repo)
        # Login as installation allows interactions that require repository permissions
        self.git_client.login_as_app_installation(
            private_key_pem=str.encode(private_key),
            app_id=app_id,
            installation_id=self.noisepage_repo_client.id)
        self.access_token = {"token": None, "exp": 0}
def generate_auth_token(section, config_file=CREDENTIALS_FILE):
    def read_two_factor():
        code = ''
        while not code:
            code = raw_input('Enter 2FA code: ')
        return code

    c = ConfigParser()
    c.add_section(section)

    username = raw_input('Enter GitHub username: '******'Enter GitHub password for {0}: '.format(username))
    enterprise_url = raw_input(
        'Enterprise URL (leave empty if using github.com): ')

    if enterprise_url:
        g = GitHubEnterprise(enterprise_url)
        auth = g.authorize(username, password, DEFAULT_SCOPE, OAUTH_NAME,
                           OAUTH_SITE)
        c.set(section, 'url', enterprise_url)
    else:
        g = GitHub()
        auth = authorize(username,
                         password,
                         DEFAULT_SCOPE,
                         OAUTH_NAME,
                         OAUTH_SITE,
                         two_factor_callback=read_two_factor)

    c.set(section, 'token', auth.token)

    with open(CREDENTIALS_FILE, 'a+') as f:
        c.write(f)
Example #5
0
def github(pytestconfig):
    """Returns a client that can interface with the GitHub API"""
    token = pytestconfig.getoption("--github-token")
    if token:
        return login(token=token)
    else:
        return GitHub()
    def __connectToGitHub(bWithToken):
        """
        private method to establish a connection to github

        :param bWithToken: true, false
        :return:
        """
        if InputOutputAgent.__gh is None or InputOutputAgent.__bWithTokenUpdated:
            if bWithToken:
                # the TokenGithubAPI is stored as an environment-variable
                try:
                    InputOutputAgent.__gh = login(
                        token=str(os.environ['TokenGithubAPI']))
                    InputOutputAgent.__bWithTokenUpdated = False
                    print('GithubToken is used for connection')

                except Exception as ex:
                    raise ConnectionError(
                        'no connection to GitHub could be established')
            else:
                try:
                    InputOutputAgent.__gh = GitHub()
                    InputOutputAgent.__bWithTokenUpdated = False
                    print('No GithubToken is used for connection')
                except Exception as ex:
                    raise ConnectionError(
                        'no connection to GitHub could be established')

            # get rate limit information
            rates = InputOutputAgent.__gh.rate_limit()
            print('normal ratelimit info: ',
                  rates['resources']['core'])  # => your normal ratelimit info
            print(
                'search ratelimit info: ',
                rates['resources']['search'])  # => your search ratelimit info
Example #7
0
    def __init__(self,
                 config,
                 dry_run=False,
                 only_from_cache=False,
                 mention_people=False):
        self._dry_run = dry_run
        self._only_from_cache = only_from_cache
        self._mention_people = mention_people
        self._mapping = config['mapping']
        self._template = config['template']

        self._gh = GitHub(token=config['token'])
        # Everything is done via _repo
        self._repo = self._gh.repository(config['owner'], config['repository'])
        self._upstream_repo = self._gh.repository(
            config['upstream_owner'], config['upstream_repository'])

        # get current set of available milestones
        self._milestones = dict({
            milestone.title: milestone.number
            for milestone in self._repo.iter_milestones()
        })

        self._users = dict()

        self._user_cache = config.get('user_cache', None)

        self._load_user_cache()
Example #8
0
def get_github_api_for_repo(keychain, owner, repo):
    gh = GitHub()
    # Apply retry policy
    gh.session.mount("http://", adapter)
    gh.session.mount("https://", adapter)

    APP_KEY = os.environ.get("GITHUB_APP_KEY", "").encode("utf-8")
    APP_ID = os.environ.get("GITHUB_APP_ID")
    if APP_ID and APP_KEY:
        installation = INSTALLATIONS.get((owner, repo))
        if installation is None:
            gh.login_as_app(APP_KEY, APP_ID, expire_in=120)
            try:
                installation = gh.app_installation_for_repository(owner, repo)
            except github3.exceptions.NotFoundError:
                raise GithubException(
                    "Could not access {}/{} using GitHub app. "
                    "Does the app need to be installed for this repository?".
                    format(owner, repo))
            INSTALLATIONS[(owner, repo)] = installation
        gh.login_as_app_installation(APP_KEY, APP_ID, installation.id)
    else:
        github_config = keychain.get_service("github")
        gh.login(github_config.username, github_config.password)
    return gh
Example #9
0
def fixPullRequestTitle(pullRequestId):
    gho = GitHub(githubUser, githubPassword)
    oPull = gho.pull_request(githubOwner, githubRepository, str(pullRequestId))
    branchName = oPull.head.ref
    issueKey = extractIssueKey(branchName)
    title = oPull.title
    foundIndex = title.find(issueKey)
    updateRequired = 0
    if foundIndex == 0:
        if issueKey == title:
            updateRequired = 1
            print 'Issue Key ' + issueKey + ' Found in Title but Update Required for ' + title
        else:
            print 'Issue Key ' + issueKey + ' found in Title for ' + title
            return
    else:
        updateRequired = 1
        print 'Issue Key ' + issueKey + ' NOT Found in Title for ' + title

    if updateRequired == 1:
        jiraIssue = jiraGetIssueInfo(issueKey, config)
        title = issueKey + ' ' + jiraIssue.fields.summary
        print title
        oPull.update(title)
        print 'Updated the Title for the Pull Request ' + oPull.html_url
Example #10
0
 def __init__(self, token=None):
     super(ServiceGithub, self).__init__(token)
     self.scope = ['public_repo']
     self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
     self.AUTH_URL = 'https://github.com/login/oauth/authorize'
     self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
     self.username = settings.TH_GITHUB['username']
     self.password = settings.TH_GITHUB['password']
     self.consumer_key = settings.TH_GITHUB['consumer_key']
     self.consumer_secret = settings.TH_GITHUB['consumer_secret']
     self.token = token
     if self.token:
         token_key, token_secret = self.token.split('#TH#')
         self.gh = GitHub(token=token_key)
     else:
         self.gh = GitHub(username=self.username, password=self.password)
Example #11
0
def main():
    args = parse_args()
    c = ConfigParser()

    # Check if token already exists or generate one
    c.read(args.file)

    if args.profile not in c.sections():
        print Colors.warn(
            "Could not find profile '%s' in '%s', generating new credentials."
            % (args.profile, args.file))
        token = generate_auth_token(args.profile, args.file)
        c.read(args.file)

    profile = c[args.profile]

    enterprise_url = profile.get('url', None)
    token = profile.get('token')

    if enterprise_url:
        g = GitHubEnterprise(enterprise_url, token=token)
    else:
        g = GitHub(token=token)

    args.command(args, handle=g)
Example #12
0
def get_github_api_for_repo(keychain, owner, repo, session=None):
    gh = GitHub(
        session=session
        or GitHubSession(default_read_timeout=30, default_connect_timeout=30)
    )
    # Apply retry policy
    gh.session.mount("http://", adapter)
    gh.session.mount("https://", adapter)

    GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
    APP_KEY = os.environ.get("GITHUB_APP_KEY", "").encode("utf-8")
    APP_ID = os.environ.get("GITHUB_APP_ID")
    if APP_ID and APP_KEY:
        installation = INSTALLATIONS.get((owner, repo))
        if installation is None:
            gh.login_as_app(APP_KEY, APP_ID, expire_in=120)
            try:
                installation = gh.app_installation_for_repository(owner, repo)
            except github3.exceptions.NotFoundError:
                raise GithubException(
                    f"Could not access {owner}/{repo} using GitHub app. "
                    "Does the app need to be installed for this repository?"
                )
            INSTALLATIONS[(owner, repo)] = installation
        gh.login_as_app_installation(APP_KEY, APP_ID, installation.id)
    elif GITHUB_TOKEN:
        gh.login(token=GITHUB_TOKEN)
    else:
        github_config = keychain.get_service("github")
        gh.login(github_config.username, github_config.password)
    return gh
    def main(args):
        try:
            from github3 import GitHub
        except ImportError:
            raise errors.DependencyError(
                'This command needs the github3.py library installed to work')

        token = get_token(args.github_token)
        args.github_token.close()

        deprecated = parse_deprecations(args.problems)
        args.problems.close()

        body_tmpl = args.template.read()
        args.template.close()

        project_name = args.project_name.strip().lower()

        gh_conn = GitHub(token=token)
        repo = gh_conn.repository('abadger', 'ansible')

        if project_name:
            project_column = find_project_todo_column(repo, project_name)

        issues = create_issues(deprecated, body_tmpl, repo)

        if project_column:
            for issue in issues:
                project_column.create_card_with_issue(issue)
                time.sleep(0.5)

        return 0
Example #14
0
async def read_config():
    with open(config_path) as f:
        config = yaml.load(f)

    assert 'trello_appkey' in config
    assert 'trello_token' in config
    assert 'trello_boardid' in config
    assert 'lists' in config

    config['sync_timeout'] = config.get("sync_timeout") or DEFAULT_SYNC_TIMEOUT
    config['workers'] = config.get("workers") or DEFAULT_WORKERS

    trello_client = TrelloClient(api_key=config['trello_appkey'],
                                 token=config['trello_token'])
    board = trello_client.get_board(config['trello_boardid'])

    github_config = {}
    github_token = config.get("github_token")
    if github_token:
        github_config = {'token': github_token}
    github_client = GitHub(**github_config)

    config.update({
        'board': board,
        'github': github_client,
    })
    return config
Example #15
0
 def run(self):
     """
     Main worker.
     """
     update_name = None  # Store update name, i.e: Eddy v0.9.1
     update_version = self.current  # Store update version, i.e: 0.9.1
     update_url = None  # Store update HTML url, i.e: http://github.com/...
     try:
         LOGGER.info(
             'Connecting to GitHub to retrieve update information (channel: %s)',
             self.channel.value)
         github = GitHub(token='6a417ccfe9a7c526598e77a74cbf1cba6e688f0e')
         repository = github.repository('danielepantaleone', 'eddy')
         for release in repository.releases():
             if self.channel is Channel.Beta or not release.prerelease:
                 try:
                     if NormalizedVersion(
                             release.tag_name[1:]) > NormalizedVersion(
                                 update_version):
                         update_name = release.name
                         update_version = release.tag_name[1:]
                         update_url = release.html_url
                 except IrrationalVersionError as e:
                     LOGGER.warning(
                         'Failed to parse version number from TAG: %s', e)
         if update_version != self.current:
             LOGGER.info('Update available: %s', update_name)
             self.sgnUpdateAvailable.emit(update_name, update_url)
         else:
             LOGGER.info('No update available')
             self.sgnNoUpdateAvailable.emit()
     except Exception as e:
         LOGGER.warning('Failed to retrieve update data: %s', e)
         self.sgnNoUpdateDataAvailable.emit()
     self.finished.emit()
Example #16
0
def starred(user, token, sort_results=False):
    """ Creates a Github-style awesome list from user stars

    Params
    ------
    user : str
        github username from whose stars the list will be made
    token : str
        github Oauth token for API; no-privilege public token
    sort_results : bool
        whether to sort results alphabetically (under language)

    Example
    -------
    # Make sorted awesome list of cookiemonster's stars
    python starred.py -u cookiemonster -s > README.md

    """
    # Get stars
    # ---------
    gh = GitHub(token=token)
    stars = gh.starred_by(user)

    # Start building .md output
    print(desc.format(user=user))
    repo_dict = {}

    # Process user stars
    # ------------------
    for s in stars:
        language = s.language or 'Others'
        description = ''
        if s.description:
            description = html_escape(s.description).replace('\n', '')
        if language not in repo_dict:
            repo_dict[language] = []
        repo_dict[language].append([s.name, s.html_url, description.strip()])

    # Sort stars
    if sort_results:
        repo_dict = OrderedDict(sorted(repo_dict.items(), key=lambda l: l[0]))

    # Output contents
    # ---------------
    # Language headings
    for language in repo_dict.keys():
        k = language
        v = '-'.join(language.lower().split())
        data = f'  - [{k}](#{v})'
        print(data)
        print('')

    for language, repos in repo_dict.items():
        print(f"## {language.replace('#', '# #')}\n")
        for repo_name, repo_url, repo_desc in repos:
            data = f"- [{repo_name}]({repo_url}) - {repo_desc}"
            print(data)
        print('')
    print(license_)
Example #17
0
def starred(username, token, sort, repository, message):
    """GitHub starred

    creating your own Awesome List used GitHub stars!

    example:
        starred --username yrqgithub --sort > README.md
    """
    if repository:
        if not token:
            click.secho('Error: create repository need set --token', fg='red')
            return
        file = BytesIO()
        sys.stdout = file
    else:
        file = None

    gh = GitHub(token=token)
    stars = gh.starred_by(username)
    click.echo(desc)
    repo_dict = {}

    for s in stars:
        language = s.language or 'Others'
        description = html_escape(s.description).replace(
            '\n', '') if s.description else ''
        if language not in repo_dict:
            repo_dict[language] = []
        repo_dict[language].append([s.name, s.html_url, description.strip()])

    if sort:
        repo_dict = OrderedDict(sorted(repo_dict.items(), key=lambda l: l[0]))

    for language in repo_dict.keys():
        data = u'  - [{}](#{})'.format(language,
                                       '-'.join(language.lower().split()))
        click.echo(data)
    click.echo('')

    for language in repo_dict:
        click.echo('## {} \n'.format(language.replace('#', '# #')))
        for repo in repo_dict[language]:
            data = u'- [{}]({}) - {}'.format(*repo)
            click.echo(data)
        click.echo('')

    click.echo(license_.format(username=username))

    if file:
        try:
            rep = gh.repository(username, repository)
            readme = rep.readme()
            readme.update(message, file.getvalue())
        except NotFoundError:
            rep = gh.create_repository(repository,
                                       'A curated list of my GitHub stars!')
            rep.create_file('README.md', 'starred initial commit',
                            file.getvalue())
        click.launch(rep.html_url)
Example #18
0
 def __init__(self, repo_user='******', repo_name='semanario'):
     """
     @params
     repo_user - name of organization or user
     repo_name - repository name
     """
     github = GitHub()
     self.repo = github.repository(repo_user, repo_name)
Example #19
0
 def client(self):
     """Unauthenticated GitHub client"""
     if current_app.config.get("GITHUBAPP_URL"):
         return GitHubEnterprise(
             current_app.config["GITHUBAPP_URL"],
             verify=current_app.config["VERIFY_SSL"],
         )
     return GitHub()
Example #20
0
def deactivate_hook(token, owner, repo):
    """Deactivate goodtables hook for GitHub repo.
    """
    client = GitHub(token=token)
    repo = client.repository(owner, repo)
    for hook in repo.iter_hooks():
        if hook.config.get('url') == settings.GITHUB_HOOK_URL:
            hook.delete()
 def get_installation(self, installation_id):
     "login as app installation without requesting previously gathered data."
     with open(self.path, 'rb') as key_file:
         client = GitHub()
         client.login_as_app_installation(private_key_pem=key_file.read(),
                                          app_id=self.app_id,
                                          installation_id=installation_id)
     return client
Example #22
0
def deactivate_hook(token, owner, repo):
    """Deactivate goodtables hook for GitHub repo.
    """
    client = GitHub(token=token)
    repo = client.repository(owner, repo)
    for hook in repo.iter_hooks():
        if hook.config.get('is_goodtables_hook'):
            hook.delete()
Example #23
0
def gh_as_app(repo_owner, repo_name):
    app_id = settings.GITHUB_APP_ID
    app_key = settings.GITHUB_APP_KEY
    gh = GitHub()
    gh.login_as_app(app_key, app_id, expire_in=120)
    installation = gh.app_installation_for_repository(repo_owner, repo_name)
    gh.login_as_app_installation(app_key, app_id, installation.id)
    return gh
Example #24
0
def get_github_api_client():
    """
    Helper function for initializing GitHub API client

    :return: github3.Github
    """
    token = config('GITHUB_TOKEN', default=None)
    return GitHub(token=token)
Example #25
0
def create_app_client(key: bytes, app_id: int, organization: str) -> Client:
    gh = GitHub()
    gh.login_as_app(key, app_id, expire_in=30)
    installation = gh.app_installation_for_organization(organization)
    print(f"Using GitHub organization installation: {installation.id}")

    gh.login_as_app_installation(key, app_id, installation.id)
    return Client(gh, organization)
Example #26
0
    def __init__(self,
                 url_github='https://github.com'):
        """
        Create Github object
        Enterprise or public Github (default)
        Auth from token if available
        Else from username/pwd
        """

        self.url_github = url_github

        load_dotenv(dotenv_path='credentials.env')

        username = os.environ.get('github-username')
        password = os.environ.get('github-password')
        token = os.environ.get('github-token')

        if url_github == 'https://github.com':
            if (token is not None):
                self.api = GitHub(token=token)
                self.test_connection('Wrong username/password')

            elif (username is not None) and (password is not None):
                self.api = GitHub(username=username,
                                  password=password)
                self.test_connection('Scopes must be "read:user, repo" at least')

            else:
                raise Exception(
                    'No credentials provided: They should be in a "credentials.env" file')
        else:
            if (token is not None):
                self.api = GitHubEnterprise(url=url_github,
                                            token=token)
                self.test_connection('Wrong username/password')

            elif (username is not None) and (password is not None):
                self.api = GitHubEnterprise(url=url_github,
                                            username=username,
                                            password=password)
                self.test_connection('Scopes must be "read:user, repo" at least')

            else:
                raise Exception(
                    'No credentials provided: They should be in a "credentials.env" file')
Example #27
0
 def __init__(self):
     super(Command, self).__init__()
     assert self.name
     commands[self.name] = self
     self.gh = GitHub()
     self.gh.set_user_agent('github-cli/{0} (http://git.io/MEmEmw)'.format(
         __version__
     ))
     self.parser = CustomOptionParser(usage=self.usage)
Example #28
0
def main(username):
    anon = GitHub()
    repos = anon.repositories_by(username)

    for short_repository in repos:
        print("\n* {0}".format(short_repository.name))
        print(short_repository.description if short_repository.
              description is not None else "(no description)")
        print("url: {0}".format(short_repository.html_url))
Example #29
0
 def install_manifests(self):
     gh_helper = github.util.GitHubRepositoryHelper(
         owner=TEKTON_GITHUB_ORG,
         name=TEKTON_DASHBOARD_REPO_NAME,
         github_api=GitHub(),
     )
     return gh_helper.retrieve_asset_contents(
         release_tag=self.version(),
         asset_label=TEKTON_DASHBOARD_RELEASE_ASSET_NAME,
     )
Example #30
0
    def __init__(self, config):
        self.config = config
        self._client = GitHub(login=self.login, password=self.secret, token=self.token)
        self.found_project = None

        printer_config = self.config.copy()
        printer_config['formatter'] = _Formatter
        printer_config['writer'] = StdoutWriter

        self.printer = Printer(printer_config)