From 3e2b417cce64a3dbc7d2a5cf6f429b691598285d Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Fri, 20 Mar 2020 20:15:43 +0800 Subject: [PATCH] FIX update repositories api --- app/controllers/repositories_controller.rb | 73 ++++++++++++++----- app/forms/contents/create_form.rb | 4 +- app/forms/contents/delete_form.rb | 4 +- app/forms/contents/update_form.rb | 4 +- .../gitea/create_file_interactor.rb | 4 +- .../gitea/delete_file_interactor.rb | 4 +- .../gitea/update_file_interactor.rb | 4 +- app/views/repositories/commits.json.jbuilder | 2 +- app/views/repositories/edit.json.jbuilder | 16 ++-- app/views/repositories/entries.json.jbuilder | 2 +- app/views/repositories/show.json.jbuilder | 12 +-- config/routes.rb | 16 ++-- 12 files changed, 83 insertions(+), 62 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index e0d65a3a5..e47f1fde5 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -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 diff --git a/app/forms/contents/create_form.rb b/app/forms/contents/create_form.rb index e0c7c8522..6a7003fd6 100644 --- a/app/forms/contents/create_form.rb +++ b/app/forms/contents/create_form.rb @@ -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 diff --git a/app/forms/contents/delete_form.rb b/app/forms/contents/delete_form.rb index f3f060ab6..b459ec4e7 100644 --- a/app/forms/contents/delete_form.rb +++ b/app/forms/contents/delete_form.rb @@ -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 diff --git a/app/forms/contents/update_form.rb b/app/forms/contents/update_form.rb index f5b4a2480..8600f0fde 100644 --- a/app/forms/contents/update_form.rb +++ b/app/forms/contents/update_form.rb @@ -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 diff --git a/app/interactors/gitea/create_file_interactor.rb b/app/interactors/gitea/create_file_interactor.rb index 9ab839c8b..b9e8ba574 100644 --- a/app/interactors/gitea/create_file_interactor.rb +++ b/app/interactors/gitea/create_file_interactor.rb @@ -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] diff --git a/app/interactors/gitea/delete_file_interactor.rb b/app/interactors/gitea/delete_file_interactor.rb index 255ce6d97..d68988d19 100644 --- a/app/interactors/gitea/delete_file_interactor.rb +++ b/app/interactors/gitea/delete_file_interactor.rb @@ -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] } diff --git a/app/interactors/gitea/update_file_interactor.rb b/app/interactors/gitea/update_file_interactor.rb index 814bc0cc7..af895d481 100644 --- a/app/interactors/gitea/update_file_interactor.rb +++ b/app/interactors/gitea/update_file_interactor.rb @@ -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], diff --git a/app/views/repositories/commits.json.jbuilder b/app/views/repositories/commits.json.jbuilder index 396efbfc0..d3fdae953 100644 --- a/app/views/repositories/commits.json.jbuilder +++ b/app/views/repositories/commits.json.jbuilder @@ -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 diff --git a/app/views/repositories/edit.json.jbuilder b/app/views/repositories/edit.json.jbuilder index fcd02a52e..8426e14d6 100644 --- a/app/views/repositories/edit.json.jbuilder +++ b/app/views/repositories/edit.json.jbuilder @@ -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 diff --git a/app/views/repositories/entries.json.jbuilder b/app/views/repositories/entries.json.jbuilder index 182376c1b..75dd197f0 100644 --- a/app/views/repositories/entries.json.jbuilder +++ b/app/views/repositories/entries.json.jbuilder @@ -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'] diff --git a/app/views/repositories/show.json.jbuilder b/app/views/repositories/show.json.jbuilder index e79002ded..ea4eeae1f 100644 --- a/app/views/repositories/show.json.jbuilder +++ b/app/views/repositories/show.json.jbuilder @@ -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 } diff --git a/config/routes.rb b/config/routes.rb index ae8262a13..7248fc5f0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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