add hooks controler and related services

This commit is contained in:
sylor_huang@126.com 2020-05-19 11:57:55 +08:00
parent 63cf2f0dc8
commit 0b89372eef
9 changed files with 167 additions and 14 deletions

View File

@ -0,0 +1,85 @@
class HooksController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :find_project_with_id
before_action :set_repository
def index
hooks_response = Gitea::Hooks::ListService.new(@user, @repository.try(:identifier)).call
if hooks_response.status == 200
lists = JSON.parse(hooks_response.body)
@hooks_size = lists.size
@hooks = paginate(lists)
else
normal_status(-1, "出现错误")
end
end
def create
# hook_params = {
# active: true,
# type: "gitea",
# branch_filter: "",
# config: {
# content_type: "application/json",
# url: "#{EduSetting.get("host_name")}/repositories/#{project.id}/repo_hooks.json",
# http_method: "post"
# },
# events: ["create", "pull", "push"],
# }
# hook_params = {
# is_active: params[:is_active] || false,
# type: params[:type],
# http_method: params[:http_method] || "POST",
# content_type: params[:content_type].to_i,
# secret: params[:secret],
# events: {
# push_only: params[:push_only] || false, # 是否为推送事件
# send_everything: params[:send_everything] || false, #是否为所有事件
# choose_events: params[:choose_events] || false, #是否为自定义事件
# branch_filter: params[:branch_filter] || "*",
# events: {
# create: params[:create] || false, #创建分支/标签
# delete: params[:delete] || false, #删除分支/标签
# fork: params[:fork] || false, #仓库被派生
# issues: params[:issues] || false, #工单
# issue_comment: params[:issue_comment] || false, #评论
# push: params[:push] || false # 推送
# pull_request: params[:pull_request] || false #合并请求
# repository: params[:repository] || false #仓库
# release: params[:release] || false #版本发布
# }
# }
# }
hook_params = params[:hook_params].compact
Gitea::Hooks::CreateService.new(@user, @repository.try(:identifier), hook_params).call #创建gitea的hook功能
end
def update
hook_params = params[:hook_params].compact
response = Gitea::Hooks::UpdateService.new(@user, @repository.try(:identifier), hook_params, params[:id]).call
if response.status == 200
normal_status(1, "更新成功")
else
normal_status(-1, "更新失败")
end
end
def destroy
response = Gitea::Hooks::DestroyService.new(@user, @repository.try(:identifier), params[:id]).call
if response.status == 204
normal_status(1, "删除成功")
else
normal_status(-1, "删除失败")
end
end
private
def set_repository
@repository = @project.repository
@user = @project.owner
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
end
end

View File

@ -0,0 +1,2 @@
module HooksHelper
end

View File

@ -1,4 +1,4 @@
class Gitea::Repository::Hooks::CreateService < Gitea::ClientService
class Gitea::Hooks::CreateService < Gitea::ClientService
attr_reader :user, :repo_name, :body
def initialize(user, repo_name, body)

View File

@ -0,0 +1,23 @@
class Gitea::Hooks::DestroyService < Gitea::ClientService
attr_reader :user, :repo_name,:hook_id
def initialize(user, repo_name, hook_id)
@user = user
@repo_name = repo_name
@hook_id = hook_id
end
def call
response = delete(url, params)
end
private
def params
Hash.new.merge(token: user.gitea_token)
end
def url
"/repos/#{user.login}/#{repo_name}/hooks/#{hook_id}".freeze
end
end

View File

@ -0,0 +1,23 @@
class Gitea::Hooks::ListService < Gitea::ClientService
attr_reader :user, :repo_name
def initialize(user, repo_name)
@user = user
@repo_name = repo_name
@body = body
end
def call
response = get(url, params)
end
private
def params
Hash.new.merge(token: user.gitea_token)
end
def url
"/repos/#{user.login}/#{repo_name}/hooks".freeze
end
end

View File

@ -0,0 +1,24 @@
class Gitea::Hooks::UpdateService < Gitea::ClientService
attr_reader :user, :repo_name, :body,:hook_id
def initialize(user, repo_name, body, hook_id)
@user = user
@repo_name = repo_name
@body = body
@hook_id = hook_id
end
def call
response = patch(url, params)
end
private
def params
body.merge(token: user.gitea_token)
end
def url
"/repos/#{user.login}/#{repo_name}/hooks/#{hook_id}".freeze
end
end

View File

@ -15,25 +15,14 @@ class Repositories::CreateService < ApplicationService
sync_project(@repository, gitea_repository)
sync_repository(@repository, gitea_repository)
if project.project_type == "common"
hook_params = {
active: true,
type: "gitea",
branch_filter: "",
config: {
content_type: "application/json",
url: "#{EduSetting.get("host_name")}/repositories/#{project.id}/repo_hooks.json",
http_method: "post"
},
events: ["create", "pull", "push"],
}
chain_params = {
type: "create",
ownername: user.try(:login),
reponame: @repository.try(:id)
}
ProjectCreateChainJob.perform_later(chain_params) #创建上链操作
#暂时gitea的hook功能未完善
#Gitea::Repository::Hooks::CreateService.new(user, @repository.try(:identifier), hook_params).call #创建gitea的hook功能
end
end
@repository

View File

@ -0,0 +1,6 @@
json.count @hooks_size
json.hooks do
json.array! @hooks.each do |h|
json.merge! h
end
end

View File

@ -53,6 +53,7 @@ Rails.application.routes.draw do
end
end
resources :projects do
resources :hooks
resources :pull_requests, except: [:destroy] do
member do
post :pr_merge