Commit b8d75d41 authored by Rémy Coutable's avatar Rémy Coutable

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
parents 53ad9522 2b8f04a6
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)
......
......@@ -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
```
......
......@@ -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,
......
......@@ -63,23 +63,60 @@ describe API::API, api: true do
end
describe 'GET /projects/:id/repository/commits/:sha/builds' do
context 'when commit does not exist in repository' do
before do
project.ensure_pipeline(pipeline.sha, 'master')
get api("/projects/#{project.id}/repository/commits/#{pipeline.sha}/builds", api_user)
get api("/projects/#{project.id}/repository/commits/1a271fd1/builds", api_user)
end
it 'responds with 404' do
expect(response).to have_http_status(404)
end
end
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
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
expect(json_response.size).to eq 2
end
end
context 'unauthorized user' do
let(:api_user) { nil }
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment