Exemplo n.º 1
0
    def test_build_successfully(self):
        project_repo = ProjectRepository()
        project = project_repo.create("pyoc", "nosetests", "git://github.com/heynemann/pyoc.git")
        repo_path = join(root_path, "tests", "functional", "repo")
        
        try:
            shutil.rmtree(repo_path)
        except:
            print "The repository folder for the test didn't exist previously. This is not an error!"
            
        service = BuildService(base_path=repo_path)
        
        build = service.build_project(project.id)

        self.failUnless(build.scm_status == ScmResult.Created or build.scm_status == ScmResult.Updated)
Exemplo n.º 2
0
    def test_build_successfully(self):
        project = self.create_project(name="pyoc", build_script="nosetests", scm_repository="git://github.com/heynemann/pyoc.git", monitor_changes=True)

        elixir.session.flush()
        elixir.session.commit()

        repo_path = join(root_path, "tests", "functional", "repo")
        
        try:
            shutil.rmtree(repo_path)
        except:
            print "The repository folder for the test didn't exist previously. This is not an error!"
            
        service = BuildService(base_path=repo_path)
        
        build = service.build_project(project.id)

        self.failUnless(build.scm_status == ScmResult.Created or build.scm_status == ScmResult.Updated)
Exemplo n.º 3
0
    def test_build(self):
        project = Project("Test Project", "make test", "git_repo")
        execute_result = ExecuteResult(project.build_script, "Ran successfully", "", 0)
        scm_result = ScmResult(ScmResult.Created)

        repository_mock = self.mock.CreateMock(ProjectRepository)
        scm_mock = self.mock.CreateMock(GitRepository)
        executer_mock = self.mock.CreateMock(ShellExecuter)

        repository_mock.get(1).AndReturn(project)
        repository_mock.update(project)
        scm_mock.create_or_update(project).AndReturn(scm_result)
        executer_mock.execute(project.build_script).AndReturn(execute_result)
        
        self.mock.ReplayAll()
        
        service = BuildService(repository=repository_mock, scm=scm_mock, executer=executer_mock)
        
        build = service.build_project(1)
        
        self.assertEqual(build.status, BuildService.Success)
        self.mock.VerifyAll()
Exemplo n.º 4
0
    def test_build(self):
        project = Project()
        project.id = 1
        project.name = "Test Project"
        project.build_script = "make test"
        project.scm_repository = "git_repo"
        project.tabs = []
        project.file_locators = []

        project2 = Project()
        project.id = 2
        project2.name = "Test Project 2"
        project2.build_script = "make test"
        project2.scm_repository = "git_repo"
        project2.tabs = []
        project2.file_locators = []

        execute_result = ExecuteResult(project.build_script, "Ran successfully", 0)
        scm_result = ScmResult(
            ScmResult.Created,
            "some/path/",
            {
                "commit_number": "1234abcd",
                "author": "Bernardo",
                "author_date": None,
                "committer": "Bernardo",
                "committer_date": None,
                "subject": "Changed some stuff",
            },
            "some log",
        )

        pipeline = Pipeline()
        pipeline.items = []
        item = PipelineItem()
        item.project = project
        pipeline.items.append(item)
        item2 = PipelineItem()
        item2.project = project2
        pipeline.items.append(item)

        repository_mock = self.mock.CreateMock(ProjectRepository)
        pipeline_repository_mock = self.mock.CreateMock(PipelineRepository)
        scm_mock = self.mock.CreateMock(GitRepository)
        executer_mock = self.mock.CreateMock(ShellExecuter)

        repository_mock.get(1).AndReturn(project)
        repository_mock.update(project, [], [])
        scm_mock.create_or_update(project).AndReturn(scm_result)
        executer_mock.execute(project.build_script, "some/path/").AndReturn(execute_result)
        pipeline_repository_mock.get_all_pipelines_for(project).AndReturn((pipeline,))

        self.mock.ReplayAll()

        service = BuildService(
            repository=repository_mock,
            pipeline_repository=pipeline_repository_mock,
            scm=scm_mock,
            executer=executer_mock,
        )

        build = service.build_project(1)

        self.assertEqual(build.status, BuildService.Success)
        self.assertEqual(build.commit_number, "1234abcd")
        self.assertEqual(build.commit_author, "Bernardo")
        self.assertEqual(build.commit_committer, "Bernardo")
        self.assertEqual(build.commit_text, "Changed some stuff")

        self.mock.VerifyAll()
Exemplo n.º 5
0
 def __init__(self):
     self.repository = ProjectRepository()
     self.build_service = BuildService()
