From 855c51a470d91594224db6aa21a5845245c0abc7 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 18 Jun 2021 16:20:50 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20=E8=A7=A3=E5=86=B3=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E4=BF=A1=E6=81=AF=E4=B8=8D=E5=87=86=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitea/create_file_interactor.rb | 10 ++++++++-- app/services/gitea/client_service.rb | 8 ++++++++ .../repository/entries/create_service.rb | 20 +++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/interactors/gitea/create_file_interactor.rb b/app/interactors/gitea/create_file_interactor.rb index acceeaa97..0da781b64 100644 --- a/app/interactors/gitea/create_file_interactor.rb +++ b/app/interactors/gitea/create_file_interactor.rb @@ -24,8 +24,14 @@ module Gitea def run Contents::CreateForm.new(valid_params).validate! - response = Gitea::Repository::Entries::CreateService.new(token, owner, @params[:identifier], @params[:filepath], file_params).call - render_result(response) + result = Gitea::Repository::Entries::CreateService.call(token, + owner, @params[:identifier], @params[:filepath], file_params) + + if result[:status] == :success + @result = result[:body] + else + fail!(result[:message]) + end rescue Exception => exception Rails.logger.info "Exception ===========> #{exception.message}" fail!(exception.message) diff --git a/app/services/gitea/client_service.rb b/app/services/gitea/client_service.rb index 0ef041997..b88d8382a 100644 --- a/app/services/gitea/client_service.rb +++ b/app/services/gitea/client_service.rb @@ -214,6 +214,14 @@ class Gitea::ClientService < ApplicationService [body, message] end + def json_parse!(body) + return nil unless body.present? + + body = JSON.parse(body) + body, message = fix_body(body) + body + end + def log_error(status, body) puts "[gitea] status: #{status}" puts "[gitea] body: #{body&.force_encoding('UTF-8')}" diff --git a/app/services/gitea/repository/entries/create_service.rb b/app/services/gitea/repository/entries/create_service.rb index 9f5abad2a..5e1a4c4b3 100644 --- a/app/services/gitea/repository/entries/create_service.rb +++ b/app/services/gitea/repository/entries/create_service.rb @@ -30,8 +30,7 @@ class Gitea::Repository::Entries::CreateService < Gitea::ClientService def call response = post(url, params) - - render_201_response(response) + response_payload(response) end private @@ -43,4 +42,21 @@ class Gitea::Repository::Entries::CreateService < Gitea::ClientService "/repos/#{owner}/#{repo_name}/contents/#{filepath}".freeze end + def response_payload(response) + status = response.status + body = response&.body + + log_error(status, body) + status_payload(status, body) + end + + def status_payload(status, body) + case status + when 201 then success(json_parse!(body)) + when 403 then error("你没有权限操作!") + when 404 then error("你操作的链接不存在!") + when 422 then error("#{filepath}文件已存在,不能重复创建!") + else error("系统错误!") + end + end end