mirror of
https://github.com/go-gitea/gitea.git
synced 2025-12-16 22:15:43 +08:00
remove duplicated code
This commit is contained in:
parent
64d6010906
commit
eb40447ffd
@ -1081,58 +1081,17 @@ func parseCompareInfo(ctx *context.APIContext, compareParam string) (result *par
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var headRepo *repo_model.Repository
|
||||
if compareReq.HeadOwner == "" {
|
||||
if compareReq.HeadRepoName != "" { // unsupported syntax
|
||||
ctx.APIErrorNotFound()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
headRepo = ctx.Repo.Repository
|
||||
} else {
|
||||
var headOwner *user_model.User
|
||||
if compareReq.HeadOwner == ctx.Repo.Owner.Name {
|
||||
headOwner = ctx.Repo.Owner
|
||||
} else {
|
||||
headOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
ctx.APIErrorNotFound("GetUserOrOrgByName")
|
||||
} else {
|
||||
ctx.APIErrorInternal(err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
if compareReq.HeadRepoName == "" {
|
||||
if headOwner.ID == baseRepo.OwnerID {
|
||||
headRepo = baseRepo
|
||||
} else {
|
||||
headRepo, err = common.FindHeadRepo(ctx, baseRepo, headOwner.ID)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return nil, nil
|
||||
}
|
||||
if headRepo == nil {
|
||||
ctx.APIError(http.StatusBadRequest, "The user "+headOwner.Name+" does not have a fork of the base repository")
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if compareReq.HeadOwner == ctx.Repo.Owner.Name && compareReq.HeadRepoName == ctx.Repo.Repository.Name {
|
||||
headRepo = ctx.Repo.Repository
|
||||
} else {
|
||||
headRepo, err = repo_model.GetRepositoryByName(ctx, headOwner.ID, compareReq.HeadRepoName)
|
||||
if err != nil {
|
||||
if repo_model.IsErrRepoNotExist(err) {
|
||||
ctx.APIErrorNotFound("GetRepositoryByName")
|
||||
} else {
|
||||
ctx.APIErrorInternal(err)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
headOwner, headRepo, err := common.GetHeadOwnerAndRepo(ctx, baseRepo, compareReq)
|
||||
switch {
|
||||
case errors.Is(err, util.ErrInvalidArgument):
|
||||
ctx.APIError(http.StatusBadRequest, err.Error())
|
||||
return nil, nil
|
||||
case err != nil:
|
||||
ctx.APIErrorInternal(err)
|
||||
return nil, nil
|
||||
case headOwner == nil || headRepo == nil:
|
||||
ctx.APIErrorNotFound()
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
isSameRepo := baseRepo.ID == headRepo.ID
|
||||
|
||||
@ -174,3 +174,51 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func GetHeadOwnerAndRepo(ctx context.Context, baseRepo *repo_model.Repository, compareReq *CompareRouterReq) (headOwner *user_model.User, headRepo *repo_model.Repository, err error) {
|
||||
if compareReq.HeadOwner == "" {
|
||||
if compareReq.HeadRepoName != "" { // unsupported syntax
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
return baseRepo.Owner, baseRepo, nil
|
||||
}
|
||||
|
||||
if compareReq.HeadOwner == baseRepo.Owner.Name {
|
||||
headOwner = baseRepo.Owner
|
||||
} else {
|
||||
headOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
if compareReq.HeadRepoName == "" {
|
||||
if headOwner.ID == baseRepo.OwnerID {
|
||||
headRepo = baseRepo
|
||||
} else {
|
||||
headRepo, err = FindHeadRepo(ctx, baseRepo, headOwner.ID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if headRepo == nil {
|
||||
return nil, nil, util.ErrorWrap(util.ErrInvalidArgument, "the user %s does not have a fork of the base repository", headOwner.Name)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if compareReq.HeadOwner == baseRepo.Owner.Name && compareReq.HeadRepoName == baseRepo.Name {
|
||||
headRepo = baseRepo
|
||||
} else {
|
||||
headRepo, err = repo_model.GetRepositoryByName(ctx, headOwner.ID, compareReq.HeadRepoName)
|
||||
if err != nil {
|
||||
if repo_model.IsErrRepoNotExist(err) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return headOwner, headRepo, nil
|
||||
}
|
||||
|
||||
@ -208,58 +208,19 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
|
||||
return nil
|
||||
}
|
||||
|
||||
if compareReq.HeadOwner == "" {
|
||||
if compareReq.HeadRepoName != "" { // unsupported syntax
|
||||
ctx.NotFound(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
ci.HeadOwner = baseRepo.Owner
|
||||
ci.HeadRepo = baseRepo
|
||||
} else {
|
||||
if compareReq.HeadOwner == ctx.Repo.Owner.Name {
|
||||
ci.HeadOwner = ctx.Repo.Owner
|
||||
} else {
|
||||
ci.HeadOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
|
||||
if err != nil {
|
||||
if user_model.IsErrUserNotExist(err) {
|
||||
ctx.NotFound(nil)
|
||||
} else {
|
||||
ctx.ServerError("GetUserByName", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if compareReq.HeadRepoName == "" {
|
||||
if ci.HeadOwner.ID == baseRepo.OwnerID {
|
||||
ci.HeadRepo = baseRepo
|
||||
} else {
|
||||
ci.HeadRepo, err = common.FindHeadRepo(ctx, baseRepo, ci.HeadOwner.ID)
|
||||
if err != nil {
|
||||
ctx.ServerError("FindHeadRepo", err)
|
||||
return nil
|
||||
}
|
||||
if ci.HeadRepo == nil {
|
||||
ctx.HTTPError(http.StatusBadRequest, "The user "+ci.HeadOwner.Name+" does not have a fork of the base repository")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if compareReq.HeadOwner == ctx.Repo.Owner.Name && compareReq.HeadRepoName == ctx.Repo.Repository.Name {
|
||||
ci.HeadRepo = ctx.Repo.Repository
|
||||
} else {
|
||||
ci.HeadRepo, err = repo_model.GetRepositoryByName(ctx, ci.HeadOwner.ID, compareReq.HeadRepoName)
|
||||
if err != nil {
|
||||
if repo_model.IsErrRepoNotExist(err) {
|
||||
ctx.NotFound(nil)
|
||||
} else {
|
||||
ctx.ServerError("GetRepositoryByName", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
ci.HeadOwner, ci.HeadRepo, err = common.GetHeadOwnerAndRepo(ctx, baseRepo, compareReq)
|
||||
switch {
|
||||
case errors.Is(err, util.ErrInvalidArgument):
|
||||
ctx.HTTPError(http.StatusBadRequest, err.Error())
|
||||
return nil
|
||||
case err != nil:
|
||||
ctx.ServerError("GetHeadOwnerAndRepo", err)
|
||||
return nil
|
||||
case ci.HeadOwner == nil || ci.HeadRepo == nil:
|
||||
ctx.NotFound(nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
ci.BaseBranch = util.Iif(compareReq.BaseOriRef == "", baseRepo.DefaultBranch, compareReq.BaseOriRef)
|
||||
ci.HeadBranch = util.Iif(compareReq.HeadOriRef == "", ci.HeadRepo.DefaultBranch, compareReq.HeadOriRef)
|
||||
ci.DirectComparison = compareReq.DirectComparison()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user