Commit 2f246b9e authored by Gabriel Mazetto's avatar Gabriel Mazetto

Geo: repository update changed to use push event from system hook

parent f619d34f
module Geo
class EnqueueProjectUpdateService
attr_reader :project
def initialize(project)
@queue = Gitlab::Geo::UpdateQueue.new('updated_projects')
@project = project
end
def execute
@queue.store({ id: @project.id, clone_url: @project.url_to_repo })
end
end
end
......@@ -2,12 +2,10 @@ module Geo
class NotifyNodesService < BaseNotify
def initialize
@proj_queue = Gitlab::Geo::UpdateQueue.new('updated_projects')
@wiki_queue = Gitlab::Geo::UpdateQueue.new('updated_wikis')
end
def execute
process(@proj_queue, :notify_projects_url)
process(@wiki_queue, :notify_wikis_url)
end
......
module Geo
class ScheduleRepoUpdateService
attr_reader :projects
attr_reader :id, :clone_url
def initialize(projects)
@projects = projects
def initialize(params)
@id = params[:project_id]
@clone_url = params[:project][:git_ssh_url]
end
def execute
@projects.each do |project|
GeoRepositoryUpdateWorker.perform_async(project['id'], project['clone_url'])
end
GeoRepositoryUpdateWorker.perform_async(@id, @clone_url)
end
end
end
......@@ -23,9 +23,6 @@ class PostReceive
# Triggers repository update on secondary nodes when Geo is enabled
Gitlab::Geo.notify_wiki_update(post_received.project) if Gitlab::Geo.enabled?
elsif post_received.regular_project?
# Triggers repository update on secondary nodes when Geo is enabled
Gitlab::Geo.notify_project_update(post_received.project) if Gitlab::Geo.enabled?
process_project_changes(post_received)
else
log("Triggered hook for unidentifiable repository type with full path \"#{repo_path} \"")
......
module API
class Geo < Grape::API
resource :geo do
# Enqueue a batch of IDs of modified projects to have their
# repositories updated
#
# Example request:
# POST /geo/refresh_projects
post 'refresh_projects' do
authenticated_as_admin!
required_attributes! [:projects]
::Geo::ScheduleRepoUpdateService.new(params[:projects]).execute
end
# Enqueue a batch of IDs of wiki's projects to have their
# wiki repositories updated
#
......@@ -35,6 +24,12 @@ module API
when 'key_create', 'key_destroy'
required_attributes! %w(key id)
::Geo::ScheduleKeyChangeService.new(params).execute
when 'push'
required_attributes! %w(event_name project_id project)
::Geo::ScheduleRepoUpdateService.new(params).execute
when 'tag_push'
required_attributes! %w(event_name project_id project)
::Geo::ScheduleWikiRepoUpdateService.new(params).execute
end
end
end
......
......@@ -34,10 +34,6 @@ module Gitlab
GeoNode.where(host: host, port: port).exists?
end
def self.notify_project_update(project)
::Geo::EnqueueProjectUpdateService.new(project).execute
end
def self.notify_wiki_update(project)
::Geo::EnqueueWikiUpdateService.new(project).execute
end
......
......@@ -2,7 +2,7 @@ module Gitlab
module Middleware
class ReadonlyGeo
DISALLOWED_METHODS = %w(POST PATCH PUT DELETE)
WHITELISTED = %w(api/v3/internal api/v3/geo/refresh_projects api/v3/geo/refresh_wikis api/v3/geo/refresh_key api/v3/geo/receive_events)
WHITELISTED = %w(api/v3/internal api/v3/geo/refresh_wikis api/v3/geo/receive_events)
APPLICATION_JSON = 'application/json'
def initialize(app)
......
......@@ -5,30 +5,28 @@ describe API::API, api: true do
let(:admin) { create(:admin) }
let(:user) { create(:user) }
let(:geo_node) { build(:geo_node) }
let(:geo_token_header) do
{ 'X-Gitlab-Token' => geo_node.system_hook.token }
end
describe 'POST /geo/refresh_projects' do
before(:each) { allow_any_instance_of(::Geo::ScheduleRepoUpdateService).to receive(:execute) }
before(:each) do
allow(Gitlab::Geo).to receive(:current_node) { geo_node }
end
it 'starts refresh process if admin and correct params' do
post api('/geo/refresh_projects', admin), projects: ['1', '2', '3']
expect(response.status).to eq 201
describe 'POST /geo/receive_events authentication' do
it 'denies access if token is not present' do
post api('/geo/receive_events')
expect(response.status).to eq 401
end
it 'denies access if not admin' do
post api('/geo/refresh_projects', user)
expect(response.status).to eq 403
it 'denies access if token is invalid' do
post api('/geo/receive_events'), nil, { 'X-Gitlab-Token' => 'nothing' }
expect(response.status).to eq 401
end
end
describe 'POST /geo/receive_events' do
before(:each) do
allow_any_instance_of(::Geo::ScheduleKeyChangeService).to receive(:execute)
allow(Gitlab::Geo).to receive(:current_node) { geo_node }
end
let(:geo_token_header) do
{ 'X-Gitlab-Token' => geo_node.system_hook.token }
end
describe 'POST /geo/receive_events key events' do
before(:each) { allow_any_instance_of(::Geo::ScheduleKeyChangeService).to receive(:execute) }
let(:key_create_payload) do
{
......@@ -61,15 +59,43 @@ describe API::API, api: true do
post api('/geo/receive_events'), key_destroy_payload, geo_token_header
expect(response.status).to eq 201
end
end
it 'denies access if token is not present' do
post api('/geo/receive_events')
expect(response.status).to eq 401
describe 'POST /geo/receive_events push events' do
before(:each) { allow_any_instance_of(::Geo::ScheduleRepoUpdateService).to receive(:execute) }
let(:push_payload) do
{
'event_name' => 'push',
'project_id' => 1,
'project' => {
'git_ssh_url' => 'git@example.com:mike/diaspora.git'
}
}
end
it 'denies access if token is invalid' do
post api('/geo/receive_events'), nil, { 'X-Gitlab-Token' => 'nothing' }
expect(response.status).to eq 401
it 'starts refresh process if admin and correct params' do
post api('/geo/receive_events'), push_payload, geo_token_header
expect(response.status).to eq 201
end
end
describe 'POST /geo/receive_events push_tag events' do
before(:each) { allow_any_instance_of(::Geo::ScheduleWikiRepoUpdateService).to receive(:execute) }
let(:tag_push_payload) do
{
'event_name' => 'tag_push',
'project_id' => 1,
'project' => {
'git_ssh_url' => 'git@example.com:mike/diaspora.git'
}
}
end
it 'starts refresh process if admin and correct params' do
post api('/geo/receive_events'), tag_push_payload, geo_token_header
expect(response.status).to eq 201
end
end
end
describe Geo::EnqueueProjectUpdateService, services: true do
subject { Geo::EnqueueProjectUpdateService.new(project) }
let(:project) { double(:project) }
let(:fake_url) { 'git@localhost:repo/path.git' }
let(:fake_id) { 999 }
let(:queue) { subject.instance_variable_get(:@queue) }
before(:each) do
queue.empty!
expect(project).to receive(:url_to_repo) { fake_url }
expect(project).to receive(:id) { fake_id }
end
describe '#execute' do
let(:stored_data) { queue.first }
before(:each) { subject.execute }
it 'persists id and clone_url to redis queue' do
expect(stored_data).to have_key('id')
expect(stored_data).to have_key('clone_url')
end
it 'persisted id is equal to original' do
expect(stored_data['id']).to eq(fake_id)
end
it 'persisted clone_url is equal to original' do
expect(stored_data['clone_url']).to eq(fake_url)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment