summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 07:53:40 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-10 07:53:40 +0000
commitcfc792b9ca064990e6540cb742e80529ea669a81 (patch)
tree147cd4256319990cebbc02fe8e4fbbbe06f5720a /app/models
parent93c6764dacd4c605027ef1cd367d3aebe420b223 (diff)
downloadgitlab-ce-cfc792b9ca064990e6540cb742e80529ea669a81.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r--app/models/clusters/applications/prometheus.rb17
-rw-r--r--app/models/clusters/cluster.rb8
-rw-r--r--app/models/concerns/ci/metadatable.rb8
-rw-r--r--app/models/concerns/expirable.rb4
-rw-r--r--app/models/environment.rb15
-rw-r--r--app/models/milestone.rb2
-rw-r--r--app/models/personal_access_token.rb1
-rw-r--r--app/models/project.rb4
-rw-r--r--app/models/project_services/prometheus_service.rb2
-rw-r--r--app/models/user.rb7
10 files changed, 56 insertions, 12 deletions
diff --git a/app/models/clusters/applications/prometheus.rb b/app/models/clusters/applications/prometheus.rb
index 91ffdac3273..2b9285e33d0 100644
--- a/app/models/clusters/applications/prometheus.rb
+++ b/app/models/clusters/applications/prometheus.rb
@@ -13,15 +13,21 @@ module Clusters
include ::Clusters::Concerns::ApplicationStatus
include ::Clusters::Concerns::ApplicationVersion
include ::Clusters::Concerns::ApplicationData
+ include AfterCommitQueue
default_value_for :version, VERSION
- after_destroy :disable_prometheus_integration
+ after_destroy do
+ run_after_commit do
+ disable_prometheus_integration
+ end
+ end
state_machine :status do
after_transition any => [:installed] do |application|
- application.cluster.projects.each do |project|
- project.find_or_initialize_service('prometheus').update!(active: true)
+ application.run_after_commit do
+ Clusters::Applications::ActivateServiceWorker
+ .perform_async(application.cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass
end
end
end
@@ -98,9 +104,8 @@ module Clusters
private
def disable_prometheus_integration
- cluster.projects.each do |project|
- project.prometheus_service&.update!(active: false)
- end
+ ::Clusters::Applications::DeactivateServiceWorker
+ .perform_async(cluster_id, ::PrometheusService.to_param) # rubocop:disable CodeReuse/ServiceClass
end
def kube_client
diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb
index 315cdcffb42..0cb18ba90c0 100644
--- a/app/models/clusters/cluster.rb
+++ b/app/models/clusters/cluster.rb
@@ -34,6 +34,7 @@ module Clusters
has_many :cluster_groups, class_name: 'Clusters::Group'
has_many :groups, through: :cluster_groups, class_name: '::Group'
+ has_many :groups_projects, through: :groups, source: :projects, class_name: '::Project'
# we force autosave to happen when we save `Cluster` model
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
@@ -177,6 +178,13 @@ module Clusters
end
end
+ def all_projects
+ return projects if project_type?
+ return groups_projects if group_type?
+
+ ::Project.all
+ end
+
def status_name
return cleanup_status_name if cleanup_errored?
return :cleanup_ongoing unless cleanup_not_started?
diff --git a/app/models/concerns/ci/metadatable.rb b/app/models/concerns/ci/metadatable.rb
index 17d431bacf2..9bfe76728e4 100644
--- a/app/models/concerns/ci/metadatable.rb
+++ b/app/models/concerns/ci/metadatable.rb
@@ -17,6 +17,7 @@ module Ci
delegate :timeout, to: :metadata, prefix: true, allow_nil: true
delegate :interruptible, to: :metadata, prefix: false, allow_nil: true
delegate :has_exposed_artifacts?, to: :metadata, prefix: false, allow_nil: true
+ delegate :environment_auto_stop_in, to: :metadata, prefix: false, allow_nil: true
before_create :ensure_metadata
end
@@ -47,8 +48,11 @@ module Ci
def options=(value)
write_metadata_attribute(:options, :config_options, value)
- # Store presence of exposed artifacts in build metadata to make it easier to query
- ensure_metadata.has_exposed_artifacts = value&.dig(:artifacts, :expose_as).present?
+ ensure_metadata.tap do |metadata|
+ # Store presence of exposed artifacts in build metadata to make it easier to query
+ metadata.has_exposed_artifacts = value&.dig(:artifacts, :expose_as).present?
+ metadata.environment_auto_stop_in = value&.dig(:environment, :auto_stop_in)
+ end
end
def yaml_variables=(value)
diff --git a/app/models/concerns/expirable.rb b/app/models/concerns/expirable.rb
index 1f274487935..512822089ba 100644
--- a/app/models/concerns/expirable.rb
+++ b/app/models/concerns/expirable.rb
@@ -3,6 +3,8 @@
module Expirable
extend ActiveSupport::Concern
+ DAYS_TO_EXPIRE = 7
+
included do
scope :expired, -> { where('expires_at <= ?', Time.current) }
end
@@ -16,6 +18,6 @@ module Expirable
end
def expires_soon?
- expires? && expires_at < 7.days.from_now
+ expires? && expires_at < DAYS_TO_EXPIRE.days.from_now
end
end
diff --git a/app/models/environment.rb b/app/models/environment.rb
index fec1f034d63..b928dcb21a6 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -162,6 +162,10 @@ class Environment < ApplicationRecord
stop_action&.play(current_user)
end
+ def reset_auto_stop
+ update_column(:auto_stop_at, nil)
+ end
+
def actions_for(environment)
return [] unless manual_actions
@@ -261,6 +265,17 @@ class Environment < ApplicationRecord
end
end
+ def auto_stop_in
+ auto_stop_at - Time.now if auto_stop_at
+ end
+
+ def auto_stop_in=(value)
+ return unless value
+ return unless parsed_result = ChronicDuration.parse(value)
+
+ self.auto_stop_at = parsed_result.seconds.from_now
+ end
+
private
def generate_slug
diff --git a/app/models/milestone.rb b/app/models/milestone.rb
index d29eb62af7a..987373aaf1b 100644
--- a/app/models/milestone.rb
+++ b/app/models/milestone.rb
@@ -341,6 +341,6 @@ class Milestone < ApplicationRecord
end
def issues_finder_params
- { project_id: project_id, group_id: group_id }.compact
+ { project_id: project_id, group_id: group_id, include_subgroups: group_id.present? }.compact
end
end
diff --git a/app/models/personal_access_token.rb b/app/models/personal_access_token.rb
index 770fc2b5c8f..9ccc90fb74d 100644
--- a/app/models/personal_access_token.rb
+++ b/app/models/personal_access_token.rb
@@ -16,6 +16,7 @@ class PersonalAccessToken < ApplicationRecord
before_save :ensure_token
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
+ scope :expiring_and_not_notified, ->(date) { where(["revoked = false AND expire_notification_delivered = false AND expires_at >= NOW() AND expires_at <= ?", date]) }
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
scope :with_impersonation, -> { where(impersonation: true) }
scope :without_impersonation, -> { where(impersonation: false) }
diff --git a/app/models/project.rb b/app/models/project.rb
index 6ee300dc7f5..88b66423e59 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -404,6 +404,7 @@ class Project < ApplicationRecord
scope :with_push, -> { joins(:events).where('events.action = ?', Event::PUSHED) }
scope :with_project_feature, -> { joins('LEFT JOIN project_features ON projects.id = project_features.project_id') }
scope :with_statistics, -> { includes(:statistics) }
+ scope :with_service, ->(service) { joins(service).eager_load(service) }
scope :with_shared_runners, -> { where(shared_runners_enabled: true) }
scope :with_container_registry, -> { where(container_registry_enabled: true) }
scope :inside_path, ->(path) do
@@ -1256,8 +1257,9 @@ class Project < ApplicationRecord
def all_clusters
group_clusters = Clusters::Cluster.joins(:groups).where(cluster_groups: { group_id: ancestors_upto } )
+ instance_clusters = Clusters::Cluster.instance_type
- Clusters::Cluster.from_union([clusters, group_clusters])
+ Clusters::Cluster.from_union([clusters, group_clusters, instance_clusters])
end
def items_for(entity)
diff --git a/app/models/project_services/prometheus_service.rb b/app/models/project_services/prometheus_service.rb
index 2ca74b081aa..3d5967de41e 100644
--- a/app/models/project_services/prometheus_service.rb
+++ b/app/models/project_services/prometheus_service.rb
@@ -88,7 +88,7 @@ class PrometheusService < MonitoringService
return false if template?
return false unless project
- project.clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
+ project.all_clusters.enabled.any? { |cluster| cluster.application_prometheus_available? }
end
def allow_local_api_url?
diff --git a/app/models/user.rb b/app/models/user.rb
index d0e758b0055..fd02db86582 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -310,6 +310,13 @@ class User < ApplicationRecord
scope :with_dashboard, -> (dashboard) { where(dashboard: dashboard) }
scope :with_public_profile, -> { where(private_profile: false) }
+ scope :with_expiring_and_not_notified_personal_access_tokens, ->(at) do
+ where('EXISTS (?)',
+ ::PersonalAccessToken
+ .where('personal_access_tokens.user_id = users.id')
+ .expiring_and_not_notified(at).select(1))
+ end
+
def self.with_visible_profile(user)
return with_public_profile if user.nil?