2020-11-03 18:37:47 +08:00
|
|
|
|
class CompareController < ApplicationController
|
|
|
|
|
|
# skip_before_action :require_login
|
|
|
|
|
|
before_action :load_repository
|
|
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def show
|
2021-07-12 17:24:23 +08:00
|
|
|
|
load_compare_params
|
2021-01-12 17:34:07 +08:00
|
|
|
|
compare
|
2021-07-12 18:10:26 +08:00
|
|
|
|
@merge_status, @merge_message = get_merge_message
|
2022-04-28 18:53:33 +08:00
|
|
|
|
@page_size = page_size <= 0 ? 1 : page_size
|
|
|
|
|
|
@page_limit = page_limit <=0 ? 15 : page_limit
|
|
|
|
|
|
@page_offset = (@page_size -1) * @page_limit
|
|
|
|
|
|
Rails.logger.info("+========#{@page_size}-#{@page_limit}-#{@page_offset}")
|
2021-01-12 17:34:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
2021-07-12 17:24:23 +08:00
|
|
|
|
def get_merge_message
|
|
|
|
|
|
if @base.blank? || @head.blank?
|
2021-07-12 18:10:26 +08:00
|
|
|
|
return -2, "请选择分支"
|
2021-07-12 17:24:23 +08:00
|
|
|
|
else
|
2022-03-07 10:53:07 +08:00
|
|
|
|
return -2, "目标仓库未开启合并请求(PR)功能" unless @project.has_menu_permission("pulls")
|
2021-07-12 17:24:23 +08:00
|
|
|
|
if @head.include?(":")
|
|
|
|
|
|
fork_project = @project.forked_projects.joins(:owner).where(users: {login: @head.to_s.split("/")[0]}).take
|
2021-07-12 18:10:26 +08:00
|
|
|
|
return -2, "请选择正确的仓库" unless fork_project.present?
|
2021-07-12 17:24:23 +08:00
|
|
|
|
@exist_pullrequest = @project.pull_requests.where(is_original: true, head: @head.to_s.split(":")[1], base: @base, status: 0, fork_project_id: fork_project.id).take
|
|
|
|
|
|
else
|
|
|
|
|
|
@exist_pullrequest = @project.pull_requests.where(is_original: false, head: @base, base: @head, status: 0).take
|
|
|
|
|
|
end
|
|
|
|
|
|
if @exist_pullrequest.present?
|
2021-10-13 11:23:43 +08:00
|
|
|
|
return -2, "在这些分支之间的合并请求已存在:<a href='/#{@owner.login}/#{@project.identifier}/pulls/#{@exist_pullrequest.id}'>#{@exist_pullrequest.try(:title)}</a>"
|
2021-07-12 17:24:23 +08:00
|
|
|
|
else
|
|
|
|
|
|
if @compare_result["Commits"].blank? && @compare_result["Diff"].blank?
|
2021-07-12 18:10:26 +08:00
|
|
|
|
return -2, "分支内容相同,无需创建合并请求"
|
2021-07-12 17:24:23 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
2021-07-12 18:10:26 +08:00
|
|
|
|
return 0, "可以合并"
|
2021-07-12 17:24:23 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2021-01-12 17:34:07 +08:00
|
|
|
|
def compare
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: 处理fork的项目向源项目发送PR的base、head参数问题
|
|
|
|
|
|
@compare_result ||=
|
2021-07-12 17:24:23 +08:00
|
|
|
|
@head.include?(":") ? gitea_compare(@base, @head) : gitea_compare(@head, @base)
|
2021-01-12 17:34:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2021-07-12 17:24:23 +08:00
|
|
|
|
def load_compare_params
|
2021-12-28 14:42:55 +08:00
|
|
|
|
# @base = Addressable::URI.unescape(params[:base])
|
2021-12-29 09:56:49 +08:00
|
|
|
|
@base = params[:base].include?(":") ? Addressable::URI.unescape(params[:base].split(":")[0]) + ':' + Base64.decode64(params[:base].split(":")[1]) : Base64.decode64(params[:base])
|
2021-11-25 14:06:55 +08:00
|
|
|
|
@head = params[:head].include?('.json') ? params[:head][0..-6] : params[:head]
|
2021-12-28 14:42:55 +08:00
|
|
|
|
# @head = Addressable::URI.unescape(@head)
|
2021-12-29 09:56:49 +08:00
|
|
|
|
@head = @head.include?(":") ? Addressable::URI.unescape(@head.split(":")[0]) + ':' + Base64.decode64(@head.split(":")[1]) : Base64.decode64(@head)
|
2021-01-12 17:34:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def gitea_compare(base, head)
|
2022-03-10 10:47:06 +08:00
|
|
|
|
Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, Addressable::URI.escape(base), Addressable::URI.escape(head), current_user.gitea_token)
|
2020-11-03 18:37:47 +08:00
|
|
|
|
end
|
2022-04-28 18:53:33 +08:00
|
|
|
|
|
|
|
|
|
|
def page_size
|
|
|
|
|
|
params.fetch(:page, 1).to_i
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def page_limit
|
|
|
|
|
|
params.fetch(:limit, 15).to_i
|
|
|
|
|
|
end
|
2020-11-03 18:37:47 +08:00
|
|
|
|
end
|