Use the REST API in the web interface for editing projects

This commit is contained in:
Eelco Dolstra 2013-10-03 17:23:41 +02:00
parent 8355dc57ee
commit ae13dd3e67
4 changed files with 84 additions and 55 deletions

View file

@ -1,5 +1,6 @@
package Hydra::Controller::Project;
use utf8;
use strict;
use warnings;
use base 'Hydra::Base::Controller::ListBuilds';
@ -51,15 +52,13 @@ sub project_PUT {
if (defined $c->stash->{project}) {
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);
}
my $uri = $c->uri_for($self->action_for("project"), [$c->stash->{project}->name]) . "#tabs-configuration";
$self->status_ok($c, entity => { redirect => "$uri" });
}
else {
@ -72,23 +71,32 @@ sub project_PUT {
# valid. Idem for the owner.
my $owner = $c->user->username;
$project = $c->model('DB::Projects')->create(
{ name => ".tmp.$$." . int(rand(100000)), displayname => "", owner => $owner });
{ name => ".tmp", 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" }
);
}
$self->status_created($c,
location => "$uri",
entity => { name => $project->name, uri => "$uri", redirect => "$uri", type => "project" });
}
}
sub project_DELETE {
my ($self, $c) = @_;
requireProjectOwner($c, $c->stash->{project});
txn_do($c->model('DB')->schema, sub {
$c->stash->{project}->jobsetevals->delete_all;
$c->stash->{project}->builds->delete_all;
$c->stash->{project}->delete;
});
my $uri = $c->res->redirect($c->uri_for("/"));
$self->status_ok($c, entity => { redirect => "$uri" });
}
sub edit : Chained('projectChain') PathPart Args(0) {
my ($self, $c) = @_;
@ -100,25 +108,6 @@ sub edit : Chained('projectChain') PathPart Args(0) {
}
sub submit : Chained('projectChain') PathPart Args(0) {
my ($self, $c) = @_;
requirePost($c);
requireProjectOwner($c, $c->stash->{project});
if (($c->request->params->{submit} // "") eq "delete") {
txn_do($c->model('DB')->schema, sub {
$c->stash->{project}->jobsetevals->delete_all;
$c->stash->{project}->builds->delete_all;
$c->stash->{project}->delete;
});
return $c->res->redirect($c->uri_for("/"));
}
project_PUT($self, $c);
}
sub requireMayCreateProjects {
my ($c) = @_;
@ -140,12 +129,6 @@ sub create : Path('/create-project') {
}
sub create_submit : Path('/create-project/submit') {
my ($self, $c) = @_;
project_PUT($self, $c);
}
sub create_jobset : Chained('projectChain') PathPart('create-jobset') Args(0) {
my ($self, $c) = @_;
@ -178,9 +161,9 @@ sub updateProject {
}
my $projectName = $c->stash->{params}->{name};
error($c, "Invalid project name $projectName.") if $projectName !~ /^$projectNameRE$/;
error($c, "Invalid project identifier $projectName.") if $projectName !~ /^$projectNameRE$/;
error($c, "Cannot rename project to $projectName since that name is already taken.")
error($c, "Cannot rename project to $projectName since that identifier is already taken.")
if $projectName ne $project->name && defined $c->model('DB::Projects')->find($projectName);
my $displayName = trim $c->stash->{params}->{displayname};

View file

@ -1,7 +1,7 @@
[% WRAPPER layout.tt title=(create ? "New project" : "Editing project $project.name") %]
[% PROCESS common.tt %]
<form class="form-horizontal" action="[% IF create %][% c.uri_for('/create-project/submit') %][% ELSE %][% c.uri_for('/project' project.name 'submit') %][% END %]" method="post">
<form class="form-horizontal">
<fieldset>
@ -54,21 +54,27 @@
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<button id="submit-project" type="submit" class="btn btn-primary">
<i class="icon-ok icon-white"></i>
[%IF create %]Create[% ELSE %]Apply changes[% END %]
</button>
[% IF !create %]
<button id="delete-project" type="submit" class="btn btn-danger" name="submit" value="delete">
<i class="icon-trash icon-white"></i>
Delete this project
</button>
<script type="text/javascript">
$("#delete-project").click(function() {
return confirm("Are you sure you want to delete this project?");
$("#submit-project").click(function() {
requestJSON({
[% IF create %]
url: "[% c.uri_for('/project' '.new') %]",
[% ELSE %]
url: "[% c.uri_for('/project' project.name) %]",
[% END %]
data: $(this).parents("form").serialize(),
type: 'PUT',
success: function(data) {
window.location = data.redirect;
},
});
return false;
});
</script>
[% END %]
</button>
</div>
</fieldset>

View file

@ -9,7 +9,8 @@
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
[% INCLUDE menuItem uri = c.uri_for('/project' project.name 'edit') title="Edit configuration" %]
<li><a href="[% c.uri_for('/project' project.name 'edit') %]"><i class="icon-edit icon-black"></i> Edit configuration</a></li>
<li><a href="javascript:deleteProject()"><i class="icon-trash icon-black"></i> Delete this project</a></li>
[% INCLUDE menuItem uri = c.uri_for(c.controller('Project').action_for('create_jobset'), [project.name]) title = "Create jobset" %]
[% INCLUDE menuItem uri = c.uri_for('/project' project.name 'create-release') title = "Create release" %]
</ul>
@ -94,7 +95,6 @@
</div>
<div id="tabs-configuration" class="tab-pane">
<a class="btn pull-right" href="[% c.uri_for('/project' project.name "edit") %]"><i class="icon-edit"></i> Edit</a>
<table class="info-table">
<tr>
<th>Display name:</th>
@ -186,4 +186,18 @@
</div>
<script>
function deleteProject() {
bootbox.confirm(
'Are you sure you want to delete this project?',
function(c) {
if (!c) return;
redirectJSON({
url: "[% c.uri_for('/project' project.name) %]",
type: 'DELETE'
});
});
};
</script>
[% END %]

View file

@ -88,3 +88,29 @@ var makeLazyTab = function(tabName, uri) {
}
});
}
var requestJSON = function(args) {
args.dataType = 'json';
args.error = function(data) {
json = {};
try {
if (data.responseText)
json = $.parseJSON(data.responseText);
} catch (err) {
}
if (json.error)
bootbox.alert(json.error);
else if (data.responseText)
bootbox.alert("Server error: " + data.responseText);
else
bootbox.alert("Unknown server error!");
};
return $.ajax(args);
}
var redirectJSON = function(args) {
args.success = function(data) {
window.location = data.redirect;
};
return requestJSON(args);
}