From a3088625672fc7fd7d52526634d6cceae0df4121 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Sun, 27 Sep 2020 11:30:37 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20=E4=BC=98=E5=8C=96ci=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E9=85=8D=E7=BD=AEgitea=E7=9B=B8=E5=85=B3data?= =?UTF-8?q?base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 35 ++++++++++++++-- app/controllers/ci/base_controller.rb | 8 ++-- .../ci/cloud_accounts_controller.rb | 4 -- app/controllers/ci/projects_controller.rb | 40 ++++++++++++++++++- .../concerns/ci/cloud_account_manageable.rb | 36 +++++++++++------ app/models/ci/repo.rb | 4 ++ app/models/concerns/droneable.rb | 16 ++++++++ app/models/project.rb | 10 +++++ config/routes.rb | 12 ++++++ 9 files changed, 138 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index f9491e8e1..f8f6d05be 100644 --- a/README.md +++ b/README.md @@ -2491,12 +2491,42 @@ https://localhost:3000/api/jasder/forgeplus/cloud_accounts.json | jq #### 激活项目 ``` -POST /api/:owner/:repo/cloud_accounts/:id/activate +POST /api/:owner/:repo/activate ``` *示例* ``` curl -X POST \ -http://localhost:3000/api/jasder/forgeplus/cloud_accounts/1/activate.json | jq +http://localhost:3000/api/jasder/forgeplus/activate.json | jq +``` +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|owner |是|string |用户登录名 | +|repo |是|string |project's identifier | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|status |int|0:成功, -1: 失败| + +``` +{ + "status": 0, + "message": "success" +} +``` +--- + +#### 取消激活项目 +``` +POST /api/:owner/:repo/deactivate +``` +*示例* +``` +curl -X POST \ +http://localhost:3000/api/jasder/forgeplus/deactivate.json | jq ``` *请求参数说明:* @@ -2504,7 +2534,6 @@ http://localhost:3000/api/jasder/forgeplus/cloud_accounts/1/activate.json | jq |-|-|-|-| |owner |是|string |用户登录名 | |repo |是|string |project's identifier | -|id |是|int |cloud_account's id | *返回参数说明:* diff --git a/app/controllers/ci/base_controller.rb b/app/controllers/ci/base_controller.rb index 57f6cdf4c..a2c4e4ab6 100644 --- a/app/controllers/ci/base_controller.rb +++ b/app/controllers/ci/base_controller.rb @@ -36,14 +36,12 @@ class Ci::BaseController < ApplicationController def find_cloud_account @cloud_account ||= current_user.ci_cloud_account + @cloud_account.blank? ? raise("未找到相关的记录") : @cloud_account end def load_ci_user - begin - @ci_user = Ci::User.find_by(user_login: params[:owner]) - rescue - render_not_found - end + @ci_user ||= Ci::User.find_by(user_login: params[:owner]) + @ci_user.blank? ? raise("未找到相关的记录") : @ci_user end end diff --git a/app/controllers/ci/cloud_accounts_controller.rb b/app/controllers/ci/cloud_accounts_controller.rb index 741ef53dc..01f4800b0 100644 --- a/app/controllers/ci/cloud_accounts_controller.rb +++ b/app/controllers/ci/cloud_accounts_controller.rb @@ -54,10 +54,6 @@ class Ci::CloudAccountsController < Ci::BaseController end end - def unactivate - - end - def show end diff --git a/app/controllers/ci/projects_controller.rb b/app/controllers/ci/projects_controller.rb index a5535fcbe..d67342ff9 100644 --- a/app/controllers/ci/projects_controller.rb +++ b/app/controllers/ci/projects_controller.rb @@ -2,9 +2,9 @@ class Ci::ProjectsController < Ci::BaseController include RepositoriesHelper before_action :load_project - before_action :load_repo, only: [:update_trustie_pipeline] + before_action :load_repo, only: [:update_trustie_pipeline, :activate, :deactivate] before_action :authorize_owner_project!, only: [:authorize] - before_action :find_cloud_account, only: [:authorize] + before_action :find_cloud_account, only: [:authorize, :activate, :deactivate] def authorize @user = current_user @@ -33,4 +33,40 @@ class Ci::ProjectsController < Ci::BaseController end end + def activate + return render_error('你还未认证') unless current_user.ci_certification? + + begin + ActiveRecord::Base.transaction do + if @repo + return render_error('该项目已经激活') if @repo.repo_active? + if @project.ci_reactivate? + @project.ci_reactivate!(@repo) + return render_ok + end + @repo.activate!(@ci_user.user_id) + else + @repo = Ci::Repo.auto_create!(@ci_user, @project) + @ci_user.update_column(:user_syncing, false) + end + + result = bind_hook!(current_user, @cloud_account, @repo) + @project.update_columns(open_devops: true, gitea_webhook_id: result['id']) + @project.increment!(:open_devops_count) + @cloud_account.update_column(:ci_user_id, @ci_user.user_id) + end + render_ok + rescue Exception => ex + render_error(ex.message) + end + end + + def deactivate + return render_error('已经是激活状态') if @repo.repo_active? + + @project.update_column(open_devops: false) + @repo.deactivate! + render_ok + end + end diff --git a/app/controllers/concerns/ci/cloud_account_manageable.rb b/app/controllers/concerns/ci/cloud_account_manageable.rb index 033aa5ebe..f9e82e2ad 100644 --- a/app/controllers/concerns/ci/cloud_account_manageable.rb +++ b/app/controllers/concerns/ci/cloud_account_manageable.rb @@ -49,7 +49,11 @@ module Ci::CloudAccountManageable redirect_url = "#{cloud_account.drone_url}/login" logger.info "######### redirect_url: #{redirect_url}" - result && !result.blank? ? cloud_account : nil + return nil unless result.present? + + gitea_oauth_grant!(current_user.gitea_uid, oauth.gitea_oauth_id) + return cloud_account + # result && !result.blank? ? cloud_account : nil end def unbind_account! @@ -64,17 +68,7 @@ module Ci::CloudAccountManageable cloud_account.destroy! end - current_user.projects.update_all(open_devops: false) - current_user.set_drone_step!(User::DEVOPS_UNINIT) - - # TODO - # 删除用户项目下的与ci相关的所有webhook - current_user.projects.select(:id, :identifier, :gitea_webhook_id).each do |project| - if project.gitea_webhook_id - result = Gitea::Hooks::DestroyService.call(current_user.gitea_token, current_user.login, project.identifier, project.gitea_webhook_id) - project.update_column(:gitea_webhook_id, nil) if result.status == 204 - end - end + current_user.unbind_account! end def bind_hook!(user, cloud_account, repo) @@ -91,7 +85,6 @@ module Ci::CloudAccountManageable result[:status].present? ? nil : result end - def check_bind_cloud_account! return [true, "你已经绑定了云帐号."] unless current_user.ci_cloud_account.blank? @@ -99,6 +92,23 @@ module Ci::CloudAccountManageable Ci::CloudAccount.exists?(ip_num: ip_num) ? [true, "#{devops_params[:ip_num]}服务器已被使用."] : [false, nil] end + def gitea_oauth_grant!(gitea_uid, application_id) + gitea_server_config = Rails.configuration.database_configuration[Rails.env]["gitea_server"] + if gitea_server_config.blank? + puts "[Gitea Server]: gitea database config missing" + return + else + puts "[Gitea Server]: gitea db config is exists." + end + + connection = establish_connection gitea_server_config + + unix_time = Time.now.to_i + sql = "INSERT INTO oauth2_grant ( user_id, application_id, counter, created_unix, updated_unix ) VALUES ( #{gitea_uid}, #{application_id}, 0, #{unix_time}, #{unix_time} );" + + connection.execute(sql) + end + private def devops_params params.permit(:account, :secret, :ip_num) diff --git a/app/models/ci/repo.rb b/app/models/ci/repo.rb index 687ea62e3..a23b7f801 100644 --- a/app/models/ci/repo.rb +++ b/app/models/ci/repo.rb @@ -57,4 +57,8 @@ class Ci::Repo < Ci::RemoteBase repo end end + + def deactivate! + update_column(:repo_active, 0) + end end diff --git a/app/models/concerns/droneable.rb b/app/models/concerns/droneable.rb index 73334f59b..546a78bba 100644 --- a/app/models/concerns/droneable.rb +++ b/app/models/concerns/droneable.rb @@ -21,6 +21,22 @@ module Droneable devops_unverified? && Ci::User.exists?(user_login: self.login) end + def unbind_account! + user_projects = selef.projects + + user_projects.update_all(open_devops: false, open_devops_count: 0) + set_drone_step!(User::DEVOPS_UNINIT) + + # TODO + # 删除用户项目下的与ci相关的所有webhook + user_projects.select(:id, :identifier, :gitea_webhook_id).each do |project| + if project.gitea_webhook_id + result = Gitea::Hooks::DestroyService.call(self.gitea_token, self.login, project.identifier, project.gitea_webhook_id) + project.update_column(:gitea_webhook_id, nil) if result.status == 204 + end + end + end + module ClassMethods end end diff --git a/app/models/project.rb b/app/models/project.rb index 24f8a85de..c30f67f1a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -180,4 +180,14 @@ class Project < ApplicationRecord return nil if project.blank? project end + + def ci_reactivate? + open_devops_count > 0 + end + + def ci_reactivate!(ci_repo) + ci_repo.update_column(:repo_active, 1) + update_column(:open_devops, true) + increment!(:open_devops_count) + end end diff --git a/config/routes.rb b/config/routes.rb index 595692371..2f172760c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -372,10 +372,22 @@ Rails.application.routes.draw do to: 'projects#update_trustie_pipeline', as: :update_trustie_pipeline ) + post( + 'activate', + to: 'projects#activate', + as: :ci_activate_project + ) + delete( + 'deactivate', + to: 'projects#deactivate', + as: :ci_deactivate_project + ) end + resources :cloud_accounts, only: [:create] do member do post :activate + delete :deactivate end end resources :builds, param: :build do