新增: 获取pr版本的diff接口及文档

This commit is contained in:
yystopf 2022-07-21 18:38:01 +08:00
parent 8757d15065
commit 6cad6ff212
6 changed files with 219 additions and 7 deletions

View File

@ -72,6 +72,25 @@ func GetPullRequestLastVersionByPullRequest(pr *PullRequest) (*PullRequestVersio
return prv, nil
}
func GetPullRequestVersionByID(prID, id int64) (*PullRequestVersion, error) {
if id < 1 {
return nil, ErrPullRequestNotExist{}
}
prv := &PullRequestVersion{
PullID: prID,
ID: id,
}
has, err := x.Get(prv)
if err != nil {
return nil, err
} else if !has {
return nil, ErrPullRequestNotExist{}
}
return prv, nil
}
// NewPullRequestDiff creates new pull request diff version for repository.
func NewPullRequestVersion(repo *Repository, pr *PullRequest, addLineNum, commitsCount, delLineNUm, filesCount int, headCommitID, baseCommitID, StartCommitID string) (err error) {
var version PullRequestVersion

View File

@ -762,6 +762,7 @@ func Routes() *web.Route {
m.Get("/issues", context.RepoRef(), repo.GetPullIssues)
m.Group("/versions", func() {
m.Get("", repo.ListPullRequestVersions)
m.Get("/{versionId}/diff", context.RepoRef(), repo.GetPullRequestVersionDiff)
})
})
m.Get("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader,

View File

@ -39,7 +39,7 @@ func GetRepoDiffs(ctx *context.APIContext) {
// required: false
// responses:
// 200:
// description: success
// "$ref": "#/responses/Diff"
// "404":
// "$ref": "#/responses/notFound"

View File

@ -7,15 +7,17 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/gitdiff"
)
// ListPullRequestVersions returns a list of all PR versions
func ListPullRequestVersions(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/versions repository repoListPullRequestVersions
// ---
// summary: List a repos's pull versions requests
// summary: List a repos's pull versions requests***
// produces:
// - application/json
// parameters:
@ -45,7 +47,7 @@ func ListPullRequestVersions(ctx *context.APIContext) {
// type: integer
// responses:
// "200":
// "$ref": "#/responses/PullRequest"
// "$ref": "#/responses/PullRequestVersionList"
// "404":
// "$ref": "#/responses/notFound"
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
@ -70,8 +72,77 @@ func ListPullRequestVersions(ctx *context.APIContext) {
}
ctx.SetLinkHeader(int(maxResults), lisOptions.PageSize)
ctx.Header().Set("X-Total", fmt.Sprintf("%d", maxResults))
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link, X-Total")
ctx.JSON(http.StatusOK, &apiPrvs)
}
// GetPullRequestVersionDiff
func GetPullRequestVersionDiff(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/pulls/{index}/versions/{versionId}/diff repository repoGetPullRequestVersionDiff
// ---
// summary: Get a pull request version diffs***
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the pull request to get
// type: integer
// format: int64
// required: true
// - name: versionId
// in: path
// description: id of the pull request version to get
// type: integer
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/Diff"
// "404":
// "$ref": "#/responses/notFound"
if ctx.Repo.Repository.IsEmpty {
ctx.NotFound()
return
}
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrPullRequestNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
}
return
}
prv, err := models.GetPullRequestVersionByID(pr.ID, ctx.ParamsInt64(":versionId"))
if err != nil {
if models.IsErrPullRequestNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetPullRequestVersionByID", err)
}
return
}
if diffs, err := gitdiff.GetDiffRange(ctx.Repo.GitRepo, prv.BaseCommitID, prv.HeadCommitID, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles); err == nil {
ctx.JSON(http.StatusOK, diffs)
} else {
ctx.Error(http.StatusInternalServerError, "GetDiffRange", err)
}
}

View File

@ -184,6 +184,13 @@ type swaggerResponsePullRequestList struct {
Body []api.PullRequest `json:"body"`
}
// PullRequestVersionList
// swagger:response PullRequestVersionList
type swaggerResponsePullRequestVersionList struct {
// in:body
Body []api.PullRequestVersion `json:"body"`
}
// PullReview
// swagger:response PullReview
type swaggerResponsePullReview struct {

View File

@ -3906,7 +3906,7 @@
],
"responses": {
"200": {
"description": "success"
"$ref": "#/responses/Diff"
},
"404": {
"$ref": "#/responses/notFound"
@ -8881,7 +8881,7 @@
"tags": [
"repository"
],
"summary": "List a repos's pull versions requests",
"summary": "List a repos's pull versions requests***",
"operationId": "repoListPullRequestVersions",
"parameters": [
{
@ -8921,7 +8921,59 @@
],
"responses": {
"200": {
"$ref": "#/responses/PullRequest"
"$ref": "#/responses/PullRequestVersionList"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/pulls/{index}/versions/{versionId}/diff": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a pull request version diffs***",
"operationId": "repoGetPullRequestVersionDiff",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "owner of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "integer",
"format": "int64",
"description": "index of the pull request to get",
"name": "index",
"in": "path",
"required": true
},
{
"type": "integer",
"format": "int64",
"description": "id of the pull request version to get",
"name": "versionId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/Diff"
},
"404": {
"$ref": "#/responses/notFound"
@ -17947,6 +17999,59 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"PullRequestVersion": {
"type": "object",
"properties": {
"add_line_num": {
"type": "integer",
"format": "int64",
"x-go-name": "AddLineNum"
},
"base_commit_sha": {
"type": "string",
"x-go-name": "BaseCommitSha"
},
"commits_count": {
"type": "integer",
"format": "int64",
"x-go-name": "CommitsCount"
},
"created_at": {
"type": "string",
"format": "date-time",
"x-go-name": "CreatedAt"
},
"del_line_num": {
"type": "integer",
"format": "int64",
"x-go-name": "DelLineNum"
},
"files_count": {
"type": "integer",
"format": "int64",
"x-go-name": "FilesCount"
},
"head_commit_sha": {
"type": "string",
"x-go-name": "HeadCommitSha"
},
"id": {
"type": "integer",
"format": "int64",
"x-go-name": "ID"
},
"start_commit_sha": {
"type": "string",
"x-go-name": "StartCommitSha"
},
"updated_at": {
"type": "string",
"format": "date-time",
"x-go-name": "UpdatedAt"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"PullReview": {
"description": "PullReview represents a pull request review",
"type": "object",
@ -19791,6 +19896,15 @@
}
}
},
"PullRequestVersionList": {
"description": "PullRequestVersionList",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/PullRequestVersion"
}
}
},
"PullReview": {
"description": "PullReview",
"schema": {