summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-06-28 17:10:07 +0000
committerRémy Coutable <remy@rymai.me>2016-06-28 17:10:07 +0000
commitb8d75d41aad1e3d06bb30e1838b1e8b2b2cd8c78 (patch)
tree1dce6077fc50b31ca930c5b31fc71ac3233fa74d
parent53ad9522c46edf8ede068145fa3d89e2b047c076 (diff)
parent2b8f04a669d9fb388983b6509135984588fb8306 (diff)
downloadgitlab-ce-b8d75d41aad1e3d06bb30e1838b1e8b2b2cd8c78.tar.gz
Merge branch 'fix/builds-api-builds-for-commit-search' into 'master'
Find all builds for commit if there are multiple pipelines for it ## What does this MR do? This MR fixes a builds API. When multiple pipelines were triggered for a commit, then API returned builds only from the last pipeline. ## What are the relevant issue numbers? Closes #18912 Closes #19243 ## Does this MR meet the acceptance criteria? - [x] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added - [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md) - [x] API support added - Tests - [x] Added for this feature/bug - [x] All builds are passing - [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides) - [x] Branch has no merge conflicts with `master` (if you do - rebase it please) - [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits) See merge request !4849
-rw-r--r--CHANGELOG1
-rw-r--r--doc/api/builds.md5
-rw-r--r--lib/api/builds.rb6
-rw-r--r--spec/requests/api/builds_spec.rb61
4 files changed, 58 insertions, 15 deletions
diff --git a/CHANGELOG b/CHANGELOG
index dcec98469cb..f25b424d115 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date.
v 8.10.0 (unreleased)
+ - Fix commit builds API, return all builds for all pipelines for given commit. !4849
- Replace Haml with Hamlit to make view rendering faster. !3666
- Wrap code blocks on Activies and Todos page. !4783 (winniehell)
- Display last commit of deleted branch in push events !4699 (winniehell)
diff --git a/doc/api/builds.md b/doc/api/builds.md
index de998944352..2adea11247e 100644
--- a/doc/api/builds.md
+++ b/doc/api/builds.md
@@ -107,6 +107,11 @@ Example of response
Get a list of builds for specific commit in a project.
+This endpoint will return all builds, from all pipelines for a given commit.
+If the commit SHA is not found, it will respond with 404, otherwise it will
+return an array of builds (an empty array if there are no builds for this
+particular commit).
+
```
GET /projects/:id/repository/commits/:sha/builds
```
diff --git a/lib/api/builds.rb b/lib/api/builds.rb
index 979328efe0e..086d8511e8f 100644
--- a/lib/api/builds.rb
+++ b/lib/api/builds.rb
@@ -33,10 +33,10 @@ module API
get ':id/repository/commits/:sha/builds' do
authorize_read_builds!
- commit = user_project.pipelines.find_by_sha(params[:sha])
- return not_found! unless commit
+ return not_found! unless user_project.commit(params[:sha])
- builds = commit.builds.order('id DESC')
+ pipelines = user_project.pipelines.where(sha: params[:sha])
+ builds = user_project.builds.where(pipeline: pipelines).order('id DESC')
builds = filter_builds(builds, params[:scope])
present paginate(builds), with: Entities::Build,
diff --git a/spec/requests/api/builds_spec.rb b/spec/requests/api/builds_spec.rb
index 2ab9d640269..f5b39c3d698 100644
--- a/spec/requests/api/builds_spec.rb
+++ b/spec/requests/api/builds_spec.rb
@@ -63,23 +63,60 @@ describe API::API, api: true do
end
describe 'GET /projects/:id/repository/commits/:sha/builds' do
- before do
- project.ensure_pipeline(pipeline.sha, 'master')
- get api("/projects/#{project.id}/repository/commits/#{pipeline.sha}/builds", api_user)
- end
+ context 'when commit does not exist in repository' do
+ before do
+ get api("/projects/#{project.id}/repository/commits/1a271fd1/builds", api_user)
+ end
- context 'authorized user' do
- it 'should return project builds for specific commit' do
- expect(response).to have_http_status(200)
- expect(json_response).to be_an Array
+ it 'responds with 404' do
+ expect(response).to have_http_status(404)
end
end
- context 'unauthorized user' do
- let(:api_user) { nil }
+ context 'when commit exists in repository' do
+ context 'when user is authorized' do
+ context 'when pipeline has builds' do
+ before do
+ create(:ci_pipeline, project: project, sha: project.commit.id)
+ create(:ci_build, pipeline: pipeline)
+ create(:ci_build)
+
+ get api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", api_user)
+ end
+
+ it 'should return project builds for specific commit' do
+ expect(response).to have_http_status(200)
+ expect(json_response).to be_an Array
+ expect(json_response.size).to eq 2
+ end
+ end
- it 'should not return project builds' do
- expect(response).to have_http_status(401)
+ context 'when pipeline has no builds' do
+ before do
+ branch_head = project.commit('feature').id
+ get api("/projects/#{project.id}/repository/commits/#{branch_head}/builds", api_user)
+ end
+
+ it 'returns an empty array' do
+ expect(response).to have_http_status(200)
+ expect(json_response).to be_an Array
+ expect(json_response).to be_empty
+ end
+ end
+ end
+
+ context 'when user is not authorized' do
+ before do
+ create(:ci_pipeline, project: project, sha: project.commit.id)
+ create(:ci_build, pipeline: pipeline)
+
+ get api("/projects/#{project.id}/repository/commits/#{project.commit.id}/builds", nil)
+ end
+
+ it 'should not return project builds' do
+ expect(response).to have_http_status(401)
+ expect(json_response.except('message')).to be_empty
+ end
end
end
end