diff --git a/Gemfile b/Gemfile index effb2be6e..175ca330c 100644 --- a/Gemfile +++ b/Gemfile @@ -134,3 +134,5 @@ gem 'jwt' gem 'doorkeeper' gem 'doorkeeper-jwt' + +gem 'gitea-client', '~> 0.8.2' \ No newline at end of file diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 000000000..9170975b5 --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -0,0 +1,18 @@ +class Api::V1::BaseController < ApplicationController + + include Api::ProjectHelper + include Api::UserHelper + + before_action :doorkeeper_authorize! + skip_before_action :user_setup + + protected + def current_user + User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token + end + + def require_manager_above + @project = load_project + return render_forbidden unless current_user.admin? && @project.manager?(current_user) + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/projects/webhooks_controller.rb b/app/controllers/api/v1/projects/webhooks_controller.rb new file mode 100644 index 000000000..bb2c2172c --- /dev/null +++ b/app/controllers/api/v1/projects/webhooks_controller.rb @@ -0,0 +1,55 @@ +class Api::V1::Projects::WebhooksController < Api::V1::BaseController + before_action :require_manager_above + before_action :find_webhook, only: [:show, :update, :destroy, :tests, :hooktasks] + + def index + # @result_object = Api::V1::Projects::Webhooks::ListService.call(@project, current_user&.gitea_token) + @webhooks = @project.webhooks + @webhooks = kaminari_paginate(@webhooks) + end + + def create + @result_object = Api::V1::Projects::Webhooks::CreateService.call(@project, webhook_params, current_user&.gitea_token) + end + + def show + @result_object = Api::V1::Projects::Webhooks::GetService.call(@project, params[:id], current_user&.gitea_token) + end + + def update + @result_object = Api::V1::Projects::Webhooks::UpdateService.call(@project, params[:id], webhook_params, current_user&.gitea_token) + end + + def destroy + @result_object = Api::V1::Projects::Webhooks::DeleteService.call(@project, params[:id], current_user&.gitea_token) + if @result_object + return render_ok + else + return render_error('删除失败!') + end + end + + def tests + @result_object = Api::V1::Projects::Webhooks::TestsService.call(@project, params[:id], current_user&.gitea_token) + if @result_object + return render_ok + else + return render_error('推送失败!') + end + end + + def hooktasks + @hooktasks = @webhook.tasks.where(is_delivered: true).order("delivered desc") + @hooktasks = kaminari_paginate(@hooktasks) + end + + private + def webhook_params + params.require(:webhook).permit(:active, :branch_filter, :http_method, :url, :content_type, :secret, events: []) + end + + def find_webhook + @webhook = Gitea::Webhook.find_by_id(params[:id]) + return render_not_found unless @webhook.present? + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb new file mode 100644 index 000000000..05a3750ca --- /dev/null +++ b/app/controllers/api/v1/projects_controller.rb @@ -0,0 +1,11 @@ +class Api::V1::ProjectsController < Api::V1::BaseController + before_action :load_project, only: [:show] + + def index + render_ok + end + + def show + @result_object = Api::V1::Projects::GetService.call(@project, current_user.gitea_token) + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/users/projects_controller.rb b/app/controllers/api/v1/users/projects_controller.rb new file mode 100644 index 000000000..ac4b48324 --- /dev/null +++ b/app/controllers/api/v1/users/projects_controller.rb @@ -0,0 +1,13 @@ +class Api::V1::Users::ProjectsController < Api::V1::BaseController + before_action :load_observe_user + + def index + @object_results = Api::V1::Users::Projects::ListService.call(@observe_user, query_params, current_user) + @projects = kaminari_paginate(@object_results) + end + + private + def query_params + params.permit(:category, :is_public, :project_type, :sort_by, :sort_direction, :search) + end +end \ No newline at end of file diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb new file mode 100644 index 000000000..55f5cfb22 --- /dev/null +++ b/app/controllers/api/v1/users_controller.rb @@ -0,0 +1,6 @@ +class Api::V1::UsersController < Api::V1::BaseController + + def index + render_ok + end +end \ No newline at end of file diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 3aa98257a..79047b516 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -33,8 +33,8 @@ class AttachmentsController < ApplicationController normal_status(-1, "参数缺失") if params[:download_url].blank? url = URI.encode(params[:download_url].to_s.gsub("http:", "https:")) if url.starts_with?(base_url) - domain = Gitea.gitea_config[:domain] - api_url = Gitea.gitea_config[:base_url] + domain = GiteaService.gitea_config[:domain] + api_url = GiteaService.gitea_config[:base_url] url = url.split(base_url)[1].gsub("api", "repos").gsub('?filepath=', '/').gsub('&', '?') request_url = [domain, api_url, url, "?ref=#{params[:ref]}&access_token=#{current_user&.gitea_token}"].join response = Faraday.get(request_url) diff --git a/app/controllers/concerns/acceleratorable.rb b/app/controllers/concerns/acceleratorable.rb index 5243ac538..88ec4dd63 100644 --- a/app/controllers/concerns/acceleratorable.rb +++ b/app/controllers/concerns/acceleratorable.rb @@ -18,15 +18,15 @@ module Acceleratorable end def accelerator_domain - Gitea.gitea_config[:accelerator]["domain"] + GiteaService.gitea_config[:accelerator]["domain"] end def accelerator_username - Gitea.gitea_config[:accelerator]["access_key_id"] + GiteaService.gitea_config[:accelerator]["access_key_id"] end def config_accelerator? - Gitea.gitea_config[:accelerator].present? + GiteaService.gitea_config[:accelerator].present? end def is_foreign_url?(clone_addr) diff --git a/app/controllers/concerns/api/project_helper.rb b/app/controllers/concerns/api/project_helper.rb new file mode 100644 index 000000000..0b444c488 --- /dev/null +++ b/app/controllers/concerns/api/project_helper.rb @@ -0,0 +1,20 @@ +module Api::ProjectHelper + extend ActiveSupport::Concern + + def load_project + namespace = params[:owner] + repo = params[:repo] + + @project, @owner = Project.find_with_namespace(namespace, repo) + + if @project + logger.info "###########:project not founded" + @project + else + logger.info "###########:project not found" + @project = nil + render_not_found and return + end + @project + end +end \ No newline at end of file diff --git a/app/controllers/concerns/api/user_helper.rb b/app/controllers/concerns/api/user_helper.rb new file mode 100644 index 000000000..e6156ea56 --- /dev/null +++ b/app/controllers/concerns/api/user_helper.rb @@ -0,0 +1,19 @@ +module Api::UserHelper + extend ActiveSupport::Concern + + def load_observe_user + username = params[:owner] + + @observe_user = User.find_by(login: username) + + if @observe_user + logger.info "###########observe_user not founded" + @observe_user + else + logger.info "###########observe_user not found" + @observe_user = nil + render_not_found and return + end + @observe_user + end +end \ No newline at end of file diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 86cef7f32..3e2dc1e35 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -54,7 +54,7 @@ class RepositoriesController < ApplicationController else @entries = Gitea::Repository::Entries::ListService.new(@owner, @project.identifier, ref: @ref).call @entries = @entries.present? ? @entries.sort_by{ |hash| hash['type'] } : [] - @path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" + @path = GiteaService.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" end end @@ -99,7 +99,7 @@ class RepositoriesController < ApplicationController end end else - @path = Gitea.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" + @path = GiteaService.gitea_config[:domain]+"/#{@project.owner.login}/#{@project.identifier}/raw/branch/#{@ref}/" interactor = Repositories::EntriesInteractor.call(@owner, @project.identifier, file_path_uri, ref: @ref) if interactor.success? result = interactor.result @@ -222,7 +222,7 @@ class RepositoriesController < ApplicationController else result = Gitea::Repository::Readme::GetService.call(@owner.login, @repository.identifier, params[:ref], current_user&.gitea_token) end - @path = Gitea.gitea_config[:domain]+"/#{@owner.login}/#{@repository.identifier}/raw/branch/#{params[:ref]}/" + @path = GiteaService.gitea_config[:domain]+"/#{@owner.login}/#{@repository.identifier}/raw/branch/#{params[:ref]}/" @readme = result[:status] === :success ? result[:body] : nil @readme['content'] = decode64_content(@readme, @owner, @repository, params[:ref], @path) @readme['replace_content'] = readme_decode64_content(@readme, @owner, @repository, params[:ref], @path) @@ -240,8 +240,8 @@ class RepositoriesController < ApplicationController end def archive - domain = Gitea.gitea_config[:domain] - api_url = Gitea.gitea_config[:base_url] + domain = GiteaService.gitea_config[:domain] + api_url = GiteaService.gitea_config[:base_url] archive_url = "/repos/#{@owner.login}/#{@repository.identifier}/archive/#{Addressable::URI.escape(params[:archive])}" file_path = [domain, api_url, archive_url].join @@ -253,8 +253,8 @@ class RepositoriesController < ApplicationController end def raw - domain = Gitea.gitea_config[:domain] - api_url = Gitea.gitea_config[:base_url] + domain = GiteaService.gitea_config[:domain] + api_url = GiteaService.gitea_config[:base_url] url = "/repos/#{@owner.login}/#{@repository.identifier}/raw/#{Addressable::URI.escape(params[:filepath])}?ref=#{Addressable::URI.escape(params[:ref])}" file_path = [domain, api_url, url].join @@ -388,4 +388,4 @@ class RepositoriesController < ApplicationController end end -end \ No newline at end of file +end diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index a44d2b35d..c1c8b3670 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -1115,15 +1115,15 @@ await octokit.request('GET /api/yystopf/csfjkkj/contributors.json') ```shell curl -X GET \ -http://localhost:3000/api/yystopf/ceshi/webhooks.json +http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json ``` ```javascript -await octokit.request('GET /api/yystopf/ceshi/webhooks.json') +await octokit.request('GET /api/v1/yystopf/ceshi/webhooks.json') ``` ### HTTP 请求 -`GET /api/:owner/:repo/webhooks.json` +`GET /api/v1/:owner/:repo/webhooks.json` ### 请求参数: 参数 | 必选 | 默认 | 类型 | 字段说明 @@ -1139,7 +1139,6 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') |url |string|地址| |http_method |string|请求方式| |is_active |bool |是否激活| -|type |string|类型| |last_status |string|最后一次推送的状态| |create_time |string|创建时间| @@ -1155,7 +1154,6 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') "url": "https://oapi.dingtalk.com/robot/send?access_token=7e1e19d0eddb6a5e33c5c2c4e66f4c88f9437184b9ed2c2653194c6374c7d513", "http_method": "", "is_active": true, - "type": "dingtalk", "last_status": "succeed", "create_time": "2021-07-12 10:50:07" }, @@ -1164,7 +1162,6 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') "url": "http://localhost:3000", "http_method": "GET", "is_active": true, - "type": "gitea", "last_status": "succeed", "create_time": "2021-07-26 10:03:45" }, @@ -1173,7 +1170,6 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') "url": "http://localhost:10081", "http_method": "POST", "is_active": true, - "type": "gitea", "last_status": "waiting", "create_time": "2021-07-26 16:56:53" }, @@ -1182,7 +1178,6 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') "url": "http://localhost:3001", "http_method": "POST", "is_active": true, - "type": "gitea", "last_status": "fail", "create_time": "2021-07-26 16:58:23" } @@ -1200,15 +1195,15 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks.json') ```shell curl -X GET \ -http://localhost:3000/api/yystopf/ceshi/webhooks/3/edit.json +http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3.json ``` ```javascript -await octokit.request('GET /api/yystopf/ceshi/webhooks/3/edit.json') +await octokit.request('GET /api/v1/yystopf/ceshi/webhooks/3.json') ``` ### HTTP 请求 -`GET /api/:owner/:repo/webhooks/:id/edit.json` +`GET /api/v1/:owner/:repo/webhooks/:id.json` ### 请求参数: 参数 | 必选 | 默认 | 类型 | 字段说明 @@ -1226,36 +1221,21 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks/3/edit.json') |content_type |string|POST Content Type| |http_method |string|请求方式| |secret| |string|密钥| -|is_active |bool |是否激活| -|type |string|类型| -|last_status |string|最后一次推送的状态, waiting 等待,fail 失败,succeed 成功| +|active |bool |是否激活| |branch_filter |string|分支过滤| |events |string|触发条件| -|create_time |string|创建时间| +|create_at |string|创建时间| 参数| 含义| --------- | ------- | ------- | |create|创建分支或标签| |delete|分支或标签删除| -|fork|仓库被fork| |push|git仓库推送| -|issue|疑修已打开、已关闭、已重新打开或编辑| -|issue_assign|疑修被指派| -|issue_label|疑修标签被更新或删除| -|issue_milestone|疑修被收入里程碑| -|issue_comment|疑修评论| |pull_request|合并请求| |pull_request_assign|合并请求被指派| -|pull_request_label|合并请求被贴上标签| -|pull_request_milestone|合并请求被记录于里程碑中| -|pull_request_comment|合并请求被评论| |pull_request_review_approved|合并请求被批准| |pull_request_review_rejected|合并请求被拒绝| -|pull_request_review_comment|合并请求被提出审查意见| -|pull_request_sync|合并请求被同步| -|repository|创建或删除仓库| -|release|版本发布| > 返回的JSON示例: @@ -1266,31 +1246,15 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks/3/edit.json') "http_method": "GET", "content_type": "form", "url": "http://localhost:3000", - "secret": "123456", - "last_status": "succeed", - "is_active": true, - "type": "gitea", - "create_time": "2021-07-26 10:03:45", + "active": true, + "create_at": "2021-07-26 10:03", "branch_filter": "*", "events": [ "create", "delete", - "fork", - "issues", - "issue_assign", - "issue_label", - "issue_milestone", - "issue_comment", "push", "pull_request", "pull_request_assign", - "pull_request_label", - "pull_request_milestone", - "pull_request_comment", - "pull_request_review", - "pull_request_sync", - "repository", - "release" ] } ``` @@ -1305,15 +1269,15 @@ await octokit.request('GET /api/yystopf/ceshi/webhooks/3/edit.json') ```shell curl -X POST \ -http://localhost:3000/api/yystopf/ceshi/webhooks.json +http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json ``` ```javascript -await octokit.request('POST /api/yystopf/ceshi/webhooks.json') +await octokit.request('POST /api/v1/yystopf/ceshi/webhooks.json') ``` ### HTTP 请求 -`POST /api/:owner/:repo/webhooks.json` +`POST /api/v1/:owner/:repo/webhooks.json` ### 请求参数: 参数 | 必选 | 默认 | 类型 | 字段说明 @@ -1321,7 +1285,6 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') |owner |是| | string |用户登录名 | |repo |是| | string |项目标识identifier | |webhook.url |是| | string |目标url | -|webhook.type |否| | string |类型| |webhook.http_method |是| | string | http方法, POST和GET | |webhook.content_type |是| | string | POST Content Type | |webhook.secret |否| | string |密钥文本| @@ -1335,24 +1298,11 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') --------- | ------- | ------- | |create|创建分支或标签| |delete|分支或标签删除| -|fork|仓库被fork| |push|git仓库推送| -|issue|疑修已打开、已关闭、已重新打开或编辑| -|issue_assign|疑修被指派| -|issue_label|疑修标签被更新或删除| -|issue_milestone|疑修被收入里程碑| -|issue_comment|疑修评论| |pull_request|合并请求| |pull_request_assign|合并请求被指派| -|pull_request_label|合并请求被贴上标签| -|pull_request_milestone|合并请求被记录于里程碑中| -|pull_request_comment|合并请求被评论| |pull_request_review_approved|合并请求被批准| |pull_request_review_rejected|合并请求被拒绝| -|pull_request_review_comment|合并请求被提出审查意见| -|pull_request_sync|合并请求被同步| -|repository|创建或删除仓库| -|release|版本发布| > 请求的JSON示例: @@ -1385,15 +1335,22 @@ await octokit.request('POST /api/yystopf/ceshi/webhooks.json') ```json { - "id": 18, - "type": "gitea", + "id": 68, "content_type": "json", - "url": "http://localhost:10000", + "http_method": "GET", + "url": "http://127.0.0.1:3000", "events": [ - "push" + "create", + "delete", + "push", + "pull_request", + "pull_request_assign", + "pull_request_review_approved", + "pull_request_review_rejected" ], "active": true, - "create_time": "2021-07-26 18:53:43" + "branch_filter": "*", + "created_at": "2022-06-23 15:52" } ```