From 936ba8973deed70c39c883bcb11cf79d10701e20 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 3 Aug 2020 14:37:43 -0500 Subject: [PATCH 1/2] Support additional parameters to Commits#cherry_pick A `dry_run` optional parameter is being added in GitLab 13.3. --- lib/gitlab/client/commits.rb | 8 ++++++-- spec/gitlab/client/commits_spec.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/client/commits.rb b/lib/gitlab/client/commits.rb index dcb2716cb..860507872 100644 --- a/lib/gitlab/client/commits.rb +++ b/lib/gitlab/client/commits.rb @@ -59,9 +59,13 @@ def commit_refs(project, sha, options = {}) # @param [Integer, String] project The ID or name of a project. # @param [String] sha The commit hash or name of a repository branch or tag # @param [String] branch The name of the branch + # @param [Hash] options A customizable set of options. + # @option options [Boolean] :dry_run Don't commit any changes # @return [Gitlab::ObjectifiedHash] - def cherry_pick_commit(project, sha, branch) - post("/projects/#{url_encode project}/repository/commits/#{sha}/cherry_pick", body: { branch: branch }) + def cherry_pick_commit(project, sha, branch, options = {}) + options[:branch] = branch + + post("/projects/#{url_encode project}/repository/commits/#{sha}/cherry_pick", body: options) end # Get the diff of a commit in a project. diff --git a/spec/gitlab/client/commits_spec.rb b/spec/gitlab/client/commits_spec.rb index 4de8449f9..b22ffa0ab 100644 --- a/spec/gitlab/client/commits_spec.rb +++ b/spec/gitlab/client/commits_spec.rb @@ -92,6 +92,19 @@ end end end + + context 'with additional options' do + it 'passes additional options' do + stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'project_commit') + .with(body: { branch: 'master', dry_run: true }) + + Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master', dry_run: true) + + expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick') + .with(body: { branch: 'master', dry_run: true })) + .to have_been_made + end + end end describe '.commit_diff' do From 5549113bad06bc0bd01ea261614cba164434ae0f Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Mon, 3 Aug 2020 14:38:46 -0500 Subject: [PATCH 2/2] Support the Commit revert API --- lib/gitlab/client/commits.rb | 17 ++++++++++ spec/fixtures/revert_commit_failure.json | 1 + spec/gitlab/client/commits_spec.rb | 43 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 spec/fixtures/revert_commit_failure.json diff --git a/lib/gitlab/client/commits.rb b/lib/gitlab/client/commits.rb index 860507872..ca9792926 100644 --- a/lib/gitlab/client/commits.rb +++ b/lib/gitlab/client/commits.rb @@ -68,6 +68,23 @@ def cherry_pick_commit(project, sha, branch, options = {}) post("/projects/#{url_encode project}/repository/commits/#{sha}/cherry_pick", body: options) end + # Reverts a commit in a given branch. + # + # @example + # Gitlab.revert_commit(42, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') + # + # @param [Integer, String] project The ID or name of a project. + # @param [String] sha The commit hash or name of a repository branch or tag + # @param [String] branch The name of the branch + # @param [Hash] options A customizable set of options. + # @option options [Boolean] :dry_run Don't commit any changes + # @return [Gitlab::ObjectifiedHash] + def revert_commit(project, sha, branch, options = {}) + options[:branch] = branch + + post("/projects/#{url_encode project}/repository/commits/#{sha}/revert", body: options) + end + # Get the diff of a commit in a project. # # @example diff --git a/spec/fixtures/revert_commit_failure.json b/spec/fixtures/revert_commit_failure.json new file mode 100644 index 000000000..1605bfd81 --- /dev/null +++ b/spec/fixtures/revert_commit_failure.json @@ -0,0 +1 @@ +{"message":"Sorry, we cannot revert this commit automatically. This commit may already have been reverted, or a more recent commit may have updated some of its content.","error_code":"empty"} diff --git a/spec/gitlab/client/commits_spec.rb b/spec/gitlab/client/commits_spec.rb index b22ffa0ab..d59b60540 100644 --- a/spec/gitlab/client/commits_spec.rb +++ b/spec/gitlab/client/commits_spec.rb @@ -107,6 +107,49 @@ end end + describe '.revert_commit' do + context 'on success' do + before do + stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'project_commit').with(body: { branch: 'master' }) + @revert_commit = Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') + end + + it 'gets the correct resource' do + expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert') + .with(body: { branch: 'master' })) + .to have_been_made + end + + it 'returns the correct response' do + expect(@revert_commit).to be_a Gitlab::ObjectifiedHash + expect(@revert_commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6') + end + end + + context 'on failure' do + it 'includes the error_code' do + stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'revert_commit_failure', 400) + + expect { Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') }.to raise_error(Gitlab::Error::BadRequest) do |ex| + expect(ex.error_code).to eq('empty') + end + end + end + + context 'with additional options' do + it 'passes additional options' do + stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert', 'project_commit') + .with(body: { branch: 'master', dry_run: true }) + + Gitlab.revert_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master', dry_run: true) + + expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/revert') + .with(body: { branch: 'master', dry_run: true })) + .to have_been_made + end + end + end + describe '.commit_diff' do before do stub_get('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/diff', 'project_commit_diff')