reposyncer同步仓库接口
This commit is contained in:
parent
0df1e5fdd0
commit
1bbde165c2
|
@ -0,0 +1,111 @@
|
|||
class ObRepositorySyncsController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :load_project
|
||||
before_action :load_ob_repository_sync, except: [:create]
|
||||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
|
||||
end
|
||||
|
||||
|
||||
def create
|
||||
tip_exception "参数错误" if params[:github_address].blank? && params[:gitee_address].blank?
|
||||
project_name ="#{@project.owner.name}:#{@project.identifier}"
|
||||
service = ObRepositorySync::ApiService.new(project_name)
|
||||
params.merge({ "gitlink_address": @project.repository.url })
|
||||
res = service.create_projects(params)
|
||||
tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
sync_id = res["data"]["id"]
|
||||
ob_repository_sync = ObRepositorySync.find_or_initialize_by(project_id: @project.id)
|
||||
ob_repository_sync.project_id = @project.id
|
||||
ob_repository_sync.user_id = current_user.id
|
||||
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.github_token = "#{params[:github_token]}"
|
||||
ob_repository_sync.gitee_token = "#{params[:gitee_token]}"
|
||||
ob_repository_sync.sync_id = sync_id
|
||||
ob_repository_sync.save!
|
||||
render_ok
|
||||
end
|
||||
|
||||
def delete
|
||||
service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
|
||||
res = service.delete_project @ob_repository_sync.sync_id
|
||||
tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
if res["code"].to_s == "200"
|
||||
@ob_repository_sync.destroy!
|
||||
end
|
||||
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"])
|
||||
end
|
||||
|
||||
def create_jobs
|
||||
service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
|
||||
res = service.create_projects_jobs(params)
|
||||
tip_exception "保存失败: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
job_id = res["data"]["id"]
|
||||
job = ObRepositorySyncJob.new
|
||||
job.ob_repository_sync_id = @ob_repository_sync.id
|
||||
job.github_branch = "#{params[:github_branch]}"
|
||||
job.gitee_branch = "#{params[:gitee_branch]}"
|
||||
job.gitlink_branch = "#{params[:gitlink_branch]}"
|
||||
job.job_type = "#{params[:type]}"
|
||||
job.base = "#{params[:base]}"
|
||||
job.job_id = job_id
|
||||
job.save
|
||||
render_ok
|
||||
end
|
||||
|
||||
|
||||
def delete_job
|
||||
tip_exception "缺少参数job_id" if params[:job_id].blank?
|
||||
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!
|
||||
render_ok
|
||||
end
|
||||
|
||||
def start_job
|
||||
tip_exception "缺少参数job_id" if params[:job_id].blank?
|
||||
service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
|
||||
res = service.start_job params[:job_id]
|
||||
tip_exception "启动错误: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
render_ok
|
||||
end
|
||||
|
||||
def stop_job
|
||||
tip_exception "缺少参数job_id" if params[:job_id].blank?
|
||||
service = ObRepositorySync::ApiService.new(@ob_repository_sync.name)
|
||||
res = service.stop_job params[:job_id]
|
||||
tip_exception "停止错误: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
render_ok
|
||||
end
|
||||
|
||||
def job_logs
|
||||
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]
|
||||
tip_exception "请求错误: #{res["msg"]}" if res["code"].to_s != "200"
|
||||
render_ok(count: res["data"]["total"], data: res["data"]["list"])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def load_ob_repository_sync
|
||||
@ob_repository_sync = ObRepositorySync.find_by(project_id: @project.id)
|
||||
end
|
||||
|
||||
def authenticate_user!
|
||||
return if @project.member?(current_user) || current_user.admin?
|
||||
render_forbidden('你没有权限操作')
|
||||
end
|
||||
end
|
|
@ -0,0 +1,28 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: ob_repository_syncs
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# project_id :integer
|
||||
# user_id :integer
|
||||
# name :string(255)
|
||||
# github_address :string(255)
|
||||
# gitee_address :string(255)
|
||||
# github_token :string(255)
|
||||
# gitee_token :string(255)
|
||||
# sync_id :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_ob_repository_syncs_on_project_id (project_id)
|
||||
# index_ob_repository_syncs_on_user_id (user_id)
|
||||
#
|
||||
|
||||
class ObRepositorySync < ApplicationRecord
|
||||
belongs_to :project
|
||||
belongs_to :user
|
||||
|
||||
has_many :ob_repository_sync_jobs, dependent: :destroy
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
# == Schema Information
|
||||
#
|
||||
# Table name: ob_repository_sync_jobs
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# ob_repository_sync_id :integer
|
||||
# github_branch :string(255)
|
||||
# gitee_branch :string(255)
|
||||
# gitlink_branch :string(255)
|
||||
# job_type :string(255)
|
||||
# base :string(255)
|
||||
# job_id :integer
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_ob_repository_sync_jobs_on_ob_repository_sync_id (ob_repository_sync_id)
|
||||
#
|
||||
|
||||
class ObRepositorySyncJob < ApplicationRecord
|
||||
belongs_to :ob_repository_sync
|
||||
|
||||
end
|
|
@ -49,7 +49,7 @@ class Repository < ApplicationRecord
|
|||
end
|
||||
|
||||
def url
|
||||
self['url'].blank? ? "#{Rails.application.config_for(:configuration)['platform_url']}/#{self.owner&.login}/#{self.identifier}.git" : self['url']
|
||||
self['url'].blank? ? "#{Gitea.gitea_config[:domain]}/#{self.owner&.login}/#{self.identifier}.git" : self['url']
|
||||
end
|
||||
|
||||
# with repository is mirror
|
||||
|
|
|
@ -0,0 +1,166 @@
|
|||
class ObRepositorySync::ApiService < ApplicationService
|
||||
attr_reader :project_name
|
||||
|
||||
def initialize(project_name)
|
||||
@project_name = project_name
|
||||
end
|
||||
|
||||
def call
|
||||
true
|
||||
end
|
||||
|
||||
def create_projects(params = {})
|
||||
projects_body = {
|
||||
"name": "#{@project_name}",
|
||||
"github_address": "#{params[:github_address]}",
|
||||
"gitee_address": "#{params[:gitee_address]}",
|
||||
"github_token": "#{params[:github_token]}",
|
||||
"gitee_token": "#{params[:gitee_token]}"
|
||||
}
|
||||
url = URI("#{domain}/cerobot/projects")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Post.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
request.body = JSON.dump(projects_body)
|
||||
|
||||
response = http.request(request)
|
||||
# Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def delete_project sync_id
|
||||
url = URI("#{domain}/cerobot/projects?id=#{sync_id}")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Delete.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
# Rails.logger.info "delete project response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def get_projects_jobs
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Get.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def create_projects_jobs(params = {})
|
||||
job_body = {
|
||||
"github_branch": "#{params[:github_branch]}",
|
||||
"gitee_branch": "#{params[:gitee_branch]}",
|
||||
"gitlink_branch": "#{params[:gitlink_branch]}",
|
||||
"type": "#{params[:type]}",
|
||||
"base": "#{params[:base]}"
|
||||
}
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Post.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
request.body = JSON.dump(job_body)
|
||||
|
||||
response = http.request(request)
|
||||
Rails.logger.info "cerobot/projects response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def delete_job job_id
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs?id=#{job_id}")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Delete.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "delete job response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def start_job job_id
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/start")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Put.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "start job response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def stop_job job_id
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/stop")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Put.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "stop job response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def set_commit job_id, commit_id
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/set_commit?commit=#{commit_id}")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Put.new(url)
|
||||
response = http.request(request)
|
||||
Rails.logger.info "set_commit job response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
res
|
||||
end
|
||||
|
||||
def job_logs job_id
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/jobs/#{job_id}/logs")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Get.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
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
|
||||
end
|
||||
|
||||
def pull_requests
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/pullrequests")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Get.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "pull_requests response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
if res["code"].to_s == "200"
|
||||
res["data"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def pull_requests_sync
|
||||
url = URI("#{domain}/cerobot/projects/#{@project_name}/pullrequests/sync")
|
||||
http = Net::HTTP.new(url.host, url.port)
|
||||
request = Net::HTTP::Get.new(url)
|
||||
request["Content-Type"] = "application/json"
|
||||
response = http.request(request)
|
||||
Rails.logger.info "pull_requests_sync response.read_body======#{response.read_body}"
|
||||
res = JSON.parse(response.body)
|
||||
if res["code"].to_s == "200"
|
||||
res["data"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
def domain
|
||||
EduSetting.get("ob_repository_sync_api_domain") || "http://106.75.110.152:50087"
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
json.status 0
|
||||
json.message "success"
|
||||
json.data do
|
||||
if @ob_repository_sync
|
||||
json.extract! @ob_repository_sync, :user_id, :name, :github_address, :gitee_address, :github_token, :gitee_token,:sync_id, :created_at, :updated_at
|
||||
end
|
||||
|
||||
end
|
|
@ -555,6 +555,16 @@ Rails.application.routes.draw do
|
|||
resources :project_trends, :path => :activity, only: [:index, :create]
|
||||
resources :issue_tags, :path => :labels, only: [:create, :edit, :update, :destroy, :index]
|
||||
resources :version_releases, :path => :releases, only: [:index,:new, :show, :create, :edit, :update, :destroy]
|
||||
resources :ob_repository_syncs, :path => :synchronizes, only: [:index, :create] do
|
||||
collection do
|
||||
delete :delete
|
||||
get :jobs
|
||||
post :create_jobs
|
||||
delete :delete_job
|
||||
post :start_job
|
||||
post :stop_job
|
||||
end
|
||||
end
|
||||
|
||||
scope module: :ci do
|
||||
scope do
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
class CreateObRepositorySyncs < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :ob_repository_syncs do |t|
|
||||
t.references :project
|
||||
t.references :user
|
||||
t.string :name
|
||||
t.string :github_address
|
||||
t.string :gitee_address
|
||||
t.string :github_token
|
||||
t.string :gitee_token
|
||||
t.integer :sync_id
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,14 @@
|
|||
class CreateObRepositorySyncJobs < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :ob_repository_sync_jobs do |t|
|
||||
t.references :ob_repository_sync
|
||||
t.string :github_branch
|
||||
t.string :gitee_branch
|
||||
t.string :gitlink_branch
|
||||
t.string :job_type
|
||||
t.string :base
|
||||
t.integer :job_id
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue