def process_hook(func, args): """ Generic helper for processing hook commands. """ credentials = None if args.login_user and args.login_pass: credentials = { 'GITHUB_USER': args.login_user, 'GITHUB_PASSWORD': args.login_pass } with app.app_context(): if credentials: credentials['GITHUB_URL'] = app.config['GITHUB_URL'] gh = github.get_client( credentials, args.user, args.repo) else: gh = github.get_client( app.config, args.user, args.repo) endpoint = url_for('start_review', _external=True) func(gh, endpoint, args.user, args.repo)
def process_pull_request(user, repo, number, lintrc): """ Starts processing a pull request and running the various lint tools against it. """ log.info('Starting to process lint for %s/%s/%s', user, repo, number) log.debug("lintrc contents '%s'", lintrc) review_config = ReviewConfig(lintrc) if len(review_config.linters()) == 0: log.info('No configured linters, skipping processing.') return gh = github.get_client(config, user, repo) try: log.info('Loading pull request data from github.') pull_request = gh.pull_requests.get(number) head_repo = pull_request.head['repo']['git_url'] pr_head = pull_request.head['sha'] # Clone/Update repository target_path = git.get_repo_path(user, repo, number, config) git.clone_or_update(head_repo, target_path, pr_head) processor = Processor(gh, number, pr_head, target_path, config) processor.load_changes() processor.run_tools(review_config) processor.publish() log.info('Completed lint processing for %s/%s/%s' % (user, repo, number)) except BaseException, e: log.exception(e)
def process_pull_request(user, repo, number, lintrc): """ Starts processing a pull request and running the various lint tools against it. """ log.info('Starting to process lint for %s/%s/%s', user, repo, number) log.debug("lintrc contents '%s'", lintrc) review_config = ReviewConfig(lintrc) if len(review_config.linters()) == 0: log.info('No configured linters, skipping processing.') return gh = github.get_client(config, user, repo) try: log.info('Loading pull request data from github.') pull_request = gh.pull_requests.get(number) head_repo = pull_request.head['repo']['clone_url'] private_repo = pull_request.head['repo']['private'] pr_head = pull_request.head['sha'] # Clone/Update repository target_path = git.get_repo_path(user, repo, number, config) git.clone_or_update(config, head_repo, target_path, pr_head, private_repo) processor = Processor(gh, number, pr_head, target_path, config) processor.load_changes() processor.run_tools(review_config) processor.publish() log.info('Completed lint processing for %s/%s/%s' % ( user, repo, number)) except BaseException, e: log.exception(e)
def test_get_client__retry_opts(): conf = config.copy() conf['GITHUB_OAUTH_TOKEN'] = 'an-oauth-token' conf['GITHUB_CLIENT_RETRY_OPTIONS'] = {'backoff_factor': 42} gh = github.get_client(conf) for proto in ('https://', 'http://'): eq_(gh.session.get_adapter(proto).max_retries.backoff_factor, 42)
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) for proto in ('https://', 'http://'): actual = gh.session.get_adapter(proto).max_retries.backoff_factor self.assertEqual(actual, 42)
def process_hook(func, args): """ Generic helper for processing hook commands. """ credentials = None if args.login_user and args.login_pass: credentials = { 'GITHUB_USER': args.login_user, 'GITHUB_PASSWORD': args.login_pass } with app.app_context(): if credentials: credentials['GITHUB_URL'] = app.config['GITHUB_URL'] gh = github.get_client(credentials, args.user, args.repo) else: gh = github.get_client(app.config, args.user, args.repo) endpoint = url_for('start_review', _external=True) func(gh, endpoint, args.user, args.repo)
def start_review(): event = request.headers.get('X-Github-Event') if event == 'ping': return Response(status=200) try: action = request.json["action"] pull_request = request.json["pull_request"] number = pull_request["number"] base_repo_url = pull_request["base"]["repo"]["git_url"] head_repo_url = pull_request["head"]["repo"]["git_url"] user = pull_request["base"]["repo"]["owner"]["login"] repo = pull_request["base"]["repo"]["name"] target_branch = pull_request["base"]["ref"] except Exception as e: log.error("Got an invalid JSON body. '%s'", e) return Response(status=403, response="You must provide a valid JSON body\n") log.info( "Received GitHub pull request notification for " "%s %s, (%s) from: %s", base_repo_url, number, action, head_repo_url) if action not in ("opened", "synchronize", "reopened", "closed"): log.info("Ignored '%s' action." % action) return Response(status=204) if action == "closed": return close_review(user, repo, pull_request) gh = get_client(app.config, user, repo) try: lintrc = get_lintrc(gh) log.debug("lintrc file contents '%s'", lintrc) except Exception as e: log.warn( "Cannot download .lintrc file for '%s', " "skipping lint checks.", base_repo_url) log.warn(e) return Response(status=204) try: log.info("Scheduling pull request for %s/%s %s", user, repo, number) process_pull_request.delay(user, repo, number, target_branch, lintrc) except: log.error('Could not publish job to celery. Make sure its running.') return Response(status=500) return Response(status=204)
def start_review(): event = request.headers.get('X-Github-Event') if event == 'ping': return Response(status=200) try: action = request.json["action"] pull_request = request.json["pull_request"] number = pull_request["number"] base_repo_url = pull_request["base"]["repo"]["git_url"] head_repo_url = pull_request["head"]["repo"]["git_url"] user = pull_request["base"]["repo"]["owner"]["login"] repo = pull_request["base"]["repo"]["name"] target_branch = pull_request["base"]["ref"] except Exception as e: log.error("Got an invalid JSON body. '%s'", e) return Response(status=403, response="You must provide a valid JSON body\n") log.info("Received GitHub pull request notification for " "%s %s, (%s) from: %s", base_repo_url, number, action, head_repo_url) if action not in ("opened", "synchronize", "reopened", "closed"): log.info("Ignored '%s' action." % action) return Response(status=204) if action == "closed": return close_review(user, repo, pull_request) gh = get_client(app.config, user, repo) try: lintrc = get_lintrc(gh) log.debug("lintrc file contents '%s'", lintrc) except Exception as e: log.warn("Cannot download .lintrc file for '%s', " "skipping lint checks.", base_repo_url) log.warn(e) return Response(status=204) try: log.info("Scheduling pull request for %s/%s %s", user, repo, number) process_pull_request.delay(user, repo, number, target_branch, lintrc) except: log.error('Could not publish job to celery. Make sure its running.') return Response(status=500) return Response(status=204)
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_lintrc(): gh = github.get_client(config, 'markstory', 'lint-review') lintrc = github.get_lintrc(gh) assert lintrc is not None, 'Should get something' assert isinstance(lintrc, str)
def test_get_client(): gh = github.get_client(config, 'markstory', 'lint-review') assert isinstance(gh, Github)
def test_get_client(): gh = github.get_client(config) assert isinstance(gh, GitHub)