From ec221240042a9bb775fe19ae6f05d822e5744ca6 Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 23 Sep 2021 18:07:46 +0800 Subject: [PATCH] add: readme by filepath --- app/controllers/repositories_controller.rb | 18 +- .../slate/source/includes/_repositories.md | 57 ++ .../gitea/repository/readme/dir_service.rb | 34 + .../repositories/contributors.json.jbuilder | 2 +- public/docs/api.html | 692 ++++++++++-------- 5 files changed, 510 insertions(+), 293 deletions(-) create mode 100644 app/services/gitea/repository/readme/dir_service.rb diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index db7be5c72..a7e909eb8 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -125,7 +125,11 @@ class RepositoriesController < ApplicationController end def contributors - @contributors = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier) + if params[:filepath].present? + @contributors = [] + else + @contributors = Gitea::Repository::Contributors::GetService.call(@owner, @repository.identifier) + end end def edit @@ -188,10 +192,16 @@ class RepositoriesController < ApplicationController end def readme - result = Gitea::Repository::Readme::GetService.call(@owner.login, @repository.identifier, params[:ref], current_user&.gitea_token) - + if params[:filepath].present? + result = Gitea::Repository::Readme::DirService.call(@owner.login, @repository.identifier, params[:filepath], params[:ref], current_user&.gitea_token) + else + result = Gitea::Repository::Readme::GetService.call(@owner.login, @repository.identifier, params[:ref], current_user&.gitea_token) + end @readme = result[:status] === :success ? result[:body] : nil - render json: @readme + + render json: @readme.slice("type", "encoding", "size", "name", "path", "content", "sha") + rescue + render json: nil end def languages diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index 1552655e4..c27243da6 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -868,6 +868,63 @@ await octokit.request('GET /api/jasder/jasder_test/sub_entries.json') Success Data. +## 获取仓库README文件 +获取仓库README文件 + +> 示例: + +```shell +curl -X GET \ +-d "ref=master" \ +-d "filepath=lib" \ +http://localhost:3000/api/yystopf/csfjkkj/readme.json +``` + +```javascript +await octokit.request('GET /api/yystopf/csfjkkj/readme.json') +``` + +### HTTP 请求 +`GET /api/:owner/:repo/readme.json` + +### 请求参数: +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +|owner |是| |string |用户登录名 | +|repo |是| |string |项目标识identifier | +|ref |否| | string |分支名称、tag名称或是提交记录id,默认为默认分支 | +|filepath |否| | string |子目录名称,默认为空 | + +### 返回字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|type |string|文件类型, file:文件,dir:文件目录 +|encoding |string |编码 | +|size |int|文件夹或文件大小 单位B +|name |string|文件夹或文件名称| +|path |string|文件夹或文件相对路径| +|content |string|文件内容 +|sha |string|文件commitid + + +> 返回的JSON示例: + +```json +{ + "type": "file", + "encoding": "base64", + "size": 24, + "name": "README.md", + "path": "lib/README.md", + "content": "ZGZhc2RhZGpmIGRrZnNsCgpzZGZkZnMK", + "sha": "860962cd21c60b1a9e07d723080c87c32c18d44a" +} +``` + + + ## 获取仓库webhooks列表 获取仓库webhooks列表 diff --git a/app/services/gitea/repository/readme/dir_service.rb b/app/services/gitea/repository/readme/dir_service.rb new file mode 100644 index 000000000..587fb5d55 --- /dev/null +++ b/app/services/gitea/repository/readme/dir_service.rb @@ -0,0 +1,34 @@ +class Gitea::Repository::Readme::DirService < Gitea::ClientService + attr_reader :owner, :repo, :ref, :dir, :token + + def initialize(owner, repo, dir, ref='', token=nil) + @owner = owner + @repo = repo + @dir = dir + @ref = ref + @token = token + end + + def call + response = get(url, params) + status, message, body = render_response(response) + json_format(status, message, body) + end + + private + def params + Hash.new.merge(token: token, ref: ref) + end + + def url + "/repos/#{owner}/#{repo}/readme/#{dir}".freeze + end + + def json_format(status, message, body) + case status + when 200 then success(body) + when 404 then error(message, 404) + else error(message, status) + end + end +end \ No newline at end of file diff --git a/app/views/repositories/contributors.json.jbuilder b/app/views/repositories/contributors.json.jbuilder index a5edb37e0..9165cf948 100644 --- a/app/views/repositories/contributors.json.jbuilder +++ b/app/views/repositories/contributors.json.jbuilder @@ -6,7 +6,7 @@ json.contributors @contributors.each do |contributor| next end json.contributions contributor["contributions"] - json.gid contributor["id"] + # json.gid contributor["id"] json.login user.login json.type user&.type json.name user.real_name diff --git a/public/docs/api.html b/public/docs/api.html index c77c08988..5e3baed1c 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -499,6 +499,9 @@
  • 获取仓库代码子目录或者文件
  • +
  • + 获取仓库README文件 +
  • 获取仓库webhooks列表
  • @@ -7247,17 +7250,19 @@ http://localhost:3000//api/jasder/jasder_test/sub_entries.json -

    获取仓库webhooks列表

    -

    获取仓库webhooks列表

    +

    获取仓库README文件

    +

    获取仓库README文件

    示例:

    curl -X GET \
    -http://localhost:3000/api/yystopf/ceshi/webhooks.json
    -
    await octokit.request('GET /api/yystopf/ceshi/webhooks.json')
    +-d "ref=master" \
    +-d "filepath=lib" \
    +http://localhost:3000/api/yystopf/csfjkkj/readme.json
    +
    await octokit.request('GET /api/yystopf/csfjkkj/readme.json')
     

    HTTP 请求

    -

    GET /api/:owner/:repo/webhooks.json

    +

    GET /api/:owner/:repo/readme.json

    请求参数:

    @@ -7282,6 +7287,20 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json + + + + + + + + + + + + + +
    string 项目标识identifier
    refstring分支名称、tag名称或是提交记录id,默认为默认分支
    filepathstring子目录名称,默认为空

    返回字段说明:

    @@ -7292,6 +7311,103 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    typestring文件类型, file:文件,dir:文件目录
    encodingstring编码
    sizeint文件夹或文件大小 单位B
    namestring文件夹或文件名称
    pathstring文件夹或文件相对路径
    contentstring文件内容
    shastring文件commitid
    + +
    +

    返回的JSON示例:

    +
    +
    {
    +    "type": "file",
    +    "encoding": "base64",
    +    "size": 24,
    +    "name": "README.md",
    +    "path": "lib/README.md",
    +    "content": "ZGZhc2RhZGpmIGRrZnNsCgpzZGZkZnMK",
    +    "sha": "860962cd21c60b1a9e07d723080c87c32c18d44a"
    +}
    +
    + +

    获取仓库webhooks列表

    +

    获取仓库webhooks列表

    + +
    +

    示例:

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

    HTTP 请求

    +

    GET /api/:owner/:repo/webhooks.json

    +

    请求参数:

    + + + + + + + + + + + + + + + + + + + + + + + +
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    +

    返回字段说明:

    + + + + + + + + @@ -7385,9 +7501,9 @@ http://localhost:3000/api/yystopf/ceshi/webhooks.json
    curl -X GET \
     http://localhost:3000/api/yystopf/ceshi/webhooks/3/edit.json
     
    await octokit.request('GET /api/yystopf/ceshi/webhooks/3/edit.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/:owner/:repo/webhooks/:id/edit.json

    -

    请求参数:

    +

    请求参数:

    参数类型字段说明
    id int id
    @@ -7419,7 +7535,7 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/3/edit.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -7620,266 +7736,8 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/3/edit.json
    curl -X POST \
     http://localhost:3000/api/yystopf/ceshi/webhooks.json
     
    await octokit.request('POST /api/yystopf/ceshi/webhooks.json')
    -

    HTTP 请求

    -

    POST /api/:owner/:repo/webhooks.json

    -

    请求参数:

    -
    参数
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    webhook.urlstring目标url
    webhook.typestring类型
    webhook.http_methodstringhttp方法, POST和GET
    webhook.content_typestringPOST Content Type
    webhook.secretstring密钥文本
    webhook.activebool是否激活
    webhook.branch_filterstring分支过滤
    webhook.eventsarray触发事件
    - -

    触发事件字段说明

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    参数含义
    create创建分支或标签
    delete分支或标签删除
    fork仓库被fork
    pushgit仓库推送
    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示例:

    -
    -
    {
    -    "active": true, 
    -    "content_type": "json",
    -    "http_method": "GET",
    -    "secret": "123456",
    -    "url": "http://localhost:10000",
    -    "branch_filter": "*",
    -    "events": ["push"]
    -}
    -

    返回字段说明:

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    参数类型字段说明
    idintid
    urlstring地址
    content_typestringPOST Content Type
    is_activebool是否激活
    typestring类型
    eventsarray触发事件
    create_timestring创建时间
    - -
    -

    返回的JSON示例:

    -
    -
    {
    -    "id": 18,
    -    "type": "gitea",
    -    "content_type": "json",
    -    "url": "http://localhost:10000",
    -    "events": [
    -        "push"
    -    ],
    -    "active": true,
    -    "create_time": "2021-07-26 18:53:43"
    -}
    -
    - -

    更新仓库webhook

    -

    更新仓库webhook

    - -
    -

    示例:

    -
    -
    curl -X PATCH \
    -http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
    -
    await octokit.request('PATCH /api/yystopf/ceshi/webhooks/7.json')
     

    HTTP 请求

    -

    PATCH /api/:owner/:repo/webhooks/:id.json

    +

    POST /api/:owner/:repo/webhooks.json

    请求参数:

    @@ -7905,13 +7763,6 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/7.json - - - - - - - @@ -8072,28 +7923,79 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/7.json "events": ["push"] }

    返回字段说明:

    +
    项目标识identifier
    idstringwebhook id
    webhook.url
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    idintid
    urlstring地址
    content_typestringPOST Content Type
    is_activebool是否激活
    typestring类型
    eventsarray触发事件
    create_timestring创建时间
    +

    返回的JSON示例:

    {
    -    "status": 0,
    -    "message": "success"
    +    "id": 18,
    +    "type": "gitea",
    +    "content_type": "json",
    +    "url": "http://localhost:10000",
    +    "events": [
    +        "push"
    +    ],
    +    "active": true,
    +    "create_time": "2021-07-26 18:53:43"
     }
     
    -

    删除仓库webhook

    -

    删除仓库webhook

    +

    更新仓库webhook

    +

    更新仓库webhook

    示例:

    -
    curl -X DELETE \
    +
    curl -X PATCH \
     http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
    -
    await octokit.request('DELETE /api/yystopf/ceshi/webhooks/7.json')
    +
    await octokit.request('PATCH /api/yystopf/ceshi/webhooks/7.json')
     

    HTTP 请求

    -

    DELETE /api/:owner/:repo/webhooks/:id.json

    +

    PATCH /api/:owner/:repo/webhooks/:id.json

    请求参数:

    @@ -8125,8 +8027,222 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/7.json + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    string webhook id
    webhook.urlstring目标url
    webhook.typestring类型
    webhook.http_methodstringhttp方法, POST和GET
    webhook.content_typestringPOST Content Type
    webhook.secretstring密钥文本
    webhook.activebool是否激活
    webhook.branch_filterstring分支过滤
    webhook.eventsarray触发事件
    -

    返回字段说明:

    + +

    触发事件字段说明

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数含义
    create创建分支或标签
    delete分支或标签删除
    fork仓库被fork
    pushgit仓库推送
    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示例:

    +
    +
    {
    +    "active": true, 
    +    "content_type": "json",
    +    "http_method": "GET",
    +    "secret": "123456",
    +    "url": "http://localhost:10000",
    +    "branch_filter": "*",
    +    "events": ["push"]
    +}
    +

    返回字段说明:

    +
    +

    返回的JSON示例:

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

    删除仓库webhook

    +

    删除仓库webhook

    + +
    +

    示例:

    +
    +
    curl -X DELETE \
    +http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
    +
    await octokit.request('DELETE /api/yystopf/ceshi/webhooks/7.json')
    +

    HTTP 请求

    +

    DELETE /api/:owner/:repo/webhooks/:id.json

    +

    请求参数:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    idstringwebhook id
    +

    返回字段说明:

    返回的JSON示例:

    @@ -8147,9 +8263,9 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/7.json
    curl -X GET \
     http://localhost:3000/api/yystopf/ceshi/webhooks/3/tasks.json
     
    await octokit.request('GET /api/yystopf/ceshi/webhooks/3/tasks.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/:owner/:repo/webhooks/:id/tasks.json

    -

    请求参数:

    +

    请求参数:

    @@ -8181,7 +8297,7 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/3/tasks.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -8418,9 +8534,9 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/3/tasks.json
    curl -X POST \
     http://localhost:3000/api/yystopf/ceshi/webhooks/3/test.json
     
    await octokit.request('POST /api/yystopf/ceshi/webhooks/3/test.json')
    -

    HTTP 请求

    +

    HTTP 请求

    POST /api/:owner/:repo/webhooks/:id/test.json

    -

    请求参数:

    +

    请求参数:

    参数
    @@ -8452,7 +8568,7 @@ http://localhost:3000/api/yystopf/ceshi/webhooks/3/test.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    返回的JSON示例: