Commit d46bea3f authored by charlie ablett's avatar charlie ablett

Add owners array to webhook data

- update docs
- update tests

Changelog: added
parent b30f4de3
...@@ -25,5 +25,9 @@ module Namespaces ...@@ -25,5 +25,9 @@ module Namespaces
def self.sti_name def self.sti_name
'User' 'User'
end end
def owners
Array.wrap(owner)
end
end end
end end
...@@ -1520,8 +1520,17 @@ class Project < ApplicationRecord ...@@ -1520,8 +1520,17 @@ class Project < ApplicationRecord
group || namespace.try(:owner) group || namespace.try(:owner)
end end
def deprecated_owner
# Kept in order to maintain webhook structures until we remove owner_name and owner_email
# See https://gitlab.com/gitlab-org/gitlab/-/issues/350603
group || namespace.try(:owner)
end
def owners def owners
Array.wrap(owner) # This will be phased out and replaced with `owners` relationship
# backed by memberships with direct/inherited Owner access roles
# See https://gitlab.com/groups/gitlab-org/-/epics/7405
team.owners
end end
def first_owner def first_owner
......
...@@ -79,8 +79,12 @@ X-Gitlab-Event: System Hook ...@@ -79,8 +79,12 @@ X-Gitlab-Event: System Hook
"updated_at": "2012-07-21T07:38:22Z", "updated_at": "2012-07-21T07:38:22Z",
"event_name": "project_create", "event_name": "project_create",
"name": "StoreCloud", "name": "StoreCloud",
"owner_email": "johnsmith@gmail.com", "owner_email": "johnsmith@example.com",
"owner_name": "John Smith", "owner_name": "John Smith",
"owners": [{
"name": "John",
"email": "user1@example.com"
}],
"path": "storecloud", "path": "storecloud",
"path_with_namespace": "jsmith/storecloud", "path_with_namespace": "jsmith/storecloud",
"project_id": 74, "project_id": 74,
...@@ -96,8 +100,12 @@ X-Gitlab-Event: System Hook ...@@ -96,8 +100,12 @@ X-Gitlab-Event: System Hook
"updated_at": "2012-07-21T07:38:22Z", "updated_at": "2012-07-21T07:38:22Z",
"event_name": "project_destroy", "event_name": "project_destroy",
"name": "Underscore", "name": "Underscore",
"owner_email": "johnsmith@gmail.com", "owner_email": "johnsmith@example.com",
"owner_name": "John Smith", "owner_name": "John Smith",
"owners": [{
"name": "John",
"email": "user1@example.com"
}],
"path": "underscore", "path": "underscore",
"path_with_namespace": "jsmith/underscore", "path_with_namespace": "jsmith/underscore",
"project_id": 73, "project_id": 73,
...@@ -117,7 +125,11 @@ X-Gitlab-Event: System Hook ...@@ -117,7 +125,11 @@ X-Gitlab-Event: System Hook
"path_with_namespace": "jsmith/underscore", "path_with_namespace": "jsmith/underscore",
"project_id": 73, "project_id": 73,
"owner_name": "John Smith", "owner_name": "John Smith",
"owner_email": "johnsmith@gmail.com", "owner_email": "johnsmith@example.com",
"owners": [{
"name": "John",
"email": "user1@example.com"
}],
"project_visibility": "internal", "project_visibility": "internal",
"old_path_with_namespace": "jsmith/overscore" "old_path_with_namespace": "jsmith/overscore"
} }
...@@ -138,7 +150,11 @@ Please refer to `group_rename` and `user_rename` for that case. ...@@ -138,7 +150,11 @@ Please refer to `group_rename` and `user_rename` for that case.
"path_with_namespace": "scores/underscore", "path_with_namespace": "scores/underscore",
"project_id": 73, "project_id": 73,
"owner_name": "John Smith", "owner_name": "John Smith",
"owner_email": "johnsmith@gmail.com", "owner_email": "johnsmith@example.com",
"owners": [{
"name": "John",
"email": "user1@example.com"
}],
"project_visibility": "internal", "project_visibility": "internal",
"old_path_with_namespace": "jsmith/overscore" "old_path_with_namespace": "jsmith/overscore"
} }
...@@ -152,8 +168,12 @@ Please refer to `group_rename` and `user_rename` for that case. ...@@ -152,8 +168,12 @@ Please refer to `group_rename` and `user_rename` for that case.
"updated_at": "2012-07-21T07:38:22Z", "updated_at": "2012-07-21T07:38:22Z",
"event_name": "project_update", "event_name": "project_update",
"name": "StoreCloud", "name": "StoreCloud",
"owner_email": "johnsmith@gmail.com", "owner_email": "johnsmith@example.com",
"owner_name": "John Smith", "owner_name": "John Smith",
"owners": [{
"name": "John",
"email": "user1@example.com"
}],
"path": "storecloud", "path": "storecloud",
"path_with_namespace": "jsmith/storecloud", "path_with_namespace": "jsmith/storecloud",
"project_id": 74, "project_id": 74,
...@@ -173,7 +193,7 @@ Please refer to `group_rename` and `user_rename` for that case. ...@@ -173,7 +193,7 @@ Please refer to `group_rename` and `user_rename` for that case.
"project_name": "StoreCloud", "project_name": "StoreCloud",
"project_path": "storecloud", "project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud", "project_path_with_namespace": "jsmith/storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41, "user_id": 41,
...@@ -193,7 +213,7 @@ Please refer to `group_rename` and `user_rename` for that case. ...@@ -193,7 +213,7 @@ Please refer to `group_rename` and `user_rename` for that case.
"project_name": "StoreCloud", "project_name": "StoreCloud",
"project_path": "storecloud", "project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud", "project_path_with_namespace": "jsmith/storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41, "user_id": 41,
...@@ -213,7 +233,7 @@ Please refer to `group_rename` and `user_rename` for that case. ...@@ -213,7 +233,7 @@ Please refer to `group_rename` and `user_rename` for that case.
"project_name": "StoreCloud", "project_name": "StoreCloud",
"project_path": "storecloud", "project_path": "storecloud",
"project_path_with_namespace": "jsmith/storecloud", "project_path_with_namespace": "jsmith/storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41, "user_id": 41,
...@@ -360,7 +380,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`. ...@@ -360,7 +380,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`.
"group_id": 78, "group_id": 78,
"group_name": "StoreCloud", "group_name": "StoreCloud",
"group_path": "storecloud", "group_path": "storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41 "user_id": 41
...@@ -378,7 +398,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`. ...@@ -378,7 +398,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`.
"group_id": 78, "group_id": 78,
"group_name": "StoreCloud", "group_name": "StoreCloud",
"group_path": "storecloud", "group_path": "storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41 "user_id": 41
...@@ -396,7 +416,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`. ...@@ -396,7 +416,7 @@ If the user is blocked via LDAP, `state` is `ldap_blocked`.
"group_id": 78, "group_id": 78,
"group_name": "StoreCloud", "group_name": "StoreCloud",
"group_path": "storecloud", "group_path": "storecloud",
"user_email": "johnsmith@gmail.com", "user_email": "johnsmith@example.com",
"user_name": "John Smith", "user_name": "John Smith",
"user_username": "johnsmith", "user_username": "johnsmith",
"user_id": 41 "user_id": 41
......
...@@ -16,6 +16,7 @@ module Gitlab ...@@ -16,6 +16,7 @@ module Gitlab
# project_id: 1, # project_id: 1,
# owner_name: "John", # owner_name: "John",
# owner_email: "user1@example.org", # owner_email: "user1@example.org",
# owners: [name: "John", email: "user1@example.org"],
# project_visibility: "internal", # project_visibility: "internal",
# old_path_with_namespace: "old-path-with-namespace" # old_path_with_namespace: "old-path-with-namespace"
# } # }
...@@ -32,19 +33,33 @@ module Gitlab ...@@ -32,19 +33,33 @@ module Gitlab
private private
def project_data def project_data
owner = project.owner owners = project.owners.compact
# When this is removed, also remove the `deprecated_owner` method
# See https://gitlab.com/gitlab-org/gitlab/-/issues/350603
owner = project.deprecated_owner
{ {
name: project.name, name: project.name,
path: project.path, path: project.path,
path_with_namespace: project.full_path, path_with_namespace: project.full_path,
project_id: project.id, project_id: project.id,
owner_name: owner.name, owner_name: owner.try(:name),
owner_email: owner.respond_to?(:email) ? owner.email : "", owner_email: user_email(owner),
owners: owners.map do |owner|
owner_data(owner)
end,
project_visibility: project.visibility.downcase project_visibility: project.visibility.downcase
} }
end end
def owner_data(user)
{ name: user.name, email: user_email(user) }
end
def user_email(user)
user.respond_to?(:email) ? user.email : ""
end
def event_specific_project_data(event) def event_specific_project_data(event)
return {} unless event == :rename || event == :transfer return {} unless event == :rename || event == :transfer
......
...@@ -13,7 +13,7 @@ RSpec.describe Gitlab::HookData::ProjectBuilder do ...@@ -13,7 +13,7 @@ RSpec.describe Gitlab::HookData::ProjectBuilder do
let(:attributes) do let(:attributes) do
[ [
:event_name, :created_at, :updated_at, :name, :path, :path_with_namespace, :project_id, :event_name, :created_at, :updated_at, :name, :path, :path_with_namespace, :project_id,
:owner_name, :owner_email, :project_visibility :owners, :owner_name, :owner_email, :project_visibility
] ]
end end
...@@ -30,6 +30,7 @@ RSpec.describe Gitlab::HookData::ProjectBuilder do ...@@ -30,6 +30,7 @@ RSpec.describe Gitlab::HookData::ProjectBuilder do
expect(data[:project_id]).to eq(project.id) expect(data[:project_id]).to eq(project.id)
expect(data[:owner_name]).to eq('John') expect(data[:owner_name]).to eq('John')
expect(data[:owner_email]).to eq('john@example.com') expect(data[:owner_email]).to eq('john@example.com')
expect(data[:owners]).to contain_exactly({ name: 'John', email: 'john@example.com' })
expect(data[:project_visibility]).to eq('internal') expect(data[:project_visibility]).to eq('internal')
end end
end end
......
...@@ -9,4 +9,13 @@ RSpec.describe Namespaces::UserNamespace, type: :model do ...@@ -9,4 +9,13 @@ RSpec.describe Namespaces::UserNamespace, type: :model do
describe 'validations' do describe 'validations' do
it { is_expected.to validate_presence_of(:owner) } it { is_expected.to validate_presence_of(:owner) }
end end
describe '#owners' do
let(:owner) { build(:user) }
let(:namespace) { build(:namespace, owner: owner) }
specify do
expect(namespace.owners).to match_array([owner])
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