Commit e5047bd3 authored by Markus Koller's avatar Markus Koller

Merge branch '290290-project-data-builder-class' into 'master'

Resolve "Expose webhook data for Project via its own data-builder class"

See merge request gitlab-org/gitlab!59614
parents 9bc0602b d477ba3f
# frozen_string_literal: true
class SystemHooksService
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES = [GroupMember, Group, ProjectMember, User].freeze
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES = [GroupMember, Group, ProjectMember, User, Project].freeze
def execute_hooks_for(model, event)
data = build_event_data(model, event)
......@@ -41,12 +41,6 @@ class SystemHooksService
if model.user
data[:username] = model.user.username
end
when Project
data.merge!(project_data(model))
if event == :rename || event == :transfer
data[:old_path_with_namespace] = model.old_path_with_namespace
end
end
data
......@@ -56,20 +50,6 @@ class SystemHooksService
"#{model.class.name.downcase}_#{event}"
end
def project_data(model)
owner = model.owner
{
name: model.name,
path: model.path,
path_with_namespace: model.full_path,
project_id: model.id,
owner_name: owner.name,
owner_email: owner.respond_to?(:email) ? owner.email : "",
project_visibility: model.visibility.downcase
}
end
def builder_driven_event_data_available?(model)
model.class.in?(BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES)
end
......@@ -84,6 +64,8 @@ class SystemHooksService
Gitlab::HookData::ProjectMemberBuilder
when User
Gitlab::HookData::UserBuilder
when Project
Gitlab::HookData::ProjectBuilder
end
builder_class.new(model).build(event)
......
# frozen_string_literal: true
module Gitlab
module HookData
class ProjectBuilder < BaseBuilder
alias_method :project, :object
# Sample data
# {
# event_name: "project_rename",
# created_at: "2021-04-19T07:05:36Z",
# updated_at: "2021-04-19T07:05:36Z",
# name: "my_project",
# path: "my_project",
# path_with_namespace: "namespace2/my_project",
# project_id: 1,
# owner_name: "John",
# owner_email: "user1@example.org",
# project_visibility: "internal",
# old_path_with_namespace: "old-path-with-namespace"
# }
def build(event)
[
event_data(event),
timestamps_data,
project_data,
event_specific_project_data(event)
].reduce(:merge)
end
private
def project_data
owner = project.owner
{
name: project.name,
path: project.path,
path_with_namespace: project.full_path,
project_id: project.id,
owner_name: owner.name,
owner_email: owner.respond_to?(:email) ? owner.email : "",
project_visibility: project.visibility.downcase
}
end
def event_specific_project_data(event)
return {} unless event == :rename || event == :transfer
{
old_path_with_namespace: project.old_path_with_namespace
}
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::HookData::ProjectBuilder do
let_it_be(:user) { create(:user, name: 'John', email: 'john@example.com') }
let_it_be(:namespace) { create(:namespace, owner: user) }
let_it_be(:project) { create(:project, :internal, name: 'my_project', namespace: namespace) }
describe '#build' do
let(:data) { described_class.new(project).build(event) }
let(:event_name) { data[:event_name] }
let(:attributes) do
[
:event_name, :created_at, :updated_at, :name, :path, :path_with_namespace, :project_id,
:owner_name, :owner_email, :project_visibility
]
end
context 'data' do
shared_examples_for 'includes the required attributes' do
it 'includes the required attributes' do
expect(data).to include(*attributes)
expect(data[:created_at]).to eq(project.created_at.xmlschema)
expect(data[:updated_at]).to eq(project.updated_at.xmlschema)
expect(data[:name]).to eq('my_project')
expect(data[:path]).to eq(project.path)
expect(data[:path_with_namespace]).to eq(project.full_path)
expect(data[:project_id]).to eq(project.id)
expect(data[:owner_name]).to eq('John')
expect(data[:owner_email]).to eq('john@example.com')
expect(data[:project_visibility]).to eq('internal')
end
end
shared_examples_for 'does not include `old_path_with_namespace` attribute' do
it 'does not include `old_path_with_namespace` attribute' do
expect(data).not_to include(:old_path_with_namespace)
end
end
shared_examples_for 'includes `old_path_with_namespace` attribute' do
it 'includes `old_path_with_namespace` attribute' do
allow(project).to receive(:old_path_with_namespace).and_return('old-path-with-namespace')
expect(data[:old_path_with_namespace]).to eq('old-path-with-namespace')
end
end
context 'on create' do
let(:event) { :create }
it { expect(event_name).to eq('project_create') }
it_behaves_like 'includes the required attributes'
it_behaves_like 'does not include `old_path_with_namespace` attribute'
end
context 'on destroy' do
let(:event) { :destroy }
it { expect(event_name).to eq('project_destroy') }
it_behaves_like 'includes the required attributes'
it_behaves_like 'does not include `old_path_with_namespace` attribute'
end
context 'on rename' do
let(:event) { :rename }
it { expect(event_name).to eq('project_rename') }
it_behaves_like 'includes the required attributes'
it_behaves_like 'includes `old_path_with_namespace` attribute'
end
context 'on transfer' do
let(:event) { :transfer }
it { expect(event_name).to eq('project_transfer') }
it_behaves_like 'includes the required attributes'
it_behaves_like 'includes `old_path_with_namespace` attribute'
end
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