コード例 #1
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
コード例 #2
0
ファイル: github.py プロジェクト: umeditor/CumulusCI
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
コード例 #3
0
ファイル: base.py プロジェクト: dlfinst/github-cli
class Command(object):
    __metaclass__ = ABCMeta
    name = None
    usage = None
    repository = ()
    user = ''
    subcommands = {}
    SUCCESS = 0
    FAILURE = 1
    COMMAND_UNKNOWN = 127

    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)

    @abstractmethod
    def run(self, options, args):
        return self.FAILURE

    def get_repo(self, options):
        self.repo = None
        if self.repository:
            self.repo = self.gh.repository(*self.repository)

        if not (self.repo or options.loc_aware):
            self.parser.error('A repository is required.')

    def get_user(self):
        if not self.user:
            self.login()
            self.user = self.gh.user()

    def login(self):
        # Get the full path to the configuration file
        config = github_config()
        parser = ConfigParser()

        # Check to make sure the file exists and we are allowed to read it
        if os.path.isfile(config) and os.access(config, os.R_OK | os.W_OK):
            parser.readfp(open(config))
            self.gh.login(token=parser.get('github', 'token'))
        else:
        # Either the file didn't exist or we didn't have the correct
        # permissions
            user = ''
            while not user:
                # We will not stop until we are given a username
                user = input('Username: '******''
            while not pw:
                # Nor will we stop until we're given a password
                pw = getpass('Password: '******'user', 'repo', 'gist'], 'github-cli',
                'http://git.io/MEmEmw'
            )
            parser.add_section('github')
            parser.set('github', 'token', auth.token)
            self.gh.login(token=auth.token)
            # Create the file if it doesn't exist. Otherwise completely blank
            # out what was there before. Kind of dangerous and destructive but
            # somewhat necessary
            parser.write(open(config, 'w+'))

    def help(self):
        self.parser.print_help()
        if self.subcommands:
            print('\nSubcommands:')
            for command in sorted(self.subcommands.keys()):
                print('  {0}:\n\t{1}'.format(
                    command, self.subcommands[command]
                ))
        sys.exit(0)
コード例 #4
0
ファイル: git_digger.py プロジェクト: arcolife/digGit
#!/usr/bin/env python

import os
from github3 import GitHub
from flask.json import jsonify
from requests_oauthlib import OAuth2Session
from diggit import Digger
from flask import Flask, request, redirect, session, url_for

GH = GitHub()
GH.login(token=os.environ.get('GITHUB_ACCESS_TOKEN'))
user_gh_profile = GH.me()
gh_scraper = Digger(GH)
# GH.ratelimit_remaining
app = Flask(__name__)

@app.route("/")
def home():
    return redirect(url_for('profile'))

@app.route("/profile", methods=["GET"])
def profile():
    """
    """
    return jsonify(user_gh_profile.as_dict())

@app.route("/search/", methods=["GET"])
@app.route("/search/<type>", methods=["GET"])
def scanner(type="user"):
    return jsonify(gh_scraper.get_class())
コード例 #5
0
ファイル: contributors.py プロジェクト: jonafato/contributors
    B. Personal name if different
    C. Link to user profile on GitHub

"""

from __future__ import print_function

from os import environ

from github3 import GitHub


gh = GitHub()
token = environ.get('GITHUB_API_SECRET')
if token:
    gh.login(token=token)


def get_markdown_output(contributors):
    links = ""
    output = []
    for contributor in contributors:
        print('.', end='', flush=True)
        user = gh.user(contributor)
        if user.name.strip():
            output.append("* {name} ([@{username}])".format(
                name=user.name,
                username=user.login
            )
            )
        else:
コード例 #6
0
    C. Link to user profile on GitHub

"""

from __future__ import absolute_import, print_function

from os import environ

from github3 import GitHub

from . import utils

gh = GitHub()
token = environ.get('GITHUB_API_SECRET')
if token:
    gh.login(token=token)


def get_html_output(contributors):
    column_size = 5

    def format_user_info(user):
        if user.name:
            return template_with_name.format(name=user.name, link=user.html_url, username=user.login)
        else:
            return template_no_name.format(link=user.html_url, username=user.login)

    # generate html now
    column_template = '    <td align=center><img width=100 src={photo}><br>{name}</td>\n'
    template_with_name = '{name} (<a href={link}>@{username}</a>)'
    template_no_name = '<a href={link}>@{username}</a>'