from buildbot.steps.source import Git as BuildbotGit
from django.conf import settings

from cthulhubot.commands.interface import Command
from cthulhubot.mongo import get_database_name

class ComputeGitVersion(Command):
    identifier = 'cthulhubot-compute-git'
    name = {
        'basic' : u"Compute version from git",
        'running' : u'Computing version from git',
        'succeeded' : u'Version computed',
        'failed' : u'Failed to compute version',
    }

    parameters = {}

    command = ["python", "setup.py", "compute_version_git"]

class Git(Command):
    identifier = 'cthulhubot-git'
    name = {
        'basic' : u"Compute version from git",
        'running' : u'Computing version from git',
        'succeeded' : u'Version computed',
        'failed' : u'Failed to compute version',
    }

    parameters = {
        'repository' : {
            'help' : u'Path to git repository',
            'value' : None,
        },
        'mode' : {
            'help' : u'Git checkout mode. Default to "export"',
            'value' : "export",
            'required' : True,
        },
        'branch' : {
            'help' : u'Which branch should I check out? Default to master.',
            'value' : "master",
            'required' : True,
        },
    }

    def get_buildbot_command(self, config=None, project=None, **kwargs):
        config = config or {}
        repourl = config.get('repository', None) or self.config.get('repository', None)
        if not repourl:
            if not project:
                raise ValueError("Repository URI must be somehow given")
            repourl = project.repository_uri

        mode = config.get('mode', None) or self.config.get('mode', None)
        branch = config.get('branch', None) or self.config.get('branch', None)
        return BuildbotGit(repourl=repourl, mode=mode, branch=branch)


class UpdateRepositoryInformation(Command):
    identifier = 'cthulhubot-update-repository-info'
    name = {
        'basic' : u"Update repository information",
        'running' : u'Updating repository information',
        'succeeded' : u'Repository information updated',
        'failed' : u'Failed to update repository information',
    }

    parameters = {}

    def _get_command_skeleton(self):
        command = [
                "python", "setup.py", "save_repository_information_git",
                "--mongodb-host=%s" % getattr(settings, "MONGODB_HOST", "localhost"),
                "--mongodb-port=%s" % getattr(settings, "MONGODB_PORT", 27017),
                "--mongodb-database=%s" % get_database_name(),
                "--mongodb-collection=repository",
        ]
        if getattr(settings, "MONGODB_USERNAME", None):
                command.append("--mongodb-username=%s" % getattr(settings, "MONGODB_USERNAME", None))
        if getattr(settings, "MONGODB_PASSWORD", None):
                command.append("--mongodb-password=%s" % getattr(settings, "MONGODB_PASSWORD", None))
        return command

    command = property(fget=_get_command_skeleton)

@@ -46,16 +46,63 @@
 dbotGit%0A
+from buildbot.steps.shell import ShellCommand%0A%0A
 from dja
@@ -2313,16 +2313,25 @@
 (self):%0A
+        %0A
         
@@ -2342,16 +2342,16 @@
 and = %5B%0A
-
         
@@ -2695,16 +2695,33 @@
 itory%22,%0A
+                %0A
         
@@ -3113,8 +3113,389 @@
 eleton)%0A
+%0A    def get_shell_command(self, config=None, project=None, **kwargs):%0A        command = super(UpdateRepositoryInformation, self).get_shell_command(config=config, project=project, **kwargs)%0A        repourl = config.get('repository', None) or self.config.get('repository', None)%0A%0A        assert repourl%0A%0A        command.append(%22--repository-url=%25s%22 %25 repourl)%0A        return command

Append repositoryuri