diff --git a/app/controllers/compare_controller.rb b/app/controllers/compare_controller.rb index 63ca58aa6..b36ffde36 100644 --- a/app/controllers/compare_controller.rb +++ b/app/controllers/compare_controller.rb @@ -6,23 +6,45 @@ class CompareController < ApplicationController end def show + load_compare_params compare + @merge_status, @merge_message = get_merge_message end private + def get_merge_message + if @base.blank? || @head.blank? + return -2, "请选择分支" + else + if @head.include?(":") + fork_project = @project.forked_projects.joins(:owner).where(users: {login: @head.to_s.split("/")[0]}).take + return -2, "请选择正确的仓库" unless fork_project.present? + @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? + return -2, "在这些分支之间的合并请求已存在:#{@exist_pullrequest.try(:title)}" + else + if @compare_result["Commits"].blank? && @compare_result["Diff"].blank? + return -2, "分支内容相同,无需创建合并请求" + end + end + end + return 0, "可以合并" + end + def compare - base, head = compare_params # TODO: 处理fork的项目向源项目发送PR的base、head参数问题 @compare_result ||= - head.include?(":") ? gitea_compare(base, head) : gitea_compare(head, base) + @head.include?(":") ? gitea_compare(@base, @head) : gitea_compare(@head, @base) end - def compare_params - base = Addressable::URI.unescape(params[:base]) - head = params[:head].include?('json') ? params[:head]&.split('.json')[0] : params[:head] + def load_compare_params + @base = Addressable::URI.unescape(params[:base]) + @head = params[:head].include?('json') ? params[:head]&.split('.json')[0] : params[:head] - [base, head] end def gitea_compare(base, head) diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index 987bb156e..4270cb389 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -150,9 +150,16 @@ class PullRequestsController < ApplicationController else ActiveRecord::Base.transaction do begin - result = PullRequests::MergeService.call(@owner, @repository, @pull_request, current_user, params) + @gitea_pull = Gitea::PullRequest::GetService.call(@owner.login, @repository.identifier, @pull_request.gitea_number, current_user&.gitea_token) - if result.status == 200 && @pull_request.merge! + if @gitea_pull["merged_by"].present? + success_condition = true + else + result = PullRequests::MergeService.call(@owner, @repository, @pull_request, current_user, params) + success_condition = result.status == 200 + end + + if success_condition && @pull_request.merge! @pull_request.project_trend_status! @issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) normal_status(1, "合并成功") diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index e704acb83..95c669b76 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -15,7 +15,7 @@ module RepositoriesHelper end def image_type?(str) - default_type = %w(png jpg gif tif psd svg) + default_type = %w(png jpg gif tif psd svg gif bmp webp jpeg) default_type.include?(str&.downcase) end @@ -86,4 +86,39 @@ module RepositoriesHelper render_decode64_content(entry['content']) end end + + def base64_to_image(path, content) + # generate to https://git.trusite.net/pawm36ozq/-/raw/branch/master/entrn.png" + content = Base64.decode64(content) + File.open(path, 'wb') { |f| f.write(content) } + end + + def render_download_image_url(dir_path, file_path, content) + # 用户名/项目标识/文件路径 + dir_path = generate_dir_path(dir_path) + + file_path = [dir_path, file_path].join('/') + + puts "##### render_download_image_url file_path: #{file_path}" + base64_to_image(file_path, content) + + file_path = file_path.split('public')[1] + File.join(base_url, file_path) + end + + def generate_dir_path(dir_path) + # tmp_dir_path + # eg: jasder/forgeplus/raw/branch/ref + dir_path = ["public", tmp_dir, dir_path].join('/') + puts "#### dir_path: #{dir_path}" + unless Dir.exists?(dir_path) + FileUtils.mkdir_p(dir_path) ##不成功这里会抛异常 + end + dir_path + end + + def tmp_dir + "repo" + end + end diff --git a/app/models/project.rb b/app/models/project.rb index 6fc1f1db4..a47ee4239 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -76,8 +76,6 @@ # - - class Project < ApplicationRecord include Matchable include Publicable diff --git a/app/views/compare/show.json.jbuilder b/app/views/compare/show.json.jbuilder index 8b75fa3dd..0037d8be8 100644 --- a/app/views/compare/show.json.jbuilder +++ b/app/views/compare/show.json.jbuilder @@ -83,3 +83,5 @@ json.diff do end end +json.status @merge_status +json.message @merge_message \ No newline at end of file diff --git a/app/views/owners/index.json.jbuilder b/app/views/owners/index.json.jbuilder index 677aff71a..6b7e5222e 100644 --- a/app/views/owners/index.json.jbuilder +++ b/app/views/owners/index.json.jbuilder @@ -2,6 +2,6 @@ json.total_count @owners.size json.owners @owners.each do |owner| json.id owner.id json.type owner.type - json.name owner.login + json.name owner&.show_real_name json.avatar_url url_to_avatar(owner) end \ No newline at end of file diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index ec2a045bb..7d41bbd23 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -11,7 +11,16 @@ if @project.forge? json.content decode64_content(entry, @owner, @repository, @ref) json.target entry['target'] - json.download_url entry['download_url'] + + download_url = + if image_type + dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/') + render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref)) + else + entry['download_url'] + end + json.download_url download_url + json.direct_download direct_download json.image_type image_type json.is_readme_file is_readme?(entry['type'], entry['name'])