| @@ -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 \ | 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 |用户登录名 | | |owner |是|string |用户登录名 | | ||||
| |repo |是|string |project's identifier | | |repo |是|string |project's identifier | | ||||
| |id |是|int |cloud_account's id | | |||||
| *返回参数说明:* | *返回参数说明:* | ||||
| @@ -36,14 +36,12 @@ class Ci::BaseController < ApplicationController | |||||
| def find_cloud_account | def find_cloud_account | ||||
| @cloud_account ||= current_user.ci_cloud_account | @cloud_account ||= current_user.ci_cloud_account | ||||
| @cloud_account.blank? ? raise("未找到相关的记录") : @cloud_account | |||||
| end | end | ||||
| def load_ci_user | 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 | ||||
| end | end | ||||
| @@ -54,10 +54,6 @@ class Ci::CloudAccountsController < Ci::BaseController | |||||
| end | end | ||||
| end | end | ||||
| def unactivate | |||||
| end | |||||
| def show | def show | ||||
| end | end | ||||
| @@ -2,9 +2,9 @@ class Ci::ProjectsController < Ci::BaseController | |||||
| include RepositoriesHelper | include RepositoriesHelper | ||||
| before_action :load_project | 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 :authorize_owner_project!, only: [:authorize] | ||||
| before_action :find_cloud_account, only: [:authorize] | |||||
| before_action :find_cloud_account, only: [:authorize, :activate, :deactivate] | |||||
| def authorize | def authorize | ||||
| @user = current_user | @user = current_user | ||||
| @@ -33,4 +33,40 @@ class Ci::ProjectsController < Ci::BaseController | |||||
| end | end | ||||
| 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 | end | ||||
| @@ -49,7 +49,11 @@ module Ci::CloudAccountManageable | |||||
| redirect_url = "#{cloud_account.drone_url}/login" | redirect_url = "#{cloud_account.drone_url}/login" | ||||
| logger.info "######### redirect_url: #{redirect_url}" | 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 | end | ||||
| def unbind_account! | def unbind_account! | ||||
| @@ -64,17 +68,7 @@ module Ci::CloudAccountManageable | |||||
| cloud_account.destroy! | cloud_account.destroy! | ||||
| end | 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 | end | ||||
| def bind_hook!(user, cloud_account, repo) | def bind_hook!(user, cloud_account, repo) | ||||
| @@ -91,7 +85,6 @@ module Ci::CloudAccountManageable | |||||
| result[:status].present? ? nil : result | result[:status].present? ? nil : result | ||||
| end | end | ||||
| def check_bind_cloud_account! | def check_bind_cloud_account! | ||||
| return [true, "你已经绑定了云帐号."] unless current_user.ci_cloud_account.blank? | 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] | Ci::CloudAccount.exists?(ip_num: ip_num) ? [true, "#{devops_params[:ip_num]}服务器已被使用."] : [false, nil] | ||||
| end | 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 | private | ||||
| def devops_params | def devops_params | ||||
| params.permit(:account, :secret, :ip_num) | params.permit(:account, :secret, :ip_num) | ||||
| @@ -57,4 +57,8 @@ class Ci::Repo < Ci::RemoteBase | |||||
| repo | repo | ||||
| end | end | ||||
| end | end | ||||
| def deactivate! | |||||
| update_column(:repo_active, 0) | |||||
| end | |||||
| end | end | ||||
| @@ -21,6 +21,22 @@ module Droneable | |||||
| devops_unverified? && Ci::User.exists?(user_login: self.login) | devops_unverified? && Ci::User.exists?(user_login: self.login) | ||||
| end | 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 | module ClassMethods | ||||
| end | end | ||||
| end | end | ||||
| @@ -180,4 +180,14 @@ class Project < ApplicationRecord | |||||
| return nil if project.blank? | return nil if project.blank? | ||||
| project | project | ||||
| end | 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 | end | ||||
| @@ -372,10 +372,22 @@ Rails.application.routes.draw do | |||||
| to: 'projects#update_trustie_pipeline', | to: 'projects#update_trustie_pipeline', | ||||
| as: :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 | end | ||||
| resources :cloud_accounts, only: [:create] do | resources :cloud_accounts, only: [:create] do | ||||
| member do | member do | ||||
| post :activate | post :activate | ||||
| delete :deactivate | |||||
| end | end | ||||
| end | end | ||||
| resources :builds, param: :build do | resources :builds, param: :build do | ||||