2009-03-04 10:59:14 +00:00
|
|
|
|
package Hydra::Controller::Project;
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use base 'Hydra::Base::Controller::ListBuilds';
|
|
|
|
|
use Hydra::Helper::Nix;
|
|
|
|
|
use Hydra::Helper::CatalystUtils;
|
|
|
|
|
|
2012-02-28 15:27:44 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub projectChain :Chained('/') :PathPart('project') :CaptureArgs(1) {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c, $projectName) = @_;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
my $project = $c->model('DB::Projects')->find($projectName, { columns => [
|
|
|
|
|
"me.name",
|
|
|
|
|
"me.displayName",
|
|
|
|
|
"me.description",
|
|
|
|
|
"me.enabled",
|
|
|
|
|
"me.hidden",
|
|
|
|
|
"me.homepage",
|
|
|
|
|
"owner.username",
|
|
|
|
|
"owner.fullname",
|
|
|
|
|
"views.name",
|
|
|
|
|
"releases.name",
|
|
|
|
|
"releases.timestamp",
|
|
|
|
|
"jobsets.name",
|
2013-07-25 17:59:13 -04:00
|
|
|
|
"jobsets.enabled",
|
2013-06-17 12:34:21 -04:00
|
|
|
|
], join => [ 'owner', 'views', 'releases', 'jobsets' ], order_by => { -desc => "releases.timestamp" }, collapse => 1 });
|
|
|
|
|
|
|
|
|
|
if ($project) {
|
|
|
|
|
$c->stash->{project} = $project;
|
|
|
|
|
} else {
|
|
|
|
|
if ($c->action->name eq "project" and $c->request->method eq "PUT") {
|
|
|
|
|
$c->stash->{projectName} = $projectName;
|
|
|
|
|
} else {
|
|
|
|
|
$self->status_not_found(
|
|
|
|
|
$c,
|
|
|
|
|
message => "Project $projectName doesn't exist."
|
|
|
|
|
);
|
|
|
|
|
$c->detach;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-28 15:27:44 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub project :Chained('projectChain') :PathPart('') :Args(0) :ActionClass('REST::ForBrowsers') { }
|
|
|
|
|
|
|
|
|
|
sub project_GET {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
|
|
|
|
$c->stash->{template} = 'project.tt';
|
|
|
|
|
|
2009-10-15 21:35:19 +00:00
|
|
|
|
$c->stash->{views} = [$c->stash->{project}->views->all];
|
2010-09-02 12:21:56 +00:00
|
|
|
|
$c->stash->{jobsets} = [jobsetOverview($c, $c->stash->{project})];
|
2013-02-21 01:23:42 +01:00
|
|
|
|
$c->stash->{releases} = [$c->stash->{project}->releases->search({},
|
|
|
|
|
{order_by => ["timestamp DESC"]})];
|
2013-06-17 12:34:21 -04:00
|
|
|
|
|
|
|
|
|
$self->status_ok(
|
|
|
|
|
$c,
|
|
|
|
|
entity => $c->stash->{project}
|
|
|
|
|
);
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub project_PUT {
|
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
|
|
|
|
if (defined $c->stash->{project}) {
|
|
|
|
|
error($c, "Cannot rename project `$c->stash->{params}->{oldName}' over existing project `$c->stash->{project}->name") if defined $c->stash->{params}->{oldName};
|
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
updateProject($c, $c->stash->{project});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($c->req->looks_like_browser) {
|
|
|
|
|
$c->res->redirect($c->uri_for($self->action_for("project"), [$c->stash->{project}->name]) . "#tabs-configuration");
|
|
|
|
|
} else {
|
|
|
|
|
$self->status_no_content($c);
|
|
|
|
|
}
|
|
|
|
|
} elsif (defined $c->stash->{params}->{oldName}) {
|
|
|
|
|
my $project = $c->model('DB::Projects')->find($c->stash->{params}->{oldName});
|
|
|
|
|
if (defined $project) {
|
|
|
|
|
requireProjectOwner($c, $project);
|
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
updateProject($c, $project);
|
|
|
|
|
});
|
2009-03-04 10:59:14 +00:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
my $uri = $c->uri_for($self->action_for("project"), [$project->name]);
|
|
|
|
|
|
|
|
|
|
if ($c->req->looks_like_browser) {
|
|
|
|
|
$c->res->redirect($uri . "#tabs-configuration");
|
|
|
|
|
} else {
|
|
|
|
|
$self->status_created(
|
|
|
|
|
$c,
|
|
|
|
|
location => "$uri",
|
|
|
|
|
entity => { name => $project->name, uri => "$uri", type => "project" }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$self->status_not_found(
|
|
|
|
|
$c,
|
|
|
|
|
message => "Project $c->stash->{params}->{oldName} doesn't exist."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
requireMayCreateProjects($c);
|
|
|
|
|
error($c, "Invalid project name: ‘$c->stash->{projectName}’") if $c->stash->{projectName} !~ /^$projectNameRE$/;
|
|
|
|
|
|
|
|
|
|
my $project;
|
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
# Note: $projectName is validated in updateProject,
|
|
|
|
|
# which will abort the transaction if the name isn't
|
|
|
|
|
# valid. Idem for the owner.
|
|
|
|
|
my $owner = $c->user->username;
|
|
|
|
|
$project = $c->model('DB::Projects')->create(
|
|
|
|
|
{name => $c->stash->{projectName}, displayname => "", owner => $owner});
|
|
|
|
|
updateProject($c, $project);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
my $uri = $c->uri_for($self->action_for("project"), [$project->name]);
|
|
|
|
|
if ($c->req->looks_like_browser) {
|
|
|
|
|
$c->res->redirect($uri . "#tabs-configuration");
|
|
|
|
|
} else {
|
|
|
|
|
$self->status_created(
|
|
|
|
|
$c,
|
|
|
|
|
location => "$uri",
|
|
|
|
|
entity => { name => $project->name, uri => "$uri", type => "project" }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub edit : Chained('projectChain') PathPart Args(0) {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
2009-03-13 15:41:19 +00:00
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
2009-03-04 10:59:14 +00:00
|
|
|
|
|
2013-02-21 01:12:57 +01:00
|
|
|
|
$c->stash->{template} = 'edit-project.tt';
|
2009-03-04 10:59:14 +00:00
|
|
|
|
$c->stash->{edit} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub submit : Chained('projectChain') PathPart Args(0) {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
2009-04-02 16:15:57 +00:00
|
|
|
|
requirePost($c);
|
2013-02-26 17:36:49 +01:00
|
|
|
|
if (($c->request->params->{submit} // "") eq "delete") {
|
2013-06-11 16:57:22 +02:00
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
$c->stash->{project}->jobsetevals->delete_all;
|
|
|
|
|
$c->stash->{project}->builds->delete_all;
|
|
|
|
|
$c->stash->{project}->delete;
|
|
|
|
|
});
|
2013-02-26 17:36:49 +01:00
|
|
|
|
return $c->res->redirect($c->uri_for("/"));
|
2012-04-17 16:53:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
my $newName = trim $c->stash->{params}->{name};
|
|
|
|
|
my $oldName = trim $c->stash->{project}->name;
|
|
|
|
|
unless ($oldName eq $newName) {
|
|
|
|
|
$c->stash->{params}->{oldName} = $oldName;
|
|
|
|
|
$c->stash->{projectName} = $newName;
|
|
|
|
|
undef $c->stash->{project};
|
|
|
|
|
}
|
|
|
|
|
project_PUT($self, $c);
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-24 14:22:59 +00:00
|
|
|
|
sub requireMayCreateProjects {
|
|
|
|
|
my ($c) = @_;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2009-03-24 14:22:59 +00:00
|
|
|
|
requireLogin($c) if !$c->user_exists;
|
|
|
|
|
|
|
|
|
|
error($c, "Only administrators or authorised users can perform this operation.")
|
|
|
|
|
unless $c->check_user_roles('admin') || $c->check_user_roles('create-projects');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-04 11:03:43 +00:00
|
|
|
|
sub create : Path('/create-project') {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
2009-03-24 14:22:59 +00:00
|
|
|
|
requireMayCreateProjects($c);
|
2009-03-04 10:59:14 +00:00
|
|
|
|
|
2013-02-21 01:12:57 +01:00
|
|
|
|
$c->stash->{template} = 'edit-project.tt';
|
2009-03-04 10:59:14 +00:00
|
|
|
|
$c->stash->{create} = 1;
|
|
|
|
|
$c->stash->{edit} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-04 11:03:43 +00:00
|
|
|
|
sub create_submit : Path('/create-project/submit') {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
$c->stash->{projectName} = trim $c->stash->{params}->{name};
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
project_PUT($self, $c);
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_jobset : Chained('projectChain') PathPart('create-jobset') Args(0) {
|
2009-04-02 16:15:57 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2013-02-21 02:33:57 +01:00
|
|
|
|
$c->stash->{template} = 'edit-jobset.tt';
|
2009-04-02 16:15:57 +00:00
|
|
|
|
$c->stash->{create} = 1;
|
|
|
|
|
$c->stash->{edit} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_jobset_submit : Chained('projectChain') PathPart('create-jobset/submit') Args(0) {
|
2009-04-02 16:15:57 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
$c->stash->{jobsetName} = trim $c->stash->{params}->{name};
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
Hydra::Controller::Jobset::jobset_PUT($self, $c);
|
2009-04-02 16:15:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
|
sub updateProject {
|
|
|
|
|
my ($c, $project) = @_;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2009-04-02 16:15:57 +00:00
|
|
|
|
my $owner = $project->owner;
|
2013-06-17 12:34:21 -04:00
|
|
|
|
if ($c->check_user_roles('admin') and defined $c->stash->{params}->{owner}) {
|
|
|
|
|
$owner = trim $c->stash->{params}->{owner};
|
2009-03-04 10:59:14 +00:00
|
|
|
|
error($c, "Invalid owner: $owner")
|
|
|
|
|
unless defined $c->model('DB::Users')->find({username => $owner});
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
my $projectName = $c->stash->{projectName} or $project->name;
|
2012-04-17 16:53:11 +02:00
|
|
|
|
error($c, "Invalid project name: ‘$projectName’") if $projectName !~ /^$projectNameRE$/;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
my $displayName = trim $c->stash->{params}->{displayname};
|
2012-04-17 16:53:11 +02:00
|
|
|
|
error($c, "Invalid display name: $displayName") if $displayName eq "";
|
|
|
|
|
|
2009-04-02 16:15:57 +00:00
|
|
|
|
$project->update(
|
|
|
|
|
{ name => $projectName
|
|
|
|
|
, displayname => $displayName
|
2013-06-17 12:34:21 -04:00
|
|
|
|
, description => trim($c->stash->{params}->{description})
|
|
|
|
|
, homepage => trim($c->stash->{params}->{homepage})
|
|
|
|
|
, enabled => defined $c->stash->{params}->{enabled} ? 1 : 0
|
|
|
|
|
, hidden => defined $c->stash->{params}->{visible} ? 0 : 1
|
2009-04-02 16:15:57 +00:00
|
|
|
|
, owner => $owner
|
|
|
|
|
});
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Hydra::Base::Controller::ListBuilds needs this.
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub get_builds : Chained('projectChain') PathPart('') CaptureArgs(0) {
|
2009-03-04 10:59:14 +00:00
|
|
|
|
my ($self, $c) = @_;
|
2009-03-13 15:41:19 +00:00
|
|
|
|
$c->stash->{allBuilds} = $c->stash->{project}->builds;
|
2009-04-03 15:37:21 +00:00
|
|
|
|
$c->stash->{jobStatus} = $c->model('DB')->resultset('JobStatusForProject')
|
|
|
|
|
->search({}, {bind => [$c->stash->{project}->name]});
|
2009-04-08 22:08:00 +00:00
|
|
|
|
$c->stash->{allJobsets} = $c->stash->{project}->jobsets;
|
|
|
|
|
$c->stash->{allJobs} = $c->stash->{project}->jobs;
|
2009-04-03 15:37:21 +00:00
|
|
|
|
$c->stash->{latestSucceeded} = $c->model('DB')->resultset('LatestSucceededForProject')
|
|
|
|
|
->search({}, {bind => [$c->stash->{project}->name]});
|
2009-03-13 15:41:19 +00:00
|
|
|
|
$c->stash->{channelBaseName} = $c->stash->{project}->name;
|
2009-03-04 10:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_view_submit : Chained('projectChain') PathPart('create-view/submit') Args(0) {
|
2009-10-20 12:26:39 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2009-10-20 12:26:39 +00:00
|
|
|
|
my $viewName = $c->request->params->{name};
|
|
|
|
|
|
|
|
|
|
my $view;
|
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
# Note: $viewName is validated in updateView, which will abort
|
|
|
|
|
# the transaction if the name isn't valid.
|
|
|
|
|
$view = $c->stash->{project}->views->create({name => $viewName});
|
|
|
|
|
Hydra::Controller::View::updateView($c, $view);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$c->res->redirect($c->uri_for($c->controller('View')->action_for('view_view'),
|
|
|
|
|
[$c->stash->{project}->name, $view->name]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_view : Chained('projectChain') PathPart('create-view') Args(0) {
|
2009-10-20 12:26:39 +00:00
|
|
|
|
my ($self, $c) = @_;
|
|
|
|
|
|
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
|
|
|
|
|
|
|
|
|
$c->stash->{template} = 'edit-view.tt';
|
|
|
|
|
$c->stash->{create} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_release : Chained('projectChain') PathPart('create-release') Args(0) {
|
2009-10-21 15:44:17 +00:00
|
|
|
|
my ($self, $c) = @_;
|
2009-10-23 09:58:23 +00:00
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
|
|
|
|
$c->stash->{template} = 'edit-release.tt';
|
|
|
|
|
$c->stash->{create} = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-06-17 12:34:21 -04:00
|
|
|
|
sub create_release_submit : Chained('projectChain') PathPart('create-release/submit') Args(0) {
|
2009-10-23 09:58:23 +00:00
|
|
|
|
my ($self, $c) = @_;
|
2013-01-22 14:41:02 +01:00
|
|
|
|
|
2009-10-23 09:58:23 +00:00
|
|
|
|
requireProjectOwner($c, $c->stash->{project});
|
|
|
|
|
|
|
|
|
|
my $releaseName = $c->request->params->{name};
|
|
|
|
|
|
|
|
|
|
my $release;
|
|
|
|
|
txn_do($c->model('DB')->schema, sub {
|
|
|
|
|
# Note: $releaseName is validated in updateRelease, which will
|
|
|
|
|
# abort the transaction if the name isn't valid.
|
|
|
|
|
$release = $c->stash->{project}->releases->create(
|
|
|
|
|
{ name => $releaseName
|
|
|
|
|
, timestamp => time
|
|
|
|
|
});
|
|
|
|
|
Hydra::Controller::Release::updateRelease($c, $release);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$c->res->redirect($c->uri_for($c->controller('Release')->action_for('view'),
|
|
|
|
|
[$c->stash->{project}->name, $release->name]));
|
2009-10-21 15:44:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
|
1;
|