[FIX]为组织创建项目做准备

This commit is contained in:
viletyy 2021-01-15 18:28:38 +08:00
parent 31858a79e3
commit 1dc43a23b7
9 changed files with 90 additions and 87 deletions

View File

@ -124,7 +124,7 @@ class VersionReleasesController < ApplicationController
private
def set_user
@user = @repository.user
@user = @repository.owner
end
def find_version

View File

@ -5,7 +5,7 @@ class SyncMirroredRepositoryJob < ApplicationJob
repo = Repository.find_by(id: repo_id)
current_user = User.find_by(id: user_id)
return if repo.blank? || current_user.blank?
result = Gitea::Repository::SyncMirroredService.new(repo.user.login, repo.identifier, token: current_user.gitea_token).call
result = Gitea::Repository::SyncMirroredService.new(repo.owner.login, repo.identifier, token: current_user.gitea_token).call
repo&.mirror.set_status! if result[:status] === 200
end
end

View File

@ -58,8 +58,7 @@
# index_users_on_type (type)
#
class Organization < ApplicationRecord
self.table_name = "users"
class Organization < Owner
default_scope { where(type: "Organization") }
has_one :organization_extension, dependent: :destroy
@ -74,8 +73,8 @@ class Organization < ApplicationRecord
scope :with_visibility, ->(visibility) { joins(:organization_extension).where(organization_extensions: {visibility: visibility}) if visibility.present? }
def self.build(name)
self.create!(login: name)
def self.build(name, gitea_token=nil)
self.create!(login: name, gitea_token: gitea_token)
end
def is_owner?(user)

9
app/models/owner.rb Normal file
View File

@ -0,0 +1,9 @@
class Owner < ApplicationRecord
self.table_name = "users"
include ProjectOperable
include ProjectAbility
has_many :projects, foreign_key: :user_id, dependent: :destroy
has_many :repositories, foreign_key: :user_id, dependent: :destroy
end

View File

@ -85,7 +85,7 @@ class Project < ApplicationRecord
belongs_to :ignore, optional: true
belongs_to :license, optional: true
belongs_to :owner, class_name: 'User', foreign_key: :user_id, optional: true
belongs_to :owner, class_name: 'Owner', foreign_key: :user_id, optional: true
belongs_to :project_category, optional: true , :counter_cache => true
belongs_to :project_language, optional: true , :counter_cache => true
has_many :project_trends, dependent: :destroy
@ -248,7 +248,7 @@ class Project < ApplicationRecord
def self.find_with_namespace(namespace_path, identifier)
logger.info "########namespace_path: #{namespace_path} ########identifier: #{identifier} "
user = User.find_by_login namespace_path
user = Owner.find_by_login namespace_path
project = user&.projects&.find_by(identifier: identifier) || Project.find_by(identifier: "#{namespace_path}/#{identifier}")
return nil if project.blank?

View File

@ -33,7 +33,7 @@
class Repository < ApplicationRecord
self.inheritance_column = nil # FIX The single-table inheritance mechanism failed
belongs_to :project, :touch => true
belongs_to :user, optional: true
belongs_to :owner, class_name: 'Owner', foreign_key: :user_id, optional: true
has_one :mirror, foreign_key: :repo_id
has_one :ci_cloud_account, class_name: 'Ci::CloudAccount', foreign_key: :repo_id
has_many :version_releases, dependent: :destroy

View File

@ -58,15 +58,13 @@
# index_users_on_type (type)
#
class User < ApplicationRecord
class User < Owner
default_scope {where(type: %w(User AnonymousUser))}
extend Enumerize
include Watchable
include Likeable
include BaseModel
include ProjectOperable
include ProjectAbility
include Droneable
# include Searchable::Dependents::User
@ -155,9 +153,6 @@ class User < ApplicationRecord
# 项目
has_many :applied_projects, dependent: :destroy
has_many :projects, dependent: :destroy
has_many :repositories, dependent: :destroy
# 教学案例
# has_many :libraries, dependent: :destroy
has_many :project_trends, dependent: :destroy

View File

@ -47,7 +47,7 @@ class Organizations::CreateService < ApplicationService
end
def create_org_and_extension
@organization = Organization.build(params[:name])
@organization = Organization.build(params[:name], user.gitea_token)
org_extension = OrganizationExtension.build(organization.id, description, website,
location, repo_admin_change_team_access,
visibility, max_repo_creation)

View File

@ -20,7 +20,7 @@ class Projects::ForkService < ApplicationService
clone_project.save!
new_repository = clone_project.repository
new_repository.user = @target_owner
new_repository.owner = @target_owner
new_repository.identifier = @project.identifier
new_repository.save!