add: gitea response process

This commit is contained in:
vilet.yy 2021-04-30 15:36:14 +08:00
parent b22330d146
commit a9c0d18a5f
5 changed files with 32 additions and 6 deletions

View File

@ -149,12 +149,12 @@ class PullRequestsController < ApplicationController
begin begin
result = PullRequests::MergeService.call(@owner, @repository, @pull_request, current_user, params) result = PullRequests::MergeService.call(@owner, @repository, @pull_request, current_user, params)
if result && @pull_request.merge! if result.status == 200 && @pull_request.merge!
@pull_request.project_trend_status! @pull_request.project_trend_status!
@issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) @issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id)
normal_status(1, "合并成功") normal_status(1, "合并成功")
else else
normal_status(-1, "合并失败") normal_status(-1, result.message)
end end
rescue => e rescue => e
normal_status(-1, e.message) normal_status(-1, e.message)

View File

@ -176,6 +176,25 @@ class Gitea::ClientService < ApplicationService
[status, message, body] [status, message, body]
end end
def render_gitea_response(response)
status = response.status
body = response&.body
log_error(status, body)
message = nil
begin
translate = YAML.load(File.read('config/gitea_response.yml'))
self.class.to_s.underscore.split("/").map{|i| translate=translate[i]}
message = body.nil? ? translate[status]['default'] : JSON.parse(body)['message']
message = translate[status][message].nil? ? message : translate[status][message]
return [status, message]
rescue
return [status, message]
end
end
def get_body_by_status(status, body) def get_body_by_status(status, body)
body, message = body, message =
case status case status

View File

@ -20,7 +20,7 @@ class Gitea::PullRequest::MergeService < Gitea::ClientService
def call def call
response = post(url, request_params) response = post(url, request_params)
render_200_no_body(response) render_gitea_response(response)
end end
private private

View File

@ -1,6 +1,6 @@
class PullRequests::MergeService < ApplicationService class PullRequests::MergeService < ApplicationService
attr_reader :owner, :repo, :pull, :current_user, :params attr_reader :owner, :repo, :pull, :current_user, :params
attr_accessor :status, :message
# eq: # eq:
# PullRequests::MergeService.call(owner, repo, pull, current_user, params) # PullRequests::MergeService.call(owner, repo, pull, current_user, params)
def initialize(owner, repo, pull, current_user, params) def initialize(owner, repo, pull, current_user, params)
@ -15,6 +15,7 @@ class PullRequests::MergeService < ApplicationService
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
gitea_pull_merge! gitea_pull_merge!
end end
self
end end
private private
@ -22,8 +23,7 @@ class PullRequests::MergeService < ApplicationService
def gitea_pull_merge! def gitea_pull_merge!
result = Gitea::PullRequest::MergeService.call(@current_user.gitea_token, @owner.login, result = Gitea::PullRequest::MergeService.call(@current_user.gitea_token, @owner.login,
@repo.identifier, @pull.gpid, gitea_merge_pull_params) @repo.identifier, @pull.gpid, gitea_merge_pull_params)
@status, @message = result
result[:status] === 200 ? true : false
end end
def gitea_merge_pull_params def gitea_merge_pull_params

View File

@ -0,0 +1,7 @@
gitea:
pull_request:
merge_service:
405:
default: "此合并请求有变更与目标分支冲突。"
'User not allowed to merge PR': "用户没有合并请求的权限"
403: