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 @@
根据分支名、标签、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')
+
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示例:
+
{
+ "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"
+ }
+ ]
+}
+
根据commit ID获取blame信息
@@ -9673,9 +9835,9 @@ http://localhost:3000/api/v1/yystopf/csfjkkj/git/trees/80dd40214a58622312393b2aecurl -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')
-
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
---|
参数 | @@ -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
---|
参数 | @@ -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
---|
参数 | @@ -10413,7 +10575,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3.jsonwebhook ID |
---|
参数 | @@ -10536,214 +10698,8 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3.json
---|
参数 | -必选 | -默认 | -类型 | -字段说明 | -
---|---|---|---|---|
owner | -是 | -- | string | -用户登录名 | -
repo | -是 | -- | string | -项目标识identifier | -
webhook.url | -是 | -- | string | -目标url | -
webhook.http_method | -是 | -- | string | -http方法, POST和GET | -
webhook.content_type | -是 | -- | string | -POST Content Type | -
webhook.secret | -否 | -- | string | -密钥文本 | -
webhook.active | -是 | -- | bool | -是否激活 | -
webhook.branch_filter | -否 | -- | string | -分支过滤 | -
webhook.events | -否 | -- | array | -触发事件 | -
触发事件字段说明
- -参数 | -含义 | -
---|---|
create | -创建分支或标签 | -
delete | -分支或标签删除 | -
push | -git仓库推送 | -
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"]
-}
-
参数 | -类型 | -字段说明 | -
---|---|---|
id | -int | -id | -
url | -string | -地址 | -
content_type | -string | -POST Content Type | -
is_active | -bool | -是否激活 | -
type | -string | -类型 | -
events | -array | -触发事件 | -
create_time | -string | -创建时间 | -
--返回的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
- ---示例:
-
curl -X PATCH \
-http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json
-
await octokit.request('PATCH /api/v1/yystopf/ceshi/webhooks/7.json')
PATCH /api/v1/:owner/:repo/webhooks/68.json
POST /api/v1/:owner/:repo/webhooks.json
项目标识identifier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id | -是 | -- | string | -webhook id | -||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
webhook.url | 是 | @@ -10877,6 +10826,219 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json "events": ["push"] } |
参数 | +类型 | +字段说明 | +
---|---|---|
id | +int | +id | +
url | +string | +地址 | +
content_type | +string | +POST Content Type | +
is_active | +bool | +是否激活 | +
type | +string | +类型 | +
events | +array | +触发事件 | +
create_time | +string | +创建时间 | +
++返回的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
+ +++示例:
+
curl -X PATCH \
+http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json
+
await octokit.request('PATCH /api/v1/yystopf/ceshi/webhooks/7.json')
+
PATCH /api/v1/:owner/:repo/webhooks/68.json
参数 | +必选 | +默认 | +类型 | +字段说明 | +
---|---|---|---|---|
owner | +是 | ++ | string | +用户登录名 | +
repo | +是 | ++ | string | +项目标识identifier | +
id | +是 | ++ | string | +webhook id | +
webhook.url | +是 | ++ | string | +目标url | +
webhook.http_method | +是 | ++ | string | +http方法, POST和GET | +
webhook.content_type | +是 | ++ | string | +POST Content Type | +
webhook.secret | +否 | ++ | string | +密钥文本 | +
webhook.active | +是 | ++ | bool | +是否激活 | +
webhook.branch_filter | +否 | ++ | string | +分支过滤 | +
webhook.events | +否 | ++ | array | +触发事件 | +
触发事件字段说明
+ +参数 | +含义 | +
---|---|
create | +创建分支或标签 | +
delete | +分支或标签删除 | +
push | +git仓库推送 | +
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"]
+}
+
@@ -10911,9 +11073,9 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json返回的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')
-
DELETE /api/v1/:owner/:repo/webhooks/:id.json
参数 | @@ -10945,7 +11107,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.jsonwebhook id |
---|
@@ -10966,9 +11128,9 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/7.json返回的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')
-
GET /api/v1/:owner/:repo/webhooks/:id/hooktasks.json
参数 | @@ -11000,7 +11162,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/hooktasks.jsonwebhook ID |
---|
参数 | @@ -11237,9 +11399,9 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/hooktasks.json
---|
参数 | @@ -11271,7 +11433,7 @@ http://localhost:3000/api/v1/yystopf/ceshi/webhooks/3/tests.jsonwebhook ID |
---|
返回的JSON示例: