From 274cd81655ce8f7356db88c44e5fe3f25279d5b6 Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 19 Jul 2022 10:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=8E=B7=E5=8F=96co?= =?UTF-8?q?mmit=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- .../api/v1/projects/commits_controller.rb | 7 +- .../slate/source/includes/_repositories.md | 83 +++ .../api/v1/projects/commits/list_service.rb | 38 ++ .../v1/projects/commits/index.json.jbuilder | 17 + config/routes/api.rb | 2 +- public/docs/api.html | 632 +++++++++++------- 7 files changed, 543 insertions(+), 238 deletions(-) create mode 100644 app/services/api/v1/projects/commits/list_service.rb create mode 100644 app/views/api/v1/projects/commits/index.json.jbuilder diff --git a/Gemfile b/Gemfile index 37687e78d..a4c7c2414 100644 --- a/Gemfile +++ b/Gemfile @@ -135,4 +135,4 @@ gem 'doorkeeper' gem 'doorkeeper-jwt' -gem 'gitea-client', '~> 0.9.4' \ No newline at end of file +gem 'gitea-client', '~> 0.10.1' \ No newline at end of file diff --git a/app/controllers/api/v1/projects/commits_controller.rb b/app/controllers/api/v1/projects/commits_controller.rb index 873dd4d93..a1545ae6f 100644 --- a/app/controllers/api/v1/projects/commits_controller.rb +++ b/app/controllers/api/v1/projects/commits_controller.rb @@ -1,5 +1,10 @@ class Api::V1::Projects::CommitsController < Api::V1::BaseController - before_action :require_public_and_member_above, only: [:diff] + before_action :require_public_and_member_above, only: [:index, :diff] + + def index + @result_object = Api::V1::Projects::Commits::ListService.call(@project, {page: page, limit: limit, sha: params[:sha]}, current_user&.gitea_token) + puts @result_object + end def diff @result_object = Api::V1::Projects::Commits::DiffService.call(@project, params[:sha], current_user&.gitea_token) diff --git a/app/docs/slate/source/includes/_repositories.md b/app/docs/slate/source/includes/_repositories.md index 2b3ca1326..c760fc624 100644 --- a/app/docs/slate/source/includes/_repositories.md +++ b/app/docs/slate/source/includes/_repositories.md @@ -1413,6 +1413,89 @@ await octokit.request('GET /api/v1/yystopf/csfjkkj/git/blobs/80dd40214a586223123 Success Data. +## 获取仓库提交列表 +根据分支名、标签、commit ID来获取提交列表 + +> 示例: + +```shell +curl -X GET \ +-d "sha=master" \ +-d "page=1" \ +-d "limit=1" \ +http://localhost:3000/api/v1/yystopf/csfjkkj/commits.json +``` + +```javascript +await octokit.request('GET /api/v1/yystopf/csfjkkj/commits.json') +``` + +### HTTP 请求 +`GET /api/v1/:owner/:repo/commits.json` + +### 请求参数: +参数 | 必选 | 默认 | 类型 | 字段说明 +--------- | ------- | ------- | -------- | ---------- +|owner|是| | string |用户登录名 | +|repo |是| | string |项目标识identifier | +|sha |否| | string |分支名、标签名或Commit ID| +|page |否| | int |页码| +|limit|否| | int |每页数量| +### 返回字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|total_count|int|提交总数| +|commits.sha|string|提交ID| +|commits.author|object|提交作者| +|commits.committer|object|提交者| +|commits.commit_message|string|提交信息| +|commits.parent_shas|array|提交父节点ID| +|commits.files|array|提交文件| +|commits.commit_date|string|提交日期| +|commits.commit_time|string|提交时间| +|commits.branch|string|提交分支| + + +> 返回的JSON示例: + +```json +{ + "total_count": 12, + "commits": [ + { + "sha": "86c62a1e91c07b58b8aa6c89b94856d89c0f7e55", + "author": { + "id": null, + "login": "viletyy", + "name": "viletyy", + "type": null, + "image_url": "system/lets/letter_avatars/2/V/39_141_222/120.png" + }, + "committer": { + "id": null, + "login": "viletyy", + "name": "viletyy", + "type": null, + "image_url": "system/lets/letter_avatars/2/V/39_141_222/120.png" + }, + "commit_message": "fix\n", + "parent_shas": [ + "411e4d259785241f1bd14faf99ca24fd1b802f2a" + ], + "files": [ + "hd.txt" + ], + "commit_date": "2022-07-05", + "commit_time": "2022-07-05 11:00:45", + "branch": "hh_ceshi" + } + ] +} +``` + + ## 获取单个提交的blame信息 根据commit ID获取blame信息 diff --git a/app/services/api/v1/projects/commits/list_service.rb b/app/services/api/v1/projects/commits/list_service.rb new file mode 100644 index 000000000..17818554c --- /dev/null +++ b/app/services/api/v1/projects/commits/list_service.rb @@ -0,0 +1,38 @@ +class Api::V1::Projects::Commits::ListService < ApplicationService + + attr_reader :project, :sha, :page, :limit, :owner, :repo, :token + attr_accessor :gitea_data + + def initialize(project, params, token=nil) + @project = project + @sha = params[:sha] + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @owner = project&.owner.login + @repo = project&.identifier + @token = token + end + + def call + load_gitea_data + + gitea_data + end + + private + def request_params + param = { + access_token: token, + page: page, + limit: limit + } + param.merge!(sha: sha) if sha.present? + + param + end + + def load_gitea_data + @gitea_data = $gitea_client.get_repos_commits_by_owner_repo(owner, repo, {query: request_params}) rescue nil + raise Error, '获取提交列表失败!' unless @gitea_data.is_a?(Hash) + end +end \ No newline at end of file diff --git a/app/views/api/v1/projects/commits/index.json.jbuilder b/app/views/api/v1/projects/commits/index.json.jbuilder new file mode 100644 index 000000000..aa13845a1 --- /dev/null +++ b/app/views/api/v1/projects/commits/index.json.jbuilder @@ -0,0 +1,17 @@ +json.total_count @result_object[:total_data].to_i +json.commits @result_object[:data].each do |commit| + json.sha commit['sha'] + json.author do + json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['commit']['author']), name: commit['commit']['author']['name'] } + end + + json.committer do + json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['commit']['committer']), name: commit['commit']['committer']['name'] } + end + json.commit_message commit['commit']['message'] + json.parent_shas commit['parents'].map{|x|x['sha']} + json.files commit['files'].map{|f|f['filename']} + json.commit_date commit['commit_date'] + json.commit_time commit['commit']['committer']['date'].to_time.strftime("%Y-%m-%d %H:%M:%S") + json.branch commit['branch'] +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index fc07a9862..9c71ea800 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -32,7 +32,7 @@ defaults format: :json do get :all end end - + resources :commits, only: [:index] get '/commits/:sha/diff', to: 'commits#diff' get '/git/blobs/:sha', to: 'git#blobs' get '/git/trees/:sha', to: 'git#trees' diff --git a/public/docs/api.html b/public/docs/api.html index 505ab44cf..f3e56574f 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -553,6 +553,9 @@
  • 获取仓库blobs内容
  • +
  • + 获取仓库提交列表 +
  • 获取单个提交的blame信息
  • @@ -9665,6 +9668,165 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/git/trees/80dd40214a58622312393b2ae +

    获取仓库提交列表

    +

    根据分支名、标签、commit ID来获取提交列表

    + +
    +

    示例:

    +
    +
    curl -X GET \
    +-d "sha=master" \
    +-d "page=1" \
    +-d "limit=1" \
    +http://localhost:3000/api/v1/yystopf/csfjkkj/commits.json
    +
    await octokit.request('GET /api/v1/yystopf/csfjkkj/commits.json')
    +

    HTTP 请求

    +

    GET /api/v1/:owner/:repo/commits.json

    +

    请求参数:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数必选默认类型字段说明
    ownerstring用户登录名
    repostring项目标识identifier
    shastring分支名、标签名或Commit ID
    pageint页码
    limitint每页数量
    +

    返回字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    total_countint提交总数
    commits.shastring提交ID
    commits.authorobject提交作者
    commits.committerobject提交者
    commits.commit_messagestring提交信息
    commits.parent_shasarray提交父节点ID
    commits.filesarray提交文件
    commits.commit_datestring提交日期
    commits.commit_timestring提交时间
    commits.branchstring提交分支
    + +
    +

    返回的JSON示例:

    +
    +
    {
    +    "total_count": 12,
    +    "commits": [
    +        {
    +            "sha": "86c62a1e91c07b58b8aa6c89b94856d89c0f7e55",
    +            "author": {
    +                "id": null,
    +                "login": "viletyy",
    +                "name": "viletyy",
    +                "type": null,
    +                "image_url": "system/lets/letter_avatars/2/V/39_141_222/120.png"
    +            },
    +            "committer": {
    +                "id": null,
    +                "login": "viletyy",
    +                "name": "viletyy",
    +                "type": null,
    +                "image_url": "system/lets/letter_avatars/2/V/39_141_222/120.png"
    +            },
    +            "commit_message": "fix\n",
    +            "parent_shas": [
    +                "411e4d259785241f1bd14faf99ca24fd1b802f2a"
    +            ],
    +            "files": [
    +                "hd.txt"
    +            ],
    +            "commit_date": "2022-07-05",
    +            "commit_time": "2022-07-05 11:00:45",
    +            "branch": "hh_ceshi"
    +        }
    +    ]
    +}
    +
    +

    获取单个提交的blame信息

    根据commit ID获取blame信息

    @@ -9673,9 +9835,9 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/git/trees/80dd40214a58622312393b2ae
    curl -X GET http://localhost:3000/api/v1/yystopf/csfjkkj/commits/80dd40214a58622312393b2ae693756a4781fab2/diff.json
     
    await octokit.request('GET /api/v1/yystopf/csfjkkj/commits/80dd40214a58622312393b2ae693756a4781fab2/diff.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/v1/:owner/:repo/commits/:sha/diff.json

    -

    请求参数:

    +

    请求参数:

    @@ -9707,7 +9869,7 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/git/trees/80dd40214a58622312393b2ae
    参数提交记录id
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -9933,9 +10095,9 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/git/trees/80dd40214a58622312393b2ae -d"to=master"\ http://localhost:3000/api/v1/yystopf/csfjkkj/compare.json
    await octokit.request('GET /api/v1/yystopf/csfjkkj/compare.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/v1/:owner/:repo/compare.json

    -

    请求参数:

    +

    请求参数:

    参数
    @@ -9974,7 +10136,7 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/compare.json
    参数目标分支、标签、commitID
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -10259,9 +10421,9 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/compare.json
    curl -X GET \
     http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json
     
    await octokit.request('GET /api/v1/yystopf/ceshi/webhooks.json')
    -

    HTTP 请求

    +

    HTTP 请求

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

    -

    请求参数:

    +

    请求参数:

    参数
    @@ -10286,7 +10448,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json
    参数项目标识identifier
    -

    返回字段说明:

    +

    返回字段说明:

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

    HTTP 请求

    +

    HTTP 请求

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

    -

    请求参数:

    +

    请求参数:

    参数
    @@ -10413,7 +10575,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -10536,214 +10698,8 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3.json
    curl -X POST \
     http://localhost:3000/api/v1/yystopf/ceshi/webhooks.json
     
    await octokit.request('POST /api/v1/yystopf/ceshi/webhooks.json')
    -

    HTTP 请求

    -

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

    -

    请求参数:

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

    触发事件字段说明

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    参数含义
    create创建分支或标签
    delete分支或标签删除
    pushgit仓库推送
    pull_request合并请求
    pull_request_assign合并请求被指派
    pull_request_review_approved合并请求被批准
    pull_request_review_rejected合并请求被拒绝
    - -
    -

    请求的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": 68,
    -    "content_type": "json",
    -    "http_method": "GET",
    -    "url": "http://127.0.0.1:3000",
    -    "events": [
    -        "create",
    -        "delete",
    -        "push",
    -        "pull_request",
    -        "pull_request_assign",
    -        "pull_request_review_approved",
    -        "pull_request_review_rejected"
    -    ],
    -    "active": true,
    -    "branch_filter": "*",
    -    "created_at": "2022-06-23 15:52"
    -}
    -
    - -

    更新仓库webhook

    -

    更新仓库webhook

    - -
    -

    示例:

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

    HTTP 请求

    -

    PATCH /api/v1/:owner/:repo/webhooks/68.json

    +

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

    请求参数:

    @@ -10769,13 +10725,6 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json - - - - - - - @@ -10877,6 +10826,219 @@ http://localhost:3000/api/v1/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示例:

    +
    +
    {
    +    "id": 68,
    +    "content_type": "json",
    +    "http_method": "GET",
    +    "url": "http://127.0.0.1:3000",
    +    "events": [
    +        "create",
    +        "delete",
    +        "push",
    +        "pull_request",
    +        "pull_request_assign",
    +        "pull_request_review_approved",
    +        "pull_request_review_rejected"
    +    ],
    +    "active": true,
    +    "branch_filter": "*",
    +    "created_at": "2022-06-23 15:52"
    +}
    +
    + +

    更新仓库webhook

    +

    更新仓库webhook

    + +
    +

    示例:

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

    HTTP 请求

    +

    PATCH /api/v1/:owner/:repo/webhooks/68.json

    +

    请求参数:

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

    触发事件字段说明

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数含义
    create创建分支或标签
    delete分支或标签删除
    pushgit仓库推送
    pull_request合并请求
    pull_request_assign合并请求被指派
    pull_request_review_approved合并请求被批准
    pull_request_review_rejected合并请求被拒绝
    + +
    +

    请求的JSON示例:

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

    返回字段说明:

    返回的JSON示例:

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

    HTTP 请求

    +

    HTTP 请求

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

    -

    请求参数:

    +

    请求参数:

    @@ -10945,7 +11107,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json
    参数webhook id
    -

    返回字段说明:

    +

    返回字段说明:

    返回的JSON示例:

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

    HTTP 请求

    +

    HTTP 请求

    GET /api/v1/:owner/:repo/webhooks/:id/hooktasks.json

    -

    请求参数:

    +

    请求参数:

    @@ -11000,7 +11162,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/hooktasks.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    @@ -11237,9 +11399,9 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/hooktasks.json
    curl -X POST \
     http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/tests.json
     
    await octokit.request('POST /api/v1/yystopf/ceshi/webhooks/3/tests.json')
    -

    HTTP 请求

    +

    HTTP 请求

    POST /api/v1/:owner/:repo/webhooks/:id/tests.json

    -

    请求参数:

    +

    请求参数:

    参数
    @@ -11271,7 +11433,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/tests.json
    参数webhook ID
    -

    返回字段说明:

    +

    返回字段说明:

    返回的JSON示例: