diff options
| author | Angelo Lakra <alakra@comverge.com> | 2013-10-03 12:26:10 -0600 |
|---|---|---|
| committer | Angelo Lakra <angelo.lakra@gmail.com> | 2013-10-08 00:43:14 -0600 |
| commit | 519cce2c5149c21cf571ef73c974bffbef1a6398 (patch) | |
| tree | 99e7b13c85d0ed94faf7225d3d122151d43bba91 | |
| parent | fc49034fb454a88626e4e705520c2eb686c6594f (diff) | |
| download | gitlab-ci-519cce2c5149c21cf571ef73c974bffbef1a6398.tar.gz | |
Adding Project manipulation support via API
* The following actions are now supported:
GET /projects/:id - returns information about a project
GET /projects - returns list of all projects
PUT /projects/:id - updates project information (see docs for more info)
DELETE /projects/:id - removes a project
POST /projects/:id/runners/:runner_id - links a project to a runner
DELETE /projects/:id/runners/:runner_id - removes link between project and runner
| -rw-r--r-- | lib/api/entities.rb | 4 | ||||
| -rw-r--r-- | lib/api/projects.rb | 122 | ||||
| -rw-r--r-- | spec/requests/projects_spec.rb | 37 |
3 files changed, 153 insertions, 10 deletions
diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 59108da..3f65a44 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -11,5 +11,9 @@ module API class Project < Grape::Entity expose :id, :name, :timeout, :scripts, :token, :default_ref, :gitlab_url, :always_build, :polling_interval, :public, :ssh_url_to_repo, :gitlab_id end + + class RunnerProject < Grape::Entity + expose :id, :project_id, :runner_id + end end end diff --git a/lib/api/projects.rb b/lib/api/projects.rb index b66e65e..7bd497d 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -4,7 +4,7 @@ module API before { authenticate! } resource :projects do - # Retrieve info for a project + # Retrieve info for a Gitlab CI project # # Parameters: # id (required) - The ID of a project @@ -15,6 +15,26 @@ module API present project, with: Entities::Project end + # Retrieve info for aall Gitlab CI projects + # + # Example Request: + # GET /projects + get do + projects = Project.all + present projects, with: Entities::Project + end + + # Create Gitlab CI project using Gitlab project info + # + # Parameters: + # name (required) - The name of the project + # gitlab_id (required) - The gitlab id of the project + # gitlab_url (required) - The gitlab web url to the project + # ssh_url_to_repo (required) - The gitlab ssh url to the repo + # scripts - The shell script provided for a runner to run + # default_ref - The branch to run against (defaults to `master`) + # Example Request: + # POST /projects post do required_attributes! [:name, :gitlab_id, :gitlab_url, :ssh_url_to_repo] @@ -39,6 +59,106 @@ module API render_api_error!(errors, 400) end end + + # Update a Gitlab CI project + # + # Parameters: + # id (required) - The ID of a project + # name - The name of the project + # gitlab_id - The gitlab id of the project + # gitlab_url - The gitlab web url to the project + # ssh_url_to_repo - The gitlab ssh url to the repo + # scripts - The shell script provided for a runner to run + # default_ref - The branch to run against (defaults to `master`) + # Example Request: + # PUT /projects/:id + put ":id" do + project = Project.find(params[:id]) + + if project.present? + attrs = attributes_for_keys [:name, :gitlab_id, :gitlab_url, :scripts, :default_ref, :ssh_url_to_repo] + + if project.update_attributes(attrs) + present project, :with => Entities::Project + else + errors = project.errors.full_messages.join(", ") + render_api_error!(errors, 400) + end + else + not_found! + end + end + + # Remove a Gitlab CI project + # + # Parameters: + # id (required) - The ID of a project + # Example Request: + # DELETE /projects/:id + delete ":id" do + project = Project.find(params[:id]) + + if project.present? + project.destroy + else + not_found! + end + end + + # Link a Gitlab CI project to a runner + # + # Parameters: + # id (required) - The ID of a CI project + # runner_id (required) - The ID of a runner + # Example Request: + # POST /projects/:id/runners/:runner_id + post ":id/runners/:runner_id" do + project_exists = Project.exists?(params[:id]) + runner_exists = Runner.exists?(params[:runner_id]) + + not_found! if project_exists.blank? or runner_exists.blank? + + options = { + :project_id => params[:id], + :runner_id => params[:runner_id] + } + + runner_project = RunnerProject.new(options) + + if runner_project.save + present runner_project, :with => Entities::RunnerProject + else + errors = project.errors.full_messages.join(", ") + render_api_error!(errors, 400) + end + end + + # Remove a Gitlab CI project from a runner + # + # Parameters: + # id (required) - The ID of a CI project + # runner_id (required) - The ID of a runner + # Example Request: + # DELETE /projects/:id/runners/:runner_id + delete ":id/runners/:runner_id" do + project_exists = Project.exists?(params[:id]) + runner_exists = Runner.exists?(params[:runner_id]) + + not_found! if project_exists.blank? or runner_exists.blank? + + options = { + :project_id => params[:id], + :runner_id => params[:runner_id] + } + + runner_project = RunnerProject.where(options).first + + if runner_project.present? + runner_project.destroy + else + not_found! + end + end end end end diff --git a/spec/requests/projects_spec.rb b/spec/requests/projects_spec.rb index 6ef6a83..851b42d 100644 --- a/spec/requests/projects_spec.rb +++ b/spec/requests/projects_spec.rb @@ -26,10 +26,13 @@ describe API::API do describe "GET /projects" do let(:project) { FactoryGirl.create(:project) } + before { project } + it "should return all projects on the CI instance" do get api("/projects"), options response.status.should == 200 - json_response.should == {} + json_response.count.should == 1 + json_response.first["id"].should == project.id end end @@ -53,7 +56,15 @@ describe API::API do end describe "POST /projects" do - let(:project_info) { {} } + let(:project_info) { + { + :name => "My project", + :gitlab_id => 1, + :gitlab_url => "http://example.com/testing/testing", + :ssh_url_to_repo => "ssh://example.com/testing/testing.git" + } + } + let(:invalid_project_info) { {} } context "with valid project info" do @@ -82,7 +93,7 @@ describe API::API do describe "PUT /projects/:id" do let(:project) { FactoryGirl.create(:project) } - let(:project_info) { {} } + let(:project_info) { {:name => "An updated name!" } } before do options.merge!(project_info) @@ -96,20 +107,20 @@ describe API::API do it "fails to update a non-existing project" do put api("/projects/non-existant-id"), options - response.status.should == 400 + response.status.should == 404 end end describe "DELETE /projects/:id" do let(:project) { FactoryGirl.create(:project) } - before do - project - end + before { project } it "should delete a specific project" do delete api("/projects/#{project.id}"), options - response.status.should == 201 #? + response.status.should == 200 + + expect { project.reload }.to raise_error end end @@ -124,6 +135,14 @@ describe API::API do project.reload project.runners.first.id.should == runner.id end + + it "should fail if it tries to link a non-existing project or runner" do + post api("/projects/#{project.id}/runners/non-existing"), options + response.status.should == 404 + + post api("/projects/non-existing/runners/#{runner.id}"), options + response.status.should == 404 + end end describe "DELETE /projects/:id/runners/:id" do @@ -137,7 +156,7 @@ describe API::API do it "should remove the project from the runner" do project.runners.should be_present delete api("/projects/#{project.id}/runners/#{runner.id}"), options - response.status.should == 201 #? + response.status.should == 200 project.reload project.runners.should be_empty |
