2015-10-06 06:01:16 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 16:32:02 -04:00
|
|
|
describe API::CommitStatuses do
|
2017-01-26 18:51:57 -05:00
|
|
|
let!(:project) { create(:project, :repository) }
|
2015-10-06 06:01:16 -04:00
|
|
|
let(:commit) { project.repository.commit }
|
2016-03-06 16:53:22 -05:00
|
|
|
let(:guest) { create_user(:guest) }
|
|
|
|
let(:reporter) { create_user(:reporter) }
|
|
|
|
let(:developer) { create_user(:developer) }
|
2016-03-01 06:33:25 -05:00
|
|
|
let(:sha) { commit.id }
|
|
|
|
|
2017-02-09 06:47:11 -05:00
|
|
|
let(:commit_status) do
|
|
|
|
create(:commit_status, status: :pending, pipeline: pipeline)
|
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
describe "GET /projects/:id/repository/commits/:sha/statuses" do
|
2016-03-01 06:33:25 -05:00
|
|
|
let(:get_url) { "/projects/#{project.id}/repository/commits/#{sha}/statuses" }
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
context 'ci commit exists' do
|
2017-09-01 04:54:07 -04:00
|
|
|
let!(:master) { project.pipelines.create(source: :push, sha: commit.id, ref: 'master', protected: false) }
|
|
|
|
let!(:develop) { project.pipelines.create(source: :push, sha: commit.id, ref: 'develop', protected: false) }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
context "reporter user" do
|
|
|
|
let(:statuses_id) { json_response.map { |status| status['id'] } }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-04-11 10:55:40 -04:00
|
|
|
def create_status(commit, opts = {})
|
2016-06-03 06:29:00 -04:00
|
|
|
create(:commit_status, { pipeline: commit, ref: commit.ref }.merge(opts))
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
|
|
|
|
2017-04-16 07:14:39 -04:00
|
|
|
let!(:status1) { create_status(master, status: 'running', retried: true) }
|
|
|
|
let!(:status2) { create_status(master, name: 'coverage', status: 'pending', retried: true) }
|
2016-04-11 10:55:40 -04:00
|
|
|
let!(:status3) { create_status(develop, status: 'running', allow_failure: true) }
|
|
|
|
let!(:status4) { create_status(master, name: 'coverage', status: 'success') }
|
|
|
|
let!(:status5) { create_status(develop, name: 'coverage', status: 'success') }
|
|
|
|
let!(:status6) { create_status(master, status: 'success') }
|
2016-02-29 07:54:33 -05:00
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
context 'latest commit statuses' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, reporter)
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
|
|
|
it 'returns latest commit statuses' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2016-02-29 07:54:33 -05:00
|
|
|
|
2017-02-20 09:39:16 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-02-29 08:20:11 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status3.id, status4.id, status5.id, status6.id)
|
2017-08-09 05:52:22 -04:00
|
|
|
json_response.sort_by! { |status| status['id'] }
|
|
|
|
expect(json_response.map { |status| status['allow_failure'] }).to eq([true, false, false, false])
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
context 'all commit statuses' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, reporter), all: 1
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
|
|
|
it 'returns all commit statuses' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-02-29 08:20:11 -05:00
|
|
|
expect(json_response).to be_an Array
|
2016-03-01 06:33:25 -05:00
|
|
|
expect(statuses_id).to contain_exactly(status1.id, status2.id,
|
|
|
|
status3.id, status4.id,
|
|
|
|
status5.id, status6.id)
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
context 'latest commit statuses for specific ref' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, reporter), ref: 'develop'
|
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
it 'returns latest commit statuses for specific ref' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-02-29 08:20:11 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(statuses_id).to contain_exactly(status3.id, status5.id)
|
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
context 'latest commit statues for specific name' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, reporter), name: 'coverage'
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
|
|
|
it 'return latest commit statuses for specific name' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2016-02-29 08:20:11 -05:00
|
|
|
expect(json_response).to be_an Array
|
2016-03-01 06:33:25 -05:00
|
|
|
expect(statuses_id).to contain_exactly(status4.id, status5.id)
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2016-02-29 07:54:33 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
context 'ci commit does not exist' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, reporter)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 07:54:33 -05:00
|
|
|
it 'returns empty array' do
|
|
|
|
expect(response.status).to eq 200
|
2015-10-06 06:01:16 -04:00
|
|
|
expect(json_response).to be_an Array
|
2016-02-29 07:54:33 -05:00
|
|
|
expect(json_response).to be_empty
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "guest user" do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url, guest)
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not return project commits" do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
get api(get_url)
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not return project commits" do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-12 06:15:35 -04:00
|
|
|
describe 'POST /projects/:id/statuses/:sha' do
|
2016-03-01 06:33:25 -05:00
|
|
|
let(:post_url) { "/projects/#{project.id}/statuses/#{sha}" }
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-06 09:01:37 -05:00
|
|
|
context 'developer user' do
|
2016-08-25 11:52:09 -04:00
|
|
|
%w[pending running success failed canceled].each do |status|
|
|
|
|
context "for #{status}" do
|
|
|
|
context 'uses only required parameters' do
|
|
|
|
it 'creates commit status' do
|
|
|
|
post api(post_url, developer), state: status
|
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq(status)
|
|
|
|
expect(json_response['name']).to eq('default')
|
|
|
|
expect(json_response['ref']).not_to be_empty
|
|
|
|
expect(json_response['target_url']).to be_nil
|
|
|
|
expect(json_response['description']).to be_nil
|
2017-08-30 14:20:54 -04:00
|
|
|
if status == 'failed'
|
2017-09-01 09:01:26 -04:00
|
|
|
expect(CommitStatus.find(json_response['id'])).to be_api_failure
|
2017-08-30 14:20:54 -04:00
|
|
|
end
|
2016-08-25 11:52:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'transitions status from pending' do
|
|
|
|
before do
|
|
|
|
post api(post_url, developer), state: 'pending'
|
|
|
|
end
|
|
|
|
|
|
|
|
%w[running success failed canceled].each do |status|
|
|
|
|
it "to #{status}" do
|
|
|
|
expect { post api(post_url, developer), state: status }.not_to change { CommitStatus.count }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['status']).to eq(status)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with all optional parameters' do
|
2017-03-01 07:59:15 -05:00
|
|
|
context 'when creating a commit status' do
|
2017-06-27 08:08:40 -04:00
|
|
|
subject do
|
2017-03-01 07:59:15 -05:00
|
|
|
post api(post_url, developer), {
|
|
|
|
state: 'success',
|
|
|
|
context: 'coverage',
|
2017-06-27 08:08:40 -04:00
|
|
|
ref: 'master',
|
2017-03-01 07:59:15 -05:00
|
|
|
description: 'test',
|
|
|
|
coverage: 80.0,
|
2017-03-01 09:11:10 -05:00
|
|
|
target_url: 'http://gitlab.com/status'
|
|
|
|
}
|
2017-06-27 08:08:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates commit status' do
|
|
|
|
subject
|
2017-03-01 07:59:15 -05:00
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq('success')
|
|
|
|
expect(json_response['name']).to eq('coverage')
|
2017-06-27 08:08:40 -04:00
|
|
|
expect(json_response['ref']).to eq('master')
|
2017-03-01 07:59:15 -05:00
|
|
|
expect(json_response['coverage']).to eq(80.0)
|
|
|
|
expect(json_response['description']).to eq('test')
|
|
|
|
expect(json_response['target_url']).to eq('http://gitlab.com/status')
|
|
|
|
end
|
2017-06-27 08:08:40 -04:00
|
|
|
|
|
|
|
context 'when merge request exists for given branch' do
|
|
|
|
let!(:merge_request) { create(:merge_request, source_project: project, source_branch: 'master', target_branch: 'develop') }
|
|
|
|
|
|
|
|
it 'sets head pipeline' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(merge_request.reload.head_pipeline).not_to be_nil
|
|
|
|
end
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2017-03-01 07:59:15 -05:00
|
|
|
context 'when updatig a commit status' do
|
|
|
|
before do
|
|
|
|
post api(post_url, developer), {
|
|
|
|
state: 'running',
|
|
|
|
context: 'coverage',
|
2017-06-27 08:08:40 -04:00
|
|
|
ref: 'master',
|
2017-03-01 07:59:15 -05:00
|
|
|
description: 'coverage test',
|
|
|
|
coverage: 0.0,
|
2017-03-01 09:11:10 -05:00
|
|
|
target_url: 'http://gitlab.com/status'
|
|
|
|
}
|
2017-03-01 07:59:15 -05:00
|
|
|
|
|
|
|
post api(post_url, developer), {
|
|
|
|
state: 'success',
|
|
|
|
name: 'coverage',
|
2017-06-27 08:08:40 -04:00
|
|
|
ref: 'master',
|
2017-03-01 07:59:15 -05:00
|
|
|
description: 'new description',
|
2017-03-01 09:11:10 -05:00
|
|
|
coverage: 90.0
|
|
|
|
}
|
2017-03-01 07:59:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates a commit status' do
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq('success')
|
|
|
|
expect(json_response['name']).to eq('coverage')
|
2017-06-27 08:08:40 -04:00
|
|
|
expect(json_response['ref']).to eq('master')
|
2017-03-01 07:59:15 -05:00
|
|
|
expect(json_response['coverage']).to eq(90.0)
|
|
|
|
expect(json_response['description']).to eq('new description')
|
|
|
|
expect(json_response['target_url']).to eq('http://gitlab.com/status')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new commit status' do
|
|
|
|
expect(CommitStatus.count).to eq 1
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-05 03:36:08 -04:00
|
|
|
context 'when retrying a commit status' do
|
|
|
|
before do
|
|
|
|
post api(post_url, developer),
|
|
|
|
{ state: 'failed', name: 'test', ref: 'master' }
|
|
|
|
|
|
|
|
post api(post_url, developer),
|
|
|
|
{ state: 'success', name: 'test', ref: 'master' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly posts a new commit status' do
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['sha']).to eq(commit.id)
|
|
|
|
expect(json_response['status']).to eq('success')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'retries a commit status' do
|
|
|
|
expect(CommitStatus.count).to eq 2
|
|
|
|
expect(CommitStatus.first).to be_retried
|
|
|
|
expect(CommitStatus.last.pipeline).to be_success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-18 06:24:53 -05:00
|
|
|
context 'when status is invalid' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
post api(post_url, developer), state: 'invalid'
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
|
|
|
it 'does not create commit status' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
|
|
|
|
2017-01-18 06:24:53 -05:00
|
|
|
context 'when request without a state made' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
post api(post_url, developer)
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2016-02-29 08:20:11 -05:00
|
|
|
it 'does not create commit status' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2017-01-18 06:24:53 -05:00
|
|
|
context 'when commit SHA is invalid' do
|
2016-03-01 06:33:25 -05:00
|
|
|
let(:sha) { 'invalid_sha' }
|
2017-06-14 14:18:56 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
post api(post_url, developer), state: 'running'
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
|
|
|
it 'returns not found error' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
2017-01-18 06:24:53 -05:00
|
|
|
|
|
|
|
context 'when target URL is an invalid address' do
|
|
|
|
before do
|
|
|
|
post api(post_url, developer), state: 'pending',
|
|
|
|
target_url: 'invalid url'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds with bad request status and validation errors' do
|
|
|
|
expect(response).to have_http_status(400)
|
|
|
|
expect(json_response['message']['target_url'])
|
|
|
|
.to include 'must be a valid URL'
|
|
|
|
end
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-02-06 09:01:37 -05:00
|
|
|
context 'reporter user' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
post api(post_url, reporter), state: 'running'
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not create commit status' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2016-02-06 09:01:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
context 'guest user' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
post api(post_url, guest), state: 'running'
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not create commit status' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthorized user' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
post api(post_url)
|
|
|
|
end
|
2016-02-29 08:20:11 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'does not create commit status' do
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-06 09:01:37 -05:00
|
|
|
|
2016-03-06 16:53:22 -05:00
|
|
|
def create_user(access_level_trait)
|
2016-02-06 09:01:37 -05:00
|
|
|
user = create(:user)
|
2016-03-06 16:53:22 -05:00
|
|
|
create(:project_member, access_level_trait, user: user, project: project)
|
2016-02-06 09:01:37 -05:00
|
|
|
user
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|