From e76d475fefb3c74bf216763c5447b3474adb6bcd Mon Sep 17 00:00:00 2001 From: jasder Date: Tue, 6 Jul 2021 11:04:00 +0800 Subject: [PATCH 01/13] FIX show realname for create project --- app/views/owners/index.json.jbuilder | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/owners/index.json.jbuilder b/app/views/owners/index.json.jbuilder index 677aff71a..6b7e5222e 100644 --- a/app/views/owners/index.json.jbuilder +++ b/app/views/owners/index.json.jbuilder @@ -2,6 +2,6 @@ json.total_count @owners.size json.owners @owners.each do |owner| json.id owner.id json.type owner.type - json.name owner.login + json.name owner&.show_real_name json.avatar_url url_to_avatar(owner) end \ No newline at end of file From 3614f3e15b700d16c0b4ae5910d296975d437d41 Mon Sep 17 00:00:00 2001 From: jasder Date: Fri, 9 Jul 2021 16:57:11 +0800 Subject: [PATCH 02/13] =?UTF-8?q?FIX=20=E8=A7=A3=E5=86=B3=E7=A7=81?= =?UTF-8?q?=E6=9C=89=E7=89=88=E6=9C=AC=E5=BA=93=E5=BA=93=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E3=80=81=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/helpers/repositories_helper.rb | 37 ++++++++++++++++++- .../repositories/_simple_entry.json.jbuilder | 11 +++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index e704acb83..95c669b76 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -15,7 +15,7 @@ module RepositoriesHelper end def image_type?(str) - default_type = %w(png jpg gif tif psd svg) + default_type = %w(png jpg gif tif psd svg gif bmp webp jpeg) default_type.include?(str&.downcase) end @@ -86,4 +86,39 @@ module RepositoriesHelper render_decode64_content(entry['content']) end end + + def base64_to_image(path, content) + # generate to https://git.trusite.net/pawm36ozq/-/raw/branch/master/entrn.png" + content = Base64.decode64(content) + File.open(path, 'wb') { |f| f.write(content) } + end + + def render_download_image_url(dir_path, file_path, content) + # 用户名/项目标识/文件路径 + dir_path = generate_dir_path(dir_path) + + file_path = [dir_path, file_path].join('/') + + puts "##### render_download_image_url file_path: #{file_path}" + base64_to_image(file_path, content) + + file_path = file_path.split('public')[1] + File.join(base_url, file_path) + end + + def generate_dir_path(dir_path) + # tmp_dir_path + # eg: jasder/forgeplus/raw/branch/ref + dir_path = ["public", tmp_dir, dir_path].join('/') + puts "#### dir_path: #{dir_path}" + unless Dir.exists?(dir_path) + FileUtils.mkdir_p(dir_path) ##不成功这里会抛异常 + end + dir_path + end + + def tmp_dir + "repo" + end + end diff --git a/app/views/repositories/_simple_entry.json.jbuilder b/app/views/repositories/_simple_entry.json.jbuilder index ec2a045bb..7d41bbd23 100644 --- a/app/views/repositories/_simple_entry.json.jbuilder +++ b/app/views/repositories/_simple_entry.json.jbuilder @@ -11,7 +11,16 @@ if @project.forge? json.content decode64_content(entry, @owner, @repository, @ref) json.target entry['target'] - json.download_url entry['download_url'] + + download_url = + if image_type + dir_path = [@owner.login, @repository.identifier, "raw/branch", @ref].join('/') + render_download_image_url(dir_path, entry['path'], decode64_content(entry, @owner, @repository, @ref)) + else + entry['download_url'] + end + json.download_url download_url + json.direct_download direct_download json.image_type image_type json.is_readme_file is_readme?(entry['type'], entry['name']) From d271a684fa053bd891866435437b5f63de08216b Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 12 Jul 2021 17:24:23 +0800 Subject: [PATCH 03/13] add: compare code return can merge message --- app/controllers/compare_controller.rb | 36 +++++++++++++++++++++------ app/views/compare/show.json.jbuilder | 1 + 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/controllers/compare_controller.rb b/app/controllers/compare_controller.rb index 63ca58aa6..4b6860153 100644 --- a/app/controllers/compare_controller.rb +++ b/app/controllers/compare_controller.rb @@ -6,26 +6,48 @@ class CompareController < ApplicationController end def show + load_compare_params compare + @merge_message = get_merge_message end private + def get_merge_message + if @base.blank? || @head.blank? + return "请选择分支" + else + if @head.include?(":") + fork_project = @project.forked_projects.joins(:owner).where(users: {login: @head.to_s.split("/")[0]}).take + return "请选择正确的仓库" unless fork_project.present? + @exist_pullrequest = @project.pull_requests.where(is_original: true, head: @head.to_s.split(":")[1], base: @base, status: 0, fork_project_id: fork_project.id).take + else + @exist_pullrequest = @project.pull_requests.where(is_original: false, head: @base, base: @head, status: 0).take + end + if @exist_pullrequest.present? + return "在这些分支之间的合并请求已存在:#{@exist_pullrequest.try(:title)}" + else + if @compare_result["Commits"].blank? && @compare_result["Diff"].blank? + return "分支内容相同,无需创建合并请求" + end + end + end + return "可以合并" + end + def compare - base, head = compare_params # TODO: 处理fork的项目向源项目发送PR的base、head参数问题 @compare_result ||= - head.include?(":") ? gitea_compare(base, head) : gitea_compare(head, base) + @head.include?(":") ? gitea_compare(@base, @head) : gitea_compare(@head, @base) end - def compare_params - base = Addressable::URI.unescape(params[:base]) - head = params[:head].include?('json') ? params[:head]&.split('.json')[0] : params[:head] + def load_compare_params + @base = Addressable::URI.unescape(params[:base]) + @head = params[:head].include?('json') ? params[:head]&.split('.json')[0] : params[:head] - [base, head] end def gitea_compare(base, head) - Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, base, head) + Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, @base, @head) end end diff --git a/app/views/compare/show.json.jbuilder b/app/views/compare/show.json.jbuilder index 8b75fa3dd..91260e560 100644 --- a/app/views/compare/show.json.jbuilder +++ b/app/views/compare/show.json.jbuilder @@ -83,3 +83,4 @@ json.diff do end end +json.message @merge_message \ No newline at end of file From 4929734a1a30dc57e144f9e4cf4f4bbd6fa8781d Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 12 Jul 2021 17:57:42 +0800 Subject: [PATCH 04/13] fix --- app/controllers/compare_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/compare_controller.rb b/app/controllers/compare_controller.rb index 4b6860153..71f495637 100644 --- a/app/controllers/compare_controller.rb +++ b/app/controllers/compare_controller.rb @@ -48,6 +48,6 @@ class CompareController < ApplicationController end def gitea_compare(base, head) - Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, @base, @head) + Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, base, head) end end From de2b54c39e05e1f6d19cf72f645dfd8b6331378a Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 12 Jul 2021 18:10:26 +0800 Subject: [PATCH 05/13] add: compare code return can merge status --- app/controllers/compare_controller.rb | 12 ++++++------ app/views/compare/show.json.jbuilder | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/compare_controller.rb b/app/controllers/compare_controller.rb index 71f495637..b36ffde36 100644 --- a/app/controllers/compare_controller.rb +++ b/app/controllers/compare_controller.rb @@ -8,30 +8,30 @@ class CompareController < ApplicationController def show load_compare_params compare - @merge_message = get_merge_message + @merge_status, @merge_message = get_merge_message end private def get_merge_message if @base.blank? || @head.blank? - return "请选择分支" + return -2, "请选择分支" else if @head.include?(":") fork_project = @project.forked_projects.joins(:owner).where(users: {login: @head.to_s.split("/")[0]}).take - return "请选择正确的仓库" unless fork_project.present? + return -2, "请选择正确的仓库" unless fork_project.present? @exist_pullrequest = @project.pull_requests.where(is_original: true, head: @head.to_s.split(":")[1], base: @base, status: 0, fork_project_id: fork_project.id).take else @exist_pullrequest = @project.pull_requests.where(is_original: false, head: @base, base: @head, status: 0).take end if @exist_pullrequest.present? - return "在这些分支之间的合并请求已存在:#{@exist_pullrequest.try(:title)}" + return -2, "在这些分支之间的合并请求已存在:#{@exist_pullrequest.try(:title)}" else if @compare_result["Commits"].blank? && @compare_result["Diff"].blank? - return "分支内容相同,无需创建合并请求" + return -2, "分支内容相同,无需创建合并请求" end end end - return "可以合并" + return 0, "可以合并" end def compare diff --git a/app/views/compare/show.json.jbuilder b/app/views/compare/show.json.jbuilder index 91260e560..0037d8be8 100644 --- a/app/views/compare/show.json.jbuilder +++ b/app/views/compare/show.json.jbuilder @@ -83,4 +83,5 @@ json.diff do end end +json.status @merge_status json.message @merge_message \ No newline at end of file From fd3fab99aec44f17681d41e43a1b4b912f1ecacc Mon Sep 17 00:00:00 2001 From: viletyy Date: Wed, 14 Jul 2021 09:55:12 +0800 Subject: [PATCH 06/13] fix: compare code need token --- app/controllers/compare_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/compare_controller.rb b/app/controllers/compare_controller.rb index b36ffde36..bc81da563 100644 --- a/app/controllers/compare_controller.rb +++ b/app/controllers/compare_controller.rb @@ -48,6 +48,6 @@ class CompareController < ApplicationController end def gitea_compare(base, head) - Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, base, head) + Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, base, head, current_user.gitea_token) end end From 2ddf1dbd0188206152cd532fa73e790f17c2ccaa Mon Sep 17 00:00:00 2001 From: viletyy Date: Wed, 14 Jul 2021 11:48:52 +0800 Subject: [PATCH 07/13] add: gitea user keys api --- .../gitea/user/keys/create_service.rb | 21 +++++++++++++++ .../gitea/user/keys/delete_service.rb | 22 ++++++++++++++++ app/services/gitea/user/keys/get_service.rb | 22 ++++++++++++++++ app/services/gitea/user/keys/list_service.rb | 26 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 app/services/gitea/user/keys/create_service.rb create mode 100644 app/services/gitea/user/keys/delete_service.rb create mode 100644 app/services/gitea/user/keys/get_service.rb create mode 100644 app/services/gitea/user/keys/list_service.rb diff --git a/app/services/gitea/user/keys/create_service.rb b/app/services/gitea/user/keys/create_service.rb new file mode 100644 index 000000000..2b720c483 --- /dev/null +++ b/app/services/gitea/user/keys/create_service.rb @@ -0,0 +1,21 @@ +class Gitea::User::Keys::CreateService < Gitea::ClientService + attr_reader :token, :params + def initialize(token, params) + @token = token + @params = params + end + + def call + response = post(url, request_params) + render_response(response) + end + + private + def request_params + Hash.new.merge({token: token, data: params}) + end + + def url + '/user/keys'.freeze + end +end \ No newline at end of file diff --git a/app/services/gitea/user/keys/delete_service.rb b/app/services/gitea/user/keys/delete_service.rb new file mode 100644 index 000000000..2e6a125fd --- /dev/null +++ b/app/services/gitea/user/keys/delete_service.rb @@ -0,0 +1,22 @@ +class Gitea::User::Keys::DeleteService < Gitea::ClientService + attr_reader :token, :key_id + + def initialize(token, key_id) + @token = token + @key_id = key_id + end + + def call + delete(url, params) + end + + private + + def params + Hash.new.merge(token: token) + end + + def url + "/user/keys/#{key_id}".freeze + end +end diff --git a/app/services/gitea/user/keys/get_service.rb b/app/services/gitea/user/keys/get_service.rb new file mode 100644 index 000000000..fbcf1c86e --- /dev/null +++ b/app/services/gitea/user/keys/get_service.rb @@ -0,0 +1,22 @@ +class Gitea::User::Keys::GetService < Gitea::ClientService + attr_reader :token, :key_id + + def initialize(token, key_id) + @token = token + @key_id = key_id + end + + def call + response = get(url, params) + render_response(response) + end + + private + def params + Hash.new.merge({token: token}) + end + + def url + "/user/keys/#{key_id}".freeze + end +end \ No newline at end of file diff --git a/app/services/gitea/user/keys/list_service.rb b/app/services/gitea/user/keys/list_service.rb new file mode 100644 index 000000000..1ddfde413 --- /dev/null +++ b/app/services/gitea/user/keys/list_service.rb @@ -0,0 +1,26 @@ +class Gitea::User::Keys::ListService < Gitea::ClientService + attr_reader :token, :page, :limit, :fingerprint + + def initialize(token, page, limit, fingerprint="") + @token = token + @page = page + @limit = limit + @fingerprint = fingerprint + end + + def call + response = get(url, params) + render_response(response) + end + + private + + def params + Hash.new.merge({token: token, fingerprint: fingerprint, page: page, limit: limit}) + end + + def url + '/user/keys'.freeze + end + +end \ No newline at end of file From 998381585a3db76c2949bf7dfa37f3e37500affd Mon Sep 17 00:00:00 2001 From: viletyy Date: Wed, 14 Jul 2021 15:38:14 +0800 Subject: [PATCH 08/13] add: user public keys controller --- app/controllers/public_keys_controller.rb | 62 +++++ app/docs/slate/source/api.html.md | 1 + .../slate/source/includes/_public_keys.md | 158 +++++++++++ app/models/gitea/public_key.rb | 9 + app/models/user.rb | 1 + app/views/public_keys/create.json.jbuilder | 5 + app/views/public_keys/index.json.jbuilder | 5 + config/routes.rb | 2 + public/docs/api.html | 259 ++++++++++++++++++ 9 files changed, 502 insertions(+) create mode 100644 app/controllers/public_keys_controller.rb create mode 100644 app/docs/slate/source/includes/_public_keys.md create mode 100644 app/models/gitea/public_key.rb create mode 100644 app/views/public_keys/create.json.jbuilder create mode 100644 app/views/public_keys/index.json.jbuilder diff --git a/app/controllers/public_keys_controller.rb b/app/controllers/public_keys_controller.rb new file mode 100644 index 000000000..a16266abd --- /dev/null +++ b/app/controllers/public_keys_controller.rb @@ -0,0 +1,62 @@ +class PublicKeysController < ApplicationController + before_action :require_login + before_action :find_public_key, only: [:destroy] + + def index + @public_keys = current_user.public_keys + @public_keys = kaminari_paginate(@public_keys) + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + end + + def create + return render_error("请输入密钥") if public_key_params[:key].blank? + return render_error("请输入标题") if public_key_params[:title].blank? + @gitea_response = Gitea::User::Keys::CreateService.call(current_user.gitea_token, public_key_params) + if @gitea_response[0] == 201 + @public_key = @gitea_response[2] + puts @public_key + else + return render_error if @gitea_response[2]["message"].nil? + return render_error("密钥格式不正确") if @gitea_response[2]["message"].starts_with?("Invalid key content") + return render_error("密钥已存在,请勿重复添加") if @gitea_response[2]["message"].starts_with?("Key content has been used as non-deploy key") + puts @gitea_response[2] + @public_key = nil + end + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + end + + def destroy + return render_not_found unless @public_key.present? + if @public_key.destroy + render_ok + else + render_error + end + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + end + + private + + def page + params[:page].to_i.zero? ? 1 : params[:page].to_i + end + + def limit + limit = params[:limit] || params[:per_page] + limit = (limit.to_i.zero? || limit.to_i > 15) ? 15 : limit.to_i + end + + def public_key_params + params.require(:public_key).permit(:key, :title) + end + + def find_public_key + @public_key = current_user.public_keys.find_by_id(params[:id]) + end +end \ No newline at end of file diff --git a/app/docs/slate/source/api.html.md b/app/docs/slate/source/api.html.md index a08823a4e..a846317c4 100644 --- a/app/docs/slate/source/api.html.md +++ b/app/docs/slate/source/api.html.md @@ -12,6 +12,7 @@ toc_footers: includes: - licenses - gitignores + - public_keys - users - projects - repositories diff --git a/app/docs/slate/source/includes/_public_keys.md b/app/docs/slate/source/includes/_public_keys.md new file mode 100644 index 000000000..60d73c01a --- /dev/null +++ b/app/docs/slate/source/includes/_public_keys.md @@ -0,0 +1,158 @@ + +# PublicKeys + +## public_keys列表 +获取public_keys列表,支持分页 + +> 示例: + +```shell +curl -X GET \ +http://localhost:3000/api/public_keys.json +``` + +```javascript +await octokit.request('GET /api/public_keys.json') +``` + +### HTTP 请求 +`GET api/public_keys.json` + +### 请求参数 +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +page |否| 1 | int | 页码 | +limit |否| 15 | int | 每页数量 | + +### 返回字段说明 +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +total_count |int |总数 | +public_keys.id |int |ID| +public_keys.name |string|密钥标题| +public_keys.content |string|密钥内容| +public_keys.fingerprint |string|密钥标识| +public_keys.created_time |string|密钥创建时间| + + +> 返回的JSON示例: + +```json +{ + "total_count": 1, + "public_keys": [ + { + "id": 16, + "name": "xxx", + "content": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com", + "fingerprint": "SHA256:cU8AK/+roqUUyiaYXIdS2Nj4+Rb2p6rqWSeRDc+aqKM", + "created_unix": 1626246596, + "created_time": "2021/07/14 15:09" + } + ] +} +``` + + +## 创建public_key +创建public_key + +> 示例: + +```shell +curl -X POST \ +http://localhost:3000/api/public_keys.json +``` + +```javascript +await octokit.request('POST /api/public_keys.json') +``` + +### HTTP 请求 +`POST api/public_keys.json` + +### 请求参数 +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +key |是 | 否 | string | 密钥 | +title |是 | 否 | string | 密钥标题 | + +> 请求的JSON示例: +```json +{ + "public_key": { + "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com", + "title": "xxx" + } +} +``` + +### 返回字段说明 +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +total_count |int |总数 | +id |int |ID| +name |string|密钥标题| +content |string|密钥内容| +fingerprint |string|密钥标识| +created_time |string|密钥创建时间| + + +> 返回的JSON示例: + +```json +{ + "id": 17, + "name": "xxx", + "content": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com", + "fingerprint": "SHA256:cU8AK/+roqUUyiaYXIdS2Nj4+Rb2p6rqWSeRDc+aqKM", + "created_time": "2021/07/14 15:26" +} +``` + + + +## 删除public_key +删除public_key + +> 示例: + +```shell +curl -X DELETE \ +http://localhost:3000/api/public_keys/:id.json +``` + +```javascript +await octokit.request('DELETE /api/public_keys/:id.json') +``` + +### HTTP 请求 +`DELETE api/public_keys/:id.json` + +### 请求参数 +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +id |是 | 否 | int | 密钥ID | + + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` + + diff --git a/app/models/gitea/public_key.rb b/app/models/gitea/public_key.rb new file mode 100644 index 000000000..bc37c3bc7 --- /dev/null +++ b/app/models/gitea/public_key.rb @@ -0,0 +1,9 @@ +class Gitea::PublicKey < Gitea::Base + self.inheritance_column = nil # FIX The single-table inheritance mechanism failed + # establish_connection :gitea_db + + self.table_name = "public_key" + + belongs_to :user, class_name: '::User', foreign_key: :gitea_uid, primary_key: :owner_id, optional: true + +end diff --git a/app/models/user.rb b/app/models/user.rb index c70678533..cbf0c66f7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -169,6 +169,7 @@ class User < Owner accepts_nested_attributes_for :is_pinned_projects has_many :issues, dependent: :destroy, foreign_key: :author_id has_many :pull_requests, dependent: :destroy + has_many :public_keys, class_name: "Gitea::PublicKey",primary_key: :gitea_uid, foreign_key: :owner_id, dependent: :destroy # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } diff --git a/app/views/public_keys/create.json.jbuilder b/app/views/public_keys/create.json.jbuilder new file mode 100644 index 000000000..bcbcb9451 --- /dev/null +++ b/app/views/public_keys/create.json.jbuilder @@ -0,0 +1,5 @@ +json.id @public_key["id"] +json.name @public_key["title"] +json.content @public_key["key"] +json.fingerprint @public_key["fingerprint"] +json.created_time @public_key["created_at"].to_time.strftime("%Y/%m/%d %H:%M") \ No newline at end of file diff --git a/app/views/public_keys/index.json.jbuilder b/app/views/public_keys/index.json.jbuilder new file mode 100644 index 000000000..6710529bb --- /dev/null +++ b/app/views/public_keys/index.json.jbuilder @@ -0,0 +1,5 @@ +json.total_count @public_keys.total_count +json.public_keys @public_keys do |public_key| + json.(public_key, :id, :name, :content, :fingerprint, :created_unix) + json.created_time Time.at(public_key.created_unix).strftime("%Y/%m/%d %H:%M") +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index de2206fdc..0cc16406c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,6 +71,8 @@ Rails.application.routes.draw do # end end + resources :public_keys, only: [:index, :create, :destroy] + resources :statistic, only: [:index] do collection do get :platform_profile diff --git a/public/docs/api.html b/public/docs/api.html index 5061a1ac2..47e51427b 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -325,6 +325,20 @@ +
  • + PublicKeys + +
  • Users
      @@ -626,6 +640,251 @@ http://localhost:3000/api/ignores.json + +

      PublicKeys

      public_keys列表

      +

      获取public_keys列表,支持分页

      + +
      +

      示例:

      +
      +
      curl -X GET \
      +http://localhost:3000/api/public_keys.json
      +
      await octokit.request('GET /api/public_keys.json')
      +

      HTTP 请求

      +

      GET api/public_keys.json

      +

      请求参数

      + + + + + + + + + + + + + + + + + + + + + + + +
      参数必选默认类型字段说明
      page1int页码
      limit15int每页数量
      +

      返回字段说明

      + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      参数类型字段说明
      total_countint总数
      public_keys.idintID
      public_keys.namestring密钥标题
      public_keys.contentstring密钥内容
      public_keys.fingerprintstring密钥标识
      public_keys.created_timestring密钥创建时间
      + +
      +

      返回的JSON示例:

      +
      +
      {
      +    "total_count": 1,
      +    "public_keys": [
      +        {
      +            "id": 16,
      +            "name": "xxx",
      +            "content": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com",
      +            "fingerprint": "SHA256:cU8AK/+roqUUyiaYXIdS2Nj4+Rb2p6rqWSeRDc+aqKM",
      +            "created_unix": 1626246596,
      +            "created_time": "2021/07/14 15:09"
      +        }
      +    ]
      +}
      +
      + +

      创建public_key

      +

      创建public_key

      + +
      +

      示例:

      +
      +
      curl -X POST \
      +http://localhost:3000/api/public_keys.json
      +
      await octokit.request('POST /api/public_keys.json')
      +

      HTTP 请求

      +

      POST api/public_keys.json

      +

      请求参数

      + + + + + + + + + + + + + + + + + + + + + + + +
      参数必选默认类型字段说明
      keystring密钥
      titlestring密钥标题
      + +
      +

      请求的JSON示例: +json +{ + "public_key": { + "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com", + "title": "xxx" + } +} +

      +
      +

      返回字段说明

      + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      参数类型字段说明
      total_countint总数
      idintID
      namestring密钥标题
      contentstring密钥内容
      fingerprintstring密钥标识
      created_timestring密钥创建时间
      + +
      +

      返回的JSON示例:

      +
      +
      {
      +    "id": 17,
      +    "name": "xxx",
      +    "content": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDe5ETOTB5PcmcYJkIhfF7+mxmJQDCLg7/LnMoKHpKoo/jYUnFU9OjfsxVo3FTNUvh2475WXMAur5KsFoNKjK9+JHxvoXyJKmyVPWgXU/NRxQyaWPnPLPK8qPRF5ksJE6feBOqtsdxsvBiHs2r1NX/U26Ecnpr6avudD0cmyrEfbYMWbupLrhsd39dswPT73f3W5jc7B9Y47Ioiv8UOju3ABt1+kpuAjaaVC6VtUQoEFiZb1y33yBnyePya7dvFyApyD4ILyyIG2rtZWK7l53YFnwZDuFsTWjEEEQD0U4FBSFdH5wtwx0WQLMSNyTtaFBSG0kJ+uiQQIrxlvikcm63df7zbC3/rWLPsKgW122Zt966dcpFqiCiJNDKZPPw3qpg8TBL6X+qIZ+FxVEk/16/zScpyEfoxQp0GvgxI7hPLErmfkC5tMsib8MAXYBNyvJXna0vg/wOaNNIaI4SAH9Ksh3f/TtalYVjp6WxIwVBfnbq51WnmlnEXePtX6XjAGL+GbF2VQ1nv/IzrY09tNbTV6wQsrSIP3VDzYQxdJ1rdsVNMoJB0H2Pu0NdcSz53Wx45N+myD0QnE05ss+zDp5StY90OYsx2aCo6qAA8Qn2jUjdta7MQWwkPfKrta4tTQ0XbWMjx4/E1+l3J5liwZkl2XOGOwhfXdRsBjaEziZ18kQ== yystopf@163.com",
      +    "fingerprint": "SHA256:cU8AK/+roqUUyiaYXIdS2Nj4+Rb2p6rqWSeRDc+aqKM",
      +    "created_time": "2021/07/14 15:26"
      +}
      +
      + +

      删除public_key

      +

      删除public_key

      + +
      +

      示例:

      +
      +
      curl -X DELETE \
      +http://localhost:3000/api/public_keys/:id.json
      +
      await octokit.request('DELETE /api/public_keys/:id.json')
      +

      HTTP 请求

      +

      DELETE api/public_keys/:id.json

      +

      请求参数

      + + + + + + + + + + + + + + + + +
      参数必选默认类型字段说明
      idint密钥ID
      + +
      +

      返回的JSON示例:

      +
      +
      {
      +    "status": 0,
      +    "message": "success"
      +}
      +
      +