FIX 优化ci初始化流程

This commit is contained in:
Jasder 2020-08-28 10:25:08 +08:00
parent b26c2f273f
commit b75896821f
7 changed files with 33 additions and 34 deletions

View File

@ -15,7 +15,7 @@ class Ci::CloudAccountsController < Ci::BaseController
if current_user&.ci_cloud_account.present? if current_user&.ci_cloud_account.present?
return render_error('该仓库已绑定了云帐号.') return render_error('该仓库已绑定了云帐号.')
else else
cloud_account = Ci::CloudAccount.new(create_params.merge(project_id: @project.id)) cloud_account = Ci::CloudAccount.new(create_params)
cloud_account.user = current_user cloud_account.user = current_user
cloud_account.save! cloud_account.save!
end end
@ -63,23 +63,18 @@ class Ci::CloudAccountsController < Ci::BaseController
end end
def activate def activate
result = return render_error('请先在指定地址做用户认证') unless current_user.ci_certification?
if current_user.devops_has_token?
# 已有drone_token的直接激活项目
Ci::Drone::API.new(@cloud_account.drone_token, @cloud_account.drone_url, @project.owner.login, @project.identifier).activate
else
# 没有token说明是第一次激活devops, 需要用户填写token值
return render_error('请先在CI服务端做用户认证.') if !current_user.devops_verified?
Ci::Drone::API.new(params[:drone_token], @cloud_account.drone_url, @project.owner.login, @project.identifier).activate
end
if result return render_error('该项目已经激活') if @repo.repo_active?
set_drone_token!(current_user, @cloud_account, params[:drone_token])
ci_user = Ci::User.find_by(user_login: current_user.login)
begin
@repo.activate!
@project.update_column(:open_devops, true) @project.update_column(:open_devops, true)
@repo.config_trustie_pipeline @cloud_account.update_column(ci_user_id: ci_user.user_id)
render_ok render_ok
else rescue Exception => ex
render_error("激活失败请检查你的token值是否正确.") render_error(ex.message)
end end
end end

View File

@ -32,7 +32,6 @@ class Ci::ProjectsController < Ci::BaseController
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, params[:owner], params.merge(identifier: @project.identifier)) interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, params[:owner], params.merge(identifier: @project.identifier))
if interactor.success? if interactor.success?
@file = interactor.result @file = interactor.result
@repo.config_trustie_pipeline
render_result(1, "更新成功") render_result(1, "更新成功")
else else
render_error(interactor.error) render_error(interactor.error)

View File

@ -16,13 +16,12 @@ class Ci::Repo < Ci::RemoteBase
[user, repo] [user, repo]
end end
def config_trustie_pipeline def activate!
update_column(:repo_config, '.trustie-pipeline.yml') update_columns(repo_active: 1,
end repo_signer: generate_code,
repo_secret: generate_code,
def activate repo_config: '.trustie-pipeline.yml',
# repo_active、repo_signer、repo_secret、repo_updated repo_updated: Time.now.to_i)
update_columns(repo_active: 1, repo_signer: generate_code, repo_secret: generate_code, repo_updated: Time.now.to_i)
end end
end end

View File

@ -12,18 +12,14 @@ module Droneable
self.devops_step === User::DEVOPS_UNVERIFIED self.devops_step === User::DEVOPS_UNVERIFIED
end end
def devops_verified?
self.devops_step === User::DEVOPS_VERIFIED
end
def devops_has_token?
self.devops_step === User::DEVOPS_HAS_TOKEN
end
def set_drone_step!(step) def set_drone_step!(step)
self.update_column(:devops_step, step) self.update_column(:devops_step, step)
end end
def ci_certification?
Ci::User.exists?(user_login: self.login)
end
module ClassMethods module ClassMethods
end end
end end

View File

@ -10,11 +10,9 @@ class User < ApplicationRecord
# include Searchable::Dependents::User # include Searchable::Dependents::User
# devops step # devops step
# devops_step column: 0: 未填写服务器信息1: 已填写服务器信息(未认证) 2: 已认证, 3: 已填写token值 # devops_step column: 0: 未填写服务器信息1: 已填写服务器信息(未认证)
DEVOPS_UNINIT = 0 DEVOPS_UNINIT = 0
DEVOPS_UNVERIFIED = 1 DEVOPS_UNVERIFIED = 1
DEVOPS_VERIFIED = 2
DEVOPS_HAS_TOKEN = 3
# Account statuses # Account statuses
STATUS_ANONYMOUS = 0 STATUS_ANONYMOUS = 0

View File

@ -0,0 +1,5 @@
class AddCiUserIdToCiCloudAccounts < ActiveRecord::Migration[5.2]
def change
add_column :ci_cloud_accounts, :ci_user_id, :integer
end
end

View File

@ -0,0 +1,7 @@
class RemoveProjectIdFromCiCloudAccounts < ActiveRecord::Migration[5.2]
def change
remove_column :ci_cloud_accounts, :project_id
remove_column :ci_cloud_accounts, :repo_id
remove_column :ci_cloud_accounts, :drone_token
end
end