Skip to content

Commit 1483291

Browse files
authored
Refactor pull request view (1) (go-gitea#37380)
Refactor preparePullViewPullInfo and related functions, split them into small ones: * preparePullViewPullInfo creates PullRequestViewInfo struct * if the PR is merged: prepareView**Merged**PullInfo * if the PR is open: prepareView**Open**PullInfo In prepareViewMergedPullInfo and preparePullViewFillInfo: call preparePullView**FillInfo** consistnently preparePullViewFillInfo calls preparePullViewFill**CompareInfo** and preparePullViewFill**CommitStatusInfo**
1 parent de99b1f commit 1483291

6 files changed

Lines changed: 249 additions & 311 deletions

File tree

modules/git/gitcmd/error.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func StderrHasPrefix(err error, prefix string) bool {
5656
return strings.HasPrefix(stderr, prefix)
5757
}
5858

59+
func StderrContains(err error, sub string) bool {
60+
stderr, ok := ErrorAsStderr(err)
61+
if !ok {
62+
return false
63+
}
64+
return strings.Contains(stderr, sub)
65+
}
66+
5967
func IsErrorExitCode(err error, code int) bool {
6068
var exitError *exec.ExitError
6169
if errors.As(err, &exitError) {

routers/api/v1/repo/pull.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ func parseCompareInfo(ctx *context.APIContext, compareParam string) (result *git
11751175
return nil, nil
11761176
}
11771177

1178-
return compareInfo, closer
1178+
return &compareInfo, closer
11791179
}
11801180

11811181
// UpdatePullRequest merge PR's baseBranch into headBranch
@@ -1419,14 +1419,14 @@ func GetPullRequestCommits(ctx *context.APIContext) {
14191419
return
14201420
}
14211421

1422-
var compareInfo *git_service.CompareInfo
14231422
baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo)
14241423
if err != nil {
14251424
ctx.APIErrorInternal(err)
14261425
return
14271426
}
14281427
defer closer.Close()
14291428

1429+
var compareInfo git_service.CompareInfo
14301430
if pr.HasMerged {
14311431
compareInfo, err = git_service.GetCompareInfo(ctx, pr.BaseRepo, pr.BaseRepo, baseGitRepo, git.RefName(pr.MergeBase), git.RefName(pr.GetGitHeadRefName()), false, false)
14321432
} else {
@@ -1552,7 +1552,7 @@ func GetPullRequestFiles(ctx *context.APIContext) {
15521552

15531553
baseGitRepo := ctx.Repo.GitRepo
15541554

1555-
var compareInfo *git_service.CompareInfo
1555+
var compareInfo git_service.CompareInfo
15561556
if pr.HasMerged {
15571557
compareInfo, err = git_service.GetCompareInfo(ctx, pr.BaseRepo, pr.BaseRepo, baseGitRepo, git.RefName(pr.MergeBase), git.RefName(pr.GetGitHeadRefName()), false, false)
15581558
} else {

routers/web/repo/compare.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,7 @@ func ParseCompareInfo(ctx *context.Context) *git_service.CompareInfo {
421421
} else {
422422
ctx.Data["BeforeCommitID"] = compareInfo.MergeBase
423423
}
424-
425-
return compareInfo
424+
return &compareInfo
426425
}
427426

428427
func prepareNewPullRequestTitleContent(ci *git_service.CompareInfo, commits []*git_model.SignCommitWithStatuses) (title, content string) {

routers/web/repo/issue_view.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,13 @@ func ViewIssue(ctx *context.Context) {
386386
prepareIssueViewSidebarTimeTracker,
387387
prepareIssueViewSidebarDependency,
388388
prepareIssueViewSidebarPin,
389-
func(ctx *context.Context, issue *issues_model.Issue) { preparePullViewPullInfo(ctx, issue) },
390-
preparePullViewReviewAndMerge,
391389
}
392-
390+
if issue.IsPull {
391+
prepareFuncs = append(prepareFuncs,
392+
func(ctx *context.Context, issue *issues_model.Issue) { preparePullViewPullInfo(ctx, issue) },
393+
preparePullViewReviewAndMerge,
394+
)
395+
}
393396
for _, prepareFunc := range prepareFuncs {
394397
prepareFunc(ctx, issue)
395398
if ctx.Written() {
@@ -443,7 +446,13 @@ func ViewPullMergeBox(ctx *context.Context) {
443446
return
444447
}
445448
preparePullViewPullInfo(ctx, issue)
449+
if ctx.Written() {
450+
return
451+
}
446452
preparePullViewReviewAndMerge(ctx, issue)
453+
if ctx.Written() {
454+
return
455+
}
447456
ctx.Data["PullMergeBoxReloading"] = issue.PullRequest.IsChecking()
448457

449458
// TODO: it should use a dedicated struct to render the pull merge box, to make sure all data is prepared correctly

0 commit comments

Comments
 (0)