2020-03-09 00:40:16 +08:00
|
|
|
|
class RepositoriesController < ApplicationController
|
2021-09-30 14:59:37 +08:00
|
|
|
|
include RepositoriesHelper
|
2020-03-09 00:40:16 +08:00
|
|
|
|
include ApplicationHelper
|
2020-05-18 18:25:50 +08:00
|
|
|
|
include OperateProjectAbilityAble
|
2021-01-19 16:21:31 +08:00
|
|
|
|
include Repository::LanguagesPercentagable
|
2020-08-11 23:20:04 +08:00
|
|
|
|
|
2020-05-18 18:25:50 +08:00
|
|
|
|
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
|
2021-09-03 13:56:48 +08:00
|
|
|
|
before_action :require_profile_completed, only: [:create_file]
|
2020-08-12 14:51:12 +08:00
|
|
|
|
before_action :load_repository
|
2021-06-22 09:39:44 +08:00
|
|
|
|
before_action :authorizate!, except: [:sync_mirror, :tags, :commit, :archive]
|
2020-06-02 15:28:43 +08:00
|
|
|
|
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
|
2021-06-22 09:39:44 +08:00
|
|
|
|
before_action :get_ref, only: %i[entries sub_entries top_counts file archive]
|
2020-07-03 16:22:23 +08:00
|
|
|
|
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
|
2020-07-03 16:17:06 +08:00
|
|
|
|
before_action :get_statistics, only: %i[top_counts]
|
2023-09-22 21:10:33 +08:00
|
|
|
|
before_action :preload_badge, only: [:badges]
|
2020-03-09 00:40:16 +08:00
|
|
|
|
|
2021-03-16 10:25:37 +08:00
|
|
|
|
def files
|
2021-03-17 11:52:36 +08:00
|
|
|
|
result = @project.educoder? ? nil : Gitea::Repository::Files::GetService.call(@owner, @project.identifier, @ref, params[:search], @owner.gitea_token)
|
2021-03-16 10:25:37 +08:00
|
|
|
|
render json: result
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2023-08-15 00:15:10 +08:00
|
|
|
|
|
|
|
|
|
|
def preload_badge
|
2023-09-22 21:10:33 +08:00
|
|
|
|
@praised_cache = "praised"
|
|
|
|
|
|
@forked_cache = "forked"
|
|
|
|
|
|
@watchered_cache = "watchered"
|
|
|
|
|
|
@badge = RepositoryBadge.find_or_create_by(user_id: observed_user.id)
|
|
|
|
|
|
$redis_cache.hset(project_statistic_key, @many_projects_cache, @badge.many_projects)
|
|
|
|
|
|
$redis_cache.hset(project_statistic_key, @manyParisesProject_cache, @badge.manyParisesProject)
|
|
|
|
|
|
$redis_cache.hset(project_statistic_key, @parises_project_cache, @badge.parises_project)
|
2023-08-15 00:15:10 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2023-09-22 21:10:33 +08:00
|
|
|
|
####################change for repository############################
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-15 00:15:10 +08:00
|
|
|
|
def badges
|
2023-09-22 21:10:33 +08:00
|
|
|
|
preload_badge
|
|
|
|
|
|
@badges_dic = Hash.new(0)
|
|
|
|
|
|
praises_count = @project.praises_count.to_i
|
|
|
|
|
|
forked_count = @project.forked_count.to_i
|
|
|
|
|
|
watchers_count = @project.watchers_count.to_i
|
|
|
|
|
|
|
|
|
|
|
|
@praised = judge_level(praises_count, 10, 20,100)
|
|
|
|
|
|
redis_judge_reload @praised_cache, @praised
|
|
|
|
|
|
@badges_dic["praised_cache"] = $redis_cache.hget(project_statistic_key, @praised_cache).to_i
|
|
|
|
|
|
|
|
|
|
|
|
@forked = judge_level(forked_count, 5, 10,100)
|
|
|
|
|
|
redis_judge_reload @forked_cache, @forked
|
|
|
|
|
|
@badges_dic["forked_cache"] = $redis_cache.hget(project_statistic_key, @forked_cache).to_i
|
|
|
|
|
|
|
|
|
|
|
|
@watchered = judge_level(watchers_count, 10, 20,100)
|
|
|
|
|
|
redis_judge_reload @watchered_cache, @watchered
|
|
|
|
|
|
@badges_dic["forked_cache"] = $redis_cache.hget(project_statistic_key, @forked_cache).to_i
|
2023-08-15 00:15:10 +08:00
|
|
|
|
|
2023-09-22 21:10:33 +08:00
|
|
|
|
|
|
|
|
|
|
badges_visable = Array.new
|
|
|
|
|
|
@badges_dic.each do |key,value|
|
|
|
|
|
|
if value > 0
|
|
|
|
|
|
badges_visable.append({"#{key}":value})
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
render :json => {list: badges_visable, count: badges_visable.size}
|
2023-08-15 00:15:10 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2023-09-22 21:10:33 +08:00
|
|
|
|
##########################################################
|
|
|
|
|
|
|
2021-03-15 19:01:35 +08:00
|
|
|
|
# 新版项目详情
|
2021-03-27 23:54:14 +08:00
|
|
|
|
def detail
|
|
|
|
|
|
@user = current_user
|
2021-03-15 19:01:35 +08:00
|
|
|
|
@result = Repositories::DetailService.call(@owner, @repository, @user)
|
|
|
|
|
|
@project_fork_id = @project.try(:forked_from_project_id)
|
|
|
|
|
|
if @project_fork_id.present?
|
|
|
|
|
|
@fork_project = Project.find_by(id: @project_fork_id)
|
|
|
|
|
|
@fork_project_user = @fork_project.owner
|
|
|
|
|
|
end
|
2021-03-27 23:54:14 +08:00
|
|
|
|
rescue Exception => e
|
2021-03-15 19:01:35 +08:00
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
def show
|
2020-06-05 18:01:58 +08:00
|
|
|
|
@user = current_user
|
2020-06-29 16:05:57 +08:00
|
|
|
|
@repo = @project.repository
|
2020-11-10 10:03:30 +08:00
|
|
|
|
@result = @project.forge? ? Gitea::Repository::GetService.new(@owner, @project.identifier).call : nil
|
2020-04-07 14:03:48 +08:00
|
|
|
|
@project_fork_id = @project.try(:forked_from_project_id)
|
|
|
|
|
|
if @project_fork_id.present?
|
|
|
|
|
|
@fork_project = Project.find_by(id: @project_fork_id)
|
|
|
|
|
|
@fork_project_user = @fork_project.owner
|
|
|
|
|
|
end
|
2020-03-09 00:40:16 +08:00
|
|
|
|
rescue Exception => e
|
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
|
tip_exception(e.message)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def entries
|
2020-03-19 16:05:44 +08:00
|
|
|
|
@project.increment!(:visits)
|
2021-10-26 15:22:42 +08:00
|
|
|
|
CacheAsyncSetJob.perform_later("project_common_service", {visits: 1}, @project.id)
|
2020-10-22 18:00:31 +08:00
|
|
|
|
if @project.educoder?
|
|
|
|
|
|
@entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder.repo_name)
|
|
|
|
|
|
else
|
2020-11-10 10:03:30 +08:00
|
|
|
|
@entries = Gitea::Repository::Entries::ListService.new(@owner, @project.identifier, ref: @ref).call
|
2020-10-22 18:00:31 +08:00
|
|
|
|
@entries = @entries.present? ? @entries.sort_by{ |hash| hash['type'] } : []
|
|
|
|
|
|
@path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/"
|
|
|
|
|
|
end
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-07-03 15:14:25 +08:00
|
|
|
|
def top_counts
|
2020-10-22 18:00:31 +08:00
|
|
|
|
@result = @project.educoder? ? nil : Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
|
2020-07-03 15:14:25 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
def sub_entries
|
|
|
|
|
|
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
2021-09-30 19:49:17 +08:00
|
|
|
|
@path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/"
|
2020-09-09 10:33:49 +08:00
|
|
|
|
|
2020-10-22 18:00:31 +08:00
|
|
|
|
if @project.educoder?
|
|
|
|
|
|
if params[:type] === 'file'
|
|
|
|
|
|
@sub_entries = Educoder::Repository::Entries::GetService.call(@project&.project_educoder&.repo_name, file_path_uri)
|
2020-10-22 22:16:55 +08:00
|
|
|
|
logger.info "######### sub_entries: #{@sub_entries}"
|
2020-10-22 18:00:31 +08:00
|
|
|
|
return render_error('该文件暂未开放,敬请期待.') if @sub_entries['status'].to_i === -1
|
2020-10-22 22:16:55 +08:00
|
|
|
|
|
|
|
|
|
|
tmp_entries = [{
|
|
|
|
|
|
"content" => @sub_entries['data']['content'],
|
|
|
|
|
|
"type" => "blob"
|
|
|
|
|
|
}]
|
|
|
|
|
|
@sub_entries = {
|
|
|
|
|
|
"trees"=>tmp_entries,
|
|
|
|
|
|
"commits" => [{}]
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
@sub_entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder&.repo_name, {path: file_path_uri})
|
2020-10-22 18:00:31 +08:00
|
|
|
|
end
|
2020-03-09 00:40:16 +08:00
|
|
|
|
else
|
2020-11-16 15:34:16 +08:00
|
|
|
|
interactor = Repositories::EntriesInteractor.call(@owner, @project.identifier, file_path_uri, ref: @ref)
|
2020-10-22 18:00:31 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
|
result = interactor.result
|
2020-11-16 15:34:16 +08:00
|
|
|
|
@sub_entries = result.is_a?(Array) ? result.sort_by{ |hash| hash['type'] } : result
|
2020-10-22 18:00:31 +08:00
|
|
|
|
else
|
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
|
end
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def commits
|
2021-10-08 14:58:29 +08:00
|
|
|
|
if params[:filepath].present?
|
|
|
|
|
|
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
|
|
|
|
|
@hash_commit = Gitea::Repository::Commits::FileListService.new(@owner.login, @project.identifier, file_path_uri,
|
|
|
|
|
|
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call
|
|
|
|
|
|
else
|
|
|
|
|
|
@hash_commit = Gitea::Repository::Commits::ListService.new(@owner.login, @project.identifier,
|
|
|
|
|
|
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token).call
|
|
|
|
|
|
end
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2021-09-08 09:45:15 +08:00
|
|
|
|
def commits_slice
|
|
|
|
|
|
@hash_commit = Gitea::Repository::Commits::ListSliceService.call(@owner.login, @project.identifier,
|
|
|
|
|
|
sha: params[:sha], page: params[:page], limit: params[:limit], token: current_user&.gitea_token)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2020-05-29 15:33:17 +08:00
|
|
|
|
def commit
|
2020-11-09 17:23:19 +08:00
|
|
|
|
@sha = params[:sha]
|
2020-11-10 18:17:46 +08:00
|
|
|
|
@commit = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, @sha, current_user&.gitea_token)
|
|
|
|
|
|
@commit_diff = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, @sha, current_user&.gitea_token, {diff: true})
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def tags
|
2021-03-28 20:05:13 +08:00
|
|
|
|
result = Gitea::Repository::Tags::ListService.call(current_user&.gitea_token, @owner.login, @project.identifier, {page: params[:page], limit: params[:limit]})
|
|
|
|
|
|
|
|
|
|
|
|
@tags = result.is_a?(Hash) && result.key?(:status) ? [] : result
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2021-03-18 16:35:40 +08:00
|
|
|
|
def contributors
|
2021-09-23 18:07:46 +08:00
|
|
|
|
if params[:filepath].present?
|
|
|
|
|
|
@contributors = []
|
|
|
|
|
|
else
|
|
|
|
|
|
@contributors = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier)
|
|
|
|
|
|
end
|
2021-03-18 16:35:40 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
def edit
|
2021-02-26 13:49:29 +08:00
|
|
|
|
return render_forbidden if !@project.manager?(current_user) && !current_user.admin?
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-20 20:15:43 +08:00
|
|
|
|
def create_file
|
2020-11-10 10:03:30 +08:00
|
|
|
|
interactor = Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params)
|
2020-03-20 20:15:43 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
|
@file = interactor.result
|
2020-11-27 16:46:12 +08:00
|
|
|
|
# create_new_pr(params)
|
2021-01-19 11:52:29 +08:00
|
|
|
|
#如果是更新流水线文件
|
|
|
|
|
|
if params[:pipeline_id]
|
|
|
|
|
|
update_pipeline(params[:pipeline_id])
|
|
|
|
|
|
end
|
2020-03-20 20:15:43 +08:00
|
|
|
|
else
|
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2021-01-19 11:52:29 +08:00
|
|
|
|
def update_pipeline(pipeline_id)
|
|
|
|
|
|
pipeline = Ci::Pipeline.find(pipeline_id)
|
|
|
|
|
|
if pipeline
|
|
|
|
|
|
pipeline.update!(sync: 1)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-20 20:15:43 +08:00
|
|
|
|
def update_file
|
2020-11-10 10:03:30 +08:00
|
|
|
|
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, params.merge(identifier: @project.identifier))
|
2020-03-20 20:15:43 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
|
@file = interactor.result
|
2020-11-27 16:46:12 +08:00
|
|
|
|
# TODO: 是否创建pr
|
|
|
|
|
|
# create_new_pr(params)
|
2020-03-24 10:20:18 +08:00
|
|
|
|
render_result(1, "更新成功")
|
2020-03-20 20:15:43 +08:00
|
|
|
|
else
|
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def delete_file
|
2020-11-10 10:03:30 +08:00
|
|
|
|
interactor = Gitea::DeleteFileInteractor.call(current_user.gitea_token, @owner.login, params.merge(identifier: @project.identifier))
|
2020-03-20 20:15:43 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
|
@file = interactor.result
|
2020-03-24 10:20:18 +08:00
|
|
|
|
render_result(1, "文件删除成功")
|
2020-03-20 20:15:43 +08:00
|
|
|
|
else
|
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2020-05-11 18:33:15 +08:00
|
|
|
|
def repo_hook
|
2020-06-29 16:05:57 +08:00
|
|
|
|
|
2020-05-11 18:33:15 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-05-18 18:25:50 +08:00
|
|
|
|
def sync_mirror
|
2020-08-12 14:51:12 +08:00
|
|
|
|
return render_error("正在镜像中..") if @repository.mirror.waiting?
|
2020-06-29 16:05:57 +08:00
|
|
|
|
|
2020-08-12 14:51:12 +08:00
|
|
|
|
@repository.sync_mirror!
|
|
|
|
|
|
SyncMirroredRepositoryJob.perform_later(@repository.id, current_user.id)
|
2020-05-18 18:25:50 +08:00
|
|
|
|
render_ok
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2021-01-15 15:38:49 +08:00
|
|
|
|
def readme
|
2021-09-23 18:07:46 +08:00
|
|
|
|
if params[:filepath].present?
|
|
|
|
|
|
result = Gitea::Repository::Readme::DirService.call(@owner.login, @repository.identifier, params[:filepath], params[:ref], current_user&.gitea_token)
|
|
|
|
|
|
else
|
|
|
|
|
|
result = Gitea::Repository::Readme::GetService.call(@owner.login, @repository.identifier, params[:ref], current_user&.gitea_token)
|
|
|
|
|
|
end
|
2021-01-15 15:38:49 +08:00
|
|
|
|
@readme = result[:status] === :success ? result[:body] : nil
|
2021-09-30 14:59:37 +08:00
|
|
|
|
@readme['content'] = decode64_content(@readme, @owner, @repository, params[:ref])
|
2021-09-23 18:07:46 +08:00
|
|
|
|
render json: @readme.slice("type", "encoding", "size", "name", "path", "content", "sha")
|
|
|
|
|
|
rescue
|
|
|
|
|
|
render json: nil
|
2021-01-15 15:38:49 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2021-01-19 16:21:31 +08:00
|
|
|
|
def languages
|
|
|
|
|
|
render json: languages_precentagable
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2021-06-22 09:39:44 +08:00
|
|
|
|
def archive
|
|
|
|
|
|
domain = Gitea.gitea_config[:domain]
|
|
|
|
|
|
api_url = Gitea.gitea_config[:base_url]
|
|
|
|
|
|
archive_url = "/repos/#{@owner.login}/#{@repository.identifier}/archive/#{params[:archive]}"
|
|
|
|
|
|
|
|
|
|
|
|
file_path = [domain, api_url, archive_url].join
|
|
|
|
|
|
file_path = [file_path, "access_token=#{current_user&.gitea_token}"].join("?") if @repository.hidden?
|
|
|
|
|
|
|
|
|
|
|
|
return render_not_found if !request.format.zip? && !request.format.gzip?
|
|
|
|
|
|
|
|
|
|
|
|
redirect_to file_path
|
|
|
|
|
|
end
|
2021-09-10 10:52:21 +08:00
|
|
|
|
|
|
|
|
|
|
def raw
|
|
|
|
|
|
domain = Gitea.gitea_config[:domain]
|
|
|
|
|
|
api_url = Gitea.gitea_config[:base_url]
|
|
|
|
|
|
|
|
|
|
|
|
url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{params[:filepath]}?ref=#{params[:ref]}"
|
|
|
|
|
|
file_path = [domain, api_url, url].join
|
|
|
|
|
|
file_path = [file_path, "access_token=#{current_user&.gitea_token}"].join("&") if @repository.hidden?
|
|
|
|
|
|
|
2021-09-29 18:50:51 +08:00
|
|
|
|
redirect_to URI.escape(file_path)
|
2021-09-10 10:52:21 +08:00
|
|
|
|
end
|
2021-06-22 09:39:44 +08:00
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
private
|
2020-03-20 20:15:43 +08:00
|
|
|
|
|
|
|
|
|
|
def find_project
|
|
|
|
|
|
@project = Project.find params[:id]
|
2020-03-23 17:31:32 +08:00
|
|
|
|
render_not_found("未找到相关的仓库") unless @project
|
2020-03-20 20:15:43 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-06-05 18:01:58 +08:00
|
|
|
|
def find_project_with_includes
|
|
|
|
|
|
@project = Project.includes(:repository, :owner, :watchers, :praise_treads).find params[:id]
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
def authorizate!
|
2020-07-16 12:11:16 +08:00
|
|
|
|
return if current_user && current_user.admin?
|
2020-03-20 20:15:43 +08:00
|
|
|
|
if @project.repository.hidden? && !@project.member?(current_user)
|
2020-03-09 00:40:16 +08:00
|
|
|
|
render_forbidden
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
2020-03-19 16:05:44 +08:00
|
|
|
|
|
2020-06-03 11:41:14 +08:00
|
|
|
|
# TODO 获取最新commit信息
|
2020-07-03 16:13:01 +08:00
|
|
|
|
def project_commits
|
2021-10-08 14:43:46 +08:00
|
|
|
|
if params[:filepath].present?
|
|
|
|
|
|
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
|
|
|
|
|
Gitea::Repository::Commits::FileListService.new(@project.owner.login, @project.identifier, file_path_uri,
|
|
|
|
|
|
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
|
|
|
|
|
|
else
|
|
|
|
|
|
Gitea::Repository::Commits::ListService.new(@project.owner.login, @project.identifier,
|
|
|
|
|
|
sha: get_ref, page: 1, limit: 1, token: current_user&.gitea_token).call
|
|
|
|
|
|
end
|
2020-06-29 16:05:57 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_statistics
|
2020-10-22 18:00:31 +08:00
|
|
|
|
@branches_count = @project.educoder? ? 0 : Gitea::Repository::Branches::ListService.new(@project.owner, @project.identifier).call&.size
|
|
|
|
|
|
@tags_count = @project.educoder? ? 0 : Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier).call&.size
|
2020-06-03 11:41:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def get_ref
|
2021-03-27 23:54:14 +08:00
|
|
|
|
@ref = params[:ref] || @project&.default_branch
|
2020-06-03 11:41:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-07-16 12:11:16 +08:00
|
|
|
|
def get_latest_commit
|
2020-10-22 18:00:31 +08:00
|
|
|
|
latest_commit = @project.educoder? ? nil : project_commits
|
|
|
|
|
|
@latest_commit = latest_commit.present? ? latest_commit[:body][0] : nil
|
|
|
|
|
|
@commits_count = latest_commit.present? ? latest_commit[:total_count] : 0
|
2020-07-03 16:13:01 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-03-20 20:15:43 +08:00
|
|
|
|
def content_params
|
|
|
|
|
|
{
|
|
|
|
|
|
filepath: params[:filepath],
|
|
|
|
|
|
branch: params[:branch],
|
|
|
|
|
|
new_branch: params[:new_branch],
|
|
|
|
|
|
content: params[:content],
|
|
|
|
|
|
message: params[:message],
|
2020-08-27 15:05:09 +08:00
|
|
|
|
committer: {
|
|
|
|
|
|
email: current_user.mail,
|
|
|
|
|
|
name: current_user.login
|
|
|
|
|
|
},
|
2020-03-20 20:15:43 +08:00
|
|
|
|
identifier: @project.identifier
|
|
|
|
|
|
}
|
2020-03-19 16:05:44 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2020-05-18 18:25:50 +08:00
|
|
|
|
def hook_params(hook_type, params)
|
2020-06-09 14:55:00 +08:00
|
|
|
|
# if hook_type == "push"
|
|
|
|
|
|
# # TODO hook返回的记录中,暂时没有文件代码数量的增减,暂时根据 commits数量来计算
|
|
|
|
|
|
# uploadPushInfo = {
|
|
|
|
|
|
# "sha": params["commits"].present? ? params["commits"].last : "",
|
|
|
|
|
|
# "branch": params["ref"].to_s.split("/").last,
|
|
|
|
|
|
# "modification_lines": params["commits"].length
|
|
|
|
|
|
# }
|
|
|
|
|
|
# elsif hook_type == "pull_request" && params["action"].to_s == "closed" #合并请求合并后才会有上链操作
|
|
|
|
|
|
# uploadPushInfo = {
|
|
|
|
|
|
# "branch": params["base"]["ref"].to_s.split("/").last,
|
|
|
|
|
|
# "sha": params["pull_request"]["merge_base"],
|
|
|
|
|
|
# "modification_lines": 1 #pull_request中没有commits数量
|
|
|
|
|
|
# }
|
|
|
|
|
|
# else
|
|
|
|
|
|
# uploadPushInfo = {}
|
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
# uploadPushInfo
|
2020-05-12 18:15:55 +08:00
|
|
|
|
end
|
2021-06-22 09:39:44 +08:00
|
|
|
|
|
2020-05-26 18:55:41 +08:00
|
|
|
|
def create_new_pr(params)
|
|
|
|
|
|
if params[:new_branch].present? && params[:new_branch] != params[:branch]
|
|
|
|
|
|
local_params = {
|
|
|
|
|
|
title: params[:message], #标题
|
|
|
|
|
|
body: params[:content], #内容
|
|
|
|
|
|
head: params[:new_branch], #源分支
|
|
|
|
|
|
base: params[:branch], #目标分支
|
|
|
|
|
|
milestone: 0 #里程碑,未与本地的里程碑关联
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
requests_params = local_params.merge({
|
|
|
|
|
|
assignee: current_user.try(:login),
|
|
|
|
|
|
assignees: [],
|
|
|
|
|
|
labels: [],
|
|
|
|
|
|
due_date: Time.now
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
issue_params = {
|
|
|
|
|
|
author_id: current_user.id,
|
|
|
|
|
|
project_id: @project.id,
|
|
|
|
|
|
subject: params[:message],
|
|
|
|
|
|
description: params[:content],
|
|
|
|
|
|
assigned_to_id: nil,
|
|
|
|
|
|
fixed_version_id: nil,
|
|
|
|
|
|
issue_tags_value: nil,
|
|
|
|
|
|
issue_classify: "pull_request",
|
|
|
|
|
|
issue_type: "1",
|
|
|
|
|
|
tracker_id: 2,
|
|
|
|
|
|
status_id: 1,
|
2020-06-12 18:28:20 +08:00
|
|
|
|
priority_id: params[:priority_id] || "2"
|
2020-05-26 18:55:41 +08:00
|
|
|
|
}
|
2020-06-05 10:57:46 +08:00
|
|
|
|
@pull_issue = Issue.new(issue_params)
|
|
|
|
|
|
if @pull_issue.save!
|
2020-06-05 11:11:37 +08:00
|
|
|
|
local_requests = PullRequest.new(local_params.merge(user_id: current_user.try(:id), project_id: @project.id, issue_id: @pull_issue.id))
|
2020-05-26 18:55:41 +08:00
|
|
|
|
if local_requests.save
|
2020-12-23 15:46:06 +08:00
|
|
|
|
gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @owner.login, @project.try(:identifier), requests_params).call
|
|
|
|
|
|
if gitea_request[:status] == :success && local_requests.update_attributes(gpid: gitea_request["body"]["number"])
|
2020-05-26 18:55:41 +08:00
|
|
|
|
local_requests.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2023-09-22 21:10:33 +08:00
|
|
|
|
|
|
|
|
|
|
def judge_level(judged, low, mid, high)
|
|
|
|
|
|
if judged < low
|
|
|
|
|
|
return 0
|
|
|
|
|
|
elsif judged < mid
|
|
|
|
|
|
return 1
|
|
|
|
|
|
elsif judged < high
|
|
|
|
|
|
return 2
|
|
|
|
|
|
else
|
|
|
|
|
|
return 3
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def redis_judge_reload(judge_cache, new_value)
|
|
|
|
|
|
if $redis_cache.hget(project_statistic_key, judge_cache).to_i != new_value
|
|
|
|
|
|
$redis_cache.del(project_statistic_key)
|
|
|
|
|
|
updateService judge_cache, new_value
|
|
|
|
|
|
load_redis
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def project_statistic_key
|
|
|
|
|
|
"project:#{@project.id}"
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def updateService(update_name, update_value)
|
|
|
|
|
|
@badge = RepositoryBadge.find_or_create_by(user_id: observed_user.id)
|
|
|
|
|
|
@badge.update_attribute(update_name,update_value)
|
|
|
|
|
|
@badge.save
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-09 00:40:16 +08:00
|
|
|
|
end
|