2021-11-22 13:15:45 +08:00
|
|
|
|
class RepositoriesController < ApplicationController
|
2021-11-22 13:21:41 +08:00
|
|
|
|
include RepositoriesHelper
|
2021-11-22 13:15:45 +08:00
|
|
|
|
include ApplicationHelper
|
|
|
|
|
include OperateProjectAbilityAble
|
2021-11-22 13:21:41 +08:00
|
|
|
|
include Repository::LanguagesPercentagable
|
2021-11-22 13:20:23 +08:00
|
|
|
|
|
2021-11-22 13:15:45 +08:00
|
|
|
|
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
|
2021-11-22 13:21:41 +08:00
|
|
|
|
before_action :require_profile_completed, only: [:create_file]
|
2021-11-22 13:20:23 +08:00
|
|
|
|
before_action :load_repository
|
2021-11-22 13:21:41 +08:00
|
|
|
|
before_action :authorizate!, except: [:sync_mirror, :tags, :commit, :archive]
|
2021-11-22 13:15:45 +08:00
|
|
|
|
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
|
2021-11-22 13:21:41 +08:00
|
|
|
|
before_action :get_ref, only: %i[entries sub_entries top_counts file archive]
|
2021-11-22 13:15:45 +08:00
|
|
|
|
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
|
|
|
|
|
before_action :get_statistics, only: %i[top_counts]
|
|
|
|
|
|
2021-11-22 13:21:41 +08:00
|
|
|
|
def files
|
|
|
|
|
result = @project.educoder? ? nil : Gitea::Repository::Files::GetService.call(@owner, @project.identifier, @ref, params[:search], @owner.gitea_token)
|
|
|
|
|
render json: result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 新版项目详情
|
|
|
|
|
def detail
|
|
|
|
|
@user = current_user
|
|
|
|
|
@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
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
tip_exception(e.message)
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:15:45 +08:00
|
|
|
|
def show
|
|
|
|
|
@user = current_user
|
|
|
|
|
@repo = @project.repository
|
2021-11-22 13:20:23 +08:00
|
|
|
|
@result = @project.forge? ? Gitea::Repository::GetService.new(@owner, @project.identifier).call : nil
|
2021-11-22 13:15:45 +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
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
uid_logger_error(e.message)
|
|
|
|
|
tip_exception(e.message)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def entries
|
|
|
|
|
@project.increment!(:visits)
|
2021-11-22 13:21:41 +08:00
|
|
|
|
CacheAsyncSetJob.perform_later("project_common_service", {visits: 1}, @project.id)
|
2021-11-22 13:20:23 +08:00
|
|
|
|
if @project.educoder?
|
|
|
|
|
@entries = Educoder::Repository::Entries::ListService.call(@project&.project_educoder.repo_name)
|
|
|
|
|
else
|
|
|
|
|
@entries = Gitea::Repository::Entries::ListService.new(@owner, @project.identifier, ref: @ref).call
|
|
|
|
|
@entries = @entries.present? ? @entries.sort_by{ |hash| hash['type'] } : []
|
|
|
|
|
@path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/"
|
|
|
|
|
end
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def top_counts
|
2021-11-22 13:20:23 +08:00
|
|
|
|
@result = @project.educoder? ? nil : Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sub_entries
|
|
|
|
|
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
2021-11-22 13:21:41 +08:00
|
|
|
|
@path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/"
|
2021-11-22 13:20:23 +08:00
|
|
|
|
|
|
|
|
|
if @project.educoder?
|
|
|
|
|
if params[:type] === 'file'
|
|
|
|
|
@sub_entries = Educoder::Repository::Entries::GetService.call(@project&.project_educoder&.repo_name, file_path_uri)
|
|
|
|
|
logger.info "######### sub_entries: #{@sub_entries}"
|
|
|
|
|
return render_error('该文件暂未开放,敬请期待.') if @sub_entries['status'].to_i === -1
|
|
|
|
|
|
|
|
|
|
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})
|
|
|
|
|
end
|
2021-11-22 13:15:45 +08:00
|
|
|
|
else
|
2021-11-22 13:20:23 +08:00
|
|
|
|
interactor = Repositories::EntriesInteractor.call(@owner, @project.identifier, file_path_uri, ref: @ref)
|
|
|
|
|
if interactor.success?
|
|
|
|
|
result = interactor.result
|
|
|
|
|
@sub_entries = result.is_a?(Array) ? result.sort_by{ |hash| hash['type'] } : result
|
|
|
|
|
else
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
end
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def commits
|
2021-11-22 13:21:41 +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
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:21:41 +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
|
|
|
|
|
|
2021-11-22 13:15:45 +08:00
|
|
|
|
def commit
|
2021-11-22 13:20:23 +08:00
|
|
|
|
@sha = params[:sha]
|
|
|
|
|
@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})
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tags
|
2021-11-22 13:21:41 +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
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def contributors
|
|
|
|
|
if params[:filepath].present?
|
|
|
|
|
@contributors = []
|
|
|
|
|
else
|
|
|
|
|
@contributors = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier)
|
|
|
|
|
end
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
2021-11-22 13:21:41 +08:00
|
|
|
|
return render_forbidden if !@project.manager?(current_user) && !current_user.admin?
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_file
|
2021-11-22 13:20:23 +08:00
|
|
|
|
interactor = Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params)
|
2021-11-22 13:15:45 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
@file = interactor.result
|
2021-11-22 13:20:23 +08:00
|
|
|
|
# create_new_pr(params)
|
2021-11-22 13:21:41 +08:00
|
|
|
|
#如果是更新流水线文件
|
|
|
|
|
if params[:pipeline_id]
|
|
|
|
|
update_pipeline(params[:pipeline_id])
|
|
|
|
|
end
|
2021-11-22 13:15:45 +08:00
|
|
|
|
else
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:21:41 +08:00
|
|
|
|
def update_pipeline(pipeline_id)
|
|
|
|
|
pipeline = Ci::Pipeline.find(pipeline_id)
|
|
|
|
|
if pipeline
|
|
|
|
|
pipeline.update!(sync: 1)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:15:45 +08:00
|
|
|
|
def update_file
|
2021-11-22 13:20:23 +08:00
|
|
|
|
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, params.merge(identifier: @project.identifier))
|
2021-11-22 13:15:45 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
@file = interactor.result
|
2021-11-22 13:20:23 +08:00
|
|
|
|
# TODO: 是否创建pr
|
|
|
|
|
# create_new_pr(params)
|
2021-11-22 13:15:45 +08:00
|
|
|
|
render_result(1, "更新成功")
|
|
|
|
|
else
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def delete_file
|
2021-11-22 13:20:23 +08:00
|
|
|
|
interactor = Gitea::DeleteFileInteractor.call(current_user.gitea_token, @owner.login, params.merge(identifier: @project.identifier))
|
2021-11-22 13:15:45 +08:00
|
|
|
|
if interactor.success?
|
|
|
|
|
@file = interactor.result
|
|
|
|
|
render_result(1, "文件删除成功")
|
|
|
|
|
else
|
|
|
|
|
render_error(interactor.error)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def repo_hook
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sync_mirror
|
2021-11-22 13:20:23 +08:00
|
|
|
|
return render_error("正在镜像中..") if @repository.mirror.waiting?
|
2021-11-22 13:15:45 +08:00
|
|
|
|
|
2021-11-22 13:20:23 +08:00
|
|
|
|
@repository.sync_mirror!
|
|
|
|
|
SyncMirroredRepositoryJob.perform_later(@repository.id, current_user.id)
|
2021-11-22 13:15:45 +08:00
|
|
|
|
render_ok
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:21:41 +08:00
|
|
|
|
def readme
|
|
|
|
|
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
|
|
|
|
|
@readme = result[:status] === :success ? result[:body] : nil
|
|
|
|
|
@readme['content'] = decode64_content(@readme, @owner, @repository, params[:ref])
|
|
|
|
|
render json: @readme.slice("type", "encoding", "size", "name", "path", "content", "sha")
|
|
|
|
|
rescue
|
|
|
|
|
render json: nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def languages
|
|
|
|
|
render json: languages_precentagable
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
|
|
|
|
|
redirect_to URI.escape(file_path)
|
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:15:45 +08:00
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_project
|
|
|
|
|
@project = Project.find params[:id]
|
|
|
|
|
render_not_found("未找到相关的仓库") unless @project
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_project_with_includes
|
|
|
|
|
@project = Project.includes(:repository, :owner, :watchers, :praise_treads).find params[:id]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authorizate!
|
2021-11-22 13:20:23 +08:00
|
|
|
|
return if current_user && current_user.admin?
|
2021-11-22 13:15:45 +08:00
|
|
|
|
if @project.repository.hidden? && !@project.member?(current_user)
|
|
|
|
|
render_forbidden
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# TODO 获取最新commit信息
|
|
|
|
|
def project_commits
|
2021-11-22 13:21:41 +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
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_statistics
|
2021-11-22 13:20:23 +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
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_ref
|
2021-11-22 13:21:41 +08:00
|
|
|
|
@ref = params[:ref] || @project&.default_branch
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-11-22 13:20:23 +08:00
|
|
|
|
def get_latest_commit
|
|
|
|
|
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
|
2021-11-22 13:15:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def content_params
|
|
|
|
|
{
|
|
|
|
|
filepath: params[:filepath],
|
|
|
|
|
branch: params[:branch],
|
|
|
|
|
new_branch: params[:new_branch],
|
|
|
|
|
content: params[:content],
|
|
|
|
|
message: params[:message],
|
2021-11-22 13:20:23 +08:00
|
|
|
|
committer: {
|
|
|
|
|
email: current_user.mail,
|
|
|
|
|
name: current_user.login
|
|
|
|
|
},
|
2021-11-22 13:15:45 +08:00
|
|
|
|
identifier: @project.identifier
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def hook_params(hook_type, params)
|
|
|
|
|
# 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
|
|
|
|
|
end
|
2021-11-22 13:21:41 +08:00
|
|
|
|
|
2021-11-22 13:15:45 +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,
|
|
|
|
|
priority_id: params[:priority_id] || "2"
|
|
|
|
|
}
|
|
|
|
|
@pull_issue = Issue.new(issue_params)
|
|
|
|
|
if @pull_issue.save!
|
|
|
|
|
local_requests = PullRequest.new(local_params.merge(user_id: current_user.try(:id), project_id: @project.id, issue_id: @pull_issue.id))
|
|
|
|
|
if local_requests.save
|
2021-11-22 13:20:23 +08:00
|
|
|
|
gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @owner.login, @project.try(:identifier), requests_params).call
|
2021-11-22 13:23:10 +08:00
|
|
|
|
if gitea_request[:status] == :success && local_requests.update_attributes(gitea_number: gitea_request["body"]["number"])
|
2021-11-22 13:15:45 +08:00
|
|
|
|
local_requests.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|