Commit 6061c9fa authored by Lin Jen-Shin's avatar Lin Jen-Shin

Send only to users have :read_build access, feedback:

https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/6342#note_17193335
parent 1cdad622
...@@ -5,7 +5,7 @@ module Ci ...@@ -5,7 +5,7 @@ module Ci
# If we can't read build we should also not have that # If we can't read build we should also not have that
# ability when looking at this in context of commit_status # ability when looking at this in context of commit_status
%w(read create update admin).each do |rule| %w[read create update admin].each do |rule|
cannot! :"#{rule}_commit_status" unless can? :"#{rule}_build" cannot! :"#{rule}_commit_status" unless can? :"#{rule}_build"
end end
end end
......
module Ci
class PipelinePolicy < BuildPolicy
end
end
...@@ -489,9 +489,14 @@ class NotificationService ...@@ -489,9 +489,14 @@ class NotificationService
end end
def reject_users_without_access(recipients, target) def reject_users_without_access(recipients, target)
return recipients unless target.is_a?(Issuable) ability = case target
when Issuable
ability = :"read_#{target.to_ability_name}" :"read_#{target.to_ability_name}"
when Ci::Pipeline
:read_build # We have build trace in pipeline emails
end
return recipients unless ability
recipients.select do |user| recipients.select do |user|
user.can?(ability, target) user.can?(ability, target)
......
...@@ -17,84 +17,114 @@ describe PipelineNotificationWorker do ...@@ -17,84 +17,114 @@ describe PipelineNotificationWorker do
describe '#execute' do describe '#execute' do
before do before do
reset_delivered_emails! reset_delivered_emails!
pipeline.project.team << [watcher, Gitlab::Access::DEVELOPER] pipeline.project.team << [pusher, Gitlab::Access::DEVELOPER]
end end
shared_examples 'sending emails' do context 'when watcher has developer access' do
it 'sends emails' do before do
perform_enqueued_jobs do pipeline.project.team << [watcher, Gitlab::Access::DEVELOPER]
subject.perform(pipeline.id) end
end
emails = ActionMailer::Base.deliveries shared_examples 'sending emails' do
actual = emails.flat_map(&:bcc).sort it 'sends emails' do
expected_receivers = [pusher, watcher].map(&:email).uniq.sort perform_enqueued_jobs do
subject.perform(pipeline.id)
end
expect(actual).to eq(expected_receivers) emails = ActionMailer::Base.deliveries
expect(emails.size).to eq(1) actual = emails.flat_map(&:bcc).sort
expect(emails.last.subject).to include(email_subject) expected_receivers = receivers.map(&:email).uniq.sort
end
end
context 'with success pipeline' do expect(actual).to eq(expected_receivers)
let(:status) { 'success' } expect(emails.size).to eq(1)
let(:email_subject) { "Pipeline ##{pipeline.id} has succeeded" } expect(emails.last.subject).to include(email_subject)
end
end
it_behaves_like 'sending emails' context 'with success pipeline' do
let(:status) { 'success' }
let(:email_subject) { "Pipeline ##{pipeline.id} has succeeded" }
let(:receivers) { [pusher, watcher] }
context 'with pipeline from someone else' do it_behaves_like 'sending emails'
let(:pusher) { create(:user) }
context 'with success pipeline notification on' do context 'with pipeline from someone else' do
let(:pusher) { create(:user) }
let(:watcher) { user } let(:watcher) { user }
before do context 'with success pipeline notification on' do
watcher.global_notification_setting. before do
update(level: 'custom', success_pipeline: true) watcher.global_notification_setting.
update(level: 'custom', success_pipeline: true)
end
it_behaves_like 'sending emails'
end end
it_behaves_like 'sending emails' context 'with success pipeline notification off' do
end let(:receivers) { [pusher] }
before do
watcher.global_notification_setting.
update(level: 'custom', success_pipeline: false)
end
context 'with success pipeline notification off' do it_behaves_like 'sending emails'
before do
watcher.global_notification_setting.
update(level: 'custom', success_pipeline: false)
end end
end
context 'with failed pipeline' do
let(:status) { 'failed' }
let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
it_behaves_like 'sending emails' it_behaves_like 'sending emails'
context 'with pipeline from someone else' do
let(:pusher) { create(:user) }
let(:watcher) { user }
context 'with failed pipeline notification on' do
before do
watcher.global_notification_setting.
update(level: 'custom', failed_pipeline: true)
end
it_behaves_like 'sending emails'
end
context 'with failed pipeline notification off' do
let(:receivers) { [pusher] }
before do
watcher.global_notification_setting.
update(level: 'custom', failed_pipeline: false)
end
it_behaves_like 'sending emails'
end
end
end end
end end
end end
context 'with failed pipeline' do context 'when watcher has no read_build access' do
let(:status) { 'failed' } let(:status) { 'failed' }
let(:email_subject) { "Pipeline ##{pipeline.id} has failed" } let(:email_subject) { "Pipeline ##{pipeline.id} has failed" }
let(:watcher) { create(:user) }
it_behaves_like 'sending emails' before do
pipeline.project.team << [watcher, Gitlab::Access::GUEST]
context 'with pipeline from someone else' do watcher.global_notification_setting.
let(:pusher) { create(:user) } update(level: 'custom', failed_pipeline: true)
context 'with failed pipeline notification on' do
let(:watcher) { user }
before do
watcher.global_notification_setting.
update(level: 'custom', failed_pipeline: true)
end
it_behaves_like 'sending emails' perform_enqueued_jobs do
subject.perform(pipeline.id)
end end
end
context 'with failed pipeline notification off' do it 'does not send emails' do
before do should_only_email(pusher, kind: :bcc)
watcher.global_notification_setting.
update(level: 'custom', failed_pipeline: false)
end
it_behaves_like 'sending emails'
end
end end
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