Commit b259d408 authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'fix-slack-integration' into 'master'

Add check for ref to deployment notifications

See merge request gitlab-org/gitlab!74423
parents 27c8c592 753a9a87
......@@ -204,7 +204,7 @@ module Integrations
when "wiki_page"
Integrations::ChatMessage::WikiPageMessage.new(data)
when "deployment"
Integrations::ChatMessage::DeploymentMessage.new(data)
Integrations::ChatMessage::DeploymentMessage.new(data) if notify_for_ref?(data)
end
end
......@@ -241,7 +241,11 @@ module Integrations
def notify_for_ref?(data)
return true if data[:object_kind] == 'tag_push'
return true if data.dig(:object_attributes, :tag)
return true if data[:object_kind] == 'deployment' && !Feature.enabled?(:chat_notification_deployment_protected_branch_filter, project)
ref = data[:ref] || data.dig(:object_attributes, :ref)
return true if ref.blank? # No need to check protected branches when there is no ref
return true if Gitlab::Git.tag_ref?(ref) # Skip protected branch check because it doesn't support tags
notify_for_branch?(data)
end
......
---
name: chat_notification_deployment_protected_branch_filter
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/74423
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/349131
milestone: '14.7'
type: development
group: group::integrations
default_enabled: false
......@@ -25,7 +25,8 @@ module Gitlab
user: deployment.deployed_by.hook_attrs,
user_url: Gitlab::UrlBuilder.build(deployment.deployed_by),
commit_url: Gitlab::UrlBuilder.build(deployment.commit),
commit_title: deployment.commit.title
commit_title: deployment.commit.title,
ref: deployment.ref
}
end
end
......
......@@ -37,6 +37,7 @@ RSpec.describe Gitlab::DataBuilder::Deployment do
expect(data[:user_url]).to eq(expected_user_url)
expect(data[:commit_url]).to eq(expected_commit_url)
expect(data[:commit_title]).to eq(commit.title)
expect(data[:ref]).to eq(deployment.ref)
end
it 'does not include the deployable URL when there is no deployable' do
......
......@@ -645,4 +645,63 @@ RSpec.shared_examples Integrations::SlackMattermostNotifier do |service_name|
end
end
end
describe 'Deployment events' do
let_it_be(:user) { create(:user) }
let_it_be_with_reload(:project) { create(:project, :repository, creator: user) }
let(:deployment) do
create(:deployment, :success, project: project, sha: project.commit.sha, ref: project.default_branch)
end
let(:data) { Gitlab::DataBuilder::Deployment.build(deployment, Time.now) }
before do
allow(chat_service).to receive_messages(
project: project,
service_hook: true,
webhook: webhook_url
)
stub_full_request(webhook_url, method: :post)
end
it_behaves_like "triggered #{service_name} service", event_type: "deployment"
context 'on a protected branch' do
before do
create(:protected_branch, :create_branch_on_repository, project: project, name: 'a-protected-branch')
end
let(:deployment) do
create(:deployment, :success, project: project, sha: project.commit.sha, ref: 'a-protected-branch')
end
context 'notification enabled only for default branch' do
it_behaves_like "untriggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default"
end
context 'notification enabled only for protected branches' do
it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "protected"
end
context 'notification enabled only for default and protected branches' do
it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default_and_protected"
end
context 'notification enabled for all branches' do
it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "all"
end
context 'when chat_notification_deployment_protected_branch_filter is disabled' do
before do
stub_feature_flags(chat_notification_deployment_protected_branch_filter: false)
end
context 'notification enabled only for default branch' do
it_behaves_like "triggered #{service_name} service", event_type: "pipeline", branches_to_be_notified: "default"
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