diff --git a/models/pull.go b/models/pull.go index c73768841..69308abdf 100644 --- a/models/pull.go +++ b/models/pull.go @@ -69,6 +69,10 @@ type PullRequest struct { MergedUnix timeutil.TimeStamp `xorm:"updated INDEX"` isHeadRepoLoaded bool `xorm:"-"` + + // add configure + CommitNum int + ChangedFiles int } // MustHeadUserName returns the HeadRepo's username if failed return blank diff --git a/modules/convert/pull.go b/modules/convert/pull.go index ad70eb5ca..2684c96fd 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -61,14 +61,17 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *models.User) *api.PullReques State: apiIssue.State, IsLocked: apiIssue.IsLocked, Comments: apiIssue.Comments, - HTMLURL: pr.Issue.HTMLURL(), - DiffURL: pr.Issue.DiffURL(), - PatchURL: pr.Issue.PatchURL(), - HasMerged: pr.HasMerged, - MergeBase: pr.MergeBase, - Deadline: apiIssue.Deadline, - Created: pr.Issue.CreatedUnix.AsTimePtr(), - Updated: pr.Issue.UpdatedUnix.AsTimePtr(), + + CommitNum: pr.CommitNum, + ChangedFiles: pr.ChangedFiles, + HTMLURL: pr.Issue.HTMLURL(), + DiffURL: pr.Issue.DiffURL(), + PatchURL: pr.Issue.PatchURL(), + HasMerged: pr.HasMerged, + MergeBase: pr.MergeBase, + Deadline: apiIssue.Deadline, + Created: pr.Issue.CreatedUnix.AsTimePtr(), + Updated: pr.Issue.UpdatedUnix.AsTimePtr(), Base: &api.PRBranchInfo{ Name: pr.BaseBranch, diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 653091b2f..4e81bab25 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -48,6 +48,9 @@ type PullRequest struct { Updated *time.Time `json:"updated_at"` // swagger:strfmt date-time Closed *time.Time `json:"closed_at"` + + CommitNum int `json:"commit_num"` + ChangedFiles int `json:"changed_files"` } // PRBranchInfo information about a branch diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index ba8bf2cc5..58b3f4490 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -5,6 +5,7 @@ package repo import ( + "container/list" "errors" "fmt" "math" @@ -19,11 +20,15 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/forms" + "code.gitea.io/gitea/services/gitdiff" + + pull_prepare "code.gitea.io/gitea/routers/web/repo" issue_service "code.gitea.io/gitea/services/issue" pull_service "code.gitea.io/gitea/services/pull" repo_service "code.gitea.io/gitea/services/repository" @@ -172,6 +177,68 @@ func GetPullRequest(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err) return } + + // issue := checkPullInfo(ctx.Context) + issue := pr.Issue + if issue == nil { + ctx.NotFound() + return + } + if ctx.Written() { + return + } + pull := issue.PullRequest + // get pull commits nums + var commits *list.List + var prInfo *git.CompareInfo + if pull.HasMerged { + prInfo = pull_prepare.PrepareMergedViewPullInfo(ctx.Context, issue) + } else { + prInfo = pull_prepare.PrepareViewPullInfo(ctx.Context, issue) + } + + if ctx.Written() { + return + } else if prInfo == nil { + ctx.NotFound("ViewPullCommits", nil) + return + } + var commitNum int + commits = prInfo.Commits + commits = models.ValidateCommitsWithEmails(commits) + commits = models.ParseCommitsWithSignature(commits, ctx.Repo.Repository) + commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository) + commitNum = commits.Len() + + //get pull changedfils + var ( + // diffRepoPath string + startCommitID string + endCommitID string + gitRepo = ctx.Repo.GitRepo + ) + gitRepo = ctx.Repo.GitRepo + headCommitId, err := gitRepo.GetRefCommitID(pull.GetGitRefName()) + if err != nil { + ctx.ServerError("GetRefCommitID", err) + return + } + startCommitID = prInfo.MergeBase + endCommitID = headCommitId + ctx.Data["WhitespaceBehavior"] = "" + + diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(gitRepo, + startCommitID, endCommitID, setting.Git.MaxGitDiffLines, + setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, + gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string))) + if err != nil { + ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err) + return + } + var changedFiles int + changedFiles = diff.NumFiles + pr.CommitNum = commitNum + pr.ChangedFiles = changedFiles ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(pr, ctx.User)) } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index b05d62e43..9b9b208ec 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2531,6 +2531,39 @@ } } }, + "/repos/{owner}/{repo}/branch_tag_count": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get branche and tag of a repository", + "operationId": "repoBranchAndTagCountGet", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/RepoBranchAndTagCount" + } + } + } + }, "/repos/{owner}/{repo}/branches": { "get": { "produces": [ @@ -16235,6 +16268,11 @@ "type": "string", "x-go-name": "Body" }, + "changed_files": { + "type": "integer", + "format": "int64", + "x-go-name": "ChangedFiles" + }, "closed_at": { "type": "string", "format": "date-time", @@ -16245,6 +16283,11 @@ "format": "int64", "x-go-name": "Comments" }, + "commit_num": { + "type": "integer", + "format": "int64", + "x-go-name": "CommitNum" + }, "created_at": { "type": "string", "format": "date-time", @@ -16616,6 +16659,22 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "RepoBranchAndTagCount": { + "type": "object", + "properties": { + "branch_count": { + "type": "integer", + "format": "int64", + "x-go-name": "BranchCount" + }, + "tag_count": { + "type": "integer", + "format": "int64", + "x-go-name": "TagCount" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "RepoCommit": { "type": "object", "title": "RepoCommit contains information of a commit in the context of a repository.", @@ -18111,6 +18170,15 @@ } } }, + "RepoBranchAndTagCount": { + "description": "RepoBranchAndTagCount", + "schema": { + "$ref": "#/definitions/RepoBranchAndTagCount" + }, + "headers": { + "body": {} + } + }, "Repository": { "description": "Repository", "schema": {