ADD create gitea webhook api
This commit is contained in:
parent
8c908a35ac
commit
eb2f1f46e0
|
@ -30,6 +30,9 @@ class Ci::CloudAccountsController < Ci::BaseController
|
||||||
ci_user = Ci::User.find_by(user_login: current_user.login)
|
ci_user = Ci::User.find_by(user_login: current_user.login)
|
||||||
repo = Ci::Repo.where(repo_namespace: current_user.login, repo_name: params[:repo]).first
|
repo = Ci::Repo.where(repo_namespace: current_user.login, repo_name: params[:repo]).first
|
||||||
begin
|
begin
|
||||||
|
bind_result = bind_hook!(current_user, @cloud_account, @repo)
|
||||||
|
return render_error('hook激活失败') unless bind_result
|
||||||
|
|
||||||
repo.activate!(ci_user.user_id)
|
repo.activate!(ci_user.user_id)
|
||||||
@project.update_column(:open_devops, true)
|
@project.update_column(:open_devops, true)
|
||||||
@cloud_account.update_column(:ci_user_id, ci_user.user_id)
|
@cloud_account.update_column(:ci_user_id, ci_user.user_id)
|
||||||
|
@ -149,4 +152,17 @@ class Ci::CloudAccountsController < Ci::BaseController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def bind_hook!(user, cloud_account, repo)
|
||||||
|
hook_params = {
|
||||||
|
"active": true,
|
||||||
|
"config": {
|
||||||
|
"content_type": "json",
|
||||||
|
"url": cloud_account.drone_url + "/hook?secret=#{repo.repo_signer}"
|
||||||
|
},
|
||||||
|
"type": "gitea"
|
||||||
|
}
|
||||||
|
result = Gitea::Hooks::CreateService.call(user.gitea_token, user.login, repo.repo_name, hook_params)
|
||||||
|
result.status == 201 ? true : false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +1,39 @@
|
||||||
class Gitea::Hooks::CreateService < Gitea::ClientService
|
class Gitea::Hooks::CreateService < Gitea::ClientService
|
||||||
attr_reader :user, :repo_name, :body
|
attr_reader :token, :owner, :repo, :body
|
||||||
|
|
||||||
def initialize(user, repo_name, body)
|
# body params:
|
||||||
@user = user
|
# {
|
||||||
@repo_name = repo_name
|
# "active": false,
|
||||||
@body = body
|
# "branch_filter": "string",
|
||||||
|
# "config": {
|
||||||
|
# "content_type": "string",
|
||||||
|
# "url": "string"
|
||||||
|
# },
|
||||||
|
# "events": [
|
||||||
|
# "create"
|
||||||
|
# ],
|
||||||
|
# "type": "gitea"
|
||||||
|
# }
|
||||||
|
# eg:
|
||||||
|
# Gitea::Hooks::CreateService.call(user.gitea_token, user.login, repo.identifier, body)
|
||||||
|
def initialize(token, owner, repo, body)
|
||||||
|
@token = token
|
||||||
|
@owner = owner
|
||||||
|
@repo = repo
|
||||||
|
@body = body
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
response = post(url, params)
|
post(url, params)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def params
|
def params
|
||||||
body.merge(token: user.gitea_token)
|
Hash.new.merge(token: token, data: body).compact!
|
||||||
end
|
end
|
||||||
|
|
||||||
def url
|
def url
|
||||||
"/repos/#{user.login}/#{repo_name}/hooks".freeze
|
"/repos/#{owner}/#{repo}/hooks".freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue