FIX update repositories api

This commit is contained in:
Jasder 2020-03-20 20:15:43 +08:00
parent 551bae911e
commit 3e2b417cce
12 changed files with 83 additions and 62 deletions

View File

@ -1,14 +1,12 @@
class RepositoriesController < ApplicationController
include ApplicationHelper
before_action :find_project_identifier
before_action :find_repository_with_project
before_action :find_user, :authorizate!
before_action :require_login, only: %i[edit]
before_action :require_login, only: %i[edit update create_file update_file delete_file]
before_action :find_project, :authorizate!
def show
@branches_count = Gitea::Repository::BranchesService.new(@user, @repo.identifier).call&.size
@commits_count = Gitea::Repository::Commits::ListService.new(@user, @repo.identifier).call[:total_count]
@result = Gitea::Repository::GetService.new(@user, @repo.identifier).call
@branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size
@commits_count = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier).call[:total_count]
@result = Gitea::Repository::GetService.new(@project.owner, @project.identifier).call
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
@ -17,13 +15,13 @@ class RepositoriesController < ApplicationController
def entries
@project.increment!(:visits)
@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'] }
end
def sub_entries
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?
@sub_entries = interactor.result
@sub_entries = [] << @sub_entries unless @sub_entries.is_a? Array
@ -34,34 +32,69 @@ class RepositoriesController < ApplicationController
end
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
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
def tags
@tags = Gitea::Repository::Tags::ListService.new(@user, @repo.identifier).call
@tags = Gitea::Repository::Tags::ListService.new(@project, @project.identifier).call
end
def edit
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
def find_project
@project = Project.find params[:id]
# render_not_found("未找到相关的仓库") unless @project
end
def authorizate!
if @repo.hidden? && @repo.user != current_user
if @project.repository.hidden? && !@project.member?(current_user)
render_forbidden
end
end
def find_project_identifier
@project = Project.find_by(id: params[:repo_identifier])
render_not_found("未找到’#{params[:repo_identifier]}’相关的项目") unless @project
def content_params
{
filepath: params[:filepath],
branch: params[:branch],
new_branch: params[:new_branch],
content: params[:content],
message: params[:message],
identifier: @project.identifier
}
end
def find_repository_with_project
@repo = @project.repository
render_not_found("未找到’#{params[:repo_identifier]}’相关的仓库") unless @repo
end
end

View File

@ -1,7 +1,7 @@
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

View File

@ -1,6 +1,6 @@
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

View File

@ -1,7 +1,7 @@
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

View File

@ -23,7 +23,7 @@ module Gitea
def run
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)
rescue Exception => exception
Rails.logger.info "Exception ===========> #{exception.message}"
@ -45,8 +45,6 @@ module Gitea
def valid_params
{
login: @params[:login],
repo_identifier: @params[:repo_identifier],
filepath: @params[:filepath],
branch: @params[:branch],
new_branch: @params[:new_branch]

View File

@ -23,7 +23,7 @@ module Gitea
def run
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)
rescue Exception => exception
fail!(exception.message)
@ -46,8 +46,6 @@ module Gitea
def valid_params
{
login: @params[:login],
repo_identifier: @params[:repo_identifier],
filepath: @params[:filepath],
sha: @params[:sha]
}

View File

@ -23,7 +23,7 @@ module Gitea
def run
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)
rescue Exception => exception
fail!(exception.message)
@ -46,8 +46,6 @@ module Gitea
def valid_params
{
login: @params[:login],
repo_identifier: @params[:repo_identifier],
filepath: @params[:filepath],
branch: @params[:branch],
new_branch: @params[:new_branch],

View File

@ -5,6 +5,6 @@ json.commits do
json.message commit['commit']['message']
json.timestamp render_unix_time(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

View File

@ -1,8 +1,8 @@
json.identifier @repo.identifier
json.project_id @repo&.project.id
json.project_name @repo&.project.name
json.project_identifier @repo.project.identifier
json.project_description @repo&.project.description
json.project_category_id @repo&.project.project_category_id
json.project_language_id @repo&.project.project_language_id
json.private @repo.hidden
json.identifier @project.identifier
json.project_id @project.id
json.project_name @rproject.name
json.project_identifier @rproject.identifier
json.project_description @project.description
json.project_category_id @project.project_category_id
json.project_language_id @project.project_language_id
json.private @rhidden

View File

@ -9,7 +9,7 @@ json.array! @entries do |entry|
# json.commit entry['commit']
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.path readme_md['path']
json.sha readme_md['sha']

View File

@ -1,15 +1,15 @@
json.identifier @repo.identifier
json.identifier @project.identifier
json.project_id @project.id
json.issues_count @project.issues_count
json.pull_requests_count @project.pull_requests_count
json.project_identifier @project.identifier
json.praises_count @repo.project.praises_count
json.forked_count @repo.project.forked_count
json.watchers_count @repo.project.watchers_count
json.praises_count @rproject.praises_count
json.forked_count @rproject.forked_count
json.watchers_count @project.watchers_count
json.branches_count @branches_count
json.commits_count @commits_count
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.praised current_user&.liked?(@project)
json.size @result['size']
@ -20,4 +20,4 @@ json.empty @result['empty']
json.full_name @result['full_name']
json.mirror @result['mirror']
json.private @result['private']
json.partial! 'author', locals: { user: @repo.user }
json.partial! 'author', locals: { user: @project.user }

View File

@ -180,23 +180,17 @@ Rails.application.routes.draw do
end
end
get '/:login/:repo_identifier', to: 'repositories#show'
get '/:login/:repo_identifier/edit', to: 'repositories#edit'
resources :repositories, path: '/:login/:repo_identifier', only: [:index] do
collection do
resources :repositories, only: [:index, :show, :edit] do
member do
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
get :single_commit
post :files
get :tags
end
end
resources :contents, path: '/:login/:repo_identifier/contents', only: [:create] do
collection do
put 'files/update', :action => 'update_file'
delete 'files/delete', :action => 'delete_file'
post :create_file
put :update_file
delete :delete_file
end
end