From de7b8e51b8b7b5bfaa72ee62a4eb08497cf69b4b Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Wed, 17 Aug 2016 10:09:42 +0200 Subject: Add endpoints for pipelines --- lib/api/api.rb | 1 + lib/api/entities.rb | 8 ++++++ lib/api/pipelines.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 lib/api/pipelines.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index d43af3f24e9..d9261c97908 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -56,6 +56,7 @@ module API mount ::API::Milestones mount ::API::Namespaces mount ::API::Notes + mount ::API::Pipelines mount ::API::ProjectHooks mount ::API::ProjectSnippets mount ::API::Projects diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 055716ab1e3..09c9aca1e22 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -502,6 +502,14 @@ module API expose :key, :value end + class Pipeline < Grape::Entity + expose :status, :ref, :sha, :before_sha, :tag, :yaml_errors + + expose :user, with: Entities::UserBasic + expose :created_at, :updated_at, :started_at, :finished_at, :committed_at + expose :duration + end + class Environment < Grape::Entity expose :id, :name, :external_url end diff --git a/lib/api/pipelines.rb b/lib/api/pipelines.rb new file mode 100644 index 00000000000..2aae75c471d --- /dev/null +++ b/lib/api/pipelines.rb @@ -0,0 +1,74 @@ +module API + class Pipelines < Grape::API + before { authenticate! } + + params do + requires :id, type: String, desc: 'The project ID' + end + resource :projects do + desc 'Get all Pipelines of the project' do + detail 'This feature was introduced in GitLab 8.11.' + success Entities::Pipeline + end + params do + optional :page, type: Integer, desc: 'Page number of the current request' + optional :per_page, type: Integer, desc: 'Number of items per page' + end + get ':id/pipelines' do + authorize! :read_pipeline, user_project + + present paginate(user_project.pipelines), with: Entities::Pipeline + end + + desc 'Gets a specific pipeline for the project' do + detail 'This feature was introduced in GitLab 8.11' + success Entities::Pipeline + end + params do + requires :pipeline_id, type: Integer, desc: 'The pipeline ID' + end + get ':id/pipelines/:pipeline_id' do + authorize! :read_pipeline, user_project + + present pipeline, with: Entities::Pipeline + end + + desc 'Retry failed builds in the pipeline' do + detail 'This feature was introduced in GitLab 8.11.' + success Entities::Pipeline + end + params do + requires :pipeline_id, type: Integer, desc: 'The pipeline ID' + end + post ':id/pipelines/:pipeline_id/retry' do + authorize! :update_pipeline, user_project + + pipeline.retry_failed(current_user) + + present pipeline, with: Entities::Pipeline + end + + desc 'Cancel all builds in the pipeline' do + detail 'This feature was introduced in GitLab 8.11.' + success Entities::Pipeline + end + params do + requires :pipeline_id, type: Integer, desc: 'The pipeline ID' + end + post ':id/pipelines/:pipeline_id/cancel' do + authorize! :update_pipeline, user_project + + pipeline.cancel_running + + status 200 + present pipeline.reload, with: Entities::Pipeline + end + end + + helpers do + def pipeline + @pipeline ||= user_project.pipelines.find(params[:pipeline_id]) + end + end + end +end -- cgit v1.2.1 From df5661b6f49d8a18db7fae6bda71f9dc733f5279 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Wed, 17 Aug 2016 15:23:58 +0200 Subject: Add docs on API for pipelines, plus minor fixes --- lib/api/entities.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 09c9aca1e22..b575af53850 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -503,7 +503,7 @@ module API end class Pipeline < Grape::Entity - expose :status, :ref, :sha, :before_sha, :tag, :yaml_errors + expose :id, :status, :ref, :sha, :before_sha, :tag, :yaml_errors expose :user, with: Entities::UserBasic expose :created_at, :updated_at, :started_at, :finished_at, :committed_at -- cgit v1.2.1 From fffe5c2b577b39be2254525d4320cf396b7ff58b Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Mon, 15 Aug 2016 15:58:22 +0200 Subject: Add Play endpoints on Builds --- lib/api/builds.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index be5a3484ec8..2bd3b65acdc 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -189,6 +189,29 @@ module API present build, with: Entities::Build, user_can_download_artifacts: can?(current_user, :read_build, user_project) end + + desc 'Trigger a manual build' do + success Entities::Build + detail 'This feature was added in GitLab 8.11' + end + params do + requires :build_id, type: Integer, desc: 'The ID of a Build' + end + post ":id/builds/:build_id/play" do + authorize_read_builds! + + build = get_build!(params[:build_id]) + + if build.playable? + build.play(current_user) + + status 200 + present build, with: Entities::Build, + user_can_download_artifacts: can?(current_user, :read_build, user_project) + else + bad_request!("Unplayable Build") + end + end end helpers do -- cgit v1.2.1 From 47d6f286eb565ae582def5a28af2cb450b2ee479 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Tue, 16 Aug 2016 08:45:23 +0200 Subject: Add deployment endpoints --- lib/api/api.rb | 1 + lib/api/deployments.rb | 40 ++++++++++++++++++++++++++++++++++++++++ lib/api/entities.rb | 12 ++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 lib/api/deployments.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index d9261c97908..6b8bfbbdae6 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -43,6 +43,7 @@ module API mount ::API::CommitStatuses mount ::API::Commits mount ::API::DeployKeys + mount ::API::Deployments mount ::API::Environments mount ::API::Files mount ::API::Groups diff --git a/lib/api/deployments.rb b/lib/api/deployments.rb new file mode 100644 index 00000000000..f782bcaf7e9 --- /dev/null +++ b/lib/api/deployments.rb @@ -0,0 +1,40 @@ +module API + # Deployments RESTfull API endpoints + class Deployments < Grape::API + before { authenticate! } + + params do + requires :id, type: String, desc: 'The project ID' + end + resource :projects do + desc 'Get all deployments of the project' do + detail 'This feature was introduced in GitLab 8.11.' + success Entities::Deployment + end + params do + optional :page, type: Integer, desc: 'Page number of the current request' + optional :per_page, type: Integer, desc: 'Number of items per page' + end + get ':id/deployments' do + authorize! :read_deployment, user_project + + present paginate(user_project.deployments), with: Entities::Deployment + end + + desc 'Gets a specific deployment' do + detail 'This feature was introduced in GitLab 8.11.' + success Entities::Deployment + end + params do + requires :deployment_id, type: Integer, desc: 'The deployment ID' + end + get ':id/deployments/:deployment_id' do + authorize! :read_deployment, user_project + + deployment = user_project.deployments.find(params[:deployment_id]) + + present deployment, with: Entities::Deployment + end + end + end +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index b575af53850..9ae68947379 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -514,6 +514,18 @@ module API expose :id, :name, :external_url end + class EnvironmentBasic < Grape::Entity + expose :id, :name, :external_url + end + + class Deployment < Grape::Entity + expose :id, :iid, :ref, :sha, :created_at + expose :project, using: Entities::Project + expose :user, using: Entities::UserBasic + expose :environment, using: Entities::EnvironmentBasic + expose :deployable, using: Entities::Build + end + class RepoLicense < Grape::Entity expose :key, :name, :nickname expose :featured, as: :popular -- cgit v1.2.1 From 39c71a19c468736a94f642173a54bf72b2dc1689 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Tue, 16 Aug 2016 08:51:27 +0200 Subject: Expose project for environments --- lib/api/entities.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 9ae68947379..a10951b9ea0 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -512,6 +512,7 @@ module API class Environment < Grape::Entity expose :id, :name, :external_url + expose :project, using: Entities::Project end class EnvironmentBasic < Grape::Entity -- cgit v1.2.1 From ba01e519e24bf716c138a0b46e371c60de4aa935 Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Thu, 18 Aug 2016 11:42:37 +0200 Subject: Incorporate feedback --- lib/api/builds.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 2bd3b65acdc..52bdbcae5a8 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -202,15 +202,13 @@ module API build = get_build!(params[:build_id]) - if build.playable? - build.play(current_user) + bad_request!("Unplayable Build") unless build.playable? - status 200 - present build, with: Entities::Build, - user_can_download_artifacts: can?(current_user, :read_build, user_project) - else - bad_request!("Unplayable Build") - end + build.play(current_user) + + status 200 + present build, with: Entities::Build, + user_can_download_artifacts: can?(current_user, :read_build, user_project) end end -- cgit v1.2.1 From 2038e035c73f80292fa1bf9757803d8ab5e6ecab Mon Sep 17 00:00:00 2001 From: "Z.J. van de Weg" Date: Thu, 18 Aug 2016 21:10:20 +0200 Subject: Do not expose projects on deployments --- lib/api/entities.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index a10951b9ea0..67420772335 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -521,10 +521,9 @@ module API class Deployment < Grape::Entity expose :id, :iid, :ref, :sha, :created_at - expose :project, using: Entities::Project - expose :user, using: Entities::UserBasic - expose :environment, using: Entities::EnvironmentBasic - expose :deployable, using: Entities::Build + expose :user, using: Entities::UserBasic + expose :environment, using: Entities::EnvironmentBasic + expose :deployable, using: Entities::Build end class RepoLicense < Grape::Entity -- cgit v1.2.1 From e2f9c87600e34a415d43c981e0182094b123771f Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Fri, 12 Aug 2016 16:16:12 -0500 Subject: Added checks for 2FA to the API `/sessions` endpoint and the Resource Owner Password Credentials flow. --- lib/api/helpers.rb | 4 ++++ lib/api/session.rb | 1 + 2 files changed, 5 insertions(+) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index d0469d6602d..bbd647684a4 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -275,6 +275,10 @@ module API end end + def render_2fa_error! + render_api_error!('401 You have 2FA enabled. Please use a personal access token to access the API', 401) + end + def render_api_error!(message, status) error!({ 'message' => message }, status) end diff --git a/lib/api/session.rb b/lib/api/session.rb index 56c202f1294..b26be3be22e 100644 --- a/lib/api/session.rb +++ b/lib/api/session.rb @@ -14,6 +14,7 @@ module API user = Gitlab::Auth.find_with_user_password(params[:email] || params[:login], params[:password]) return unauthorized! unless user + return render_2fa_error! if user.two_factor_enabled? present user, with: Entities::UserLogin end end -- cgit v1.2.1 From c29780086201b331091be3ba5df0653381cf0c2c Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 17 Aug 2016 11:56:50 -0500 Subject: Removed unnecessary service for user retrieval and improved API error message. --- lib/api/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index bbd647684a4..3e906f6f929 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -276,7 +276,7 @@ module API end def render_2fa_error! - render_api_error!('401 You have 2FA enabled. Please use a personal access token to access the API', 401) + render_api_error!('401 Unauthorized. You have 2FA enabled. Please use a personal access token to access the API', 401) end def render_api_error!(message, status) -- cgit v1.2.1 From a4137411c62d093a55dc171665dc90325182bb04 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 17 Aug 2016 17:39:20 -0500 Subject: Small refactor and syntax fixes. --- lib/api/helpers.rb | 4 ---- lib/api/session.rb | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 3e906f6f929..d0469d6602d 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -275,10 +275,6 @@ module API end end - def render_2fa_error! - render_api_error!('401 Unauthorized. You have 2FA enabled. Please use a personal access token to access the API', 401) - end - def render_api_error!(message, status) error!({ 'message' => message }, status) end diff --git a/lib/api/session.rb b/lib/api/session.rb index b26be3be22e..55ec66a6d67 100644 --- a/lib/api/session.rb +++ b/lib/api/session.rb @@ -14,7 +14,7 @@ module API user = Gitlab::Auth.find_with_user_password(params[:email] || params[:login], params[:password]) return unauthorized! unless user - return render_2fa_error! if user.two_factor_enabled? + return render_api_error!('401 Unauthorized. You have 2FA enabled. Please use a personal access token to access the API', 401) if user.two_factor_enabled? present user, with: Entities::UserLogin end end -- cgit v1.2.1