Browse Source

FIX 优化ci流程,并配置gitea相关database

tags/v1.0.0
Jasder 5 years ago
parent
commit
a308862567
9 changed files with 138 additions and 27 deletions
  1. +32
    -3
      README.md
  2. +3
    -5
      app/controllers/ci/base_controller.rb
  3. +0
    -4
      app/controllers/ci/cloud_accounts_controller.rb
  4. +38
    -2
      app/controllers/ci/projects_controller.rb
  5. +23
    -13
      app/controllers/concerns/ci/cloud_account_manageable.rb
  6. +4
    -0
      app/models/ci/repo.rb
  7. +16
    -0
      app/models/concerns/droneable.rb
  8. +10
    -0
      app/models/project.rb
  9. +12
    -0
      config/routes.rb

+ 32
- 3
README.md View File

@@ -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 |

*返回参数说明:*



+ 3
- 5
app/controllers/ci/base_controller.rb View File

@@ -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

+ 0
- 4
app/controllers/ci/cloud_accounts_controller.rb View File

@@ -54,10 +54,6 @@ class Ci::CloudAccountsController < Ci::BaseController
end
end

def unactivate

end

def show
end



+ 38
- 2
app/controllers/ci/projects_controller.rb View File

@@ -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

+ 23
- 13
app/controllers/concerns/ci/cloud_account_manageable.rb View File

@@ -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)


+ 4
- 0
app/models/ci/repo.rb View File

@@ -57,4 +57,8 @@ class Ci::Repo < Ci::RemoteBase
repo
end
end

def deactivate!
update_column(:repo_active, 0)
end
end

+ 16
- 0
app/models/concerns/droneable.rb View File

@@ -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

+ 10
- 0
app/models/project.rb View File

@@ -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

+ 12
- 0
config/routes.rb View File

@@ -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


Loading…
Cancel
Save