summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Sizov <vsv2711@gmail.com>2015-04-17 21:04:43 +0300
committerValery Sizov <vsv2711@gmail.com>2015-04-21 19:24:53 +0300
commitf876234f92f868ee34038865521d86e1ebb3ae22 (patch)
tree81246cd7f61a07c918cdd8ebca19e281a539bec4
parentb3e2939aa2904c92aba0399d511cb05228bfc9be (diff)
downloadgitlab-ci-f876234f92f868ee34038865521d86e1ebb3ae22.tar.gz
API for deploy jobs
-rw-r--r--doc/api/projects.md26
-rw-r--r--lib/api/entities.rb11
-rw-r--r--lib/api/projects.rb39
-rw-r--r--spec/requests/api/projects_spec.rb48
4 files changed, 117 insertions, 7 deletions
diff --git a/doc/api/projects.md b/doc/api/projects.md
index dae4591..766ee12 100644
--- a/doc/api/projects.md
+++ b/doc/api/projects.md
@@ -161,7 +161,7 @@ Parameters:
* `id` (required) - The ID of the Gitlab CI project
-### Add a Job to a Project
+### Add a parallel Job to a Project
Adds a Job to a Gitlab CI Project (only via
authorized user).
@@ -173,10 +173,26 @@ Parameters:
* `id` (required) - The ID of the Gitlab CI project
* `name` (required) - The name of the Job to add
* `commands` (required) - The script commands of the job
- * `active` (optional) - The command is active of not
- * `build_branches` (optional) - Trigger commit builds
- * `build_tags` (optional) - Trigger tag builds
- * `tags` (optional) - The tags associated with this job
+ * `active` (optional) - The command is active of not
+ * `build_branches` (optional) - Trigger commit builds
+ * `build_tags` (optional) - Trigger tag builds
+ * `tags` (optional) - The tags associated with this job
+
+### Add a deploy Job to a Project
+
+Adds a deploy Job to a Gitlab CI Project (only via
+authorized user).
+
+ POST /projects/:id/deploy_jobs
+
+Parameters:
+
+ * `id` (required) - The ID of the Gitlab CI project
+ * `name` (required) - The name of the Job to add
+ * `commands` (required) - The script commands of the job
+ * `active` (optional) - The command is active of not
+ * `refs` (optional) - The list of refs
+ * `tags` (optional) - The tags associated with this job
### Remove a Job from a Project
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 8c4b3c1..3d98866 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -17,7 +17,8 @@ module API
end
class Project < Grape::Entity
- expose :id, :name, :timeout, :token, :default_ref, :gitlab_url, :always_build, :polling_interval, :public, :ssh_url_to_repo, :gitlab_id
+ expose :id, :name, :timeout, :token, :default_ref, :gitlab_url,
+ :always_build, :polling_interval, :public, :ssh_url_to_repo, :gitlab_id
end
class RunnerProject < Grape::Entity
@@ -29,7 +30,13 @@ module API
end
class Job < Grape::Entity
- expose :id, :project_id, :commands, :active, :name, :build_branches, :build_tags, :tags
+ expose :id, :project_id, :commands, :active, :name, :build_branches,
+ :build_tags, :tags, :job_type, :tag_list
+ end
+
+ class DeployJob < Grape::Entity
+ expose :id, :project_id, :commands, :active, :name,
+ :refs, :tags, :job_type, :refs, :tag_list
end
end
end
diff --git a/lib/api/projects.rb b/lib/api/projects.rb
index ea10a82..38c8c1b 100644
--- a/lib/api/projects.rb
+++ b/lib/api/projects.rb
@@ -83,6 +83,45 @@ module API
end
end
+ # Add a new deploy job to a project
+ #
+ # Parameters
+ # id (required) - The ID of a project
+ # name (required) - The job name
+ # commands (required) - The command line script for the job
+ # active (optional) - The command is active of not
+ # refs (optional) - The list of refs
+ # tags (optional) - The tags associated with this job
+ # Example Request
+ # POST /projects/:id/deploy_jobs
+ post ":id/deploy_jobs" do
+ required_attributes! [:name, :commands]
+
+ project = Project.find(params[:id])
+
+ not_found! if project.blank?
+ unauthorized! unless current_user.can_manage_project?(project.gitlab_id)
+
+ job_params =
+ {
+ name: params[:name],
+ commands: params[:commands],
+ job_type: "deploy"
+ }
+
+ job_params[:active] = params[:active] unless params[:active].nil?
+ job_params[:refs] = params[:refs] unless params[:refs].nil?
+ job_params[:tag_list] = params[:tags] unless params[:tags].nil?
+
+ job = project.jobs.new(job_params)
+ if job.save
+ present job, with: Entities::DeployJob
+ else
+ errors = job.errors.full_messages.join(", ")
+ render_api_error!(errors, 400)
+ end
+ end
+
# Delete a job for a project
#
# Parameters
diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
index 03d704b..cf0831b 100644
--- a/spec/requests/api/projects_spec.rb
+++ b/spec/requests/api/projects_spec.rb
@@ -101,6 +101,54 @@ describe API::API do
end
end
+ describe "POST /projects/:project_id/deploy_jobs" do
+ let!(:project) { FactoryGirl.create(:project) }
+
+ let(:job_info) {
+ {
+ name: "A Job Name",
+ commands: "ls -lad",
+ active: false,
+ refs: "master",
+ tags: "release, deployment",
+ }
+ }
+ let(:invalid_job_info) { {} }
+
+ context "Invalid Job Info" do
+ before do
+ options.merge!(invalid_job_info)
+ end
+
+ it "should error with invalid data" do
+ post api("/projects/#{project.id}/deploy_jobs"), options
+ response.status.should == 400
+ end
+ end
+
+ context "Valid Job Info" do
+ before do
+ options.merge!(job_info)
+ end
+
+ it "should create a job for specified project" do
+ post api("/projects/#{project.id}/deploy_jobs"), options
+ response.status.should == 201
+ json_response["name"].should == job_info[:name]
+ json_response["commands"].should == job_info[:commands]
+ json_response["active"].should == job_info[:active]
+ json_response["refs"].should == job_info[:refs]
+ json_response["tags"].should have(2).items
+ end
+
+ it "fails to create job for non existsing project" do
+ post api("/projects/non-existant-id/deploy_jobs"), options
+ response.status.should == 404
+ end
+ end
+ end
+
+
describe "GET /projects/:project_id/jobs" do
let!(:project) { FactoryGirl.create(:project) }
let(:job_info) {