From a8c3328bd668f2e92bef515e699db2e65c022c39 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Fri, 6 Nov 2020 17:23:14 +0800 Subject: [PATCH] ADD get a commit api --- README.md | 201 +++++------------- app/controllers/repositories_controller.rb | 5 +- .../gitea/repository/commits/get_service.rb | 10 +- app/views/repositories/commit.json.jbuilder | 30 +-- 4 files changed, 74 insertions(+), 172 deletions(-) diff --git a/README.md b/README.md index afc548a3d..3a9b40389 100644 --- a/README.md +++ b/README.md @@ -1825,18 +1825,19 @@ http://localhost:3000/api/repositories/89/commits.json | jq ## 获取某个提交记录(包含diff) ``` -GET /api/repositories/:id/commits/:sha +GET /api/:owner/:repo/commits/:sha ``` *示例* ``` curl -X GET \ -http://localhost:3000/api/repositories/5845/commits/b0c4a4a1487d53acebf2addc544b29938cad12df.json | jq +http://localhost:3000/api/jasder/repo/commits/b0c4a4a1487d53acebf2addc544b29938cad12df.json | jq ``` *请求参数说明:* |参数名|必选|类型|说明| |-|-|-|-| -|id |是|int |仓库repository的id | +|owner |是|string |仓库拥有者 | +|repo |是|string |仓库的identifier值 | |sha |否|string |git的ref或者是提交记录commit的sha标识 | @@ -1909,161 +1910,77 @@ http://localhost:3000/api/repositories/5845/commits/b0c4a4a1487d53acebf2addc544b 返回值 ``` { - "additions": 243, - "deletions": 32, - "sha": "c6c17ad47d6dbe4369d559847197b37b4090a46e", - "url": "http://localhost:3000/api/repositories/5845/commits/c6c17ad47d6dbe4369d559847197b37b4090a46e.json", + "files_count": 6, + "total_addition": 447, + "total_deletion": 0, "commit": { + "sha": "8f5faee0d3b3be1b8063e84da0c79dd75327b968", + "message": "add some file\n* Add the tag list page to the release page\n* Apply suggestions from code review\n* Add the tags list view\n* Add the delete tag way on ui\n* Not delete tag and clear message when delete a release\n", "author": { - "name": "GitHub", - "email": "noreply@github.com", - "date": "2020-05-20T20:15:27+08:00" + "name": "Jasder", + "email": "2053003901@@qq.com", + "date": "2020-11-03T13:56:22+08:00" }, "committer": { - "name": "GitHub", - "email": "noreply@github.com", - "date": "2020-05-20T20:15:27+08:00" - } + "name": "Jasder", + "email": "2053003901@@qq.com", + "date": "2020-11-03T13:56:22+08:00" + }, + "timestamp": 1604382982, + "time_from_now": "3天前" }, "author": null, "committer": null, "parents": [ { - "sha": "c8b8cfd85e375ad376833d04b9ca499bf9da355b", - "url": "http://localhost:3003//api/repositories/intelligent-test-platform/commits/c8b8cfd85e375ad376833d04b9ca499bf9da355b" + "sha": "c7f5b90725f30d8ad840a026773f9df92debc3af" }, - { - "sha": "b0c4a4a1487d53acebf2addc544b29938cad12df", - "url": "http://localhost:3003//api/repositories/intelligent-test-platform/commits/b0c4a4a1487d53acebf2addc544b29938cad12df" - } - ], "files": [ { - "Name": "Dockerfile", - "OldName": "Dockerfile", - "Index": 1, - "Addition": 11, - "Deletion": 0, - "Type": 1, - "IsCreated": true, - "IsDeleted": false, - "IsBin": false, - "IsLFSFile": false, - "IsRenamed": false, - "IsSubmodule": false, - "Sections": [ + "name": "build.go", + "old_name": "build.go", + "index": 1, + "addition": 33, + "deletion": 0, + "type": 1, + "isCreated": true, + "isDeleted": false, + "isBin": false, + "isLFSFile": false, + "isRenamed": false, + "isSubmodule": false, + "sections": [ { - "Name": "", - "Lines": [ + "fileName": "build.go", + "name": "", + "lines": [ { - "LeftIdx": 0, - "RightIdx": 0, - "Type": 4, - "Content": "@@ -0,0 +1,11 @@", - "Comments": null, - "SectionInfo": { - "Path": "Dockerfile", - "LastLeftIdx": 0, - "LastRightIdx": 0, - "LeftIdx": 0, - "RightIdx": 1, - "LeftHunkSize": 0, - "RightHunkSize": 11 + "leftIdx": 0, + "rightIdx": 0, + "type": 4, + "content": "@@ -0,0 +1,33 @@", + "sectionInfo": { + "path": "build.go", + "lastLeftIdx": 0, + "lastRightIdx": 0, + "leftIdx": 0, + "rightIdx": 1, + "leftHunkSize": 0, + "rightHunkSize": 33 } }, { - "LeftIdx": 0, - "RightIdx": 1, - "Type": 2, - "Content": "+FROM adoptopenjdk/maven-openjdk8:latest", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 2, - "Type": 2, - "Content": "+", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 3, - "Type": 2, - "Content": "+ADD ./target/markov-demo-0.0.1-SNAPSHOT.jar /usr/local/markov-demo-0.0.1-SNAPSHOT.jar", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 4, - "Type": 2, - "Content": "+", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 5, - "Type": 2, - "Content": "+# Add docker-compose-wait tool -------------------", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 6, - "Type": 2, - "Content": "+ENV WAIT_VERSION 2.7.2", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 7, - "Type": 2, - "Content": "+ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 8, - "Type": 2, - "Content": "+RUN chmod +x /wait", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 9, - "Type": 2, - "Content": "+", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 10, - "Type": 2, - "Content": "+EXPOSE 8080", - "Comments": null, - "SectionInfo": null - }, - { - "LeftIdx": 0, - "RightIdx": 11, - "Type": 2, - "Content": "+ENTRYPOINT [ \"java\", \"-jar\", \"/usr/local/markov-demo-0.0.1-SNAPSHOT.jar\" ]", - "Comments": null, - "SectionInfo": null + "leftIdx": 0, + "rightIdx": 1, + "type": 2, + "content": "+// Copyright 2020 The Gitea Authors. All rights reserved.", + "sectionInfo": null } ] } - ], - "IsIncomplete": false + ] } + ] } ``` --- @@ -2549,7 +2466,7 @@ GET /api/:owner/:repo/pulls/:id/files.json *示例* ``` curl -X GET \ -http://localhost:3000/api/Jason/repo/1/files.json | jq +http://localhost:3000/api/Jason/repo/pulls/1/files.json | jq ``` *请求参数说明:* @@ -2730,10 +2647,10 @@ GET /api/:owner/:repo/compare/{base}...{head}.json *示例* ``` curl -X GET \ -http://localhost:3003/api/v1/repos/Jason/test-txt/compare/master...develop | jq +http://localhost:3000/api/Jason/test-txt/compare/master...develop | jq curl -X GET \ -http://localhost:3003/api/v1/repos/jasder/test-txt/compare/master...Jason/test-txt:develop +http://localhost:3000/api/ysfns/test-txt/compare/master...Jason/test-txt:develop ``` *请求参数说明:* @@ -2782,7 +2699,7 @@ http://localhost:3003/api/v1/repos/jasder/test-txt/compare/master...Jason/test-t |---- lines |array|| |------ leftIdx |string|文件变动之前所在行数| |------ rightIdx |string|文件更改后所在行数| -|------ type |string|文件变更类型,1: 新增,2: 修改, 3: 删除, 4: diff统计信息| +|------ type |string|文件变更类型,1: 内容未改动,2: 添加, 3: 删除, 4: diff统计信息| |------ content |string|文件变更的内容| |------ sectionInfo |object|| |-------- path |string|文件相对仓库的路径| @@ -2791,7 +2708,7 @@ http://localhost:3003/api/v1/repos/jasder/test-txt/compare/master...Jason/test-t |-------- leftHunkSize |int|文件变更之前的行数| |-------- rightHunkSize |int|文件变更之后的行数(及当前页面编辑器显示的总行数)| |-------- leftIdx |int|文件变更之前所在行数| -|-------- rightIdx |int|文件变更之后所在行数(即:页面编辑器开始显示的行数)| +|-------- rightIdx |int|文件变更之后所在行数| 返回值 ```json diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index ce8de3bfb..e44f4f24b 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -82,11 +82,12 @@ class RepositoriesController < ApplicationController end def commit - @commit = Gitea::Repository::Commits::GetService.new(@repository.user.login, @repository.identifier, params[:sha], current_user.gitea_token).call + @commit = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, params[:sha], current_user.gitea_token) + @commit_diff = Gitea::Repository::Commits::GetService.call(@owner.login, @repository.identifier, params[:sha], current_user.gitea_token, {diff: true}) end def tags - @tags = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier, {page: params[:page], limit: params[:limit]}).call + @tags = Gitea::Repository::Tags::ListService.call(current_user&.gitea_token, @project.owner.login, @project.identifier, {page: params[:page], limit: params[:limit]}) end def edit diff --git a/app/services/gitea/repository/commits/get_service.rb b/app/services/gitea/repository/commits/get_service.rb index 019658d1f..caee29f55 100644 --- a/app/services/gitea/repository/commits/get_service.rb +++ b/app/services/gitea/repository/commits/get_service.rb @@ -1,16 +1,16 @@ # Get a single commit from a repository class Gitea::Repository::Commits::GetService < Gitea::ClientService - attr_reader :token, :owner, :repo, :sha, :custom + attr_reader :token, :owner, :repo, :sha, :hash # sha: the commit hash # ex: Gitea::Repository::Commits::GetService.new(@repo.user.login, repo.identifier, params[:sha], current_user.gitea_token) # TODO custom参数用于判断调用哪个api - def initialize(owner, repo, sha, token, custom=false) + def initialize(owner, repo, sha, token, hash={}) @token = token @owner = owner @sha = sha @repo = repo - @custom = custom + @hash = hash end def call @@ -24,10 +24,10 @@ class Gitea::Repository::Commits::GetService < Gitea::ClientService end def url - if custom + if hash[:diff] # TODO # 平台自己编写的gitea接口,后续可能会通过提交pr的形式合并到gitea原有的接口上 - "/repos/#{owner}/#{repo}/commits/diff/#{sha}".freeze + "/repos/#{owner}/#{repo}/commits/#{sha}/diff".freeze else "/repos/#{owner}/#{repo}/git/commits/#{sha}".freeze end diff --git a/app/views/repositories/commit.json.jbuilder b/app/views/repositories/commit.json.jbuilder index 8901cad3c..469357367 100644 --- a/app/views/repositories/commit.json.jbuilder +++ b/app/views/repositories/commit.json.jbuilder @@ -1,27 +1,11 @@ -json.key_format! camelize: :lower -json.additions @commit['commit_diff']['TotalAddition'] -json.deletions @commit['commit_diff']['TotalDeletion'] -json.sha @commit['sha'] -json.url request.url -json.commit do - @commit['commit'].delete('url') - json.author @commit['commit']['author'] - json.committer @commit['commit']['committer'] - json.message @commit['commit']['message'] - json.tree do - @commit['commit']['tree']['sha'] - end -end -json.author do - json.partial! 'commit_author', user: render_commit_author(@commit['author']) -end -json.committer do - json.partial! 'commit_author', user: render_commit_author(@commit['committer']) -end +# json.key_format! camelize: :lower +json.files_count @commit_diff['NumFiles'] +json.total_addition @commit_diff['TotalAddition'] +json.total_deletion @commit_diff['TotalDeletion'] +json.files @commit_diff['Files'], partial: 'pull_requests/diff_file', as: :file +json.partial! 'commit', commit: @commit, project: @project json.parents @commit['parents'] do |parent| json.sha parent['sha'] - json.url EduSetting.get('host_name') + commit_repository_path(@repo, parent['sha']) + # json.url EduSetting.get('host_name') + commit_repository_path(@repo, parent['sha']) end - -json.files @commit['commit_diff']['Files']