示例#1
0
def configure_retries(session: GitHubSession):
    """Configure retries for a github3 client.
    """
    # https://cumulusci.readthedocs.io/en/latest/_modules/cumulusci/core/github.html
    retries = Retry(status_forcelist=(401, 502, 503, 504), backoff_factor=0.3)
    adapter = HTTPAdapter(max_retries=retries)
    session.mount("http://", adapter)
    session.mount("https://", adapter)
示例#2
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
示例#3
0
    def __init__(self, json, session=None):
        super(GitHubCore, self).__init__(json)
        if hasattr(session, '_session'):
            # i.e. session is actually a GitHub object
            session = session._session
        elif session is None:
            session = GitHubSession()
        self._session = session

        # set a sane default
        self._github_url = 'https://api.github.com'
示例#4
0
    def setUp(self):
        repo = Mock(spec=GithubRepository)
        pr = Mock(spec=GithubPullRequest,
                  head='abc123',
                  display_name='markstory/lint-review#1',
                  number=2)
        repo.pull_request.return_value = pr

        self.repo, self.pr = repo, pr
        self.config = build_review_config(fixer_ini, config)

        self.session = GitHubSession()
def betamax_github_session(monkeypatch):
    monkeypatch.setenv("GITHUB_TOKEN", "FAKE_TOKEN")

    session = GitHubSession()

    def __init__(self, username='', password='', token='', *args, **kwargs):
        super(Github, self).__init__(username=username,
                                     password=password,
                                     token=token,
                                     session=session)

    monkeypatch.setattr(Github, "__init__", __init__)

    return session
示例#6
0
from mock import call, Mock
from unittest import TestCase

import lintreview.github as github

from . import load_fixture
import github3
from github3 import GitHub
from github3.session import GitHubSession
from github3.repos import Repository
from github3.repos.hook import Hook

config = {
    'GITHUB_URL': 'https://api.github.com/',
}
session = GitHubSession()


class TestGithub(TestCase):
    def test_get_client(self):
        conf = config.copy()
        conf['GITHUB_OAUTH_TOKEN'] = 'an-oauth-token'
        gh = github.get_client(conf)
        assert isinstance(gh, GitHub)

    def test_get_client__retry_opts(self):
        conf = config.copy()
        conf['GITHUB_OAUTH_TOKEN'] = 'an-oauth-token'
        conf['GITHUB_CLIENT_RETRY_OPTIONS'] = {'backoff_factor': 42}
        gh = github.get_client(conf)
示例#7
0
def create_commits(data):
    session = GitHubSession()
    return [ShortCommit(f, session) for f in json.loads(data)]
示例#8
0
def create_pull_files(data):
    session = GitHubSession()
    return [PullFile(f, session) for f in json.loads(data)]
示例#9
0
import os
from github3 import GitHub
from github3.session import GitHubSession

print("Logging into Github")
token = os.environ["GITHUB_TOKEN"]

gh = GitHub(token=token, session=GitHubSession(default_connect_timeout=3600, default_read_timeout=3600))

print("Fetching repository")
repo = gh.repository("LlewVallis", "OpenMissileWars")

print("Fetching latest release")
latest_release = repo.latest_release().tag_name
version = str(int("".join([c for c in latest_release if str.isdigit(c)])) + 1)

print("Creating new release")
release = repo.create_release(tag_name="prebuilt-" + version, name="Prebuilt server #" + version)

print("Reading server archive")
asset = open("server.tar.gz", "rb").read()

print("Uploading server archive")
release.upload_asset(content_type="application/gzip", name="server.tar.gz", asset=asset)
示例#10
0
 def setUp(self):
     self.session = GitHubSession()
示例#11
0
 def setUp(self):
     self.session = GitHubSession()
     self.tool_patcher = patch('lintreview.processor.tools')
     self.tool_stub = self.tool_patcher.start()
     self.fixer_patcher = patch('lintreview.processor.fixers')
     self.fixer_stub = self.fixer_patcher.start()
示例#12
0
 def setUp(self):
     self.problems = Problems()
     self.session = GitHubSession()
示例#13
0
 def setUp(self):
     fixture = load_fixture('pull_request.json')
     self.session = GitHubSession()
     self.model = PullRequest.from_json(fixture, self.session)
示例#14
0
 def setUp(self):
     fixture = load_fixture('repository.json')
     self.session = GitHubSession()
     self.repo_model = Repository.from_json(fixture, self.session)