Exemplo n.º 6
0
class ProjectController(BaseController):
    def __init__(self):
        self.repository = ProjectRepository()
        self.build_service = BuildService()

    @authenticated()
    @template.output("create_project.html")
    def new(self):
        return template.render(authenticated=self.authenticated(), project=None)

    @authenticated()
    @template.output("create_project.html")
    def edit(self, project_id):
        project = self.repository.get(project_id)
        return template.render(authenticated=self.authenticated(), project=project)

    def __process_tabs_for(self, data):
        tabs = None
        if data.has_key("additional_tab_name"):
            tab_names = [name for name in data["additional_tab_name"] if name != u""]
            tab_commands = [command for command in data["additional_tab_command"] if command != u""]
            tab_content_types = [
                content_type for content_type in data["additional_tab_content_type"] if content_type != u""
            ][1:]

            if len(tab_names) > len(tab_commands) or len(tab_names) > len(tab_content_types):
                raise ValueError("The number of tabs, commands and content types MUST be the same.")

            tabs = []
            for tab_index in range(len(tab_names)):
                tab = ProjectTab(
                    name=tab_names[tab_index],
                    command=tab_commands[tab_index],
                    content_type=tab_content_types[tab_index],
                )
                tabs.append(tab)
        return tabs

    def __process_file_locators_for(self, data):
        locators = None
        if data.has_key("additional_file_locator"):
            locators = [locator for locator in data["additional_file_locator"] if locator != u""]
        return locators

    @authenticated()
    def create(self, name, build_script, scm_repository, monitor_changes=None, **data):
        project = self.repository.create(
            name=name,
            build_script=build_script,
            scm_repository=scm_repository,
            monitor_changes=not monitor_changes is None,
            tabs=self.__process_tabs_for(data),
            file_locators=self.__process_file_locators_for(data),
        )
        PluginEvents.on_project_created(project)
        raise cherrypy.HTTPRedirect("/")

    @authenticated()
    def update(self, project_id, name, build_script, scm_repository, monitor_changes=None, **data):
        project = self.repository.get(project_id)
        project.name = name
        project.build_script = build_script
        project.scm_repository = scm_repository
        project.monitor_changes = not monitor_changes is None
        self.repository.update(
            project, self.__process_tabs_for(data), file_locators=self.__process_file_locators_for(data)
        )
        PluginEvents.on_project_updated(project)
        raise cherrypy.HTTPRedirect("/")

    @authenticated()
    def delete(self, project_id):
        project = self.repository.get(project_id)
        self.repository.delete(project_id)
        self.build_service.delete_scm_repository(project)
        PluginEvents.on_project_deleted(project)
        raise cherrypy.HTTPRedirect("/")

    @authenticated()
    def build(self, project_id):
        print "Adding project %s to the queue" % project_id
        SkinkContext.current().build_queue.append(project_id)
        raise cherrypy.HTTPRedirect("/project/%s" % project_id)

    @template.output("current_build.html")
    def current_build_report(self, **data):
        return template.render(authenticated=self.authenticated())

    def current_status(self, **data):
        ctx = SkinkContext.current()
        result = {}
        result["project"] = ctx.current_project and ctx.current_project.name or ""
        result["project_id"] = ctx.current_project and ctx.current_project.id or ""
        result["command"] = ctx.current_command
        result["log"] = ctx.current_log and u"<br />".join(unicode(ctx.current_log).splitlines()[-30:]) or ""

        return demjson.encode(result)

    def build_status(self, **data):
        ctx = SkinkContext.current()
        projects = self.repository.get_all()
        projects_being_built = [int(project_id) for project_id in ctx.projects_being_built]
        result = {}
        for project in projects:
            if project.id in projects_being_built:
                result[project.id] = (project.name, "BUILDING")
            else:
                result[project.id] = (
                    project.name,
                    (project.last_builds is not None and len(project.last_builds) > 0) and "BUILT" or "UNKNOWN",
                )

        return "\n".join(["%s=%s@@%s" % (k, v[0], v[1]) for k, v in result.items()])

    @template.output("project_details.html")
    def details(self, project_id):
        return self.render_details(project_id)

    @template.output("project_details.html")
    def build_details(self, project_id, build_id):
        return self.render_details(project_id, build_id)

    def build_tab_details(self, build_tab_id):
        return self.repository.get_build_tab_by_id(build_tab_id=build_tab_id).log

    def build_file_details(self, build_file_id):
        build_file = self.repository.get_build_file_by_id(build_file_id=build_file_id)
        response.headers["Content-Type"] = "application/x-download"
        response.headers["Content-Disposition"] = 'attachment; filename="%s"' % build_file.name
        response.headers["Accept-Ranges"] = "bytes"
        response.headers["Content-Length"] = len(build_file.content)
        response.body = build_file.content
        return response.body

    def get_all_status(self, **data):
        projects = self.repository.get_all()
        serialized_projects = []
        for project in projects:
            serialized_projects.append(project.to_dict())
        values = {}
        values["projects"] = serialized_projects

        cherrypy.response.headers["Content-Type"] = "application/json"
        return demjson.encode(values)

    def render_details(self, project_id, build_id=None):
        project = self.repository.get(project_id)
        if not build_id:
            build = project.last_builds and project.last_builds[0] or None
        else:
            build = project.get_build_by_id(int(build_id))
        build_log = ""
        if build and build.log:
            build_log = highlight(build.log, BashLexer(), HtmlFormatter())
        return template.render(
            authenticated=self.authenticated(), project=project, current_build=build, build_log=build_log
        )