From 6ec7a1329316dc0072aab898c4d42b333a3e1624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cxxq250=E2=80=9D?= <“xxq250@qq.com”> Date: Thu, 11 Aug 2022 09:31:40 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20reposyncer=20=E5=88=86=E6=94=AF?= =?UTF-8?q?=E7=BB=84=E5=90=88=E5=B7=B2=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ob_repository_syncs_controller.rb | 27 ++++++++++++++----- .../ob_repository_sync/api_service.rb | 14 +++++----- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/controllers/ob_repository_syncs_controller.rb b/app/controllers/ob_repository_syncs_controller.rb index 10732093..ebae8e42 100644 --- a/app/controllers/ob_repository_syncs_controller.rb +++ b/app/controllers/ob_repository_syncs_controller.rb @@ -13,7 +13,8 @@ class ObRepositorySyncsController < ApplicationController tip_exception "参数错误" if params[:github_address].blank? && params[:gitee_address].blank? project_name ="#{@project.owner.name}:#{@project.identifier}" service = ObRepositorySync::ApiService.new(project_name) - project_params = params.merge({ "gitlink_address": @project.repository.url }) + domain = GiteaService.gitea_config[:domain] + project_params = params.merge({ "gitlink_address": "#{domain}/#{@project.owner&.login}/#{@project.identifier}.git" }) res = service.create_projects(project_params) tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200" sync_id = res["data"]["id"] @@ -23,7 +24,6 @@ class ObRepositorySyncsController < ApplicationController ob_repository_sync.name = project_name ob_repository_sync.github_address = "#{params[:github_address]}" ob_repository_sync.gitee_address = "#{params[:gitee_address]}" - ob_repository_sync.gitlink_address = @project.repository.url ob_repository_sync.github_token = "#{params[:github_token]}" ob_repository_sync.gitee_token = "#{params[:gitee_token]}" ob_repository_sync.sync_id = sync_id @@ -38,16 +38,30 @@ class ObRepositorySyncsController < ApplicationController if res["code"].to_s == "200" @ob_repository_sync.destroy! end + render_ok end def jobs tip_exception "该项目未创建同步任务" if @ob_repository_sync.blank? service = ObRepositorySync::ApiService.new(@ob_repository_sync.name) res = service.get_projects_jobs - render_ok(count: res["data"]["total"], data: res["data"]["list"]) + data = res["data"]["list"] + if params[:type] && params[:type].to_s.downcase == "github" + data = data.select { |row| row["github_branch"].present? } + elsif params[:type] && params[:type].to_s.downcase == "gitee" + data = data.select { |row| row["gitee_branch"].present? } + end + render_ok(count: res["data"]["total"], data: data) end def create_jobs + tip_exception "必须配置一个分支" if params[:github_branch].blank? && params[:gitee_branch].blank? && params[:gitlink_branch].blank? + ob_jobs = ObRepositorySyncJob.where(ob_repository_sync_id: @ob_repository_sync.id) + ob_jobs = ob_jobs.where(job_type: params[:job_type]) if params[:job_type].present? + ob_jobs = ob_jobs.where(github_branch: params[:github_branch]) if params[:github_branch].present? + ob_jobs = ob_jobs.where(gitee_branch: params[:gitee_branch]) if params[:gitee_branch].present? + ob_jobs = ob_jobs.where(gitlink_branch: params[:gitlink_branch]) if params[:gitlink_branch].present? + tip_exception "该分支组合已配置,不能重复!" if ob_jobs.count > 0 service = ObRepositorySync::ApiService.new(@ob_repository_sync.name) res = service.create_projects_jobs(params) tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200" @@ -57,7 +71,7 @@ class ObRepositorySyncsController < ApplicationController job.github_branch = "#{params[:github_branch]}" job.gitee_branch = "#{params[:gitee_branch]}" job.gitlink_branch = "#{params[:gitlink_branch]}" - job.job_type = "#{params[:type]}" + job.job_type = "#{params[:job_type]}" job.base = "#{params[:base]}" job.job_id = job_id job.save @@ -70,7 +84,8 @@ class ObRepositorySyncsController < ApplicationController service = ObRepositorySync::ApiService.new(@ob_repository_sync.name) res = service.delete_job params[:job_id] tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200" - @ob_repository_sync.destroy! + job = ObRepositorySyncJob.find_by(ob_repository_sync_id: @ob_repository_sync.id, job_id: params[:job_id]) + job.destroy! if job.present? render_ok end @@ -94,7 +109,7 @@ class ObRepositorySyncsController < ApplicationController tip_exception "该项目未创建同步任务" if @ob_repository_sync.blank? tip_exception "缺少参数job_id" if params[:job_id].blank? service = ObRepositorySync::ApiService.new(@ob_repository_sync.name) - @data = service.job_logs params[:job_id] + res = service.job_logs params[:job_id] tip_exception "请求错误: #{res["msg"]}" if res["code"].to_s != "200" render_ok(count: res["data"]["total"], data: res["data"]["list"]) end diff --git a/app/services/ob_repository_sync/api_service.rb b/app/services/ob_repository_sync/api_service.rb index 7bed888e..5ecd48af 100644 --- a/app/services/ob_repository_sync/api_service.rb +++ b/app/services/ob_repository_sync/api_service.rb @@ -15,7 +15,9 @@ class ObRepositorySync::ApiService < ApplicationService "github_address": "#{params[:github_address]}", "gitee_address": "#{params[:gitee_address]}", "github_token": "#{params[:github_token]}", - "gitee_token": "#{params[:gitee_token]}" + "gitee_token": "#{params[:gitee_token]}", + "gitlink_address": "#{params[:gitlink_address]}", + "gitlink_token": "#{params[:gitlink_token]}" } url = URI("#{domain}/cerobot/projects") http = Net::HTTP.new(url.host, url.port) @@ -41,7 +43,7 @@ class ObRepositorySync::ApiService < ApplicationService end def get_projects_jobs - url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs") + url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs?pageSize=100&pageNum=1") http = Net::HTTP.new(url.host, url.port) request = Net::HTTP::Get.new(url) request["Content-Type"] = "application/json" @@ -56,7 +58,7 @@ class ObRepositorySync::ApiService < ApplicationService "github_branch": "#{params[:github_branch]}", "gitee_branch": "#{params[:gitee_branch]}", "gitlink_branch": "#{params[:gitlink_branch]}", - "type": "#{params[:type]}", + "type": "#{params[:job_type]}", "base": "#{params[:base]}" } url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs") @@ -122,11 +124,7 @@ class ObRepositorySync::ApiService < ApplicationService response = http.request(request) Rails.logger.info "set_commit job response.read_body======#{response.read_body}" res = JSON.parse(response.body) - if res["code"].to_s == "200" - res["data"] - else - [] - end + res end def pull_requests