Commit 8f2d0620 authored by Nick Thomas's avatar Nick Thomas

Merge branch '323627-add-members-oncall-schedule-data-to-helper' into 'master'

Add OncallScheduleEntity with data to members

See merge request gitlab-org/gitlab!58848
parents d2526c10 511aef55
......@@ -8,7 +8,9 @@ module EE
def preload_all
super
ActiveRecord::Associations::Preloader.new.preload(members.map(&:user), group_saml_identities: :saml_provider)
users = members.map(&:user)
ActiveRecord::Associations::Preloader.new.preload(users, group_saml_identities: :saml_provider)
ActiveRecord::Associations::Preloader.new.preload(users, oncall_participants: { rotation: :schedule })
end
end
end
......@@ -73,6 +73,10 @@ module EE
has_many :user_permission_export_uploads
has_many :oncall_participants, class_name: 'IncidentManagement::OncallParticipant', inverse_of: :user
has_many :oncall_rotations, class_name: 'IncidentManagement::OncallRotation', through: :oncall_participants, source: :rotation
has_many :oncall_schedules, class_name: 'IncidentManagement::OncallSchedule', through: :oncall_rotations, source: :schedule
scope :not_managed, ->(group: nil) {
scope = where(managing_group_id: nil)
scope = scope.or(where.not(managing_group_id: group.id)) if group
......
......@@ -22,6 +22,8 @@ module IncidentManagement
scope :for_iid, -> (iid) { where(iid: iid) }
delegate :name, to: :project, prefix: true
private
def timezones
......
......@@ -7,6 +7,11 @@ module EE
prepended do
unexpose :gitlab_employee
unexpose :email
expose :oncall_schedules, with: ::IncidentManagement::OncallScheduleEntity
def oncall_schedules
object.oncall_schedules.uniq
end
end
end
end
# frozen_string_literal: true
module IncidentManagement
class OncallScheduleEntity < Grape::Entity
include Gitlab::Routing
expose :name
expose :project_name
expose :schedule_url do |schedule|
project_incident_management_oncall_schedules_url(schedule.project)
end
expose :project_url do |schedule|
project_url(schedule.project)
end
end
end
---
title: Add oncall schedule data to member user API
merge_request: 58848
author:
type: added
{
"type": "object",
"allOf": [
{ "$ref": "../../../../../../spec/fixtures/api/schemas/entities/member_user.json" },
{
"required" : [
"oncall_schedules"
],
"properties" : {
"oncall_schedules": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"project_name": { "type": "string" }
}
}
}
}
}
]
}
......@@ -28,6 +28,9 @@ RSpec.describe User do
it { is_expected.to have_many(:board_preferences) }
it { is_expected.to have_many(:boards_epic_user_preferences).class_name('Boards::EpicUserPreference') }
it { is_expected.to have_many(:user_permission_export_uploads) }
it { is_expected.to have_many(:oncall_participants).class_name('IncidentManagement::OncallParticipant') }
it { is_expected.to have_many(:oncall_rotations).class_name('IncidentManagement::OncallRotation').through(:oncall_participants) }
it { is_expected.to have_many(:oncall_schedules).class_name('IncidentManagement::OncallSchedule').through(:oncall_rotations) }
end
describe 'nested attributes' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IncidentManagement::OncallScheduleEntity do
let(:schedule) { create(:incident_management_oncall_schedule) }
let(:schedule_url) { Gitlab::Routing.url_helpers.project_incident_management_oncall_schedules_url(schedule.project) }
let(:project_url) { Gitlab::Routing.url_helpers.project_url(schedule.project) }
subject { described_class.new(schedule) }
describe '.as_json' do
it 'includes oncall schdule attributes' do
attributes = subject.as_json
expect(attributes[:name]).to eq(schedule.name)
expect(attributes[:project_name]).to eq(schedule.project.name)
expect(attributes[:schedule_url]).to eq(schedule_url)
expect(attributes[:project_url]).to eq(project_url)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MemberUserEntity do
let_it_be_with_reload(:user) { create(:user) }
let(:entity) { described_class.new(user) }
let(:entity_hash) { entity.as_json }
it 'matches json schema' do
expect(entity.to_json).to match_schema('entities/member_user')
end
context 'with oncall schedules' do
let_it_be(:oncall_schedule) { create(:incident_management_oncall_participant, user: user).rotation.schedule }
it 'correctly exposes `oncall_schedules`' do
expect(entity_hash[:oncall_schedules]).to include(schedule_hash(oncall_schedule))
end
it 'exposed and de-dupes the schedules' do
allow(user).to receive(:oncall_schedules).and_return([oncall_schedule, oncall_schedule])
expect(entity_hash[:oncall_schedules].size).to eq(1)
expect(entity_hash[:oncall_schedules]).to include(schedule_hash(oncall_schedule))
end
def schedule_hash(schedule)
schedule_url = Gitlab::Routing.url_helpers.project_incident_management_oncall_schedules_url(schedule.project)
project_url = Gitlab::Routing.url_helpers.project_url(schedule.project)
{
name: oncall_schedule.name,
project_name: oncall_schedule.project.name,
schedule_url: schedule_url,
project_url: project_url
}
end
end
end
......@@ -18,6 +18,5 @@
},
"additionalProperties": false
}
},
"additionalProperties": false
}
}
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