FIX code review for create pull request

This commit is contained in:
Jasder 2020-12-23 15:46:06 +08:00
parent 6cbaf410b2
commit ffb71512fc
6 changed files with 175 additions and 71 deletions

View File

@ -40,8 +40,5 @@ class PraiseTreadController < ApplicationController
end
private
def render_result
end
end

View File

@ -23,7 +23,7 @@ class PullRequestsController < ApplicationController
end
def new
@all_branches = PullRequests::BranchesService.new(@owner, @project).call
@all_branches = Branches::ListService.call(@owner, @project)
@is_fork = @project.forked_from_project_id.present?
@projects_names = [{
project_user_login: @owner.try(:login),
@ -44,66 +44,20 @@ class PullRequestsController < ApplicationController
end
def get_branches
branch_result = PullRequests::BranchesService.new(@owner, @project).call
branch_result = Branches::ListService.call(@owner, @project)
render json: branch_result
# return json: branch_result
end
def create
if params[:title].nil?
normal_status(-1, "名称不能为空")
elsif params[:issue_tag_ids].nil?
normal_status(-1, "标签不能为空")
else
ActiveRecord::Base.transaction do
begin
merge_params
pull_issue = Issue.new(@issue_params)
if pull_issue.save!
pr_params = {
user_id: current_user.try(:id),
project_id: @project.id,
issue_id: pull_issue.id,
fork_project_id: params[:fork_project_id],
is_original: params[:is_original],
files_count: params[:files_count] || 0,
commits_count: params[:commits_count] || 0
}
local_requests = PullRequest.new(@local_params.merge(pr_params))
if local_requests.save
remote_pr_params = @local_params
remote_pr_params = remote_pr_params.merge(head: "#{params[:merge_user_login]}:#{params[:head]}").compact if local_requests.is_original && params[:merge_user_login]
gitea_request = Gitea::PullRequest::CreateService.call(current_user.try(:gitea_token), @project.owner, @repository.try(:identifier), remote_pr_params.except(:milestone))
if gitea_request && local_requests.update_attributes(gpid: gitea_request["number"])
if params[:issue_tag_ids].present?
params[:issue_tag_ids].each do |tag|
IssueTagsRelate.create!(issue_id: pull_issue.id, issue_tag_id: tag)
end
end
if params[:assigned_to_id].present?
Tiding.create!(user_id: params[:assigned_to_id], trigger_user_id: current_user.id,
container_id: local_requests.id, container_type: 'PullRequest',
parent_container_id: @project.id, parent_container_type: "Project",
tiding_type: 'pull_request', status: 0)
end
local_requests.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
if params[:title].to_s.include?("WIP:")
pull_issue.custom_journal_detail("WIP", "", "这个合并请求被标记为尚未完成的工作。完成后请从标题中移除WIP:前缀。", current_user&.id)
end
# render :json => { status: 0, message: "PullRequest创建成功", id: pull_issue.id}
normal_status(0, "PullRequest创建成功")
else
normal_status(-1, "PullRequest创建失败")
end
else
normal_status(-1, "PullRequest创建失败")
end
end
rescue => e
normal_status(-1, e.message)
raise ActiveRecord::Rollback
end
ActiveRecord::Base.transaction do
@pull_request, @gitea_pull_request = PullRequests::CreateService.call(current_user, @owner, @project, params)
if @gitea_pull_request[:status] == :success
@pull_request.bind_gitea_pull_request!(@gitea_pull_request[:body]["number"])
render_ok
else
render_error("create pull request error: #{@gitea_pull_request[:status]}")
raise ActiveRecord::Rollback
end
end
end

View File

@ -243,8 +243,8 @@ class RepositoriesController < ApplicationController
if @pull_issue.save!
local_requests = PullRequest.new(local_params.merge(user_id: current_user.try(:id), project_id: @project.id, issue_id: @pull_issue.id))
if local_requests.save
gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @project.owner, @project.try(:identifier), requests_params).call
if gitea_request && local_requests.update_attributes(gpid: gitea_request["number"])
gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @owner.login, @project.try(:identifier), requests_params).call
if gitea_request[:status] == :success && local_requests.update_attributes(gpid: gitea_request["body"]["number"])
local_requests.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
end
end

View File

@ -1,4 +1,4 @@
class PullRequests::BranchesService < ApplicationService
class Branches::ListService < ApplicationService
attr_reader :user, :project
@ -31,4 +31,4 @@ class PullRequests::BranchesService < ApplicationService
return branches_array
end
end
end

View File

