Commit 1436433c7b982fbe17b10f744f2f65f208305d3c
1 parent
7f50c7f6
Exists in
spb-stable
and in
3 other branches
Add project import state machine
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
22 additions
and
12 deletions
Show diff stats
app/models/project.rb
| ... | ... | @@ -59,13 +59,10 @@ class Project < ActiveRecord::Base |
| 59 | 59 | has_one :gemnasium_service, dependent: :destroy |
| 60 | 60 | has_one :forked_project_link, dependent: :destroy, foreign_key: "forked_to_project_id" |
| 61 | 61 | has_one :forked_from_project, through: :forked_project_link |
| 62 | - | |
| 63 | 62 | # Merge Requests for target project should be removed with it |
| 64 | 63 | has_many :merge_requests, dependent: :destroy, foreign_key: "target_project_id" |
| 65 | - | |
| 66 | 64 | # Merge requests from source project should be kept when source project was removed |
| 67 | 65 | has_many :fork_merge_requests, foreign_key: "source_project_id", class_name: MergeRequest |
| 68 | - | |
| 69 | 66 | has_many :issues, -> { order "state DESC, created_at DESC" }, dependent: :destroy |
| 70 | 67 | has_many :services, dependent: :destroy |
| 71 | 68 | has_many :events, dependent: :destroy |
| ... | ... | @@ -74,10 +71,8 @@ class Project < ActiveRecord::Base |
| 74 | 71 | has_many :snippets, dependent: :destroy, class_name: "ProjectSnippet" |
| 75 | 72 | has_many :hooks, dependent: :destroy, class_name: "ProjectHook" |
| 76 | 73 | has_many :protected_branches, dependent: :destroy |
| 77 | - | |
| 78 | 74 | has_many :users_projects, dependent: :destroy |
| 79 | 75 | has_many :users, through: :users_projects |
| 80 | - | |
| 81 | 76 | has_many :deploy_keys_projects, dependent: :destroy |
| 82 | 77 | has_many :deploy_keys, through: :deploy_keys_projects |
| 83 | 78 | |
| ... | ... | @@ -97,15 +92,12 @@ class Project < ActiveRecord::Base |
| 97 | 92 | validates :issues_enabled, :wall_enabled, :merge_requests_enabled, |
| 98 | 93 | :wiki_enabled, inclusion: { in: [true, false] } |
| 99 | 94 | validates :issues_tracker_id, length: { maximum: 255 }, allow_blank: true |
| 100 | - | |
| 101 | 95 | validates :namespace, presence: true |
| 102 | 96 | validates_uniqueness_of :name, scope: :namespace_id |
| 103 | 97 | validates_uniqueness_of :path, scope: :namespace_id |
| 104 | - | |
| 105 | 98 | validates :import_url, |
| 106 | 99 | format: { with: URI::regexp(%w(git http https)), message: "should be a valid url" }, |
| 107 | 100 | if: :import? |
| 108 | - | |
| 109 | 101 | validate :check_limit, on: :create |
| 110 | 102 | |
| 111 | 103 | # Scopes |
| ... | ... | @@ -118,14 +110,30 @@ class Project < ActiveRecord::Base |
| 118 | 110 | scope :sorted_by_activity, -> { reorder("projects.last_activity_at DESC") } |
| 119 | 111 | scope :personal, ->(user) { where(namespace_id: user.namespace_id) } |
| 120 | 112 | scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) } |
| 121 | - | |
| 122 | 113 | scope :public_only, -> { where(visibility_level: Project::PUBLIC) } |
| 123 | 114 | scope :public_and_internal_only, -> { where(visibility_level: Project.public_and_internal_levels) } |
| 124 | - | |
| 125 | 115 | scope :non_archived, -> { where(archived: false) } |
| 126 | 116 | |
| 127 | 117 | enumerize :issues_tracker, in: (Gitlab.config.issues_tracker.keys).append(:gitlab), default: :gitlab |
| 128 | 118 | |
| 119 | + state_machine :import_status, initial: :none do | |
| 120 | + event :import_start do | |
| 121 | + transition :none => :started | |
| 122 | + end | |
| 123 | + | |
| 124 | + event :import_finish do | |
| 125 | + transition :started => :finished | |
| 126 | + end | |
| 127 | + | |
| 128 | + event :import_fail do | |
| 129 | + transition :started => :timeout | |
| 130 | + end | |
| 131 | + | |
| 132 | + state :started | |
| 133 | + state :finished | |
| 134 | + state :timeout | |
| 135 | + end | |
| 136 | + | |
| 129 | 137 | class << self |
| 130 | 138 | def public_and_internal_levels |
| 131 | 139 | [Project::PUBLIC, Project::INTERNAL] | ... | ... |
app/workers/repository_import_worker.rb
| ... | ... | @@ -6,16 +6,18 @@ class RepositoryImportWorker |
| 6 | 6 | |
| 7 | 7 | def perform(project_id) |
| 8 | 8 | project = Project.find(project_id) |
| 9 | + project.import_start | |
| 10 | + | |
| 9 | 11 | result = gitlab_shell.send(:import_repository, |
| 10 | 12 | project.path_with_namespace, |
| 11 | 13 | project.import_url) |
| 12 | 14 | |
| 13 | 15 | if result |
| 14 | - project.imported = true | |
| 16 | + project.import_finish | |
| 15 | 17 | project.save |
| 16 | 18 | project.satellite.create unless project.satellite.exists? |
| 17 | 19 | else |
| 18 | - project.imported = false | |
| 20 | + project.import_fail | |
| 19 | 21 | end |
| 20 | 22 | end |
| 21 | 23 | end | ... | ... |