From 74906f3dd8ec6084e56cc28523e59f7634a9837e Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 1 Mar 2017 13:59:15 +0100 Subject: [PATCH 1/3] Fix updaing commit status with optional attributes Passing different optional attributes in case of updating an existing commit status should not create a new commit status with the same name. --- lib/api/commit_statuses.rb | 9 +-- spec/requests/api/commit_statuses_spec.rb | 67 +++++++++++++++++------ 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb index dba0831664c..9d9f82fdb83 100644 --- a/lib/api/commit_statuses.rb +++ b/lib/api/commit_statuses.rb @@ -72,14 +72,15 @@ module API status = GenericCommitStatus.running_or_pending.find_or_initialize_by( project: @project, pipeline: pipeline, - user: current_user, name: name, ref: ref, - target_url: params[:target_url], - description: params[:description], - coverage: params[:coverage] + user: current_user ) + optional_attributes = + attributes_for_keys(%w[target_url description coverage]) + + status.update(optional_attributes) if optional_attributes.any? render_validation_error!(status) if status.invalid? begin diff --git a/spec/requests/api/commit_statuses_spec.rb b/spec/requests/api/commit_statuses_spec.rb index 81a8856b8f1..4b67ac85c0a 100644 --- a/spec/requests/api/commit_statuses_spec.rb +++ b/spec/requests/api/commit_statuses_spec.rb @@ -151,26 +151,59 @@ describe API::CommitStatuses, api: true do end context 'with all optional parameters' do - before do - optional_params = { state: 'success', - context: 'coverage', - ref: 'develop', - description: 'test', - coverage: 80.0, - target_url: 'http://gitlab.com/status' } + context 'when creating a commit status' do + it 'creates commit status' do + post api(post_url, developer), { + state: 'success', + context: 'coverage', + ref: 'develop', + description: 'test', + coverage: 80.0, + target_url: 'http://gitlab.com/status' } - post api(post_url, developer), optional_params + 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') + expect(json_response['ref']).to eq('develop') + 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 end - it 'creates 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') - expect(json_response['ref']).to eq('develop') - 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') + context 'when updatig a commit status' do + before do + post api(post_url, developer), { + state: 'running', + context: 'coverage', + ref: 'develop', + description: 'coverage test', + coverage: 0.0, + target_url: 'http://gitlab.com/status' } + + post api(post_url, developer), { + state: 'success', + name: 'coverage', + ref: 'develop', + description: 'new description', + coverage: 90.0 } + 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') + expect(json_response['ref']).to eq('develop') + 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 end end From 2bc938ef896b8b5c81bb0997125d1a01f4c29b8d Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 1 Mar 2017 14:26:01 +0100 Subject: [PATCH 2/3] Add Changelog entry for commit status update fix --- changelogs/unreleased/fix-gb-update-commit-status-api.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelogs/unreleased/fix-gb-update-commit-status-api.yml diff --git a/changelogs/unreleased/fix-gb-update-commit-status-api.yml b/changelogs/unreleased/fix-gb-update-commit-status-api.yml new file mode 100644 index 00000000000..aa4fcba4e89 --- /dev/null +++ b/changelogs/unreleased/fix-gb-update-commit-status-api.yml @@ -0,0 +1,4 @@ +--- +title: Fix updaing commit status when using optional attributes +merge_request: 9618 +author: From e19687b22480103d1a475ea8b56a3a36a0e77e1b Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 1 Mar 2017 15:11:10 +0100 Subject: [PATCH 3/3] Fix Rubocop offenses in commit status API specs --- spec/requests/api/commit_statuses_spec.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/requests/api/commit_statuses_spec.rb b/spec/requests/api/commit_statuses_spec.rb index 4b67ac85c0a..d8b3cc041a5 100644 --- a/spec/requests/api/commit_statuses_spec.rb +++ b/spec/requests/api/commit_statuses_spec.rb @@ -159,7 +159,8 @@ describe API::CommitStatuses, api: true do ref: 'develop', description: 'test', coverage: 80.0, - target_url: 'http://gitlab.com/status' } + target_url: 'http://gitlab.com/status' + } expect(response).to have_http_status(201) expect(json_response['sha']).to eq(commit.id) @@ -180,14 +181,16 @@ describe API::CommitStatuses, api: true do ref: 'develop', description: 'coverage test', coverage: 0.0, - target_url: 'http://gitlab.com/status' } + target_url: 'http://gitlab.com/status' + } post api(post_url, developer), { state: 'success', name: 'coverage', ref: 'develop', description: 'new description', - coverage: 90.0 } + coverage: 90.0 + } end it 'updates a commit status' do