From 9300ad3f99bd22a64aaacfaf3aaf95a314a5d270 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Wed, 11 Nov 2015 16:31:19 +0100 Subject: [PATCH] Implement Project mirror helper methods. --- app/models/project.rb | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index e7ca5a940a..eb3188b9b8 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -156,6 +156,7 @@ class Project < ActiveRecord::Base validates :import_url, format: { with: /\A#{URI.regexp(%w(ssh git http https))}\z/, message: 'should be a valid url' }, if: :external_import? + validates :import_url, presence: true, if: :mirror? validates :star_count, numericality: { greater_than_or_equal_to: 0 } validate :check_limit, on: :create validate :avatar_type, @@ -180,6 +181,7 @@ class Project < ActiveRecord::Base scope :public_only, -> { where(visibility_level: Project::PUBLIC) } scope :public_and_internal_only, -> { where(visibility_level: Project.public_and_internal_levels) } scope :non_archived, -> { where(archived: false) } + scope :mirror, -> { where(mirror: true) } state_machine :import_status, initial: :none do event :import_start do @@ -204,6 +206,21 @@ class Project < ActiveRecord::Base after_transition any => :started, do: :schedule_add_import_job after_transition any => :finished, do: :clear_import_data + + after_transition started: :finished do |project, transaction| + if project.mirror? + timestamp = DateTime.now + project.mirror_last_update_at = timestamp + project.mirror_last_successful_update_at = timestamp + project.save + end + end + + after_transition started: :failed do |project, transaction| + if project.mirror? + project.update(mirror_last_update_at: DateTime.now) + end + end end class << self @@ -254,7 +271,7 @@ class Project < ActiveRecord::Base joins(:namespace). iwhere('namespaces.path' => namespace_path) - projects.where('projects.path' => project_path).take || + projects.where('projects.path' => project_path).take || projects.iwhere('projects.path' => project_path).take end @@ -358,6 +375,48 @@ class Project < ActiveRecord::Base original_url end + def mirror_updated? + mirror? && self.mirror_last_update_at + end + + def updating_mirror? + mirror? && import_in_progress? && !empty_repo? + end + + def mirror_last_update_status + return unless mirror_updated? + + if self.mirror_last_update_at == self.mirror_last_successful_update_at + :success + else + :failed + end + end + + def mirror_last_update_success? + mirror_last_update_status == :success + end + + def mirror_last_update_failed? + mirror_last_update_status == :failed + end + + def mirror_ever_updated_successfully? + mirror_updated? && self.mirror_last_successful_update_at + end + + def update_mirror + return unless mirror? + + return if import_in_progress? + + if import_failed? + import_retry + else + import_start + end + end + def check_limit unless creator.can_create_project? or namespace.kind == 'group' errors[:limit_reached] << ("Your project limit is #{creator.projects_limit} projects! Please contact your administrator to increase it")