Skip to content

Commit 743cebe

Browse files
committed
Some improvements
1 parent a2804a5 commit 743cebe

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

routers/api/v1/repo/pull.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ func CreatePullRequest(ctx *context.APIContext) {
423423
return
424424
}
425425

426+
// we just need to check the head repository's permission here because the base
427+
// repository's permission is already checked in api.go with
428+
// mustAllowPulls, reqRepoReader(unit.TypeCode)
426429
if !ci.IsSameRepo() {
427430
// user should have permission to read headrepo's codes
428431
permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer)

routers/common/compare.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ type CompareInfo struct {
101101
HeadUser *user_model.User
102102
HeadRepo *repo_model.Repository
103103
HeadGitRepo *git.Repository
104-
RootRepo *repo_model.Repository
105-
OwnForkRepo *repo_model.Repository
106104
CompareInfo *git.CompareInfo
107105
close func()
108106
IsBaseCommit bool
@@ -289,38 +287,45 @@ func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *rep
289287
}
290288
}
291289

290+
ci.BaseFullRef, ci.IsBaseCommit, err = detectFullRef(ctx, baseRepo.ID, baseGitRepo, ci.BaseOriRef)
291+
if err != nil {
292+
ci.Close()
293+
return nil, err
294+
}
295+
296+
ci.HeadFullRef, ci.IsHeadCommit, err = detectFullRef(ctx, ci.HeadRepo.ID, ci.HeadGitRepo, ci.HeadOriRef)
297+
if err != nil {
298+
ci.Close()
299+
return nil, err
300+
}
301+
return ci, nil
302+
}
303+
304+
func (ci *CompareInfo) LoadRootRepoAndOwnForkRepo(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (*repo_model.Repository, *repo_model.Repository, error) {
292305
// find root repo
306+
var rootRepo *repo_model.Repository
307+
var err error
293308
if !baseRepo.IsFork {
294-
ci.RootRepo = baseRepo
309+
rootRepo = baseRepo
295310
} else {
296311
if !ci.HeadRepo.IsFork {
297-
ci.RootRepo = ci.HeadRepo
312+
rootRepo = ci.HeadRepo
298313
} else {
299-
ci.RootRepo, err = getRootRepo(ctx, baseRepo)
314+
rootRepo, err = getRootRepo(ctx, baseRepo)
300315
if err != nil {
301-
return nil, err
316+
return nil, nil, err
302317
}
303318
}
304319
}
305320

306321
// find ownfork repo
322+
var ownForkRepo *repo_model.Repository
307323
if doer != nil && ci.HeadRepo.OwnerID != doer.ID && baseRepo.OwnerID != doer.ID {
308-
ci.OwnForkRepo, err = findHeadRepo(ctx, baseRepo, doer.ID)
324+
ownForkRepo, err = findHeadRepo(ctx, baseRepo, doer.ID)
309325
if err != nil {
310-
return nil, err
326+
return nil, nil, err
311327
}
312328
}
313329

314-
ci.BaseFullRef, ci.IsBaseCommit, err = detectFullRef(ctx, baseRepo.ID, baseGitRepo, ci.BaseOriRef)
315-
if err != nil {
316-
ci.Close()
317-
return nil, err
318-
}
319-
320-
ci.HeadFullRef, ci.IsHeadCommit, err = detectFullRef(ctx, ci.HeadRepo.ID, ci.HeadGitRepo, ci.HeadOriRef)
321-
if err != nil {
322-
ci.Close()
323-
return nil, err
324-
}
325-
return ci, nil
330+
return rootRepo, ownForkRepo, nil
326331
}

routers/web/repo/compare.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,19 @@ func prepareCompareRepoBranchesTagsDropdowns(ctx *context.Context, ci *common.Co
444444
ctx.Data["HeadTags"] = headTags
445445
}
446446

447-
if ci.RootRepo != nil &&
448-
ci.RootRepo.ID != ci.HeadRepo.ID &&
449-
ci.RootRepo.ID != baseRepo.ID {
450-
canRead := access_model.CheckRepoUnitUser(ctx, ci.RootRepo, ctx.Doer, unit.TypeCode)
447+
rootRepo, ownForkRepo, err := ci.LoadRootRepoAndOwnForkRepo(ctx, baseRepo, ctx.Doer)
448+
if err != nil {
449+
ctx.ServerError("LoadRootRepoAndOwnForkRepo", err)
450+
return
451+
}
452+
453+
if rootRepo != nil &&
454+
rootRepo.ID != ci.HeadRepo.ID &&
455+
rootRepo.ID != baseRepo.ID {
456+
canRead := access_model.CheckRepoUnitUser(ctx, rootRepo, ctx.Doer, unit.TypeCode)
451457
if canRead {
452-
ctx.Data["RootRepo"] = ci.RootRepo
453-
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.RootRepo)
458+
ctx.Data["RootRepo"] = rootRepo
459+
branches, tags, err := getBranchesAndTagsForRepo(ctx, rootRepo)
454460
if err != nil {
455461
ctx.ServerError("GetBranchesForRepo", err)
456462
return
@@ -460,12 +466,12 @@ func prepareCompareRepoBranchesTagsDropdowns(ctx *context.Context, ci *common.Co
460466
}
461467
}
462468

463-
if ci.OwnForkRepo != nil &&
464-
ci.OwnForkRepo.ID != ci.HeadRepo.ID &&
465-
ci.OwnForkRepo.ID != baseRepo.ID &&
466-
(ci.RootRepo == nil || ci.OwnForkRepo.ID != ci.RootRepo.ID) {
467-
ctx.Data["OwnForkRepo"] = ci.OwnForkRepo
468-
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.OwnForkRepo)
469+
if ownForkRepo != nil &&
470+
ownForkRepo.ID != ci.HeadRepo.ID &&
471+
ownForkRepo.ID != baseRepo.ID &&
472+
(rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
473+
ctx.Data["OwnForkRepo"] = ownForkRepo
474+
branches, tags, err := getBranchesAndTagsForRepo(ctx, ownForkRepo)
469475
if err != nil {
470476
ctx.ServerError("GetBranchesForRepo", err)
471477
return

0 commit comments

Comments
 (0)