Commit 8f135245 authored by Priyan Sureshbabu's avatar Priyan Sureshbabu Committed by Markus Koller

Expose webhook data for ProjectMember via its own data-builder class

parent e8043280
# frozen_string_literal: true # frozen_string_literal: true
class SystemHooksService class SystemHooksService
BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES = [GroupMember, Group].freeze BUILDER_DRIVEN_EVENT_DATA_AVAILABLE_FOR_CLASSES = [GroupMember, Group, ProjectMember].freeze
def execute_hooks_for(model, event) def execute_hooks_for(model, event)
data = build_event_data(model, event) data = build_event_data(model, event)
...@@ -56,23 +56,14 @@ class SystemHooksService ...@@ -56,23 +56,14 @@ class SystemHooksService
when :failed_login when :failed_login
data[:state] = model.state data[:state] = model.state
end end
when ProjectMember
data.merge!(project_member_data(model))
end end
data data
end end
def build_event_name(model, event) def build_event_name(model, event)
case model
when ProjectMember
return "user_add_to_team" if event == :create
return "user_remove_from_team" if event == :destroy
return "user_update_for_team" if event == :update
else
"#{model.class.name.downcase}_#{event}" "#{model.class.name.downcase}_#{event}"
end end
end
def project_data(model) def project_data(model)
owner = model.owner owner = model.owner
...@@ -88,23 +79,6 @@ class SystemHooksService ...@@ -88,23 +79,6 @@ class SystemHooksService
} }
end end
def project_member_data(model)
project = model.project || Project.unscoped.find(model.source_id)
{
project_name: project.name,
project_path: project.path,
project_path_with_namespace: project.full_path,
project_id: project.id,
user_username: model.user.username,
user_name: model.user.name,
user_email: model.user.email,
user_id: model.user.id,
access_level: model.human_access,
project_visibility: Project.visibility_levels.key(project.visibility_level_value).downcase
}
end
def user_data(model) def user_data(model)
{ {
name: model.name, name: model.name,
...@@ -124,6 +98,8 @@ class SystemHooksService ...@@ -124,6 +98,8 @@ class SystemHooksService
Gitlab::HookData::GroupMemberBuilder Gitlab::HookData::GroupMemberBuilder
when Group when Group
Gitlab::HookData::GroupBuilder Gitlab::HookData::GroupBuilder
when ProjectMember
Gitlab::HookData::ProjectMemberBuilder
end end
builder_class.new(model).build(event) builder_class.new(model).build(event)
......
# frozen_string_literal: true
module Gitlab
module HookData
class ProjectMemberBuilder < BaseBuilder
alias_method :project_member, :object
# Sample data
# {
# :created_at=>"2021-03-02T10:43:17Z",
# :updated_at=>"2021-03-02T10:43:17Z",
# :project_name=>"gitlab",
# :project_path=>"gitlab",
# :project_path_with_namespace=>"namespace1/gitlab",
# :project_id=>1,
# :user_username=>"johndoe",
# :user_name=>"John Doe",
# :user_email=>"john@example.com",
# :user_id=>2,
# :access_level=>"Developer",
# :project_visibility=>"internal",
# :event_name=>"user_update_for_team"
# }
def build(event)
[
timestamps_data,
project_member_data,
event_data(event)
].reduce(:merge)
end
private
def project_member_data
project = project_member.project || Project.unscoped.find(project_member.source_id)
{
project_name: project.name,
project_path: project.path,
project_path_with_namespace: project.full_path,
project_id: project.id,
user_username: project_member.user.username,
user_name: project_member.user.name,
user_email: project_member.user.email,
user_id: project_member.user.id,
access_level: project_member.human_access,
project_visibility: project.visibility
}
end
def event_data(event)
event_name = case event
when :create
'user_add_to_team'
when :destroy
'user_remove_from_team'
when :update
'user_update_for_team'
end
{ event_name: event_name }
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::HookData::ProjectMemberBuilder do
let_it_be(:project) { create(:project, :internal, name: 'gitlab') }
let_it_be(:user) { create(:user, name: 'John Doe', username: 'johndoe', email: 'john@example.com') }
let_it_be(:project_member) { create(:project_member, :developer, user: user, project: project) }
describe '#build' do
let(:data) { described_class.new(project_member).build(event) }
let(:event_name) { data[:event_name] }
let(:attributes) do
[
:event_name, :created_at, :updated_at, :project_name, :project_path, :project_path_with_namespace, :project_id, :user_username, :user_name, :user_email, :user_id, :access_level, :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[:project_name]).to eq('gitlab')
expect(data[:project_path]).to eq(project.path)
expect(data[:project_path_with_namespace]).to eq(project.full_path)
expect(data[:project_id]).to eq(project.id)
expect(data[:user_username]).to eq('johndoe')
expect(data[:user_name]).to eq('John Doe')
expect(data[:user_id]).to eq(user.id)
expect(data[:user_email]).to eq('john@example.com')
expect(data[:access_level]).to eq('Developer')
expect(data[:project_visibility]).to eq('internal')
end
end
context 'on create' do
let(:event) { :create }
it { expect(event_name).to eq('user_add_to_team') }
it_behaves_like 'includes the required attributes'
end
context 'on update' do
let(:event) { :update }
it { expect(event_name).to eq('user_update_for_team') }
it_behaves_like 'includes the required attributes'
end
context 'on destroy' do
let(:event) { :destroy }
it { expect(event_name).to eq('user_remove_from_team') }
it_behaves_like 'includes the required attributes'
end
end
end
end
...@@ -149,9 +149,6 @@ RSpec.describe SystemHooksService do ...@@ -149,9 +149,6 @@ RSpec.describe SystemHooksService do
it { expect(event_name(project, :rename)).to eq "project_rename" } it { expect(event_name(project, :rename)).to eq "project_rename" }
it { expect(event_name(project, :transfer)).to eq "project_transfer" } it { expect(event_name(project, :transfer)).to eq "project_transfer" }
it { expect(event_name(project, :update)).to eq "project_update" } it { expect(event_name(project, :update)).to eq "project_update" }
it { expect(event_name(project_member, :create)).to eq "user_add_to_team" }
it { expect(event_name(project_member, :destroy)).to eq "user_remove_from_team" }
it { expect(event_name(project_member, :update)).to eq "user_update_for_team" }
it { expect(event_name(key, :create)).to eq 'key_create' } it { expect(event_name(key, :create)).to eq 'key_create' }
it { expect(event_name(key, :destroy)).to eq 'key_destroy' } it { expect(event_name(key, :destroy)).to eq 'key_destroy' }
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