@ -1,9 +1,9 @@
class Gitea::PullRequest::CreateService < Gitea::ClientService
attr_reader :token, :user, :repo, :params
attr_reader :token, :owner, :repo, :params
# 同一个项目下发送pr例子如下
# 参数说明:
# user: 项目拥有者
# owner: 项目拥有者
# repo 项目名称
# params:
# {
@ -17,7 +17,7 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService
# fork的项目向源项目发送pr例子如下
# 参数说明:
# user源项目拥有者
# owner源项目拥有者
# repo源项目仓库名称
# params:
# {
@ -28,24 +28,31 @@ class Gitea::PullRequest::CreateService < Gitea::ClientService
# }
# 以上例子说明jasder用户fork的项目master分支向源项目的develop分支发送pr
# Gitea::PullRequest::CreateService.call('token', '源项目拥有者', '源项目名称', params)
def initialize(token, user, repo, params={})
@token = token
@user = user
def initialize(token, owner, repo, params={})
@token = token
@owner = owner
@repo = repo
@params = params
end
def call
post(url, request_params)
response = post(url, request_params)
json_format(response)
end
private
def url
"/repos/#{@user.login}/#{@repo}/pulls".freeze
"/repos/#{@owner}/#{@repo}/pulls".freeze
end
def request_params
Hash.new.merge(token: token, data: @params)
end
def json_format(response)
status, message, body = render_response(response)
status === 201 ? success(body) : error(message, status)
end
end

View File

@ -0,0 +1,146 @@
class PullRequests::CreateService < ApplicationService
attr_reader :current_user, :owner, :project, :params
def initialize(current_user, owner, project, params)
@owner = owner
@project = project
@params = params
@current_user = current_user
end
def call
validate!
save_pull_issue!
save_pull_request!
save_issue_tags_relates!
save_tiding!
save_project_trend!
save_custom_journal_detail!
[pull_request, gitea_pull_request]
end
def pull_issue_params
{
user: @current_user,
project: @project,
subject: @params[:title],
description: @params[:body],
assigned_to_id: @params[:assigned_to_id],
fixed_version_id: @params[:fixed_version_id],
issue_tags_value: @params[:issue_tag_ids].present? ? @params[:issue_tag_ids].join(",") : "",
priority_id: @params[:priority_id] || "2",
issue_classify: "pull_request",
issue_type: @params[:issue_type] || "1",
tracker_id: 2,
status_id: 1,
}
end
def pull_issue
@pull_issue ||= Issue.new(pull_issue_params.compact)
end
def save_pull_issue!
pull_issue.save
end
def pull_request
@pull_request ||= @project.pull_requests.new(pull_request_params.compact)
end
def save_pull_request!
pull_request.save
end
def save_issue_tags_relates!
issue_tag_ids.each do |tag|
IssueTagsRelate.create!(issue_id: pull_issue.id, issue_tag_id: tag)
end
end
def issue_tag_ids
Array(@params[:issue_tag_ids])
end
def save_tiding!
if @params[:assigned_to_id].present?
Tiding.create!(user_id: @params[:assigned_to_id],
trigger_user: @current_user,
container: pull_request,
parent_container: @project,
tiding_type: 'pull_request',
status: 0)
end
end
def save_project_trend!
project_trend.save
end
def project_trend
@project_trend ||= pull_request.project_trends.new(
user: @current_user,
project: @project,
action_type: "create")
end
def pull_request_params
base_pull_params.merge({
user: @current_user,
issue: pull_issue,
fork_project_id: @params[:fork_project_id],
is_original: @params[:is_original],
files_count: @params[:files_count] || 0,
commits_count: @params[:commits_count] || 0
})
end
def save_custom_journal_detail!
if @params[:title].to_s.include?("WIP:")
pull_issue.custom_journal_detail("WIP", "", "这个合并请求被标记为尚未完成的工作。完成后请从标题中移除WIP:前缀。", @current_user&.id)
end
end
def gitea_pull_request
@gitea_pull_request ||= create_gitea_pull_request!
end
def create_gitea_pull_request!
@gitea_pull_request =
Gitea::PullRequest::CreateService.call(
@current_user&.gitea_token,
@owner.login,
@project&.identifier,
gitea_pull_request_params.compact)
end
def gitea_pull_request_params
merge_original_pull_params.except(:milestone)
end
def merge_original_pull_params
if pull_request.is_original && @params[:merge_user_login]
base_pull_params.merge(head: "#{@params[:merge_user_login]}:#{@params[:head]}")
else
base_pull_params
end
end
def base_pull_params
{
title: @params[:title], #标题
body: @params[:body], #内容
head: @params[:head], #源分支
base: @params[:base], #目标分支
milestone: 0, #里程碑,未与本地的里程碑关联
}
end
def validate!
raise "title参数不能为空" if @params[:title].blank?
raise "head参数不能为空" if @params[:head].blank?
raise "base参数不能为空" if @params[:base].blank?
end
end