Example #1
0
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']
            repo = github.get_repository(
                credentials,
                args.user,
                args.repo)
        else:
            repo = github.get_repository(
                app.config,
                args.user,
                args.repo)
        endpoint = url_for('start_review', _external=True)
    func(repo, endpoint)
Example #2
0
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']
            repo = github.get_repository(
                credentials,
                args.user,
                args.repo)
        else:
            repo = github.get_repository(
                app.config,
                args.user,
                args.repo)
        endpoint = url_for('start_review', _external=True)
    func(repo, endpoint)
Example #3
0
 def repository(self):
     """Get the underlying repository model
     """
     if not self.repo:
         self.repo = github.get_repository(self.config, self.user,
                                           self.repo_name)
     return self.repo
Example #4
0
 def repository(self):
     """Get the underlying repository model
     """
     self.repo = github.get_repository(
         self.config,
         self.user,
         self.repo_name)
     return self.repo
Example #5
0
    def test_get_repository_uses_credentials_with_client_to_produce_repo(self):
        mock_client = Mock()
        mock_config = Mock()
        mock_repo = Mock()

        mock_get_client = conditionally_return(mock_client, mock_config)
        mock_client.repository = conditionally_return(mock_repo, owner='user', repository='repo')

        with patch('lintreview.github.get_client', mock_get_client):
            self.assertEqual(github.get_repository(mock_config, 'user', 'repo'), mock_repo)
Example #6
0
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"]
        head_repo_ref = pull_request["head"]["ref"]
        user = pull_request["base"]["repo"]["owner"]["login"]
        head_user = pull_request["head"]["repo"]["owner"]["login"]
        repo = pull_request["base"]["repo"]["name"]
        head_repo = pull_request["head"]["repo"]["name"]
    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_repository(app.config, head_user, head_repo)
    try:
        lintrc = get_lintrc(gh, head_repo_ref)
        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, lintrc)
    except:
        log.error('Could not publish job to celery. Make sure its running.')
        return Response(status=500)
    return Response(status=204)
Example #7
0
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"]
        head_repo_ref = pull_request["head"]["ref"]
        user = pull_request["base"]["repo"]["owner"]["login"]
        head_user = pull_request["head"]["repo"]["owner"]["login"]
        repo = pull_request["base"]["repo"]["name"]
        head_repo = pull_request["head"]["repo"]["name"]
    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_repository(app.config, head_user, head_repo)
    try:
        lintrc = get_lintrc(gh, head_repo_ref)
        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, lintrc)
    except:
        log.error('Could not publish job to celery. Make sure its running.')
        return Response(status=500)
    return Response(status=204)
Example #8
0
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 = build_review_config(lintrc, config)

    if len(review_config.linters()) == 0:
        log.info('No configured linters, skipping processing.')
        return

    try:
        log.info(
            'Loading pull request data from github. user=%s '
            'repo=%s number=%s', user, repo, number)
        gh = github.get_repository(config, user, repo)
        pull_request = gh.pull_request(number)

        pr_dict = pull_request.as_dict()
        head_repo = pr_dict['head']['repo']['clone_url']
        private_repo = pr_dict['head']['repo']['private']
        pr_head = pr_dict['head']['sha']

        target_branch = pr_dict['base']['ref']
        if target_branch in review_config.ignore_branches():
            log.info(
                'Pull request into ignored branch %s, skipping processing.' %
                target_branch)
            return

        # 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 as e:
        log.exception(e)
Example #9
0
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)
    lintrc_defaults = get_lintrc_defaults(config)
    review_config = ReviewConfig(lintrc, lintrc_defaults)

    if len(review_config.linters()) == 0:
        log.info('No configured linters, skipping processing.')
        return

    try:
        log.info('Loading pull request data from github. user=%s '
                 'repo=%s number=%s', user, repo, number)
        gh = github.get_repository(config, user, repo)
        pull_request = gh.pull_request(number)

        pr_dict = pull_request.as_dict()
        head_repo = pr_dict['head']['repo']['clone_url']
        private_repo = pr_dict['head']['repo']['private']
        pr_head = pr_dict['head']['sha']

        target_branch = pr_dict['base']['ref']
        if target_branch in review_config.ignore_branches():
            log.info('Pull request into ignored branch %s, skipping processing.' %
                     target_branch)
            return

        # 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)
Example #10
0
def main():
    parser = argparse.ArgumentParser(description='Scan the code for issues and report as comments in GitHub')
    parser.add_argument('--pull-request', dest="pull_request", help="The PR to scan", type=int)
## Disabled for now
#    parser.add_argument('--commit', help="The commit to scan", type=str)
    parser.add_argument('--user', help="The GitHub User who owns the repo",
            type=str, default=app.config['GITHUB_USER'])
    parser.add_argument('--repo', help="The GitHub Repo name", type=str)
    runtime_config = parser.parse_args()

    gh = get_repository(app.config, runtime_config.user, runtime_config.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 2

    if (runtime_config.pull_request > 0):
        tasks.process_pull_request(runtime_config.user, runtime_config.repo, runtime_config.pull_request, lintrc)