diff --git a/app/controllers/api/v1/projects/branches_controller.rb b/app/controllers/api/v1/projects/branches_controller.rb index e2d04eeb6..2546027e6 100644 --- a/app/controllers/api/v1/projects/branches_controller.rb +++ b/app/controllers/api/v1/projects/branches_controller.rb @@ -24,6 +24,17 @@ class Api::V1::Projects::BranchesController < Api::V1::BaseController render :json => response.read_body end + def gitlink + url = URI("https://www.gitlink.org.cn/api/#{params[:owner]}/#{params[:repo]}/branches?access_token=#{params[:token]}&page=#{page}&per_page=#{limit}") + https = Net::HTTP.new(url.host, url.port) + https.use_ssl = true + + request = Net::HTTP::Get.new(url) + # request["Authorization"] = "Bearer #{params[:token]}" + response = https.request(request) + render :json => response.read_body + end + def index @result_object = Api::V1::Projects::Branches::ListService.call(@project, {name: params[:keyword], state: params[:state], page: page, limit: limit}, current_user&.gitea_token) end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6d7ac04b7..19e2cb5e5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -295,6 +295,10 @@ class ApplicationController < ActionController::Base end end end + elsif params[:access_token].present? + user = User.find_by(gitea_token: params[:access_token]) + tip_exception(401, "无效token") if user.blank? + User.current = user else User.current = find_current_user uid_logger("user_setup: " + (User.current.logged? ? "#{User.current.try(:login)} (id=#{User.current.try(:id)})" : "anonymous")) diff --git a/app/jobs/touch_sync_job.rb b/app/jobs/touch_sync_job.rb index 3beaf52e5..92c65383b 100644 --- a/app/jobs/touch_sync_job.rb +++ b/app/jobs/touch_sync_job.rb @@ -4,7 +4,7 @@ class TouchSyncJob < ApplicationJob def perform(touchable) puts "aaaa" case touchable.class.to_s - when 'SyncRepositories::Github' || 'SyncRepositories::Gitee' + when 'SyncRepositories::Github' || 'SyncRepositories::Gitee' || 'SyncRepositories::Gitlink' Reposync::SyncRepoService.call(touchable.repo_name) when 'SyncRepositoryBranch' sync_repository = touchable.sync_repository diff --git a/app/models/sync_repositories/gitlink.rb b/app/models/sync_repositories/gitlink.rb new file mode 100644 index 000000000..6fc9dfa50 --- /dev/null +++ b/app/models/sync_repositories/gitlink.rb @@ -0,0 +1,24 @@ +# == Schema Information +# +# Table name: sync_repositories +# +# id :integer not null, primary key +# project_id :integer +# type :string(255) +# repo_name :string(255) +# external_repo_address :string(255) +# sync_granularity :integer +# sync_direction :integer +# created_at :datetime not null +# updated_at :datetime not null +# external_token :string(255) +# webhook_gid :integer +# +# Indexes +# +# index_sync_repositories_on_project_id (project_id) +# + +class SyncRepositories::Gitlink < SyncRepository + +end diff --git a/app/services/api/v1/projects/sync_repositories/create_service.rb b/app/services/api/v1/projects/sync_repositories/create_service.rb index 4a0dc5091..3d33fe3a6 100644 --- a/app/services/api/v1/projects/sync_repositories/create_service.rb +++ b/app/services/api/v1/projects/sync_repositories/create_service.rb @@ -5,7 +5,7 @@ class Api::V1::Projects::SyncRepositories::CreateService < ApplicationService attr_reader :project, :type, :external_token, :external_repo_address, :sync_granularity, :external_branch_name, :gitlink_branch_name, :first_sync_direction attr_accessor :sync_repository1, :sync_repository2, :sync_repository_branch1, :sync_repository_branch2, :gitea_webhook - validates :type, inclusion: {in: %w(SyncRepositories::Gitee SyncRepositories::Github)} + validates :type, inclusion: {in: %w(SyncRepositories::Gitee SyncRepositories::Github SyncRepositories::Gitlink)} validates :external_repo_address, format: { with: CustomRegexp::URL_REGEX, multiline: true, message: "地址格式不正确" } validates :sync_granularity, :first_sync_direction, inclusion: {in: [1,2]} # validate :check_gitlink_branch_name @@ -76,7 +76,9 @@ class Api::V1::Projects::SyncRepositories::CreateService < ApplicationService url = "" if type == "SyncRepositories::Gitee" url = "#{Rails.application.config_for(:configuration)['platform_url']}/api/v1/#{project&.owner&.login}/#{project&.identifier}/sync_repositories/sync?sync_direction=1&repo_type=SyncRepositories::Gitee" - else + elsif type == "SyncRepositories::Gitlink" + url = "#{Rails.application.config_for(:configuration)['platform_url']}/api/v1/#{project&.owner&.login}/#{project&.identifier}/sync_repositories/sync?sync_direction=1&repo_type=SyncRepositories::Gitlink" + else url = "#{Rails.application.config_for(:configuration)['platform_url']}/api/v1/#{project&.owner&.login}/#{project&.identifier}/sync_repositories/sync?sync_direction=1&repo_type=SyncRepositories::Github" end webhook_params = { @@ -94,6 +96,8 @@ class Api::V1::Projects::SyncRepositories::CreateService < ApplicationService def repo_name(sync_direction) if type == "SyncRepositories::Gitee" return "gitee:#{project.id}:#{sync_granularity}:#{sync_direction}" + elsif type == "SyncRepositories::Gitlink" + return "gitlink:#{project.id}:#{sync_granularity}:#{sync_direction}" else return "github:#{project.id}:#{sync_granularity}:#{sync_direction}" end diff --git a/config/routes/api.rb b/config/routes/api.rb index ec695a48a..8d113fd68 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -269,6 +269,7 @@ defaults format: :json do collection do get :gitee get :github + get :gitlink get :all post :restore patch :update_default_branch