diff --git a/lib/gitlab/client/remote_mirrors.rb b/lib/gitlab/client/remote_mirrors.rb index fb75a2006..a2ba302db 100644 --- a/lib/gitlab/client/remote_mirrors.rb +++ b/lib/gitlab/client/remote_mirrors.rb @@ -16,6 +16,19 @@ def remote_mirrors(project) get("/projects/#{url_encode project}/remote_mirrors") end + # Get a specific remote mirror. + # + # @example + # Gitlab.remote_mirror(42, 1234) + # + # @param [Integer, String] project The ID or path of a project. + # @param [Integer] id The ID of the remote mirror. + # + # @return [Gitlab::ObjectifiedHash] Information about the specified remote mirror. + def remote_mirror(project, id) + get("/projects/#{url_encode project}/remote_mirrors/#{id}") + end + # Create a remote mirror # # @example @@ -47,5 +60,31 @@ def create_remote_mirror(project, url, options = {}) def edit_remote_mirror(project, id, options = {}) put("/projects/#{url_encode project}/remote_mirrors/#{id}", body: options) end + + # Delete a remote mirror. + # + # @example + # Gitlab.delete_remote_mirror(42, 1234) + # + # @param [Integer, String] project The ID or path of a project. + # @param [Integer] id The ID of the remote mirror. + # + # @return [Gitlab::ObjectifiedHash] + def delete_remote_mirror(project, id) + delete("/projects/#{url_encode project}/remote_mirrors/#{id}") + end + + # Force push mirror update. + # + # @example + # Gitlab.sync_remote_mirror(42, 1234) + # + # @param [Integer, String] project The ID or path of a project. + # @param [Integer] id The ID of the remote mirror. + # + # @return [Gitlab::ObjectifiedHash] + def sync_remote_mirror(project, id) + post("/projects/#{url_encode project}/remote_mirrors/#{id}/sync") + end end end diff --git a/spec/fixtures/remote_mirror.json b/spec/fixtures/remote_mirror.json new file mode 100644 index 000000000..f7262a9f1 --- /dev/null +++ b/spec/fixtures/remote_mirror.json @@ -0,0 +1 @@ +{"enabled":true,"id":123456,"keep_divergent_refs":true,"last_error":"Some refs have diverged and have not been updated on the remote:\n\nrefs/heads/master","last_successful_update_at":"2020-08-11T15:36:04.668Z","last_update_at":"2020-08-11T15:37:20.701Z","last_update_started_at":"2020-08-11T15:37:17.659Z","only_protected_branches":true,"update_status":"failed","url":"https://*****:*****@gitlab.com/mirror/target.git"} diff --git a/spec/gitlab/client/remote_mirrors_spec.rb b/spec/gitlab/client/remote_mirrors_spec.rb index c71c013ea..8c6a00a91 100644 --- a/spec/gitlab/client/remote_mirrors_spec.rb +++ b/spec/gitlab/client/remote_mirrors_spec.rb @@ -20,6 +20,23 @@ end end + describe '.remote_mirror' do + before do + stub_get('/projects/5/remote_mirrors/123456', 'remote_mirror') + @mirror = Gitlab.remote_mirror(5, 123_456) + end + + it 'gets the correct resource' do + expect(a_get('/projects/5/remote_mirrors/123456')).to have_been_made + end + + it "returns a paginated response of project's push mirrors" do + expect(@mirror).to be_a Gitlab::ObjectifiedHash + expect(@mirror.enabled).to be(true) + expect(@mirror.url).to include('gitlab.com/mirror/target.git') + end + end + describe '.create_remote_mirror' do let(:api_path) { '/projects/5/remote_mirrors' } let(:mirror_path) { 'https://username:token@example.com/gitlab/example.git' } @@ -67,4 +84,34 @@ expect(@mirror.keep_divergent_refs).to be(true) end end + + describe '.delete_remote_mirror' do + before do + stub_request(:delete, "#{Gitlab.endpoint}/projects/5/remote_mirrors/123456") + .with(headers: { 'PRIVATE-TOKEN' => Gitlab.private_token }) + .to_return(status: 204) + @mirror = Gitlab.delete_remote_mirror(5, 123_456) + end + + it 'deletes the correct resource' do + expect(a_delete('/projects/5/remote_mirrors/123456')).to have_been_made + end + + it 'removes the mirror' do + expect(@mirror.to_h).to be_empty + end + end + + describe '.sync_remote_mirror' do + before do + stub_request(:post, "#{Gitlab.endpoint}/projects/5/remote_mirrors/123456/sync") + .with(headers: { 'PRIVATE-TOKEN' => Gitlab.private_token }) + .to_return(status: 204) + @mirror = Gitlab.sync_remote_mirror(5, 123_456) + end + + it 'executes a POST to the correct resource' do + expect(a_post('/projects/5/remote_mirrors/123456/sync')).to have_been_made + end + end end