Merge pull request '在pulls/{index}接口添加字段' (#31) from wonderful/gitea-1120-rc1:fix_pulls into develop

This commit is contained in:
jasder 2021-10-13 15:13:07 +08:00
commit e9a6a54229
4 changed files with 108 additions and 35 deletions

View File

@ -65,6 +65,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

View File

@ -55,6 +55,8 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
State: apiIssue.State,
IsLocked: apiIssue.IsLocked,
Comments: apiIssue.Comments,
CommitNum: pr.CommitNum,
ChangedFiles: pr.ChangedFiles,
HTMLURL: pr.Issue.HTMLURL(),
DiffURL: pr.Issue.DiffURL(),
PatchURL: pr.Issue.PatchURL(),
@ -63,7 +65,6 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
Deadline: apiIssue.Deadline,
Created: pr.Issue.CreatedUnix.AsTimePtr(),
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
Base: &api.PRBranchInfo{
Name: pr.BaseBranch,
Ref: pr.BaseBranch,

View File

@ -23,6 +23,8 @@ type PullRequest struct {
State StateType `json:"state"`
IsLocked bool `json:"is_locked"`
Comments int `json:"comments"`
CommitNum int `json:"commit_num"`
ChangedFiles int `json:"changed_files"`
HTMLURL string `json:"html_url"`
DiffURL string `json:"diff_url"`

View File

@ -169,6 +169,72 @@ func GetPullRequest(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "LoadHeadRepo", err)
return
}
issue := checkPullInfo(ctx.Context)
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 = PrepareMergedViewPullInfo(ctx.Context, issue)
} else {
prInfo = 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 *git.Repository
)
diffRepoPath = ctx.Repo.GitRepo.Path
gitRepo = ctx.Repo.GitRepo
headCommitId, err := gitRepo.GetRefCommitID(pull.GetGitRefName())
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return
}
startCommitID = prInfo.MergeBase
endCommitID = headCommitId
whitespaceFlags := map[string]string{
"ignore-all": "-w",
"ignore-change": "-b",
"ignore-eol": "--ignore-space-at-eol",
"": ""}
ctx.Data["WhitespaceBehavior"] = ""
diff, err1 := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath, startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
if err1 != nil {
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err1)
return
}
var changedFiles int
changedFiles = diff.NumFiles
pr.CommitNum = commitNum
pr.ChangedFiles = changedFiles
ctx.JSON(http.StatusOK, convert.ToAPIPullRequest(pr))
}