From a6ac790d1a8436b3b93c8082a2bb088931a77bed Mon Sep 17 00:00:00 2001 From: hang Date: Wed, 13 Oct 2021 10:27:46 +0800 Subject: [PATCH] fix pulls/{index} --- models/pull.go | 4 +++ modules/convert/pull.go | 45 ++++++++++++------------- modules/structs/pull.go | 28 ++++++++-------- routers/api/v1/repo/pull.go | 66 +++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/models/pull.go b/models/pull.go index 9f1f48526..2b4587e56 100644 --- a/models/pull.go +++ b/models/pull.go @@ -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 diff --git a/modules/convert/pull.go b/modules/convert/pull.go index 2fa22efcb..44e79859f 100644 --- a/modules/convert/pull.go +++ b/modules/convert/pull.go @@ -42,28 +42,29 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest { } apiPullRequest := &api.PullRequest{ - ID: pr.ID, - URL: pr.Issue.HTMLURL(), - Index: pr.Index, - Poster: apiIssue.Poster, - Title: apiIssue.Title, - Body: apiIssue.Body, - Labels: apiIssue.Labels, - Milestone: apiIssue.Milestone, - Assignee: apiIssue.Assignee, - Assignees: apiIssue.Assignees, - 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(), - + ID: pr.ID, + URL: pr.Issue.HTMLURL(), + Index: pr.Index, + Poster: apiIssue.Poster, + Title: apiIssue.Title, + Body: apiIssue.Body, + Labels: apiIssue.Labels, + Milestone: apiIssue.Milestone, + Assignee: apiIssue.Assignee, + Assignees: apiIssue.Assignees, + 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(), + 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, Ref: pr.BaseBranch, diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 653091b2f..11301c74d 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -10,19 +10,21 @@ import ( // PullRequest represents a pull request type PullRequest struct { - ID int64 `json:"id"` - URL string `json:"url"` - Index int64 `json:"number"` - Poster *User `json:"user"` - Title string `json:"title"` - Body string `json:"body"` - Labels []*Label `json:"labels"` - Milestone *Milestone `json:"milestone"` - Assignee *User `json:"assignee"` - Assignees []*User `json:"assignees"` - State StateType `json:"state"` - IsLocked bool `json:"is_locked"` - Comments int `json:"comments"` + ID int64 `json:"id"` + URL string `json:"url"` + Index int64 `json:"number"` + Poster *User `json:"user"` + Title string `json:"title"` + Body string `json:"body"` + Labels []*Label `json:"labels"` + Milestone *Milestone `json:"milestone"` + Assignee *User `json:"assignee"` + Assignees []*User `json:"assignees"` + 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"` diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index ac12c797c..5cd3da96d 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -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)) }