FIX merge bug

This commit is contained in:
Jasder 2020-08-13 09:44:38 +08:00
commit 78b091ddd3
23 changed files with 649 additions and 559 deletions

View File

@ -531,17 +531,18 @@ curl -X POST http://localhost:3000/api/repositories/1244/sync_mirror | jq
#### 项目详情
```
GET api/projects/:id
GET /api/:namespace_id/:id
```
*示例*
```
curl -X GET http://localhost:3000/api/projects/3263 | jq
curl -X GET http://localhost:3000/api/jasder/jasder_test | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|int |项目id |
|namespace_id |是|string |用户登录名 |
|id |是|string |项目标识identifier |
*返回参数说明:*
@ -570,6 +571,50 @@ curl -X GET http://localhost:3000/api/projects/3263 | jq
```
---
#### 项目详情(简版)
```
GET /api/:namespace_id/:id/simple
```
*示例*
```
curl -X GET http://localhost:3000/api/jasder/jasder_test/simple | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|int |项目id |
*返回参数说明:*
|参数名|类型|说明|
|-|-|-|
|id |int |id |
|name |string|项目名称|
|identifier |string|项目标识|
|is_public |boolean|项目是否公开, true:公开false:私有|
|description |string|项目简介|
|repo_id |int|仓库id|
|repo_identifier|string|仓库标识|
返回值
```
{
"identifier": "jasder_test",
"name": "jasder的测试项目",
"id": 4967,
"type": 0,
"author": {
"login": "jasder",
"name": "姓名",
"image_url": "avatars/User/b"
}
}
```
---
#### 编辑仓库信息
```
GET /api/repositories/:id/edit.json
@ -897,13 +942,13 @@ curl -X POST http://localhost:3000/api/projects/3297/forks | jq
#### 获取代码目录列表
```
POST /api/repositories/:id/entries.json
POST /api/:namespace_id/:project_id/repository/entries
```
*示例*
```
curl -X GET \
-d "ref=develop" \
http://localhost:3000//api/repositories/3687/entries.json | jq
http://localhost:3000//api/jasder/jasder_test/repository/entries | jq
```
*请求参数说明:*
@ -1320,11 +1365,11 @@ http://localhost:3000/api/projects | jq
### 获取分支列表
```
GET /api/projects/:id/branches
GET /api/:namespace_id/:id/branches
```
*示例*
```
curl -X GET http://localhost:3000/api/projects/4797/branches | jq
curl -X GET http://localhost:3000/api/jasder/jasder_test/branches | jq
```
*请求参数说明:*
@ -1487,18 +1532,19 @@ http://localhost:3000/api/repositories/5836/tags.json | jq
## 仓库详情
```
GET /api/repositories/:id
GET /api/:namespace_id/:project_id/repository
```
*示例*
```
curl -X GET \
http://localhost:3000/api/repositories/23.json | jq
http://192.168.2.230:3000/api/jasder/forgeplus/repository | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|string |项目id |
|namespace_id |是|string |用户登录名 |
|project_id |是|string |项目标识identifier |
*返回参数说明:*

View File

@ -66,8 +66,8 @@ class AccountsController < ApplicationController
sync_params = {}
if (user_params["mail"] && user_params["mail"] != user_mail) || (user_params["login"] && user_params["login"] != params[:old_user_login])
sync_params = sync_params.merge(email: user_params["mail"], login_name: user_params["login"], full_name: user_params["login"])
if (user_params["mail"] && user_params["mail"] != user_mail)
sync_params = sync_params.merge(email: user_params["mail"])
end
if sync_params.present?

View File

@ -728,11 +728,6 @@ class ApplicationController < ActionController::Base
render_not_found("未找到’#{project}’相关的项目") unless @project
end
def find_project_with_identifier
@project = Project.find_by_identifier! params[:id]
render_not_found("未找到’#{params[:id]}’相关的项目") unless @project
end
def find_project_with_id
@project = Project.find(params[:project_id] || params[:id])
rescue Exception => e
@ -744,6 +739,32 @@ class ApplicationController < ActionController::Base
interactor.success? ? render_ok : render_error(interactor.error)
end
# projects
def load_project
namespace = params[:owner]
id = params[:repo] || params[:id]
@project = Project.find_with_namespace(namespace, id)
if @project and current_user.can_read_project?(@project)
logger.info "########### has project and can read project"
@project
elsif current_user.is_a?(AnonymousUser)
logger.info "###########This is AnonymousUser"
@project = nil if !@project.is_public?
render_forbidden and return
else
logger.info "###########project not found"
@project = nil
render_not_found and return
end
@project
end
def load_repository
@repository ||= load_project.repository
end
private
def object_not_found
uid_logger("Missing template or cant't find record, responding with 404")

View File

@ -10,7 +10,7 @@ module OperateProjectAbilityAble
end
def authorizate_user_can_edit_repo!
return if @repo.project.manager?(current_user) || current_user.admin?
return if @repository.project.manager?(current_user) || current_user.admin?
render_forbidden('你没有权限操作.')
end

View File

@ -1,5 +1,6 @@
class ForksController < ApplicationController
before_action :require_login, :find_project_with_id
before_action :require_login
before_action :load_project
before_action :authenticate_project!, :authenticate_user!
def create

View File

@ -1,7 +1,7 @@
class IssueTagsController < ApplicationController
before_action :require_login, except: [:index]
before_action :find_project_with_id
before_action :set_project
before_action :load_repository
before_action :set_user
before_action :check_issue_permission, except: :index
before_action :set_issue_tag, only: [:edit, :update, :destroy]
@ -121,13 +121,8 @@ class IssueTagsController < ApplicationController
private
def set_project
# @project = Project.find_by_identifier! params[:project_id]
@repository = @project.repository
def set_user
@user = @project.owner
normal_status(-1, "项目不存在") unless @project.present?
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def check_issue_permission
@ -143,4 +138,4 @@ class IssueTagsController < ApplicationController
end
end
end
end

View File

@ -1,7 +1,7 @@
class IssuesController < ApplicationController
before_action :require_login, except: [:index, :show, :index_chosen]
before_action :find_project_with_id
before_action :set_project_and_user
before_action :load_project
before_action :set_user
before_action :check_issue_permission
before_action :check_project_public, only: [:index ,:show, :copy, :index_chosen, :close_issue]
@ -16,7 +16,7 @@ class IssuesController < ApplicationController
@user_admin_or_member = current_user.present? && current_user.logged? && (current_user.admin || @project.member?(current_user))
issues = @project.issues.issue_issue.issue_index_includes
issues = issues.where(is_private: false) unless @user_admin_or_member
@all_issues_size = issues.size
@open_issues_size = issues.where.not(status_id: 5).size
@close_issues_size = issues.where(status_id: 5).size
@ -281,7 +281,19 @@ class IssuesController < ApplicationController
update_hash = {}
update_hash.merge!(assigned_to_id: params[:assigned_to_id]) if params[:assigned_to_id].present?
update_hash.merge!(fixed_version_id: params[:fixed_version_id]) if params[:fixed_version_id].present?
update_hash.merge!(status_id: params[:status_id]) if params[:status_id].present?
# update_hash.merge!(status_id: params[:status_id]) if params[:status_id].present?
if params[:status_id].present?
status_id = params[:status_id].to_i
update_hash.merge!(status_id: status_id)
done_ratio = nil
case status_id
when 1
done_ratio = 0
when 3
done_ratio = 100
end
update_hash.merge!(done_ratio: done_ratio) if done_ratio
end
# update_hash = params[:issue]
issue_ids = params[:ids]
if issue_ids.present?
@ -371,11 +383,8 @@ class IssuesController < ApplicationController
end
private
def set_project_and_user
# @project = Project.find_by_identifier(params[:project_id]) || (Project.find params[:project_id]) || (Project.find params[:id])
def set_user
@user = @project&.owner
# normal_status(-1, "项目不存在") unless @project.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def check_project_public

View File

@ -1,6 +1,6 @@
class MembersController < ApplicationController
before_action :require_login
before_action :find_project_with_id
before_action :load_project
before_action :find_user_with_id, only: %i[create remove change_role]
before_action :operate!, except: %i[index]
before_action :check_member_exists!, only: %i[create]

View File

@ -1,5 +1,5 @@
class ProjectTrendsController < ApplicationController
before_action :find_project_with_id
before_action :load_repository
before_action :check_project_public
def index
@ -44,4 +44,4 @@ class ProjectTrendsController < ApplicationController
normal_status(-1, "您没有权限")
end
end
end
end

View File

@ -1,5 +1,7 @@
class Projects::BaseController < ApplicationController
include PaginateHelper
before_action :require_login, :check_auth
before_action :load_project
before_action :load_repository
end

View File

@ -2,8 +2,8 @@ class ProjectsController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
include ProjectsHelper
before_action :require_login, except: %i[index branches group_type_list simple]
before_action :find_project_with_id, only: %i[show branches update destroy fork_users praise_users watch_users]
before_action :require_login, except: %i[index branches group_type_list simple show]
before_action :load_project, except: %i[index group_type_list migrate]
before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_users]
@ -100,10 +100,10 @@ class ProjectsController < ApplicationController
end
def simple
project = Project.includes(:owner, :repository).select(:id, :name, :identifier, :user_id, :project_type, :open_devops).find params[:id]
json_response(project)
json_response(@project)
end
private
def project_params
params.permit(:user_id, :name, :description, :repository_name,

View File

@ -1,7 +1,7 @@
class PullRequestsController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :find_project_with_id
before_action :set_repository
before_action :load_repository
before_action :set_user, only: [:new, :get_branches]
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos]
# before_action :get_relatived, only: [:edit]
include TagChosenHelper
@ -234,12 +234,8 @@ class PullRequestsController < ApplicationController
private
def set_repository
@repository = @project.repository
def set_user
@user = @project.owner
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def find_pull_request

View File

@ -1,11 +1,10 @@
class RepositoriesController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
before_action :find_project_with_includes, only: :show
before_action :find_project, except: [:tags, :commit, :sync_mirror, :show]
before_action :load_repository
before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
before_action :find_repository_by_id, only: %i[commit sync_mirror tags]
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_ref, only: %i[entries sub_entries top_counts]
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
@ -56,11 +55,11 @@ class RepositoriesController < ApplicationController
end
def commit
@commit = Gitea::Repository::Commits::GetService.new(@repo.user.login, @repo.identifier, params[:sha], current_user.gitea_token).call
@commit = Gitea::Repository::Commits::GetService.new(@repository.user.login, @repository.identifier, params[:sha], current_user.gitea_token).call
end
def tags
@tags = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @repo.user.login, @repo.identifier, {page: params[:page], limit: params[:limit]}).call
@tags = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier, {page: params[:page], limit: params[:limit]}).call
end
def edit
@ -102,10 +101,10 @@ class RepositoriesController < ApplicationController
end
def sync_mirror
return render_error("正在镜像中..") if @repo.mirror.waiting?
return render_error("正在镜像中..") if @repository.mirror.waiting?
@repo.sync_mirror!
SyncMirroredRepositoryJob.perform_later(@repo.id, current_user.id)
@repository.sync_mirror!
SyncMirroredRepositoryJob.perform_later(@repository.id, current_user.id)
render_ok
end

View File

@ -1,302 +1,313 @@
class SyncForgeController < ApplicationController
# before_action :check_token
def create
ActiveRecord::Base.transaction do
params.permit!
sync_params = params[:sync_params]
project_user = User.where(login: sync_params[:owner_login])&.first
#以前已同步的项目,那么肯定存在仓库
SyncLog.sync_log("=================begin_to_sync_forge: project_identifier: #{sync_params[:identifier]}========")
user_projects = Project.where(user_id: project_user.id)
if user_projects.where(id: sync_params[:id], identifier: sync_params[:identifier]).present?
has_project = true
project = user_projects.where(id: sync_params[:id], identifier: sync_params[:identifier])&.first
elsif user_projects.where(id: sync_params[:id]).present?
has_project = true
project = user_projects.where(id: sync_params[:id])&.first
elsif user_projects.where(identifier: sync_params[:identifier]).present?
has_project = true
project = user_projects.where(identifier: sync_params[:identifier])&.first
else
has_project = false
end
if has_project
SyncLog.sync_log("=================begin_to_update_project========")
check_sync_project(project, sync_params)
else #新建项目
SyncLog.sync_log("=================begin_to_create_new_project========")
project_params = {
repository_name: sync_params[:identifier],
user_id: project_user.id,
private: !sync_params[:is_public],
name: sync_params[:name]
}
project = Projects::CreateService.new(project_user, project_params).call
if project.present?
if sync_params[:project_score].present?
sync_params.permit!
score_params = sync_params[:project_score].merge(project_id: project.id)
new_project_score = ProjectScore.create(score_params)
SyncLog.sync_log("=================new_project_score:#{new_project_score.try(:id)}========")
end
SyncRepositoryJob.perform_later(sync_params[:owner_login], sync_params[:identifier], sync_params[:repository], get_sudomain) if sync_params[:repository].present?
check_new_project(project, sync_params)
else
SyncLog.sync_project_log("=============new_project_create_failed, trustie_project_id==:#{params[:sync_params][:id]}")
end
end
end
rescue Exception => e
SyncLog.sync_project_log("=============sync_has_errors:==#{e.message}, project_id==:#{params[:sync_params][:id]}")
end
def sync_users
params.permit!
sync_params = params[:sync_params]
users_params = sync_params[:users]
users_params.each do |u|
if User.exists?(login: u[:user_params][:login])
SyncLog.sync_log("=================sync_to_user_been_exists====#{u[:user_params][:login]}")
else
# new_user = User.new(u[:user_params])
if u[:user_params][:mail].blank?
u_mail = "#{u[:user_params][:login]}@example.com"
else
u_mail = u[:user_params][:mail]
end
new_user = User.new(u[:user_params].merge(mail: u_mail))
username = new_user.login
password = "12345678"
# if new_user.save!
# SyncLog.sync_log("=================sync_to_user_success==#{new_user.login}")
# else
# SyncLog.sync_log("=================sync_to_user_failed,user_login==#{new_user.login}")
# end
ActiveRecord::Base.transaction do
interactor = Gitea::RegisterInteractor.call({username: username, email: new_user.mail, password: password})
if interactor.success?
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.new(username, password).call
new_user.gitea_token = result['sha1']
new_user.gitea_uid = gitea_user['id']
if new_user.save!
UserExtension.create!(u[:user_extensions][:user_extensions].merge(user_id: new_user.id)) if u[:user_extensions].present? && u[:user_extensions][:user_extensions].present?
else
SyncLog.sync_log("=================sync_to_user_failed,user_login==#{new_user.login}")
end
else
SyncLog.sync_log("=============sync_to_user_failed,user_login====#{new_user.login}")
SyncLog.sync_log("=================sync_to_user_failed,user_login====#{new_user.login}")
end
end
end
end
# normal_status(1, "completed_sync")
rescue Exception => e
SyncLog.sync_log("=================sync_user_failed====#{e}")
end
private
def check_sync_project(project,sync_params)
begin
gitea_main = "https://www.trustie.net/"
# if request.subdomain === 'testforgeplus'
# gitea_main = "https://ucloudtest.trustie.net/"
# end
SyncLog.sync_log("----begin_to_check_sync_project----project_id:#{project.id}---------------")
change_project_score(project, sync_params[:project_score], sync_params[:repository]) if sync_params[:repository].present? #更新project_score
change_project_issues(project, sync_params[:issues],project.id, gitea_main)
change_project_members(project, sync_params[:members],gitea_main)
change_project_versions(project, sync_params[:project_versions],gitea_main)
change_project_watchers(project, sync_params[:project_watchers],gitea_main)
change_project_praises(project, sync_params[:praise_trends],gitea_main)
rescue => e
SyncLog.sync_log("=========check_sync_project_errors:#{e}===================")
end
end
def check_new_project(project,sync_params)
SyncLog.sync_log("***8. begin_to_sync_new_project---------------")
sync_projects_params = {
type: "Project",
ids: sync_params[:id],
token: get_token,
sync_params: sync_params,
new_project_id: project.id
}
gitea_main = "https://www.trustie.net/"
# if request.subdomain === 'testforgeplus'
# gitea_main = "https://ucloudtest.trustie.net/"
# end
SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
SyncLog.sync_log("***8. end_to_sync_new_project---------------")
end
def change_project_praises(project, praises,gitea_main)
SyncLog.sync_log("***6. begin_to_sync_parises---------------")
forge_praises_ids = project&.praise_treads&.select(:id)&.pluck(:id)
diff_target_ids = praises[:ids] - forge_praises_ids
if diff_target_ids.size > 0
sync_projects_params = {
type: "PraiseTread",
ids: diff_target_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
SyncLog.sync_log("***6. end_to_sync_parises---------------")
def sync_range_projects
sync_counts = params[:sync_count] || 10
projects = Project.includes(:project_score, :repository)
.where(is_public: true)
.where.not(identifier: ["educoder","trustieforge", "gitlab"])
.joins(:project_score).order("project_scores.changeset_num desc").limit(sync_counts.to_i)
projects.each do | project |
SyncProjectMilitaryJob.perform_later(project, project.repository, project.project_score)
end
end
#检查repository和project_score
def change_project_score(project, project_scores, repository_params)
SyncLog.sync_log("***1. begin_to_sync_project_score---------------")
begin
pre_project_score = project.project_score
if pre_project_score.present?
change_num = 0
project_scores.each do |k,v|
unless pre_project_score.send("#{k}") == v
change_num += 1
pre_project_score[:"#{k}"] = v
end
if k == "changeset_num" && v.to_i > pre_project_score.changeset_num.to_i && repository_params[:url].present?
SyncRepositoryJob.perform_later(project.owner.try(:login), project.identifier, repository_params, get_sudomain)
end
end
pre_project_score.save! if change_num > 0 #如果 project_score有变化则更新
else
ProjectScore.create!(project_scores.merge(project_id: project.id))
end
SyncLog.sync_log("***1. end_to_sync_project_score---------------")
rescue Exception => e
SyncLog.sync_log("=========change_project_score_errors:#{e}===================")
end
end
# def create
# ActiveRecord::Base.transaction do
# params.permit!
# sync_params = params[:sync_params]
# project_user = User.where(login: sync_params[:owner_login])&.first
# #以前已同步的项目,那么肯定存在仓库
# SyncLog.sync_log("=================begin_to_sync_forge: project_identifier: #{sync_params[:identifier]}========")
# user_projects = Project.where(user_id: project_user.id)
# if user_projects.where(id: sync_params[:id], identifier: sync_params[:identifier]).present?
# has_project = true
# project = user_projects.where(id: sync_params[:id], identifier: sync_params[:identifier])&.first
# elsif user_projects.where(id: sync_params[:id]).present?
# has_project = true
# project = user_projects.where(id: sync_params[:id])&.first
# elsif user_projects.where(identifier: sync_params[:identifier]).present?
# has_project = true
# project = user_projects.where(identifier: sync_params[:identifier])&.first
# else
# has_project = false
# end
def change_project_issues(project, old_issues_params,project_id, gitea_main)
SyncLog.sync_log("***2. begin_to_syncissues---------------")
begin
forge_issue_ids = project&.issues&.select(:id)&.pluck(:id)
sync_projects_params = {}
SyncLog.sync_log("***2--01. forge_issue_ids-#{forge_issue_ids.size.to_i}--------------")
if forge_issue_ids.size.to_i <= old_issues_params[:count].to_i
diff_issue_ids = old_issues_params[:ids] - forge_issue_ids
# if has_project
# SyncLog.sync_log("=================begin_to_update_project========")
# check_sync_project(project, sync_params)
# else #新建项目
# SyncLog.sync_log("=================begin_to_create_new_project========")
if diff_issue_ids.size == 0 #issue数量一样判断评论是否有增减
forge_journal_ids = Journal.select([:id, :journalized_id, :journalized_type]).where(journalized_id: forge_issue_ids).pluck(:id)
diff_journal_ids = old_issues_params[:journals][:ids] - forge_journal_ids
unless diff_journal_ids.size == 0
sync_projects_params = {
type: "Journal",
ids: diff_journal_ids,
token: get_token,
parent_id: project_id
}
SyncLog.sync_log("***2--02. sync_projects_params-#{sync_projects_params}--------------")
SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
end
else
new_diff_ids = diff_issue_ids.in_groups_of(200).map{|k| k.reject(&:blank?)}
diff_len = new_diff_ids.length
(1..diff_len).each do |len|
sync_projects_params = {
type: "Issue",
ids: new_diff_ids[len-1],
token: get_token,
parent_id: project_id
}
SyncLog.sync_log("***2--030#{len}. sync_projects_params_groups-#{sync_projects_params}--------------")
SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
end
# sync_projects_params = {
# type: "Issue",
# ids: diff_issue_ids,
# token: get_token,
# parent_id: project_id
# }
# SyncLog.sync_log("***2--03. sync_projects_params_groups-#{sync_projects_params}--------------")
# SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
# project_params = {
# repository_name: sync_params[:identifier],
# user_id: project_user.id,
# private: !sync_params[:is_public],
# name: sync_params[:name]
# }
# project = Projects::CreateService.new(project_user, project_params).call
# if project.present?
# if sync_params[:project_score].present?
# sync_params.permit!
# score_params = sync_params[:project_score].merge(project_id: project.id)
# new_project_score = ProjectScore.create(score_params)
# SyncLog.sync_log("=================new_project_score:#{new_project_score.try(:id)}========")
# end
end
end
# SyncRepositoryJob.perform_later(sync_params[:owner_login], sync_params[:identifier], sync_params[:repository], get_sudomain) if sync_params[:repository].present?
# check_new_project(project, sync_params)
# else
# SyncLog.sync_project_log("=============new_project_create_failed, trustie_project_id==:#{params[:sync_params][:id]}")
# end
# end
# end
# rescue Exception => e
# SyncLog.sync_project_log("=============sync_has_errors:==#{e.message}, project_id==:#{params[:sync_params][:id]}")
# end
# SyncProjectsJob.perform_later(sync_projects_params, gitea_main) if sync_projects_params.present?
SyncLog.sync_log("***2. end_to_syncissues---------------")
rescue Exception => e
SyncLog.sync_log("=========change_project_issues_errors:#{e}===================")
end
end
# def sync_users
# params.permit!
# sync_params = params[:sync_params]
# users_params = sync_params[:users]
def change_project_watchers(project, watchers,gitea_main)
SyncLog.sync_log("***5. begin_to_sync_watchers---------------")
forge_watchers_ids = project&.watchers&.select(:id)&.pluck(:id)
if forge_watchers_ids.size.to_i <= watchers[:count].to_i
diff_target_ids = watchers[:ids] - forge_watchers_ids
if diff_target_ids.size > 0
sync_projects_params = {
type: "Watcher",
ids: diff_target_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
# users_params.each do |u|
# if User.exists?(login: u[:user_params][:login])
# SyncLog.sync_log("=================sync_to_user_been_exists====#{u[:user_params][:login]}")
# else
# # new_user = User.new(u[:user_params])
end
end
SyncLog.sync_log("***5. begin_to_sync_watchers---------------")
end
# if u[:user_params][:mail].blank?
# u_mail = "#{u[:user_params][:login]}@example.com"
# else
# u_mail = u[:user_params][:mail]
# end
def change_project_versions(project, versions,gitea_main)
SyncLog.sync_log("***4. begin_to_sync_versions---------------")
forge_version_ids = project&.versions&.select(:id)&.pluck(:id)
if forge_version_ids.size <= versions[:count].to_i
diff_version_ids = versions[:ids] - forge_version_ids
if diff_version_ids.size > 0
sync_projects_params = {
type: "Version",
ids: diff_version_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
end
# new_user = User.new(u[:user_params].merge(mail: u_mail))
SyncLog.sync_log("***4. end_to_sync_versions---------------")
end
end
# username = new_user.login
# password = "12345678"
# # if new_user.save!
# # SyncLog.sync_log("=================sync_to_user_success==#{new_user.login}")
# # else
# # SyncLog.sync_log("=================sync_to_user_failed,user_login==#{new_user.login}")
# # end
# ActiveRecord::Base.transaction do
# interactor = Gitea::RegisterInteractor.call({username: username, email: new_user.mail, password: password})
# if interactor.success?
# gitea_user = interactor.result
# result = Gitea::User::GenerateTokenService.new(username, password).call
# new_user.gitea_token = result['sha1']
# new_user.gitea_uid = gitea_user['id']
# if new_user.save!
# UserExtension.create!(u[:user_extensions][:user_extensions].merge(user_id: new_user.id)) if u[:user_extensions].present? && u[:user_extensions][:user_extensions].present?
# else
# SyncLog.sync_log("=================sync_to_user_failed,user_login==#{new_user.login}")
# end
# else
# SyncLog.sync_log("=============sync_to_user_failed,user_login====#{new_user.login}")
# SyncLog.sync_log("=================sync_to_user_failed,user_login====#{new_user.login}")
# end
# end
# end
# end
# # normal_status(1, "completed_sync")
# rescue Exception => e
# SyncLog.sync_log("=================sync_user_failed====#{e}")
# end
def change_project_members(project, members,gitea_main)
SyncLog.sync_log("***3. begin_to_sync_members---------------")
forge_member_ids = project&.members&.select(:id)&.pluck(:id)
if forge_member_ids.size <= members[:count]
diff_member_ids = members[:ids] - forge_member_ids
if diff_member_ids.size > 0
sync_projects_params = {
type: "Member",
ids: diff_member_ids,
token: get_token,
parent_id: project.id
}
SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
end
# private
SyncLog.sync_log("***3. end_to_sync_members---------------")
end
end
# def check_sync_project(project,sync_params)
# begin
# gitea_main = "https://www.trustie.net/"
# # if request.subdomain === 'testforgeplus'
# # gitea_main = "https://ucloudtest.trustie.net/"
# # end
# SyncLog.sync_log("----begin_to_check_sync_project----project_id:#{project.id}---------------")
# change_project_score(project, sync_params[:project_score], sync_params[:repository]) if sync_params[:repository].present? #更新project_score
# change_project_issues(project, sync_params[:issues],project.id, gitea_main)
# change_project_members(project, sync_params[:members],gitea_main)
# change_project_versions(project, sync_params[:project_versions],gitea_main)
# change_project_watchers(project, sync_params[:project_watchers],gitea_main)
# change_project_praises(project, sync_params[:praise_trends],gitea_main)
# rescue => e
# SyncLog.sync_log("=========check_sync_project_errors:#{e}===================")
# end
# end
# def check_new_project(project,sync_params)
# SyncLog.sync_log("***8. begin_to_sync_new_project---------------")
# sync_projects_params = {
# type: "Project",
# ids: sync_params[:id],
# token: get_token,
# sync_params: sync_params,
# new_project_id: project.id
# }
# gitea_main = "https://www.trustie.net/"
# # if request.subdomain === 'testforgeplus'
# # gitea_main = "https://ucloudtest.trustie.net/"
# # end
# SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
# SyncLog.sync_log("***8. end_to_sync_new_project---------------")
# end
# def change_project_praises(project, praises,gitea_main)
# SyncLog.sync_log("***6. begin_to_sync_parises---------------")
# forge_praises_ids = project&.praise_treads&.select(:id)&.pluck(:id)
# diff_target_ids = praises[:ids] - forge_praises_ids
# if diff_target_ids.size > 0
# sync_projects_params = {
# type: "PraiseTread",
# ids: diff_target_ids,
# token: get_token,
# parent_id: project.id
# }
# SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
# SyncLog.sync_log("***6. end_to_sync_parises---------------")
# end
# end
# #检查repository和project_score
# def change_project_score(project, project_scores, repository_params)
# SyncLog.sync_log("***1. begin_to_sync_project_score---------------")
# begin
# pre_project_score = project.project_score
# if pre_project_score.present?
# change_num = 0
# project_scores.each do |k,v|
# unless pre_project_score.send("#{k}") == v
# change_num += 1
# pre_project_score[:"#{k}"] = v
# end
# if k == "changeset_num" && v.to_i > pre_project_score.changeset_num.to_i && repository_params[:url].present?
# SyncRepositoryJob.perform_later(project.owner.try(:login), project.identifier, repository_params, get_sudomain)
# end
# end
# pre_project_score.save! if change_num > 0 #如果 project_score有变化则更新
# else
# ProjectScore.create!(project_scores.merge(project_id: project.id))
# end
# SyncLog.sync_log("***1. end_to_sync_project_score---------------")
# rescue Exception => e
# SyncLog.sync_log("=========change_project_score_errors:#{e}===================")
# end
# end
# def change_project_issues(project, old_issues_params,project_id, gitea_main)
# SyncLog.sync_log("***2. begin_to_syncissues---------------")
# begin
# forge_issue_ids = project&.issues&.select(:id)&.pluck(:id)
# sync_projects_params = {}
# SyncLog.sync_log("***2--01. forge_issue_ids-#{forge_issue_ids.size.to_i}--------------")
# if forge_issue_ids.size.to_i <= old_issues_params[:count].to_i
# diff_issue_ids = old_issues_params[:ids] - forge_issue_ids
# if diff_issue_ids.size == 0 #issue数量一样判断评论是否有增减
# forge_journal_ids = Journal.select([:id, :journalized_id, :journalized_type]).where(journalized_id: forge_issue_ids).pluck(:id)
# diff_journal_ids = old_issues_params[:journals][:ids] - forge_journal_ids
# unless diff_journal_ids.size == 0
# sync_projects_params = {
# type: "Journal",
# ids: diff_journal_ids,
# token: get_token,
# parent_id: project_id
# }
# SyncLog.sync_log("***2--02. sync_projects_params-#{sync_projects_params}--------------")
# SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
# end
# else
# new_diff_ids = diff_issue_ids.in_groups_of(200).map{|k| k.reject(&:blank?)}
# diff_len = new_diff_ids.length
# (1..diff_len).each do |len|
# sync_projects_params = {
# type: "Issue",
# ids: new_diff_ids[len-1],
# token: get_token,
# parent_id: project_id
# }
# SyncLog.sync_log("***2--030#{len}. sync_projects_params_groups-#{sync_projects_params}--------------")
# SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
# end
# # sync_projects_params = {
# # type: "Issue",
# # ids: diff_issue_ids,
# # token: get_token,
# # parent_id: project_id
# # }
# # SyncLog.sync_log("***2--03. sync_projects_params_groups-#{sync_projects_params}--------------")
# # SyncProjectsJob.perform_later(sync_projects_params, gitea_main)
# end
# end
# # SyncProjectsJob.perform_later(sync_projects_params, gitea_main) if sync_projects_params.present?
# SyncLog.sync_log("***2. end_to_syncissues---------------")
# rescue Exception => e
# SyncLog.sync_log("=========change_project_issues_errors:#{e}===================")
# end
# end
# def change_project_watchers(project, watchers,gitea_main)
# SyncLog.sync_log("***5. begin_to_sync_watchers---------------")
# forge_watchers_ids = project&.watchers&.select(:id)&.pluck(:id)
# if forge_watchers_ids.size.to_i <= watchers[:count].to_i
# diff_target_ids = watchers[:ids] - forge_watchers_ids
# if diff_target_ids.size > 0
# sync_projects_params = {
# type: "Watcher",
# ids: diff_target_ids,
# token: get_token,
# parent_id: project.id
# }
# SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
# end
# end
# SyncLog.sync_log("***5. begin_to_sync_watchers---------------")
# end
# def change_project_versions(project, versions,gitea_main)
# SyncLog.sync_log("***4. begin_to_sync_versions---------------")
# forge_version_ids = project&.versions&.select(:id)&.pluck(:id)
# if forge_version_ids.size <= versions[:count].to_i
# diff_version_ids = versions[:ids] - forge_version_ids
# if diff_version_ids.size > 0
# sync_projects_params = {
# type: "Version",
# ids: diff_version_ids,
# token: get_token,
# parent_id: project.id
# }
# SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
# end
# SyncLog.sync_log("***4. end_to_sync_versions---------------")
# end
# end
# def change_project_members(project, members,gitea_main)
# SyncLog.sync_log("***3. begin_to_sync_members---------------")
# forge_member_ids = project&.members&.select(:id)&.pluck(:id)
# if forge_member_ids.size <= members[:count]
# diff_member_ids = members[:ids] - forge_member_ids
# if diff_member_ids.size > 0
# sync_projects_params = {
# type: "Member",
# ids: diff_member_ids,
# token: get_token,
# parent_id: project.id
# }
# SyncProjectsJob.perform_later(sync_projects_params,gitea_main)
# end
# SyncLog.sync_log("***3. end_to_sync_members---------------")
# end
# end
# def check_token
# sync_params = params[:sync_params]
@ -305,19 +316,19 @@ class SyncForgeController < ApplicationController
# end
# end
def get_token
"34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e"
end
# def get_token
# "34c82f51e0b699d9d16d70fd6497c9b1e4821d6ea3e872558a6537a091076b8e"
# end
def get_sudomain
SyncLog.sync_log("=================request.subdomain:#{request.subdomain}========")
gitea_main = "gitea.trustie.net"
if request.subdomain === 'testforgeplus'
gitea_main = "testgitea2.trustie.net"
# elsif request.subdomain === 'forgeplus'
# gitea_main = "gitea.trustie.net"
end
return gitea_main
end
# def get_sudomain
# SyncLog.sync_log("=================request.subdomain:#{request.subdomain}========")
# gitea_main = "gitea.trustie.net"
# if request.subdomain === 'testforgeplus'
# gitea_main = "testgitea2.trustie.net"
# # elsif request.subdomain === 'forgeplus'
# # gitea_main = "gitea.trustie.net"
# end
# return gitea_main
# end
end

View File

@ -1,6 +1,6 @@
class VersionReleasesController < ApplicationController
before_action :find_project_with_id
before_action :set_user_and_project
before_action :load_repository
before_action :set_user
before_action :require_login, except: [:index]
before_action :find_version , only: [:edit, :update, :destroy]
@ -78,14 +78,14 @@ class VersionReleasesController < ApplicationController
ActiveRecord::Base.transaction do
begin
version_params = releases_params
if @version.update_attributes!(version_params)
create_attachments(params[:attachment_ids], @version) if params[:attachment_ids].present?
git_version_release = Gitea::Versions::UpdateService.new(@user.gitea_token, @user.try(:login), @repository.try(:identifier), version_params, @version.try(:version_gid)).call
unless git_version_release
raise Error, "更新失败"
end
normal_status(0, "更新成功")
else
normal_status(-1, "更新失败")
@ -123,14 +123,8 @@ class VersionReleasesController < ApplicationController
private
def set_user_and_project
# @project = Project.find_by_id(params[:project_id])
@repository = @project.repository #项目的仓库
@user = @project.owner
unless @user.present? && @project.present? && @repository.present?
normal_status(-1, "仓库不存在")
end
def set_user
@user = @repository.user
end
def find_version
@ -140,7 +134,7 @@ class VersionReleasesController < ApplicationController
end
end
def releases_params
def releases_params
{
body: params[:body],
draft: params[:draft] || false,
@ -151,7 +145,7 @@ class VersionReleasesController < ApplicationController
}
end
def create_attachments(attachment_ids, target)
def create_attachments(attachment_ids, target)
attachment_ids.each do |id|
attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id)
unless attachment.blank?

View File

@ -1,6 +1,6 @@
class VersionsController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :find_project_with_id
before_action :load_repository
before_action :check_issue_permission, except: [:show, :index]
before_action :set_version, only: [:edit, :update, :destroy, :show,:update_status]
@ -166,4 +166,4 @@ class VersionsController < ApplicationController
end
end
end
end

View File

@ -0,0 +1,22 @@
require 'uri'
require 'net/http'
class SyncProjectMilitaryJob < ApplicationJob
queue_as :default
def perform(project, repository, project_socre)
Rails.logger.info("============begin to sync project ===========")
project_except_params = %w(praises_count watchers_count issues_count pull_requests_count versions_count issue_tags_count closed_issues_count)
project_params = {
project: project.as_json(except: project_except_params),
repository: repository.as_json,
project_socre: project_socre.as_json
}
url = "http://47.93.212.82:49999/sync_forges" #trustie上的相关路由
uri = URI.parse(url)
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = false
response = http.send_request('POST', uri.path, project_params.to_json, {'Content-Type' => 'application/json'})
Rails.logger.info("============end to sync project, status: #{response.code} ===========")
end
end

View File

@ -28,6 +28,7 @@ class SyncRepositoryJob < ApplicationJob
SyncLog.sync_log("=============force_push_erros==#{path}/#{image_repo_name}++branch:#{branch}")
else
SyncLog.sync_log("=============force_push_success==#{path}/#{image_repo_name}++branch+++#{branch}")
system("rm -rf #{path}/#{image_repo_name}")
end
end
else

View File

@ -0,0 +1,14 @@
module ProjectAbility
extend ActiveSupport::Concern
included do
end
def can_read_project?(project)
return true if self.admin?
return false if !project.is_public? && !project.member?(self.id)
true
end
end

View File

@ -169,4 +169,15 @@ class Project < ApplicationRecord
joins(:members).where(members: { user_id: member_user_id})
end
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)
return nil if project.blank?
project
end
end

View File

@ -5,7 +5,7 @@ class User < ApplicationRecord
include Likeable
include BaseModel
include ProjectOperable
include Droneable
include ProjectAbility
# include Searchable::Dependents::User
# devops step
@ -78,9 +78,14 @@ class User < ApplicationRecord
# 关注
has_many :be_watchers, foreign_key: :user_id, dependent: :destroy # 我的关注
has_many :be_watcher_users, through: :be_watchers, dependent: :destroy # 我关注的用户
<<<<<<< HEAD
has_many :watchers, as: :watchable, dependent: :destroy
has_one :dev_ops_cloud_account, class_name: 'DevOps::CloudAccount', dependent: :destroy
=======
has_many :watchers, as: :watchable, dependent: :destroy
>>>>>>> dev_change_route
# 认证
has_many :apply_user_authentication

View File

@ -1,6 +1,6 @@
json.commit do
json.sha commit['sha']
json.url EduSetting.get('host_name') + commit_repository_path(project.repository, commit['sha'])
# json.url EduSetting.get('host_name') + commit_repository_path(project.repository, commit['sha'])
json.message commit['commit']['message']
json.author commit['commit']['author']
json.committer commit['commit']['committer']

View File

@ -40,6 +40,7 @@ Rails.application.routes.draw do
resources :sync_forge, only: [:create] do
collection do
post :sync_users
post :sync_range_projects
end
end
resources :composes do
@ -88,41 +89,6 @@ Rails.application.routes.draw do
end
end
resources :projects do
resources :hooks
resources :pull_requests, except: [:destroy] do
member do
post :pr_merge
# post :check_merge
post :refuse_merge
end
collection do
post :check_can_merge
get :create_merge_infos
get :get_branches
end
end
resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy]
resources :project_trends, only: [:index, :create]
resources :issues do
collection do
get :commit_issues
get :index_chosen
post :clean
post :series_update
end
member do
post :copy
post :close_issue
post :lock_issue
end
end
resources :issue_tags, only: [:create, :edit, :update, :destroy, :index]
resources :versions do
member do
post :update_status
end
end
resources :praise_tread, only: [:index] do
collection do
post :like
@ -130,25 +96,10 @@ Rails.application.routes.draw do
get :check_like
end
end
resources :members, only: [:index, :create] do
collection do
delete :remove
put :change_role
end
end
resources :forks, only: [:create]
collection do
post :migrate
get :group_type_list
post :watch
end
member do
get :branches
post :watch
get :watch_users
get :praise_users
get :fork_users
get :simple
end
end
@ -208,11 +159,6 @@ Rails.application.routes.draw do
# resource :unread_message_info, only: [:show]
end
resources :projects, module: :users, only: [] do
get :search, on: :collection
end
resources :tidings, only: [:index]
scope module: :users do
@ -231,23 +177,6 @@ Rails.application.routes.draw do
end
end
resources :repositories, only: [:index, :show, :edit] do
member do
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
post :files
get :tags
post :create_file
put :update_file
delete :delete_file
post :repo_hook
post :sync_mirror
get :top_counts
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
end
end
resources :users_for_private_messages, only: [:index]
resources :files, only: [:index, :show, :update] do
@ -267,119 +196,6 @@ Rails.application.routes.draw do
end
end
resources :courses do
member do
get 'settings', :action => 'settings', :as => 'settings'
post 'set_invite_code_halt'
post 'set_public_or_private'
post 'search_teacher_candidate'
post 'add_teacher'
post 'create_graduation_group'
post 'join_graduation_group'
post 'set_course_group'
post 'change_course_admin'
post 'change_member_role'
post 'change_course_teacher'
post 'delete_course_teacher'
post 'teacher_application_review'
post 'transfer_to_course_group'
post 'delete_from_course'
post 'add_students_by_search'
post 'create_group_by_importing_file'
post 'duplicate_course'
post 'visits_plus_one'
get 'get_historical_courses'
get 'get_historical_course_students'
get 'course_group_list'
get 'add_teacher_popup'
get 'teachers'
get 'apply_teachers'
get 'graduation_group_list'
get 'top_banner'
get 'left_banner'
get 'students'
get 'all_course_groups'
get 'search_users'
get 'base_info'
get 'attahcment_category_list'
get 'export_member_scores_excel' #导出课堂信息
get 'export_couser_info'
get 'export_member_act_score'
post 'switch_to_teacher'
post 'switch_to_assistant'
post 'switch_to_student'
post 'exit_course'
get 'informs'
post 'update_informs'
post 'new_informs'
delete 'delete_informs'
get 'online_learning'
post 'join_excellent_course'
get 'tasks_list'
post 'update_task_position'
get 'course_groups'
post 'join_course_group'
get 'work_score'
get 'act_score'
get 'statistics'
get 'course_videos'
delete 'delete_course_video'
post :inform_up
post :inform_down
end
collection do
post 'apply_to_join_course'
post 'search_course_list'
get 'board_list'
get 'mine'
get 'search_slim'
end
resources :course_stages, shallow: true do
member do
post :up_position
post :down_position
end
end
resources :course_groups, shallow: true do
member do
post 'rename_group'
post 'move_category'
post 'set_invite_code_halt'
end
end
end
resources :course_modules, shallow: true do
member do
get 'sticky_module'
get 'hidden_module'
post 'rename_module'
post 'add_second_category'
end
collection do
post 'unhidden_modules'
end
end
resources :course_second_categories, shallow: true do
member do
post 'rename_category'
post 'move_category'
end
end
resources :repertoires, only: [:index]
scope module: :projects do
resources :project_applies, only: [:create]
end
namespace :wechats do
resource :js_sdk_signature, only: [:create]
end
@ -432,6 +248,151 @@ Rails.application.routes.draw do
end
end
end
# Project Area START
scope "/:owner/:repo" do
scope do
get(
'/activity',
to: 'project_trends#index',
as: :project_activity
)
end
resource :projects, path: '/', except: [:show] do
member do
get :branches
get :simple
get :watchers, to: 'projects#watch_users'
get :stargazers, to: 'projects#praise_users'
get :members, to: 'projects#fork_users'
end
end
resource :repositories, path: '/', only: [:show, :create, :edit] do
member do
get :archive
get :top_counts
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
get :tags
post :create_file
put :update_file
delete :delete_file
post :repo_hook
post :sync_mirror
get :top_counts
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
end
end
resources :issues do
collection do
get :commit_issues
get :index_chosen
post :clean
post :series_update
end
member do
post :copy
post :close_issue
post :lock_issue
end
end
resources :pull_requests, :path => :pulls, except: [:destroy] do
member do
post :pr_merge
# post :check_merge
post :refuse_merge
end
collection do
post :check_can_merge
get :create_merge_infos
get :get_branches
end
end
resources :versions, :path => :milestones do
member do
post :update_status
end
end
resources :members, :path => :collaborators, only: [:index, :create] do
collection do
delete :remove
put :change_role
end
end
resources :hooks
resources :forks, only: [:create]
resources :project_trends, :path => :activity, only: [:index, :create]
resources :issue_tags, :path => :labels, only: [:create, :edit, :update, :destroy, :index]
resources :version_releases, :path => :releases, only: [:index,:new, :create, :edit, :update, :destroy]
scope module: :projects do
scope do
get(
'/blob/*id/diff',
to: 'blob#diff',
constraints: { id: /.+/, format: false },
as: :blob_diff
)
get(
'/blob/*id',
to: 'blob#show',
constraints: { id: /.+/, format: false },
as: :blob
)
delete(
'/blob/*id',
to: 'blob#destroy',
constraints: { id: /.+/, format: false }
)
put(
'/blob/*id',
to: 'blob#update',
constraints: { id: /.+/, format: false }
)
post(
'/blob/*id',
to: 'blob#create',
constraints: { id: /.+/, format: false }
)
end
scope do
get(
'/raw/*id',
to: 'raw#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :raw
)
end
scope do
get(
'/blame/*id',
to: 'blame#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :blame
)
end
scope do
get(
'/tree/*id',
to: 'tree#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :tree
)
end
end
end
# Project Area END
end
namespace :admins do
@ -707,4 +668,6 @@ Rails.application.routes.draw do
## react用
get '*path', to: 'main#index', constraints: ReactConstraint.new
end