1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-10 17:54:27 +00:00

Some improvements

This commit is contained in:
Lunny Xiao 2024-12-12 11:53:04 -08:00
parent a2804a5efc
commit 743cebe66f
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 50 additions and 36 deletions

View File

@ -423,6 +423,9 @@ func CreatePullRequest(ctx *context.APIContext) {
return return
} }
// we just need to check the head repository's permission here because the base
// repository's permission is already checked in api.go with
// mustAllowPulls, reqRepoReader(unit.TypeCode)
if !ci.IsSameRepo() { if !ci.IsSameRepo() {
// user should have permission to read headrepo's codes // user should have permission to read headrepo's codes
permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer) permHead, err := access_model.GetUserRepoPermission(ctx, ci.HeadRepo, ctx.Doer)

View File

@ -101,8 +101,6 @@ type CompareInfo struct {
HeadUser *user_model.User HeadUser *user_model.User
HeadRepo *repo_model.Repository HeadRepo *repo_model.Repository
HeadGitRepo *git.Repository HeadGitRepo *git.Repository
RootRepo *repo_model.Repository
OwnForkRepo *repo_model.Repository
CompareInfo *git.CompareInfo CompareInfo *git.CompareInfo
close func() close func()
IsBaseCommit bool IsBaseCommit bool
@ -289,28 +287,6 @@ func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *rep
} }
} }
// find root repo
if !baseRepo.IsFork {
ci.RootRepo = baseRepo
} else {
if !ci.HeadRepo.IsFork {
ci.RootRepo = ci.HeadRepo
} else {
ci.RootRepo, err = getRootRepo(ctx, baseRepo)
if err != nil {
return nil, err
}
}
}
// find ownfork repo
if doer != nil && ci.HeadRepo.OwnerID != doer.ID && baseRepo.OwnerID != doer.ID {
ci.OwnForkRepo, err = findHeadRepo(ctx, baseRepo, doer.ID)
if err != nil {
return nil, err
}
}
ci.BaseFullRef, ci.IsBaseCommit, err = detectFullRef(ctx, baseRepo.ID, baseGitRepo, ci.BaseOriRef) ci.BaseFullRef, ci.IsBaseCommit, err = detectFullRef(ctx, baseRepo.ID, baseGitRepo, ci.BaseOriRef)
if err != nil { if err != nil {
ci.Close() ci.Close()
@ -324,3 +300,32 @@ func ParseComparePathParams(ctx context.Context, pathParam string, baseRepo *rep
} }
return ci, nil return ci, nil
} }
func (ci *CompareInfo) LoadRootRepoAndOwnForkRepo(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (*repo_model.Repository, *repo_model.Repository, error) {
// find root repo
var rootRepo *repo_model.Repository
var err error
if !baseRepo.IsFork {
rootRepo = baseRepo
} else {
if !ci.HeadRepo.IsFork {
rootRepo = ci.HeadRepo
} else {
rootRepo, err = getRootRepo(ctx, baseRepo)
if err != nil {
return nil, nil, err
}
}
}
// find ownfork repo
var ownForkRepo *repo_model.Repository
if doer != nil && ci.HeadRepo.OwnerID != doer.ID && baseRepo.OwnerID != doer.ID {
ownForkRepo, err = findHeadRepo(ctx, baseRepo, doer.ID)
if err != nil {
return nil, nil, err
}
}
return rootRepo, ownForkRepo, nil
}

View File

@ -444,13 +444,19 @@ func prepareCompareRepoBranchesTagsDropdowns(ctx *context.Context, ci *common.Co
ctx.Data["HeadTags"] = headTags ctx.Data["HeadTags"] = headTags
} }
if ci.RootRepo != nil && rootRepo, ownForkRepo, err := ci.LoadRootRepoAndOwnForkRepo(ctx, baseRepo, ctx.Doer)
ci.RootRepo.ID != ci.HeadRepo.ID && if err != nil {
ci.RootRepo.ID != baseRepo.ID { ctx.ServerError("LoadRootRepoAndOwnForkRepo", err)
canRead := access_model.CheckRepoUnitUser(ctx, ci.RootRepo, ctx.Doer, unit.TypeCode) return
}
if rootRepo != nil &&
rootRepo.ID != ci.HeadRepo.ID &&
rootRepo.ID != baseRepo.ID {
canRead := access_model.CheckRepoUnitUser(ctx, rootRepo, ctx.Doer, unit.TypeCode)
if canRead { if canRead {
ctx.Data["RootRepo"] = ci.RootRepo ctx.Data["RootRepo"] = rootRepo
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.RootRepo) branches, tags, err := getBranchesAndTagsForRepo(ctx, rootRepo)
if err != nil { if err != nil {
ctx.ServerError("GetBranchesForRepo", err) ctx.ServerError("GetBranchesForRepo", err)
return return
@ -460,12 +466,12 @@ func prepareCompareRepoBranchesTagsDropdowns(ctx *context.Context, ci *common.Co
} }
} }
if ci.OwnForkRepo != nil && if ownForkRepo != nil &&
ci.OwnForkRepo.ID != ci.HeadRepo.ID && ownForkRepo.ID != ci.HeadRepo.ID &&
ci.OwnForkRepo.ID != baseRepo.ID && ownForkRepo.ID != baseRepo.ID &&
(ci.RootRepo == nil || ci.OwnForkRepo.ID != ci.RootRepo.ID) { (rootRepo == nil || ownForkRepo.ID != rootRepo.ID) {
ctx.Data["OwnForkRepo"] = ci.OwnForkRepo ctx.Data["OwnForkRepo"] = ownForkRepo
branches, tags, err := getBranchesAndTagsForRepo(ctx, ci.OwnForkRepo) branches, tags, err := getBranchesAndTagsForRepo(ctx, ownForkRepo)
if err != nil { if err != nil {
ctx.ServerError("GetBranchesForRepo", err) ctx.ServerError("GetBranchesForRepo", err)
return return