diff options
author | Mayra Cabrera <mcabrera@gitlab.com> | 2018-09-06 19:20:42 +0000 |
---|---|---|
committer | Kamil TrzciĆski <ayufan@ayufan.eu> | 2018-09-06 19:20:42 +0000 |
commit | 177d847cf5c0ad9f282f599fbd5e9dafdc3b6289 (patch) | |
tree | cf94abe3e3ae6b79582d9aa87e1d595b3f0dbda2 /app/services | |
parent | a286e20d014e4092be7f03a2e1679f8f8434afa2 (diff) | |
download | gitlab-ce-177d847cf5c0ad9f282f599fbd5e9dafdc3b6289.tar.gz |
Automatically disable Auto DevOps for project upon first pipeline failure
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/notification_service.rb | 6 | ||||
-rw-r--r-- | app/services/projects/auto_devops/disable_service.rb | 39 |
2 files changed, 45 insertions, 0 deletions
diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 4511c500fca..50fa373025b 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -407,6 +407,12 @@ class NotificationService end end + def autodevops_disabled(pipeline, recipients) + recipients.each do |recipient| + mailer.autodevops_disabled_email(pipeline, recipient).deliver_later + end + end + def pages_domain_verification_succeeded(domain) recipients_for_pages_domain(domain).each do |user| mailer.pages_domain_verification_succeeded_email(domain, user).deliver_later diff --git a/app/services/projects/auto_devops/disable_service.rb b/app/services/projects/auto_devops/disable_service.rb new file mode 100644 index 00000000000..9745ab67dbd --- /dev/null +++ b/app/services/projects/auto_devops/disable_service.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Projects + module AutoDevops + class DisableService < BaseService + def execute + return false unless implicitly_enabled_and_first_pipeline_failure? + + disable_auto_devops + end + + private + + def implicitly_enabled_and_first_pipeline_failure? + project.has_auto_devops_implicitly_enabled? && + first_pipeline_failure? + end + + # We're using `limit` to optimize `auto_devops pipeline` query, + # since we only care about the first element, and using only `.count` + # is an expensive operation. See + # https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21172#note_99037378 + # for more context. + def first_pipeline_failure? + auto_devops_pipelines.success.limit(1).count.zero? && + auto_devops_pipelines.failed.limit(1).count.nonzero? + end + + def disable_auto_devops + project.auto_devops_attributes = { enabled: false } + project.save! + end + + def auto_devops_pipelines + @auto_devops_pipelines ||= project.pipelines.auto_devops_source + end + end + end +end |