Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Remote Mirrors API #618

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Client < API
include ProjectReleases
include Projects
include ProtectedTags
include RemoteMirrors
include Repositories
include RepositoryFiles
include RepositorySubmodules
Expand Down
51 changes: 51 additions & 0 deletions lib/gitlab/client/remote_mirrors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

class Gitlab::Client
# Defines methods related to remote mirrors.
# @see https://docs.gitlab.com/ee/api/remote_mirrors.html
module RemoteMirrors
# List a project's remote mirrors
#
# @example
# Gitlab.remote_mirrors(42)
# Gitlab.remote_mirrors('gitlab-org/gitlab')
#
# @param [Integer, String] project The ID or name of a project.
# @return [Array<Gitlab::ObjectifiedHash>]
def remote_mirrors(project)
get("/projects/#{url_encode project}/remote_mirrors")
end

# Create a remote mirror
#
# @example
# Gitlab.create_remote_mirror(42, 'https://mirror-bot@gitlab.com/gitlab-org/gitlab.git', enabled: true)
#
# @param [Integer, String] project The ID or name of a project.
# @param [String] url The full URL of the remote repository.
# @param [Hash] options A customizable set of options.
# @option options [Boolean] :enabled Determines if the mirror is enabled.
# @option options [Boolean] :only_protected_branches Determines if only protected branches are mirrored.
# @option options [Boolean] :keep_divergent_refs Determines if divergent refs are skipped.
# @return [Gitlab::ObjectifiedHash]
def create_remote_mirror(project, url, options = {})
post("/projects/#{url_encode project}/remote_mirrors", body: options.merge(url: url))
end

# Update a remote mirror's attributes
#
# @example
# Gitlab.edit_remote_mirror(42, 66, only_protected_branches: true)
#
# @param [Integer, String] project The ID or name of a project.
# @param [Integer] id The ID of the remote mirror.
# @param [Hash] options A customizable set of options.
# @option options [Boolean] :enabled Determines if the mirror is enabled.
# @option options [Boolean] :only_protected_branches Determines if only protected branches are mirrored.
# @option options [Boolean] :keep_divergent_refs Determines if divergent refs are skipped.
# @return [Gitlab::ObjectifiedHash]
def edit_remote_mirror(project, id, options = {})
put("/projects/#{url_encode project}/remote_mirrors/#{id}", body: options)
end
end
end
12 changes: 12 additions & 0 deletions spec/fixtures/remote_mirror_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"enabled": false,
"id": 123456,
"last_error": null,
"last_successful_update_at": null,
"last_update_at": null,
"last_update_started_at": null,
"only_protected_branches": false,
"keep_divergent_refs": false,
"update_status": "none",
"url": "https://*****:*****@example.com/gitlab/example.git"
}
12 changes: 12 additions & 0 deletions spec/fixtures/remote_mirror_edit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"enabled": false,
"id": 123456,
"last_error": null,
"last_successful_update_at": "2020-01-06T17:32:02.823Z",
"last_update_at": "2020-01-06T17:32:02.823Z",
"last_update_started_at": "2020-01-06T17:31:55.864Z",
"only_protected_branches": true,
"keep_divergent_refs": true,
"update_status": "finished",
"url": "https://*****:*****@gitlab.com/gitlab-org/security/gitlab.git"
}
14 changes: 14 additions & 0 deletions spec/fixtures/remote_mirrors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"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"
}
]
70 changes: 70 additions & 0 deletions spec/gitlab/client/remote_mirrors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Client do
describe '.remote_mirrors' do
before do
stub_get('/projects/5/remote_mirrors', 'remote_mirrors')
@mirrors = Gitlab.remote_mirrors(5)
end

it 'gets the correct resource' do
expect(a_get('/projects/5/remote_mirrors')).to have_been_made
end

it "returns a paginated response of project's push mirrors" do
expect(@mirrors).to be_an Gitlab::PaginatedResponse
expect(@mirrors.first).to be_a Gitlab::ObjectifiedHash
expect(@mirrors.first.enabled).to eq(true)
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' }

before do
stub_post(api_path, 'remote_mirror_create')
@mirror = Gitlab.create_remote_mirror(5, mirror_path, enabled: false)
end

it 'posts to the correct resource' do
expect(a_post(api_path).with(body: { url: mirror_path, enabled: false }))
.to have_been_made
end

it 'returns a single remote mirror' do
expect(@mirror).to be_a Gitlab::ObjectifiedHash
expect(@mirror.enabled).to eq(false)
expect(@mirror.id).to eq(123_456)
expect(@mirror.url).to include('example.com/gitlab/example.git')
end
end

describe '.edit_remote_mirror' do
let(:api_path) { '/projects/5/remote_mirrors/123456' }

before do
stub_put(api_path, 'remote_mirror_edit')
@mirror = Gitlab.edit_remote_mirror(
5,
123_456,
only_protected_branches: true,
keep_divergent_refs: true
)
end

it 'puts to the correct resource' do
expect(a_put(api_path).with(body: { only_protected_branches: true, keep_divergent_refs: true }))
.to have_been_made
end

it 'returns a single remote mirror' do
expect(@mirror).to be_a Gitlab::ObjectifiedHash
expect(@mirror.enabled).to eq(false)
expect(@mirror.only_protected_branches).to eq(true)
expect(@mirror.keep_divergent_refs).to eq(true)
end
end
end