FIX update repositories api
This commit is contained in:
parent
551bae911e
commit
3e2b417cce
|
@ -1,14 +1,12 @@
|
||||||
class RepositoriesController < ApplicationController
|
class RepositoriesController < ApplicationController
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
before_action :find_project_identifier
|
before_action :require_login, only: %i[edit update create_file update_file delete_file]
|
||||||
before_action :find_repository_with_project
|
before_action :find_project, :authorizate!
|
||||||
before_action :find_user, :authorizate!
|
|
||||||
before_action :require_login, only: %i[edit]
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@branches_count = Gitea::Repository::BranchesService.new(@user, @repo.identifier).call&.size
|
@branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size
|
||||||
@commits_count = Gitea::Repository::Commits::ListService.new(@user, @repo.identifier).call[:total_count]
|
@commits_count = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier).call[:total_count]
|
||||||
@result = Gitea::Repository::GetService.new(@user, @repo.identifier).call
|
@result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
uid_logger_error(e.message)
|
uid_logger_error(e.message)
|
||||||
tip_exception(e.message)
|
tip_exception(e.message)
|
||||||
|
@ -17,13 +15,13 @@ class RepositoriesController < ApplicationController
|
||||||
def entries
|
def entries
|
||||||
@project.increment!(:visits)
|
@project.increment!(:visits)
|
||||||
@ref = params[:branch] || "master"
|
@ref = params[:branch] || "master"
|
||||||
@entries = Gitea::Repository::Entries::ListService.new(@user, @repo.identifier, ref:@ref).call
|
@entries = Gitea::Repository::Entries::ListService.new(@project.owner, @project.identifier, ref:@ref).call
|
||||||
@entries = @entries.sort_by{ |hash| hash['type'] }
|
@entries = @entries.sort_by{ |hash| hash['type'] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def sub_entries
|
def sub_entries
|
||||||
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
file_path_uri = URI.parse(URI.encode(params[:filepath].to_s.strip))
|
||||||
interactor = Repositories::EntriesInteractor.call(@user, @repo.identifier, file_path_uri, ref: params[:ref])
|
interactor = Repositories::EntriesInteractor.call(@project.owner, @project.identifier, file_path_uri, ref: params[:ref])
|
||||||
if interactor.success?
|
if interactor.success?
|
||||||
@sub_entries = interactor.result
|
@sub_entries = interactor.result
|
||||||
@sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array
|
@sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array
|
||||||
|
@ -34,34 +32,69 @@ class RepositoriesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def commits
|
def commits
|
||||||
@hash_commit = Gitea::Repository::Commits::ListService.new(@user, @repo.identifier, sha: params[:sha], page: params[:page]).call
|
@hash_commit = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier, sha: params[:sha], page: params[:page]).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def single_commit
|
def single_commit
|
||||||
@commit = Gitea::Repository::Commits::GetService.new(@user, @repo.identifier, params[:sha]).call
|
@commit = Gitea::Repository::Commits::GetService.new(@project.owner, @project.identifier, params[:sha]).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
@tags = Gitea::Repository::Tags::ListService.new(@user, @repo.identifier).call
|
@tags = Gitea::Repository::Tags::ListService.new(@project, @project.identifier).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_file
|
||||||
|
interactor = Gitea::CreateFileInteractor.call(current_user, content_params)
|
||||||
|
if interactor.success?
|
||||||
|
@file = interactor.result
|
||||||
|
else
|
||||||
|
render_error(interactor.error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_file
|
||||||
|
interactor = Gitea::UpdateFileInteractor.call(current_user, params.merge(identifier: @project.identifier).compact)
|
||||||
|
if interactor.success?
|
||||||
|
@file = interactor.result
|
||||||
|
else
|
||||||
|
render_error(interactor.error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_file
|
||||||
|
interactor = Gitea::DeleteFileInteractor.call(current_user, params.merge(identifier: @project.identifier).compact)
|
||||||
|
if interactor.success?
|
||||||
|
@file = interactor.result
|
||||||
|
else
|
||||||
|
render_error(interactor.error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def find_project
|
||||||
|
@project = Project.find params[:id]
|
||||||
|
# render_not_found("未找到相关的仓库") unless @project
|
||||||
|
end
|
||||||
|
|
||||||
def authorizate!
|
def authorizate!
|
||||||
if @repo.hidden? && @repo.user != current_user
|
if @project.repository.hidden? && !@project.member?(current_user)
|
||||||
render_forbidden
|
render_forbidden
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_project_identifier
|
def content_params
|
||||||
@project = Project.find_by(id: params[:repo_identifier])
|
{
|
||||||
render_not_found("未找到’#{params[:repo_identifier]}’相关的项目") unless @project
|
filepath: params[:filepath],
|
||||||
|
branch: params[:branch],
|
||||||
|
new_branch: params[:new_branch],
|
||||||
|
content: params[:content],
|
||||||
|
message: params[:message],
|
||||||
|
identifier: @project.identifier
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_repository_with_project
|
|
||||||
@repo = @project.repository
|
|
||||||
render_not_found("未找到’#{params[:repo_identifier]}’相关的仓库") unless @repo
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class Contents::CreateForm < BaseForm
|
class Contents::CreateForm < BaseForm
|
||||||
attr_accessor :login, :repo_identifier, :filepath, :branch, :new_branch
|
attr_accessor :filepath, :branch, :new_branch
|
||||||
|
|
||||||
validates :login, :repo_identifier, :filepath, presence: true
|
validates :filepath, presence: true
|
||||||
|
|
||||||
validate :check_branch
|
validate :check_branch
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class Contents::DeleteForm < BaseForm
|
class Contents::DeleteForm < BaseForm
|
||||||
attr_accessor :login, :repo_identifier, :filepath, :branch, :new_branch, :sha
|
attr_accessor :filepath, :branch, :new_branch, :sha
|
||||||
|
|
||||||
validates :login, :repo_identifier, :filepath, :sha, presence: true
|
validates :filepath, :sha, presence: true
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class Contents::UpdateForm < BaseForm
|
class Contents::UpdateForm < BaseForm
|
||||||
attr_accessor :login, :repo_identifier, :filepath, :branch, :new_branch, :sha
|
attr_accessor :filepath, :branch, :new_branch, :sha
|
||||||
|
|
||||||
validates :login, :repo_identifier, :filepath, :sha, presence: true
|
validates :filepath, :sha, presence: true
|
||||||
|
|
||||||
validate :check_branch
|
validate :check_branch
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ module Gitea
|
||||||
|
|
||||||
def run
|
def run
|
||||||
Contents::CreateForm.new(valid_params).validate!
|
Contents::CreateForm.new(valid_params).validate!
|
||||||
response = Gitea::Repository::Entries::CreateService.new(user, @params[:repo_identifier], @params[:filepath], file_params).call
|
response = Gitea::Repository::Entries::CreateService.new(user, @params[:identifier], @params[:filepath], file_params).call
|
||||||
render_result(response)
|
render_result(response)
|
||||||
rescue Exception => exception
|
rescue Exception => exception
|
||||||
Rails.logger.info "Exception ===========> #{exception.message}"
|
Rails.logger.info "Exception ===========> #{exception.message}"
|
||||||
|
@ -45,8 +45,6 @@ module Gitea
|
||||||
|
|
||||||
def valid_params
|
def valid_params
|
||||||
{
|
{
|
||||||
login: @params[:login],
|
|
||||||
repo_identifier: @params[:repo_identifier],
|
|
||||||
filepath: @params[:filepath],
|
filepath: @params[:filepath],
|
||||||
branch: @params[:branch],
|
branch: @params[:branch],
|
||||||
new_branch: @params[:new_branch]
|
new_branch: @params[:new_branch]
|
||||||
|
|
|
@ -23,7 +23,7 @@ module Gitea
|
||||||
|
|
||||||
def run
|
def run
|
||||||
Contents::DeleteForm.new(valid_params).validate!
|
Contents::DeleteForm.new(valid_params).validate!
|
||||||
response = Gitea::Repository::Entries::DeleteService.new(user, @params[:repo_identifier], @params[:filepath], file_params).call
|
response = Gitea::Repository::Entries::DeleteService.new(user, @params[:identifier], @params[:filepath], file_params).call
|
||||||
render_result(response)
|
render_result(response)
|
||||||
rescue Exception => exception
|
rescue Exception => exception
|
||||||
fail!(exception.message)
|
fail!(exception.message)
|
||||||
|
@ -46,8 +46,6 @@ module Gitea
|
||||||
|
|
||||||
def valid_params
|
def valid_params
|
||||||
{
|
{
|
||||||
login: @params[:login],
|
|
||||||
repo_identifier: @params[:repo_identifier],
|
|
||||||
filepath: @params[:filepath],
|
filepath: @params[:filepath],
|
||||||
sha: @params[:sha]
|
sha: @params[:sha]
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ module Gitea
|
||||||
|
|
||||||
def run
|
def run
|
||||||
Contents::UpdateForm.new(valid_params).validate!
|
Contents::UpdateForm.new(valid_params).validate!
|
||||||
response = Gitea::Repository::Entries::UpdateService.new(user, @params[:repo_identifier], @params[:filepath], file_params).call
|
response = Gitea::Repository::Entries::UpdateService.new(user, @params[:identifier], @params[:filepath], file_params).call
|
||||||
render_result(response)
|
render_result(response)
|
||||||
rescue Exception => exception
|
rescue Exception => exception
|
||||||
fail!(exception.message)
|
fail!(exception.message)
|
||||||
|
@ -46,8 +46,6 @@ module Gitea
|
||||||
|
|
||||||
def valid_params
|
def valid_params
|
||||||
{
|
{
|
||||||
login: @params[:login],
|
|
||||||
repo_identifier: @params[:repo_identifier],
|
|
||||||
filepath: @params[:filepath],
|
filepath: @params[:filepath],
|
||||||
branch: @params[:branch],
|
branch: @params[:branch],
|
||||||
new_branch: @params[:new_branch],
|
new_branch: @params[:new_branch],
|
||||||
|
|
|
@ -5,6 +5,6 @@ json.commits do
|
||||||
json.message commit['commit']['message']
|
json.message commit['commit']['message']
|
||||||
json.timestamp render_unix_time(commit['commit']['author']['date'])
|
json.timestamp render_unix_time(commit['commit']['author']['date'])
|
||||||
json.time_from_now time_from_now(commit['commit']['author']['date'])
|
json.time_from_now time_from_now(commit['commit']['author']['date'])
|
||||||
json.partial! 'author', user: @repo.user
|
json.partial! 'author', user: @project.user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
json.identifier @repo.identifier
|
json.identifier @project.identifier
|
||||||
json.project_id @repo&.project.id
|
json.project_id @project.id
|
||||||
json.project_name @repo&.project.name
|
json.project_name @rproject.name
|
||||||
json.project_identifier @repo.project.identifier
|
json.project_identifier @rproject.identifier
|
||||||
json.project_description @repo&.project.description
|
json.project_description @project.description
|
||||||
json.project_category_id @repo&.project.project_category_id
|
json.project_category_id @project.project_category_id
|
||||||
json.project_language_id @repo&.project.project_language_id
|
json.project_language_id @project.project_language_id
|
||||||
json.private @repo.hidden
|
json.private @rhidden
|
||||||
|
|
|
@ -9,7 +9,7 @@ json.array! @entries do |entry|
|
||||||
# json.commit entry['commit']
|
# json.commit entry['commit']
|
||||||
|
|
||||||
if entry['name'] == "README.md"
|
if entry['name'] == "README.md"
|
||||||
readme_md = Gitea::Repository::Entries::GetService.new(@user, @repo.identifier, entry['path'], ref:@ref).call
|
readme_md = Gitea::Repository::Entries::GetService.new(@project.owner, @project.identifier, entry['path'], ref:@ref).call
|
||||||
json.name readme_md['name']
|
json.name readme_md['name']
|
||||||
json.path readme_md['path']
|
json.path readme_md['path']
|
||||||
json.sha readme_md['sha']
|
json.sha readme_md['sha']
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
json.identifier @repo.identifier
|
json.identifier @project.identifier
|
||||||
json.project_id @project.id
|
json.project_id @project.id
|
||||||
json.issues_count @project.issues_count
|
json.issues_count @project.issues_count
|
||||||
json.pull_requests_count @project.pull_requests_count
|
json.pull_requests_count @project.pull_requests_count
|
||||||
json.project_identifier @project.identifier
|
json.project_identifier @project.identifier
|
||||||
json.praises_count @repo.project.praises_count
|
json.praises_count @rproject.praises_count
|
||||||
json.forked_count @repo.project.forked_count
|
json.forked_count @rproject.forked_count
|
||||||
json.watchers_count @repo.project.watchers_count
|
json.watchers_count @project.watchers_count
|
||||||
json.branches_count @branches_count
|
json.branches_count @branches_count
|
||||||
json.commits_count @commits_count
|
json.commits_count @commits_count
|
||||||
json.permission render_edit_project_permission(current_user, @project)
|
json.permission render_edit_project_permission(current_user, @project)
|
||||||
json.mirror_url @repo.mirror_url
|
json.mirror_url @project&.repository.mirror_url
|
||||||
json.watched current_user&.watched?(@project)
|
json.watched current_user&.watched?(@project)
|
||||||
json.praised current_user&.liked?(@project)
|
json.praised current_user&.liked?(@project)
|
||||||
json.size @result['size']
|
json.size @result['size']
|
||||||
|
@ -20,4 +20,4 @@ json.empty @result['empty']
|
||||||
json.full_name @result['full_name']
|
json.full_name @result['full_name']
|
||||||
json.mirror @result['mirror']
|
json.mirror @result['mirror']
|
||||||
json.private @result['private']
|
json.private @result['private']
|
||||||
json.partial! 'author', locals: { user: @repo.user }
|
json.partial! 'author', locals: { user: @project.user }
|
||||||
|
|
|
@ -180,23 +180,17 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/:login/:repo_identifier', to: 'repositories#show'
|
resources :repositories, only: [:index, :show, :edit] do
|
||||||
get '/:login/:repo_identifier/edit', to: 'repositories#edit'
|
member do
|
||||||
resources :repositories, path: '/:login/:repo_identifier', only: [:index] do
|
|
||||||
collection do
|
|
||||||
get :entries
|
get :entries
|
||||||
match :sub_entries, :via => [:get, :put]
|
match :sub_entries, :via => [:get, :put]
|
||||||
get :commits
|
get :commits
|
||||||
get :single_commit
|
get :single_commit
|
||||||
post :files
|
post :files
|
||||||
get :tags
|
get :tags
|
||||||
end
|
post :create_file
|
||||||
end
|
put :update_file
|
||||||
|
delete :delete_file
|
||||||
resources :contents, path: '/:login/:repo_identifier/contents', only: [:create] do
|
|
||||||
collection do
|
|
||||||
put 'files/update', :action => 'update_file'
|
|
||||||
delete 'files/delete', :action => 'delete_file'
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue