From 8735d95af16a6066e9f256a62f401d02c2c7e108 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Wed, 13 Jul 2016 23:05:57 +0800 Subject: Implement API for downloading artifacts from ref and build name: Basically: GET /api/projects/:id/artifacts/:ref_name/:build_name Also added tests for it. --- lib/api/api.rb | 1 + lib/api/artifacts.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 lib/api/artifacts.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index 3d7d67510a8..f18258bf5a3 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -26,6 +26,7 @@ module API # Ensure the namespace is right, otherwise we might load Grape::API::Helpers helpers ::API::Helpers + mount ::API::Artifacts mount ::API::AwardEmoji mount ::API::Branches mount ::API::Builds diff --git a/lib/api/artifacts.rb b/lib/api/artifacts.rb new file mode 100644 index 00000000000..6ce2bed8260 --- /dev/null +++ b/lib/api/artifacts.rb @@ -0,0 +1,34 @@ +module API + # Projects artifacts API + class Artifacts < Grape::API + before do + authenticate! + authorize!(:read_build, user_project) + end + + resource :projects do + # Download the artifacts file from ref_name and build_name + # + # Parameters: + # id (required) - The ID of a project + # ref_name (required) - The ref from repository + # build_name (required) - The name for the build + # Example Request: + # GET /projects/:id/artifacts/:ref_name/:build_name + get ':id/artifacts/:ref_name/:build_name', + requirements: { ref_name: /.+/ } do + builds = user_project.builds_for( + params[:build_name], params[:ref_name]) + + latest_build = builds.success.latest.first + + if latest_build + redirect( + "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") + else + not_found! + end + end + end + end +end -- cgit v1.2.1 From 1c7871e92f679f65e5b5d065d7478dd2e77f9b77 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 14 Jul 2016 14:39:26 +0800 Subject: Introduce get_build! so we could omit one early return --- lib/api/builds.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index d36047acd1f..f6e96ee7f3a 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -52,8 +52,7 @@ module API get ':id/builds/:build_id' do authorize_read_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) present build, with: Entities::Build, user_can_download_artifacts: can?(current_user, :read_build, user_project) @@ -69,8 +68,7 @@ module API get ':id/builds/:build_id/artifacts' do authorize_read_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) artifacts_file = build.artifacts_file @@ -97,8 +95,7 @@ module API get ':id/builds/:build_id/trace' do authorize_read_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) header 'Content-Disposition', "infile; filename=\"#{build.id}.log\"" content_type 'text/plain' @@ -118,8 +115,7 @@ module API post ':id/builds/:build_id/cancel' do authorize_update_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) build.cancel @@ -137,8 +133,7 @@ module API post ':id/builds/:build_id/retry' do authorize_update_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) return forbidden!('Build is not retryable') unless build.retryable? build = Ci::Build.retry(build, current_user) @@ -157,8 +152,7 @@ module API post ':id/builds/:build_id/erase' do authorize_update_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build + build = get_build!(params[:build_id]) return forbidden!('Build is not erasable!') unless build.erasable? build.erase(erased_by: current_user) @@ -176,8 +170,8 @@ module API post ':id/builds/:build_id/artifacts/keep' do authorize_update_builds! - build = get_build(params[:build_id]) - return not_found!(build) unless build && build.artifacts? + build = get_build!(params[:build_id]) + return not_found!(build) unless build.artifacts? build.keep_artifacts! @@ -192,6 +186,10 @@ module API user_project.builds.find_by(id: id.to_i) end + def get_build!(id) + get_build(id) || not_found! + end + def filter_builds(builds, scope) return builds if scope.nil? || scope.empty? -- cgit v1.2.1 From e01c421b911a46774f8c5be92d383d8da14750c3 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 14 Jul 2016 16:36:09 +0800 Subject: Prefer if so it's more clear what's going on --- lib/api/builds.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index f6e96ee7f3a..b3b28541382 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -69,16 +69,17 @@ module API authorize_read_builds! build = get_build!(params[:build_id]) - artifacts_file = build.artifacts_file - unless artifacts_file.file_storage? - return redirect_to build.artifacts_file.url - end + if !artifacts_file.file_storage? + redirect_to(build.artifacts_file.url) - return not_found! unless artifacts_file.exists? + elsif artifacts_file.exists? + present_file!(artifacts_file.path, artifacts_file.filename) - present_file!(artifacts_file.path, artifacts_file.filename) + else + not_found! + end end # Get a trace of a specific build of a project -- cgit v1.2.1 From 70f508f5d48a3541a430539d7f8b41dfa99127a1 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 14 Jul 2016 18:50:34 +0800 Subject: Serve artifacts from Builds --- lib/api/api.rb | 1 - lib/api/artifacts.rb | 34 ---------------------------------- lib/api/builds.rb | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 39 deletions(-) delete mode 100644 lib/api/artifacts.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index f18258bf5a3..3d7d67510a8 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -26,7 +26,6 @@ module API # Ensure the namespace is right, otherwise we might load Grape::API::Helpers helpers ::API::Helpers - mount ::API::Artifacts mount ::API::AwardEmoji mount ::API::Branches mount ::API::Builds diff --git a/lib/api/artifacts.rb b/lib/api/artifacts.rb deleted file mode 100644 index 6ce2bed8260..00000000000 --- a/lib/api/artifacts.rb +++ /dev/null @@ -1,34 +0,0 @@ -module API - # Projects artifacts API - class Artifacts < Grape::API - before do - authenticate! - authorize!(:read_build, user_project) - end - - resource :projects do - # Download the artifacts file from ref_name and build_name - # - # Parameters: - # id (required) - The ID of a project - # ref_name (required) - The ref from repository - # build_name (required) - The name for the build - # Example Request: - # GET /projects/:id/artifacts/:ref_name/:build_name - get ':id/artifacts/:ref_name/:build_name', - requirements: { ref_name: /.+/ } do - builds = user_project.builds_for( - params[:build_name], params[:ref_name]) - - latest_build = builds.success.latest.first - - if latest_build - redirect( - "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") - else - not_found! - end - end - end - end -end diff --git a/lib/api/builds.rb b/lib/api/builds.rb index b3b28541382..505ba1a66fe 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -71,12 +71,27 @@ module API build = get_build!(params[:build_id]) artifacts_file = build.artifacts_file - if !artifacts_file.file_storage? - redirect_to(build.artifacts_file.url) + present_artifact!(artifacts_file) + end - elsif artifacts_file.exists? - present_file!(artifacts_file.path, artifacts_file.filename) + # Download the artifacts file from ref_name and build_name + # + # Parameters: + # id (required) - The ID of a project + # ref_name (required) - The ref from repository + # job (required) - The name for the build + # Example Request: + # GET /projects/:id/artifacts/:ref_name/:build_name + get ':id/builds/artifacts/:ref_name', + requirements: { ref_name: /.+/ } do + builds = user_project.builds_for( + params[:job], params[:ref_name]) + latest_build = builds.success.latest.first + + if latest_build + redirect( + "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") else not_found! end @@ -191,6 +206,18 @@ module API get_build(id) || not_found! end + def present_artifact!(artifacts_file) + if !artifacts_file.file_storage? + redirect_to(build.artifacts_file.url) + + elsif artifacts_file.exists? + present_file!(artifacts_file.path, artifacts_file.filename) + + else + not_found! + end + end + def filter_builds(builds, scope) return builds if scope.nil? || scope.empty? -- cgit v1.2.1 From c4496de8bf90ded13245fedc6b760805f15f6942 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 14 Jul 2016 19:02:07 +0800 Subject: Provide the file directly rather than redirecting --- lib/api/builds.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 505ba1a66fe..5c14444f91a 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -69,9 +69,8 @@ module API authorize_read_builds! build = get_build!(params[:build_id]) - artifacts_file = build.artifacts_file - present_artifact!(artifacts_file) + present_artifact!(build.artifacts_file) end # Download the artifacts file from ref_name and build_name @@ -90,8 +89,7 @@ module API latest_build = builds.success.latest.first if latest_build - redirect( - "/projects/#{user_project.id}/builds/#{latest_build.id}/artifacts") + present_artifact!(latest_build.artifacts_file) else not_found! end -- cgit v1.2.1 From e96401f097e3d3bffe3a34d6e053af356109370b Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 15 Jul 2016 01:24:18 +0800 Subject: Add a download prefix so that we could add file prefix in the future --- lib/api/builds.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 5c14444f91a..6792afb064e 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -80,11 +80,10 @@ module API # ref_name (required) - The ref from repository # job (required) - The name for the build # Example Request: - # GET /projects/:id/artifacts/:ref_name/:build_name - get ':id/builds/artifacts/:ref_name', + # GET /projects/:id/artifacts/download/:ref_name?job=name + get ':id/builds/artifacts/download/:ref_name', requirements: { ref_name: /.+/ } do - builds = user_project.builds_for( - params[:job], params[:ref_name]) + builds = user_project.builds_for(params[:job], params[:ref_name]) latest_build = builds.success.latest.first -- cgit v1.2.1 From c23f2aa53099c6c906a8c6457183502450fa5703 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 15 Jul 2016 01:58:13 +0800 Subject: Fix outdated comment --- lib/api/builds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 6792afb064e..e65dfa88746 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -73,7 +73,7 @@ module API present_artifact!(build.artifacts_file) end - # Download the artifacts file from ref_name and build_name + # Download the artifacts file from ref_name and job # # Parameters: # id (required) - The ID of a project -- cgit v1.2.1 From 4bb3787eee282434263c37194a443edf6a93c1b7 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 15 Jul 2016 02:01:10 +0800 Subject: Try to make the URL more consistent between Rails and API --- lib/api/builds.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index e65dfa88746..237a88adcc7 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -80,8 +80,8 @@ module API # ref_name (required) - The ref from repository # job (required) - The name for the build # Example Request: - # GET /projects/:id/artifacts/download/:ref_name?job=name - get ':id/builds/artifacts/download/:ref_name', + # GET /projects/:id/artifacts/:ref_name/download?job=name + get ':id/builds/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do builds = user_project.builds_for(params[:job], params[:ref_name]) -- cgit v1.2.1 From 53a9dee6cb54b75fa2999b4a33a59928b3b73ec3 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Fri, 15 Jul 2016 02:22:29 +0800 Subject: Introduce Project#latest_success_builds_for: So it's more accessible for views to access the names of jobs. Only filter Build#name from where we really need to download it. --- lib/api/builds.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 237a88adcc7..53774b5c10f 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -83,9 +83,8 @@ module API # GET /projects/:id/artifacts/:ref_name/download?job=name get ':id/builds/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do - builds = user_project.builds_for(params[:job], params[:ref_name]) - - latest_build = builds.success.latest.first + builds = user_project.latest_success_builds_for(params[:ref_name]) + latest_build = builds.where(name: params[:job]).first if latest_build present_artifact!(latest_build.artifacts_file) -- cgit v1.2.1 From 6dcb75f98515d8f3a723edc1900e80cf9427c486 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Mon, 18 Jul 2016 13:41:37 +0800 Subject: Remove blank lines between clauses, feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5142#note_13125597 --- lib/api/builds.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 53774b5c10f..d988c669cb1 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -205,10 +205,8 @@ module API def present_artifact!(artifacts_file) if !artifacts_file.file_storage? redirect_to(build.artifacts_file.url) - elsif artifacts_file.exists? present_file!(artifacts_file.path, artifacts_file.filename) - else not_found! end -- cgit v1.2.1 From cc91f09ac39a7c201d527734e835d01dc13e5059 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Mon, 18 Jul 2016 14:34:03 +0800 Subject: Just use find_by! and we're rescuing ActiveRecord::RecordNotFound Feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5142#note_13125645 --- lib/api/builds.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index d988c669cb1..a27397a82f7 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -84,13 +84,9 @@ module API get ':id/builds/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do builds = user_project.latest_success_builds_for(params[:ref_name]) - latest_build = builds.where(name: params[:job]).first + latest_build = builds.find_by!(name: params[:job]) - if latest_build - present_artifact!(latest_build.artifacts_file) - else - not_found! - end + present_artifact!(latest_build.artifacts_file) end # Get a trace of a specific build of a project -- cgit v1.2.1 From 85ceb8b72f5a67d21bc9530fe835fdece98f3d4e Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Tue, 19 Jul 2016 17:51:45 +0800 Subject: Rename latest_success* to latest_successful: Feedback: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5142#note_13164642 --- lib/api/builds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/builds.rb b/lib/api/builds.rb index a27397a82f7..bb9e8f1ae6e 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -83,7 +83,7 @@ module API # GET /projects/:id/artifacts/:ref_name/download?job=name get ':id/builds/artifacts/:ref_name/download', requirements: { ref_name: /.+/ } do - builds = user_project.latest_success_builds_for(params[:ref_name]) + builds = user_project.latest_successful_builds_for(params[:ref_name]) latest_build = builds.find_by!(name: params[:job]) present_artifact!(latest_build.artifacts_file) -- cgit v1.2.1 From 9a5f40878c6d71e7fd4858f7fa1d948af6c371ae Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 3 Aug 2016 11:32:29 +0300 Subject: Add API to list merge request diff versions Signed-off-by: Dmitriy Zaporozhets --- lib/api/api.rb | 1 + lib/api/entities.rb | 11 +++++++++++ lib/api/merge_request_diffs.rb | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 lib/api/merge_request_diffs.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index bd16806892b..9d8f297191f 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -67,5 +67,6 @@ module API mount ::API::Triggers mount ::API::Users mount ::API::Variables + mount ::API::MergeRequestDiffs end end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 3e21b7a0b8a..af069063f0c 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -228,6 +228,17 @@ module API end end + class MergeRequestDiff < Grape::Entity + expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha, + :created_at, :merge_request_id, :state, :real_size + + expose :commits, using: Entities::RepoCommit + + expose :diffs, using: Entities::RepoDiff do |compare, _| + compare.diffs(all_diffs: true).to_a + end + end + class SSHKey < Grape::Entity expose :id, :title, :key, :created_at end diff --git a/lib/api/merge_request_diffs.rb b/lib/api/merge_request_diffs.rb new file mode 100644 index 00000000000..4bd149d1603 --- /dev/null +++ b/lib/api/merge_request_diffs.rb @@ -0,0 +1,25 @@ +module API + # MergeRequestDiff API + class MergeRequestDiffs < Grape::API + before { authenticate! } + + resource :projects do + # List merge requests diff versions + # + # Parameters: + # id (required) - The ID of a project + # merge_request_id (required) - The ID of MR + # + # Example: + # GET /projects/:id/merge_requests/:merge_request_id/versions + # + get ":id/merge_requests/:merge_request_id/versions" do + merge_request = user_project.merge_requests. + find(params[:merge_request_id]) + + authorize! :read_merge_request, merge_request + present merge_request.merge_request_diffs, with: Entities::MergeRequestDiff + end + end + end +end -- cgit v1.2.1 From 69096ad9da77ce51d544a8f23f4e8873df273f4e Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 3 Aug 2016 11:58:09 +0300 Subject: Update merge request versions API to match styleguide Signed-off-by: Dmitriy Zaporozhets --- lib/api/merge_request_diffs.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'lib/api') diff --git a/lib/api/merge_request_diffs.rb b/lib/api/merge_request_diffs.rb index 4bd149d1603..be954ff96ac 100644 --- a/lib/api/merge_request_diffs.rb +++ b/lib/api/merge_request_diffs.rb @@ -4,15 +4,16 @@ module API before { authenticate! } resource :projects do - # List merge requests diff versions - # - # Parameters: - # id (required) - The ID of a project - # merge_request_id (required) - The ID of MR - # - # Example: - # GET /projects/:id/merge_requests/:merge_request_id/versions - # + desc 'Get a list of merge request diff versions' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::MergeRequestDiff + end + + params do + requires :id, type: Integer, desc: 'The ID of a project' + requests :merge_request_id, type: Integer, desc: 'The ID of a merge request' + end + get ":id/merge_requests/:merge_request_id/versions" do merge_request = user_project.merge_requests. find(params[:merge_request_id]) -- cgit v1.2.1 From 08f5f897889dc7e7f00a65f56e988449350e60f2 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 3 Aug 2016 12:35:20 +0300 Subject: Fix project id param for merge request version API Signed-off-by: Dmitriy Zaporozhets --- lib/api/merge_request_diffs.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/merge_request_diffs.rb b/lib/api/merge_request_diffs.rb index be954ff96ac..a2f92f81107 100644 --- a/lib/api/merge_request_diffs.rb +++ b/lib/api/merge_request_diffs.rb @@ -10,8 +10,8 @@ module API end params do - requires :id, type: Integer, desc: 'The ID of a project' - requests :merge_request_id, type: Integer, desc: 'The ID of a merge request' + requires :id, type: String, desc: 'The ID of a project' + requires :merge_request_id, type: Integer, desc: 'The ID of a merge request' end get ":id/merge_requests/:merge_request_id/versions" do -- cgit v1.2.1 From 4b559c9afb34b80b910efec514653c6ea65adba8 Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 11 Aug 2016 17:26:04 +0800 Subject: Reverse ref and sha in args and rename pipeline to pipeline_for --- lib/api/commit_statuses.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb index 4df6ca8333e..5e3c9563703 100644 --- a/lib/api/commit_statuses.rb +++ b/lib/api/commit_statuses.rb @@ -64,7 +64,7 @@ module API ref = branches.first end - pipeline = @project.ensure_pipeline(commit.sha, ref, current_user) + pipeline = @project.ensure_pipeline(ref, commit.sha, current_user) name = params[:name] || params[:context] status = GenericCommitStatus.running_or_pending.find_by(pipeline: pipeline, name: name, ref: params[:ref]) -- cgit v1.2.1 From ac072132b8f0e36badf297208a5964109dbed126 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 12 Aug 2016 14:44:49 +0300 Subject: Add single merge request diff API endpoint Signed-off-by: Dmitriy Zaporozhets --- lib/api/entities.rb | 2 ++ lib/api/merge_request_diffs.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index af069063f0c..f582b2ce250 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -231,7 +231,9 @@ module API class MergeRequestDiff < Grape::Entity expose :id, :head_commit_sha, :base_commit_sha, :start_commit_sha, :created_at, :merge_request_id, :state, :real_size + end + class MergeRequestDiffFull < MergeRequestDiff expose :commits, using: Entities::RepoCommit expose :diffs, using: Entities::RepoDiff do |compare, _| diff --git a/lib/api/merge_request_diffs.rb b/lib/api/merge_request_diffs.rb index a2f92f81107..07435d78468 100644 --- a/lib/api/merge_request_diffs.rb +++ b/lib/api/merge_request_diffs.rb @@ -21,6 +21,25 @@ module API authorize! :read_merge_request, merge_request present merge_request.merge_request_diffs, with: Entities::MergeRequestDiff end + + desc 'Get a single merge request diff version' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::MergeRequestDiffFull + end + + params do + requires :id, type: String, desc: 'The ID of a project' + requires :merge_request_id, type: Integer, desc: 'The ID of a merge request' + requires :version_id, type: Integer, desc: 'The ID of a merge request diff version' + end + + get ":id/merge_requests/:merge_request_id/versions/:version_id" do + merge_request = user_project.merge_requests. + find(params[:merge_request_id]) + + authorize! :read_merge_request, merge_request + present merge_request.merge_request_diffs.find(params[:version_id]), with: Entities::MergeRequestDiffFull + end end end end -- cgit v1.2.1 From dde44b83ddeae75a5c14810d0571ee5c7ab0a26d Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 22 Aug 2016 15:30:23 +0300 Subject: Fix merge request diff api Signed-off-by: Dmitriy Zaporozhets --- 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 3f050a8fd81..3241cb7ca56 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -246,7 +246,7 @@ module API expose :commits, using: Entities::RepoCommit expose :diffs, using: Entities::RepoDiff do |compare, _| - compare.diffs(all_diffs: true).to_a + compare.raw_diffs(all_diffs: true).to_a end end -- cgit v1.2.1 From 76872372376e57cd7d55ba9b9c63b25fe53c82df Mon Sep 17 00:00:00 2001 From: barthc Date: Wed, 17 Aug 2016 12:21:06 +0100 Subject: prevent authored awardable thumbs votes prevent authored awardable thumbs votes prevent authored awardable thumbs votes --- lib/api/award_emoji.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/award_emoji.rb b/lib/api/award_emoji.rb index 2efe7e3adf3..7c22b17e4e5 100644 --- a/lib/api/award_emoji.rb +++ b/lib/api/award_emoji.rb @@ -54,7 +54,7 @@ module API post endpoint do required_attributes! [:name] - not_found!('Award Emoji') unless can_read_awardable? + not_found!('Award Emoji') unless can_read_awardable? && can_award_awardable? award = awardable.create_award_emoji(params[:name], current_user) @@ -92,6 +92,10 @@ module API can?(current_user, ability, awardable) end + def can_award_awardable? + awardable.user_can_award?(current_user, params[:name]) + end + def awardable @awardable ||= begin -- cgit v1.2.1 From 7532c012c26fc116f7c39f7c88ac3b08d818955c Mon Sep 17 00:00:00 2001 From: tiagonbotelho Date: Mon, 8 Aug 2016 17:25:39 +0100 Subject: user is now notified when creating an issue through the api --- lib/api/issues.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 077258faee1..1121285f0af 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,6 +154,20 @@ module API render_api_error!({ labels: errors }, 400) end + if params[:labels].present? + params[:labels] = params[:labels].split(",").each { |word| word.strip! } + attrs[:label_ids] = [] + + params[:labels].each do |label| + existing_label = user_project.labels.where(title: label).first + + unless existing_label.nil? + attrs[:label_ids] << existing_label.id + params[:labels].delete(label) + end + end + end + project = user_project issue = ::Issues::CreateService.new(project, current_user, attrs.merge(request: request, api: true)).execute @@ -163,10 +177,10 @@ module API end if issue.valid? - # Find or create labels and attach to issue. Labels are valid because + # create new labels and attach to issue. Labels are valid because # we already checked its name, so there can't be an error here if params[:labels].present? - issue.add_labels_by_names(params[:labels].split(',')) + issue.add_labels_by_names(params[:labels]) end present issue, with: Entities::Issue, current_user: current_user -- cgit v1.2.1 From b7d29ce659412e9a2acc411c841420eb13d115ba Mon Sep 17 00:00:00 2001 From: tiagonbotelho Date: Tue, 9 Aug 2016 23:08:59 +0100 Subject: adds test to check whether or not an email is sent to label subscribers after creating a new issue through the api --- lib/api/issues.rb | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'lib/api') diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 1121285f0af..9a042e6e70d 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,35 +154,22 @@ module API render_api_error!({ labels: errors }, 400) end + # Find or create labels if params[:labels].present? - params[:labels] = params[:labels].split(",").each { |word| word.strip! } - attrs[:label_ids] = [] - - params[:labels].each do |label| - existing_label = user_project.labels.where(title: label).first - - unless existing_label.nil? - attrs[:label_ids] << existing_label.id - params[:labels].delete(label) - end + attrs[:label_ids] = params[:labels].split(",").map do |label_name| + user_project.labels.create_with(color: Label::DEFAULT_COLOR) + .find_or_create_by(title: label_name.strip) + .id end end - project = user_project - - issue = ::Issues::CreateService.new(project, current_user, attrs.merge(request: request, api: true)).execute + issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute if issue.spam? render_api_error!({ error: 'Spam detected' }, 400) end if issue.valid? - # create new labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels].present? - issue.add_labels_by_names(params[:labels]) - end - present issue, with: Entities::Issue, current_user: current_user else render_validation_error!(issue) -- cgit v1.2.1 From 7f0bcf04323ad69b64a90112896971ea8d1a5f99 Mon Sep 17 00:00:00 2001 From: tiagonbotelho Date: Mon, 15 Aug 2016 17:50:41 +0100 Subject: refactors update issue api request and some minor comments --- lib/api/helpers.rb | 8 ++++++++ lib/api/issues.rb | 26 ++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index da4b1bf9902..dbad86d8926 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -102,6 +102,14 @@ module API label || not_found!('Label') end + def get_label_ids(labels) + labels.split(",").map do |label_name| + user_project.labels.create_with(color: Label::DEFAULT_COLOR) + .find_or_create_by(title: label_name.strip) + .id + end + end + def find_project_issue(id) issue = user_project.issues.find(id) not_found! unless can?(current_user, :read_issue, issue) diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 9a042e6e70d..39a46f69f16 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,14 +154,9 @@ module API render_api_error!({ labels: errors }, 400) end - # Find or create labels - if params[:labels].present? - attrs[:label_ids] = params[:labels].split(",").map do |label_name| - user_project.labels.create_with(color: Label::DEFAULT_COLOR) - .find_or_create_by(title: label_name.strip) - .id - end - end + # Find or create labels to attach to the issue. Labels are vaild + # because we already checked its name, so there can't be an error here + attrs[:label_ids] = get_label_ids(params[:labels]) if params[:labels].present? issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute @@ -203,17 +198,16 @@ module API render_api_error!({ labels: errors }, 400) end + # Find or create labels and attach to issue. Labels are valid because + # we already checked its name, so there can't be an error here + if params[:labels] && can?(current_user, :admin_issue, user_project) + issue.remove_labels + attrs[:label_ids] = get_label_ids(params[:labels]) + end + issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) if issue.valid? - # Find or create labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels] && can?(current_user, :admin_issue, user_project) - issue.remove_labels - # Create and add labels to the new created issue - issue.add_labels_by_names(params[:labels].split(',')) - end - present issue, with: Entities::Issue, current_user: current_user else render_validation_error!(issue) -- cgit v1.2.1 From 76c2901eac89b1b3a9975ec0f91fb929fbed2e70 Mon Sep 17 00:00:00 2001 From: tiagonbotelho Date: Thu, 18 Aug 2016 11:24:44 +0100 Subject: if issue is not valid we revert back to the old labels when updating --- lib/api/helpers.rb | 8 -------- lib/api/issues.rb | 11 ++--------- 2 files changed, 2 insertions(+), 17 deletions(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index dbad86d8926..da4b1bf9902 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -102,14 +102,6 @@ module API label || not_found!('Label') end - def get_label_ids(labels) - labels.split(",").map do |label_name| - user_project.labels.create_with(color: Label::DEFAULT_COLOR) - .find_or_create_by(title: label_name.strip) - .id - end - end - def find_project_issue(id) issue = user_project.issues.find(id) not_found! unless can?(current_user, :read_issue, issue) diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 39a46f69f16..d0bc7243e54 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,9 +154,7 @@ module API render_api_error!({ labels: errors }, 400) end - # Find or create labels to attach to the issue. Labels are vaild - # because we already checked its name, so there can't be an error here - attrs[:label_ids] = get_label_ids(params[:labels]) if params[:labels].present? + attrs[:labels] = params[:labels] if params[:labels] issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute @@ -198,12 +196,7 @@ module API render_api_error!({ labels: errors }, 400) end - # Find or create labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels] && can?(current_user, :admin_issue, user_project) - issue.remove_labels - attrs[:label_ids] = get_label_ids(params[:labels]) - end + attrs[:labels] = params[:labels] if params[:labels] issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) -- cgit v1.2.1 From 99ee86206e3e19dd93910a4e7a3a5b6e3a7add9a Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Mon, 8 Aug 2016 10:07:15 -0700 Subject: remove six, and use a Set instead --- lib/api/helpers.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index da4b1bf9902..1afca5fe2e8 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -409,11 +409,7 @@ module API end def abilities - @abilities ||= begin - abilities = Six.new - abilities << Ability - abilities - end + Ability end def secret_token -- cgit v1.2.1 From 5853c96b49010aaf33b85caeb94dfc18873d5656 Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Mon, 8 Aug 2016 11:55:13 -0700 Subject: remove Ability.abilities --- lib/api/helpers.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/api') diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 1afca5fe2e8..fdb70af694d 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -148,7 +148,7 @@ module API end def can?(object, action, subject) - abilities.allowed?(object, action, subject) + Ability.allowed?(object, action, subject) end # Checks the occurrences of required attributes, each attribute must be present in the params hash @@ -408,10 +408,6 @@ module API links.join(', ') end - def abilities - Ability - end - def secret_token File.read(Gitlab.config.gitlab_shell.secret_file).chomp end -- cgit v1.2.1 From 2bdcef4d672121a387fca6da720d333dda8f7af6 Mon Sep 17 00:00:00 2001 From: "http://jneen.net/" Date: Thu, 18 Aug 2016 16:12:32 -0700 Subject: use a nil subject when we want to check global abilities --- lib/api/groups.rb | 2 +- lib/api/helpers.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/groups.rb b/lib/api/groups.rb index 9d8b8d737a9..f981ec0dbfe 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -30,7 +30,7 @@ module API # Example Request: # POST /groups post do - authorize! :create_group, current_user + authorize! :create_group required_attributes! [:name, :path] attrs = attributes_for_keys [:name, :path, :description, :visibility_level] diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index fdb70af694d..6a20ba95a79 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -129,7 +129,7 @@ module API forbidden! unless current_user.is_admin? end - def authorize!(action, subject) + def authorize!(action, subject = nil) forbidden! unless can?(current_user, action, subject) end -- cgit v1.2.1 From 0227e98d0db2eb7fc6a35ddfcd3a0581ab550948 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Wed, 24 Aug 2016 17:36:58 -0500 Subject: Added CHANGELOG, documentation, and API functionality --- lib/api/entities.rb | 2 +- lib/api/projects.rb | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index cbb324dd06d..61fcccf2959 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -78,7 +78,7 @@ module API expose :path, :path_with_namespace expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled expose :created_at, :last_activity_at - expose :shared_runners_enabled + expose :shared_runners_enabled, :enable_lfs expose :creator_id expose :namespace expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? } diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 71efd4f33ca..d98fb2611ff 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -105,6 +105,7 @@ module API # visibility_level (optional) - 0 by default # import_url (optional) # public_builds (optional) + # enable_lfs (optional) # Example Request # POST /projects post do @@ -124,7 +125,8 @@ module API :visibility_level, :import_url, :public_builds, - :only_allow_merge_if_build_succeeds] + :only_allow_merge_if_build_succeeds, + :enable_lfs] attrs = map_public_to_visibility_level(attrs) @project = ::Projects::CreateService.new(current_user, attrs).execute if @project.saved? @@ -220,6 +222,7 @@ module API # public (optional) - if true same as setting visibility_level = 20 # visibility_level (optional) - visibility level of a project # public_builds (optional) + # enable_lfs (optional) # Example Request # PUT /projects/:id put ':id' do @@ -237,7 +240,8 @@ module API :public, :visibility_level, :public_builds, - :only_allow_merge_if_build_succeeds] + :only_allow_merge_if_build_succeeds, + :enable_lfs] attrs = map_public_to_visibility_level(attrs) authorize_admin_project authorize! :rename_project, user_project if attrs[:name].present? -- cgit v1.2.1 From cf37d623e197dae5cc7efb021c1b1d85ca9674ee Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Tue, 30 Aug 2016 17:17:45 -0500 Subject: Renamed `enable_lfs` to `lfs_enabled` for the Project field, and related fixes. --- lib/api/entities.rb | 2 +- lib/api/projects.rb | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 61fcccf2959..4335e3055ef 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -78,7 +78,7 @@ module API expose :path, :path_with_namespace expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled expose :created_at, :last_activity_at - expose :shared_runners_enabled, :enable_lfs + expose :shared_runners_enabled, :lfs_enabled expose :creator_id expose :namespace expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? } diff --git a/lib/api/projects.rb b/lib/api/projects.rb index d98fb2611ff..f8979a1cc29 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -105,7 +105,7 @@ module API # visibility_level (optional) - 0 by default # import_url (optional) # public_builds (optional) - # enable_lfs (optional) + # lfs_enabled (optional) # Example Request # POST /projects post do @@ -126,7 +126,7 @@ module API :import_url, :public_builds, :only_allow_merge_if_build_succeeds, - :enable_lfs] + :lfs_enabled] attrs = map_public_to_visibility_level(attrs) @project = ::Projects::CreateService.new(current_user, attrs).execute if @project.saved? @@ -158,6 +158,7 @@ module API # visibility_level (optional) # import_url (optional) # public_builds (optional) + # lfs_enabled (optional) # Example Request # POST /projects/user/:user_id post "user/:user_id" do @@ -176,7 +177,8 @@ module API :visibility_level, :import_url, :public_builds, - :only_allow_merge_if_build_succeeds] + :only_allow_merge_if_build_succeeds, + :lfs_enabled] attrs = map_public_to_visibility_level(attrs) @project = ::Projects::CreateService.new(user, attrs).execute if @project.saved? @@ -222,7 +224,7 @@ module API # public (optional) - if true same as setting visibility_level = 20 # visibility_level (optional) - visibility level of a project # public_builds (optional) - # enable_lfs (optional) + # lfs_enabled (optional) # Example Request # PUT /projects/:id put ':id' do @@ -241,7 +243,7 @@ module API :visibility_level, :public_builds, :only_allow_merge_if_build_succeeds, - :enable_lfs] + :lfs_enabled] attrs = map_public_to_visibility_level(attrs) authorize_admin_project authorize! :rename_project, user_project if attrs[:name].present? -- cgit v1.2.1 From 727dff3f158b9ef852b2b014d4efe0abd69a23d0 Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Fri, 26 Aug 2016 09:37:57 +0530 Subject: Don't expose a user's private token in the `/api/v3/user` API. - This would allow anyone with a personal access token (even a read-only token, once scopes are implemented) to escalate their access by obtaining the private token. --- lib/api/users.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/api') diff --git a/lib/api/users.rb b/lib/api/users.rb index 8a376d3c2a3..c440305ff0f 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -327,7 +327,7 @@ module API # Example Request: # GET /user get do - present @current_user, with: Entities::UserLogin + present @current_user, with: Entities::UserFull end # Get currently authenticated user's keys -- cgit v1.2.1 From 036cc8c27e8340a3eed63444bd3f42f86037f350 Mon Sep 17 00:00:00 2001 From: Robert Schilling Date: Fri, 15 Jul 2016 16:21:53 +0200 Subject: API: Expose issue#confidential --- lib/api/entities.rb | 1 + lib/api/issues.rb | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 4335e3055ef..e3a8ff6de80 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -211,6 +211,7 @@ module API expose :user_notes_count expose :upvotes, :downvotes expose :due_date + expose :confidential expose :web_url do |issue, options| Gitlab::UrlBuilder.build(issue) diff --git a/lib/api/issues.rb b/lib/api/issues.rb index d0bc7243e54..556684187d8 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -140,12 +140,13 @@ module API # labels (optional) - The labels of an issue # created_at (optional) - Date time string, ISO 8601 formatted # due_date (optional) - Date time string in the format YEAR-MONTH-DAY + # confidential (optional) - Boolean parameter if the issue should be confidential # Example Request: # POST /projects/:id/issues post ':id/issues' do required_attributes! [:title] - keys = [:title, :description, :assignee_id, :milestone_id, :due_date] + keys = [:title, :description, :assignee_id, :milestone_id, :due_date, :confidential] keys << :created_at if current_user.admin? || user_project.owner == current_user attrs = attributes_for_keys(keys) @@ -156,6 +157,10 @@ module API attrs[:labels] = params[:labels] if params[:labels] + # Convert and filter out invalid confidential flags + attrs['confidential'] = to_boolean(attrs['confidential']) + attrs.delete('confidential') if attrs['confidential'].nil? + issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute if issue.spam? @@ -182,12 +187,13 @@ module API # state_event (optional) - The state event of an issue (close|reopen) # updated_at (optional) - Date time string, ISO 8601 formatted # due_date (optional) - Date time string in the format YEAR-MONTH-DAY + # confidential (optional) - Boolean parameter if the issue should be confidential # Example Request: # PUT /projects/:id/issues/:issue_id put ':id/issues/:issue_id' do issue = user_project.issues.find(params[:issue_id]) authorize! :update_issue, issue - keys = [:title, :description, :assignee_id, :milestone_id, :state_event, :due_date] + keys = [:title, :description, :assignee_id, :milestone_id, :state_event, :due_date, :confidential] keys << :updated_at if current_user.admin? || user_project.owner == current_user attrs = attributes_for_keys(keys) @@ -198,6 +204,10 @@ module API attrs[:labels] = params[:labels] if params[:labels] + # Convert and filter out invalid confidential flags + attrs['confidential'] = to_boolean(attrs['confidential']) + attrs.delete('confidential') if attrs['confidential'].nil? + issue = ::Issues::UpdateService.new(user_project, current_user, attrs).execute(issue) if issue.valid? -- cgit v1.2.1 From 9c3db830c033750c19197fe6a3f15a7705e2aa3b Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 23 Aug 2016 13:29:59 -0400 Subject: entities: expose {,merge_commit_}sha in MergeRequest Fixes #20456. --- lib/api/entities.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 4335e3055ef..3b05f0487ff 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -232,6 +232,8 @@ module API expose :milestone, using: Entities::Milestone expose :merge_when_build_succeeds expose :merge_status + expose :diff_head_sha, as: :sha + expose :merge_commit_sha expose :subscribed do |merge_request, options| merge_request.subscribed?(options[:current_user]) end -- cgit v1.2.1 From 6a58af3a4a1c3122d57238505898f61c40bf7b54 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 25 Aug 2016 18:34:01 -0500 Subject: Add BroadcastMessage API implementation --- lib/api/api.rb | 1 + lib/api/broadcast_messages.rb | 99 +++++++++++++++++++++++++++++++++++++++++++ lib/api/entities.rb | 5 +++ 3 files changed, 105 insertions(+) create mode 100644 lib/api/broadcast_messages.rb (limited to 'lib/api') diff --git a/lib/api/api.rb b/lib/api/api.rb index 4602e627fdb..e14464c1b0d 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -31,6 +31,7 @@ module API mount ::API::AccessRequests mount ::API::AwardEmoji mount ::API::Branches + mount ::API::BroadcastMessages mount ::API::Builds mount ::API::CommitStatuses mount ::API::Commits diff --git a/lib/api/broadcast_messages.rb b/lib/api/broadcast_messages.rb new file mode 100644 index 00000000000..fb2a4148011 --- /dev/null +++ b/lib/api/broadcast_messages.rb @@ -0,0 +1,99 @@ +module API + class BroadcastMessages < Grape::API + before { authenticate! } + before { authenticated_as_admin! } + + resource :broadcast_messages do + helpers do + def find_message + BroadcastMessage.find(params[:id]) + end + end + + desc 'Get all broadcast messages' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::BroadcastMessage + end + params do + optional :page, type: Integer, desc: 'Current page number' + optional :per_page, type: Integer, desc: 'Number of messages per page' + end + get do + messages = BroadcastMessage.all + + present paginate(messages), with: Entities::BroadcastMessage + end + + desc 'Create a broadcast message' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::BroadcastMessage + end + params do + requires :message, type: String, desc: 'Message to display' + optional :starts_at, type: DateTime, desc: 'Starting time', default: -> { Time.zone.now } + optional :ends_at, type: DateTime, desc: 'Ending time', default: -> { 1.hour.from_now } + optional :color, type: String, desc: 'Background color' + optional :font, type: String, desc: 'Foreground color' + end + post do + create_params = declared(params, include_missing: false).to_h + message = BroadcastMessage.create(create_params) + + if message.persisted? + present message, with: Entities::BroadcastMessage + else + render_validation_error!(message) + end + end + + desc 'Get a specific broadcast message' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::BroadcastMessage + end + params do + requires :id, type: Integer, desc: 'Broadcast message ID' + end + get ':id' do + message = find_message + + present message, with: Entities::BroadcastMessage + end + + desc 'Update a broadcast message' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::BroadcastMessage + end + params do + requires :id, type: Integer, desc: 'Broadcast message ID' + optional :message, type: String, desc: 'Message to display' + optional :starts_at, type: DateTime, desc: 'Starting time' + optional :ends_at, type: DateTime, desc: 'Ending time' + optional :color, type: String, desc: 'Background color' + optional :font, type: String, desc: 'Foreground color' + end + put ':id' do + message = find_message + update_params = declared(params, include_missing: false).to_h + + if message.update(update_params) + present message, with: Entities::BroadcastMessage + else + render_validation_error!(message) + end + end + + desc 'Delete a broadcast message' do + detail 'This feature was introduced in GitLab 8.12.' + success Entities::BroadcastMessage + end + params do + requires :id, type: Integer, desc: 'Broadcast message ID' + end + delete ':id' do + message = find_message + + present message.destroy, with: Entities::BroadcastMessage + end + end + end +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index e3a8ff6de80..fe7468dd681 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -575,5 +575,10 @@ module API class Template < Grape::Entity expose :name, :content end + + class BroadcastMessage < Grape::Entity + expose :id, :message, :starts_at, :ends_at, :color, :font + expose :active?, as: :active + end end end -- cgit v1.2.1 From 8b6154c145b22d34146fc08c49d2e2d1569d44a0 Mon Sep 17 00:00:00 2001 From: Drew Blessing Date: Wed, 24 Aug 2016 19:58:05 -0500 Subject: Minor edits to two_factor_recovery_codes API error catching --- lib/api/internal.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'lib/api') diff --git a/lib/api/internal.rb b/lib/api/internal.rb index 5b54c11ef62..6e6efece7c4 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -105,15 +105,19 @@ module API post '/two_factor_recovery_codes' do status 200 - key = Key.find(params[:key_id]) - user = key.user + key = Key.find_by(id: params[:key_id]) + + unless key + return { 'success' => false, 'message' => 'Could not find the given key' } + end - # Make sure this isn't a deploy key - unless key.type.nil? + if key.is_a?(DeployKey) return { success: false, message: 'Deploy keys cannot be used to retrieve recovery codes' } end - unless user.present? + user = key.user + + unless user return { success: false, message: 'Could not find a user for the given key' } end -- cgit v1.2.1 From 892dea67717c0efbd6a28f7639f34535ec0a8747 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Mon, 1 Aug 2016 19:31:21 -0300 Subject: Project tools visibility level --- lib/api/entities.rb | 12 ++++++++++-- lib/api/groups.rb | 2 +- lib/api/projects.rb | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) (limited to 'lib/api') diff --git a/lib/api/entities.rb b/lib/api/entities.rb index fe7468dd681..e7fe437ee0d 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -76,7 +76,15 @@ module API expose :owner, using: Entities::UserBasic, unless: ->(project, options) { project.group } expose :name, :name_with_namespace expose :path, :path_with_namespace - expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :builds_enabled, :snippets_enabled, :container_registry_enabled + expose :container_registry_enabled + + # Expose old field names with the new permissions methods to keep API compatible + expose(:issues_enabled) { |project, options| project.feature_available?(:issues, options[:user]) } + expose(:merge_requests_enabled) { |project, options| project.feature_available?(:merge_requests, options[:user]) } + expose(:wiki_enabled) { |project, options| project.feature_available?(:wiki, options[:user]) } + expose(:builds_enabled) { |project, options| project.feature_available?(:builds, options[:user]) } + expose(:snippets_enabled) { |project, options| project.feature_available?(:snippets, options[:user]) } + expose :created_at, :last_activity_at expose :shared_runners_enabled, :lfs_enabled expose :creator_id @@ -84,7 +92,7 @@ module API expose :forked_from_project, using: Entities::BasicProjectDetails, if: lambda{ |project, options| project.forked? } expose :avatar_url expose :star_count, :forks_count - expose :open_issues_count, if: lambda { |project, options| project.issues_enabled? && project.default_issues_tracker? } + expose :open_issues_count, if: lambda { |project, options| project.feature_available?(:issues, options[:user]) && project.default_issues_tracker? } expose :runners_token, if: lambda { |_project, options| options[:user_can_admin_project] } expose :public_builds expose :shared_with_groups do |project, options| diff --git a/lib/api/groups.rb b/lib/api/groups.rb index f981ec0dbfe..d2df77238d5 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -97,7 +97,7 @@ module API group = find_group(params[:id]) projects = GroupProjectsFinder.new(group).execute(current_user) projects = paginate projects - present projects, with: Entities::Project + present projects, with: Entities::Project, user: current_user end # Transfer a project to the Group namespace diff --git a/lib/api/projects.rb b/lib/api/projects.rb index f8979a1cc29..a1fd598414a 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -51,7 +51,7 @@ module API @projects = current_user.viewable_starred_projects @projects = filter_projects(@projects) @projects = paginate @projects - present @projects, with: Entities::Project + present @projects, with: Entities::Project, user: current_user end # Get all projects for admin user -- cgit v1.2.1