ADD 同步educoder平台实训项目

This commit is contained in:
Jasder 2020-10-19 22:59:41 +08:00
parent 913f4b26a4
commit 2eef143251
7 changed files with 129 additions and 22 deletions

View File

@ -39,6 +39,20 @@ module ProjectsHelper
first_sync: repo.first_sync?
})
end
author =
if project.educoder?
{
login: project.project_educoder.owner,
name: project.project_educoder.owner,
image_url: project.project_educoder.image_url
}
else
{
login: project.owner.login,
name: project.owner.real_name,
image_url: url_to_avatar(project.owner)
}
end
tmp_json = tmp_json.merge({
identifier: project.identifier,
@ -47,12 +61,13 @@ module ProjectsHelper
repo_id: repo.id,
open_devops: (user.blank? || user.is_a?(AnonymousUser)) ? false : project.open_devops?,
type: project.numerical_for_project_type,
author: {
login: project.owner.login,
name: project.owner.real_name,
image_url: url_to_avatar(project.owner)
}
author: author
}).compact
render json: tmp_json
end
def render_author(project)
end
end

View File

@ -10,16 +10,19 @@ class Project < ApplicationRecord
#
enum project_type: { sync_mirror: 2, mirror: 1, common: 0 }
# forge: trustie平台项目 educoder: educoder平台项目 默认为forge平台
enum platform: { forge: 0, educoder: 1 }
belongs_to :ignore, optional: true
belongs_to :license, optional: true
belongs_to :owner, class_name: 'User', foreign_key: :user_id
belongs_to :owner, class_name: 'User', 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
has_many :watchers, as: :watchable, dependent: :destroy
has_many :fork_users, dependent: :destroy
has_many :forked_users, class_name: 'ForkUser', foreign_key: :fork_project_id, dependent: :destroy
# has_many :commits, dependent: :destroy
has_one :project_educoder, dependent: :destroy
has_one :project_score, dependent: :destroy
has_one :repository, dependent: :destroy
@ -175,12 +178,16 @@ 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
return nil if user.blank?
project = user.projects.find_by(identifier: identifier)
project = Project.find_by(identifier: identifier)
return nil if project.blank?
if project.forge?
user = User.find_by_login namespace_path
return nil if user.blank?
project = user.projects.find_by(identifier: identifier)
return nil if project.blank?
end
project
end
@ -193,4 +200,65 @@ class Project < ApplicationRecord
update_column(:open_devops, true)
increment!(:open_devops_count)
end
def self.sync_educoder_shixun(url, private_token, page, per_page)
uri = URI("#{url}?page=#{page}&per_page=#{per_page}&private_token=#{private_token}")
puts "-------url: #{uri}"
response = Net::HTTP.get_response(uri)
result = JSON.parse(response.body)
if result['status'] != 0
logger.info "======= 接口请求失败!"
return
end
result['data']['repositories'].each do |re|
language = ProjectLanguage.find_by_name re['language']
language = ProjectLanguage.create!(name: re['language']) if language.blank?
category = ProjectCategory.find_by_name re['category']
category = ProjectCategory.create!(name: re['category']) if category.blank?
project_params =
{
name: re['name'],
# user_id: params[:user_id],
description: re['description'],
project_category_id: category.id,
project_language_id: language.id,
is_public: true,
# ignore_id: params[:ignore_id],
# license_id: params[:license_id],
identifier: re['repo_name'],
platform: Project.platforms[:educoder]
}
project = Project.new(project_params)
ActiveRecord::Base.transaction do
if project.save!
repo_params =
{
hidden: false,
project_id: project.id,
identifier: re['repo_name']
}
ProjectEducoder.create!(
project_id: project.id,
owner: re['username'],
repo_name: re['repo_name'],
image_url: re['image_url'])
repo = Repository.new(repo_params)
repo.save!
logger.info "项目: #{re['name']} 同步成功"
else
logger.info "项目: #{re['name']} 同步失败"
end
end
end
end
end

View File

@ -0,0 +1,3 @@
class ProjectEducoder < ApplicationRecord
belongs_to :project, optional: true
end

View File

@ -1,7 +1,7 @@
class Repository < ApplicationRecord
self.inheritance_column = nil # FIX The single-table inheritance mechanism failed
belongs_to :project, :touch => true
belongs_to :user
belongs_to :user, 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

@ -1,8 +1,4 @@
user = project.owner
if user.blank?
nil
else
json.id project.id
json.id project.id
json.repo_id project&.repository&.id
json.identifier project.identifier
json.name project.name
@ -18,10 +14,19 @@ json.time_ago time_from_now(project.updated_on)
json.forked_from_project_id project.forked_from_project_id
json.open_devops project.open_devops?
json.author do
json.name user.try(:show_real_name)
json.login user.login
json.image_url url_to_avatar(project.owner)
if project.educoder?
project_educoder = project.project_educoder
json.name project_educoder&.owner
json.login project_educoder#.owner
json.image_url project_educoder.image_url
else
user = project.owner
json.name user.try(:show_real_name)
json.login user.login
json.image_url url_to_avatar(project.owner)
end
end
json.category do
if project.project_category.blank?
json.nil!
@ -38,4 +43,3 @@ json.language do
json.name project.project_language.name
end
end
end

View File

@ -0,0 +1,5 @@
class AddPlatformToProjects < ActiveRecord::Migration[5.2]
def change
add_column :projects, :platform, :integer, default: 0
end
end

View File

@ -0,0 +1,12 @@
class CreateProjectEducoders < ActiveRecord::Migration[5.2]
def change
create_table :project_educoders do |t|
t.string :owner
t.string :repo_name
t.string :image_url
t.integer :project_id
t.timestamps
end
end
end