Setting up the bot to handle Git repositories and pass the repository paths to the tools

Review Request #5869 — Created May 23, 2014 and discarded — Latest diff uploaded

Information

ReviewBot
release-0.2.x

Reviewers

Sets up repository on ReviewBot load
- stores repositories in root folder
- updates repositories programatically

Supports Git, but easy to extend for new types of repositories
- switch to base commit
- use SCM tool to apply patch to temp repository

Fixed bug in make_tempfile

Delete temporary folders

Added cached_property to ReviewBot
- turned File properties into cached properties
- cached path property on Repository_Clone

Added new Tool base class to support access to entire repository
- sending repository path to tool
- comment on a file by its path
- created example ReviewBot tool to look at entire repository
- added more Queues to Celery so that a bot only picks up a task that it has the repository for

Added flag to extension model to signal if ReviewBot tool supports entire directory
- added UI in admin panel to show this

The following configuration was used in reviewbot/config.py

from reviewbot.revisioning.constants import GIT


BOT_REPOSITORY_SETTINGS = {
    'http://localhost:8080/': {
        'ReviewBot': {
            'type': GIT,
            'url': 'git://github.com/reviewboard/ReviewBot.git',
        },
    },
}

The following tool was added at the path reviewbot/tools/pep8repo.py

import os

from reviewbot.tools import RepositoryTool
from reviewbot.tools.process import execute
from reviewbot.utils import is_exe_in_path


class PEP8RepoTool(RepositoryTool):
    name = 'PEP8 Style Checker - Entire Repository'
    version = '0.2'
    description = "Checks code for style errors using the PEP8 tool."

    def check_dependencies(self):
        return is_exe_in_path('pep8')

    def handle_repository(self, repository_working_directory):
        path = repository_working_directory.path
        if not path:
            return False

        for root, subFolders, files in os.walk(path):
            for file_path in files:
                if not file_path.endswith('.py'):
                    continue

                full_path = os.path.join(root, file_path)

                output = execute(
                    [
                        'pep8',
                        '-r',
                        full_path
                    ],
                    split_lines=True,
                    ignore_errors=True)

                for line in output:
                    parsed = line.split(':', 3)
                    lnum = int(parsed[1])
                    col = int(parsed[2])
                    msg = parsed[3]
                    repository_working_directory.comment(
                        full_path,
                        'Col: %s\n%s' % (col, msg),
                        lnum)

        return True

I used a server running on "http://localhost:8080" to test my changes. The setup process is the same as if you were setting up ReviewBot normally. When refreshing the list of tools, PEP8 Style Checker - Entire Repository should show up with a value of true in the 'working directory required' column. To test I set both it and the normal PEP8 Style Checker to run automatically to compare output.

Tests:
1. On the second startup of the bot, it should take less time than the second to start Celery (if you include the reviewboard repository it is more noticeable). This indicates that the repository is being stored between bot sessions as expected. These repositories should stored in a folder system ~/ReviewBotRepositories/server_url/repo_name/.
2. In the celery startup output, it should be shown that one of the Celery Queues the bot is listening on is: pep8repo.0.2.http://localhost:8080/.ReviewBot exchange=pep8repo.0.2.http://localhost:8080/.ReviewBot(direct) key=pep8repo.0.2.http://localhost:8080/.ReviewBot
3. The reviewboard page /admin/extensions/reviewbotext.extension.ReviewBotExtension/db/reviewbotext/reviewbottool/ should show a column called Working directory required. For the tool 'PEP8 Style Checker - Entire Repository' there should be a green check.
5. In the middle of a review being made (celery.contrib.rdb.set_trace() was used to pause in the middle), there should be a file in /tmp/ that contains the patch to apply to the repository to get the code as it is in the review request.
4. In the middle of a review being made (celery.contrib.rdb.set_trace() was used to pause in the middle), there should be a folder in /tmp/ that contains the ReviewBot repository without the .git folder. The state of the repository should be identical to that of the review request.
5. After a publishing a review, checking the /tmp/ folder of the bot should show there are no temp files or directories.
6. Both the normal PEP8 and the full repository PEP8 should complete without error. The resulting code comments should be identical. Of particular note, there should be no comments on files not changed in the review request.