Skip to content

Commit eb40447

Browse files
committed
remove duplicated code
1 parent 64d6010 commit eb40447

File tree

3 files changed

+71
-103
lines changed

3 files changed

+71
-103
lines changed

routers/api/v1/repo/pull.go

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,58 +1081,17 @@ func parseCompareInfo(ctx *context.APIContext, compareParam string) (result *par
10811081
return nil, nil
10821082
}
10831083

1084-
var headRepo *repo_model.Repository
1085-
if compareReq.HeadOwner == "" {
1086-
if compareReq.HeadRepoName != "" { // unsupported syntax
1087-
ctx.APIErrorNotFound()
1088-
return nil, nil
1089-
}
1090-
1091-
headRepo = ctx.Repo.Repository
1092-
} else {
1093-
var headOwner *user_model.User
1094-
if compareReq.HeadOwner == ctx.Repo.Owner.Name {
1095-
headOwner = ctx.Repo.Owner
1096-
} else {
1097-
headOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
1098-
if err != nil {
1099-
if user_model.IsErrUserNotExist(err) {
1100-
ctx.APIErrorNotFound("GetUserOrOrgByName")
1101-
} else {
1102-
ctx.APIErrorInternal(err)
1103-
}
1104-
return nil, nil
1105-
}
1106-
}
1107-
if compareReq.HeadRepoName == "" {
1108-
if headOwner.ID == baseRepo.OwnerID {
1109-
headRepo = baseRepo
1110-
} else {
1111-
headRepo, err = common.FindHeadRepo(ctx, baseRepo, headOwner.ID)
1112-
if err != nil {
1113-
ctx.APIErrorInternal(err)
1114-
return nil, nil
1115-
}
1116-
if headRepo == nil {
1117-
ctx.APIError(http.StatusBadRequest, "The user "+headOwner.Name+" does not have a fork of the base repository")
1118-
return nil, nil
1119-
}
1120-
}
1121-
} else {
1122-
if compareReq.HeadOwner == ctx.Repo.Owner.Name && compareReq.HeadRepoName == ctx.Repo.Repository.Name {
1123-
headRepo = ctx.Repo.Repository
1124-
} else {
1125-
headRepo, err = repo_model.GetRepositoryByName(ctx, headOwner.ID, compareReq.HeadRepoName)
1126-
if err != nil {
1127-
if repo_model.IsErrRepoNotExist(err) {
1128-
ctx.APIErrorNotFound("GetRepositoryByName")
1129-
} else {
1130-
ctx.APIErrorInternal(err)
1131-
}
1132-
return nil, nil
1133-
}
1134-
}
1135-
}
1084+
headOwner, headRepo, err := common.GetHeadOwnerAndRepo(ctx, baseRepo, compareReq)
1085+
switch {
1086+
case errors.Is(err, util.ErrInvalidArgument):
1087+
ctx.APIError(http.StatusBadRequest, err.Error())
1088+
return nil, nil
1089+
case err != nil:
1090+
ctx.APIErrorInternal(err)
1091+
return nil, nil
1092+
case headOwner == nil || headRepo == nil:
1093+
ctx.APIErrorNotFound()
1094+
return nil, nil
11361095
}
11371096

11381097
isSameRepo := baseRepo.ID == headRepo.ID

routers/common/compare.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,51 @@ func findHeadRepoFromRootBase(ctx context.Context, baseRepo *repo_model.Reposito
174174
}
175175
return nil, nil
176176
}
177+
178+
func GetHeadOwnerAndRepo(ctx context.Context, baseRepo *repo_model.Repository, compareReq *CompareRouterReq) (headOwner *user_model.User, headRepo *repo_model.Repository, err error) {
179+
if compareReq.HeadOwner == "" {
180+
if compareReq.HeadRepoName != "" { // unsupported syntax
181+
return nil, nil, nil
182+
}
183+
184+
return baseRepo.Owner, baseRepo, nil
185+
}
186+
187+
if compareReq.HeadOwner == baseRepo.Owner.Name {
188+
headOwner = baseRepo.Owner
189+
} else {
190+
headOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
191+
if err != nil {
192+
if user_model.IsErrUserNotExist(err) {
193+
return nil, nil, nil
194+
}
195+
return nil, nil, err
196+
}
197+
}
198+
if compareReq.HeadRepoName == "" {
199+
if headOwner.ID == baseRepo.OwnerID {
200+
headRepo = baseRepo
201+
} else {
202+
headRepo, err = FindHeadRepo(ctx, baseRepo, headOwner.ID)
203+
if err != nil {
204+
return nil, nil, err
205+
}
206+
if headRepo == nil {
207+
return nil, nil, util.ErrorWrap(util.ErrInvalidArgument, "the user %s does not have a fork of the base repository", headOwner.Name)
208+
}
209+
}
210+
} else {
211+
if compareReq.HeadOwner == baseRepo.Owner.Name && compareReq.HeadRepoName == baseRepo.Name {
212+
headRepo = baseRepo
213+
} else {
214+
headRepo, err = repo_model.GetRepositoryByName(ctx, headOwner.ID, compareReq.HeadRepoName)
215+
if err != nil {
216+
if repo_model.IsErrRepoNotExist(err) {
217+
return nil, nil, nil
218+
}
219+
return nil, nil, err
220+
}
221+
}
222+
}
223+
return headOwner, headRepo, nil
224+
}

routers/web/repo/compare.go

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -208,58 +208,19 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
208208
return nil
209209
}
210210

211-
if compareReq.HeadOwner == "" {
212-
if compareReq.HeadRepoName != "" { // unsupported syntax
213-
ctx.NotFound(nil)
214-
return nil
215-
}
216-
217-
ci.HeadOwner = baseRepo.Owner
218-
ci.HeadRepo = baseRepo
219-
} else {
220-
if compareReq.HeadOwner == ctx.Repo.Owner.Name {
221-
ci.HeadOwner = ctx.Repo.Owner
222-
} else {
223-
ci.HeadOwner, err = user_model.GetUserOrOrgByName(ctx, compareReq.HeadOwner)
224-
if err != nil {
225-
if user_model.IsErrUserNotExist(err) {
226-
ctx.NotFound(nil)
227-
} else {
228-
ctx.ServerError("GetUserByName", err)
229-
}
230-
return nil
231-
}
232-
}
233-
if compareReq.HeadRepoName == "" {
234-
if ci.HeadOwner.ID == baseRepo.OwnerID {
235-
ci.HeadRepo = baseRepo
236-
} else {
237-
ci.HeadRepo, err = common.FindHeadRepo(ctx, baseRepo, ci.HeadOwner.ID)
238-
if err != nil {
239-
ctx.ServerError("FindHeadRepo", err)
240-
return nil
241-
}
242-
if ci.HeadRepo == nil {
243-
ctx.HTTPError(http.StatusBadRequest, "The user "+ci.HeadOwner.Name+" does not have a fork of the base repository")
244-
return nil
245-
}
246-
}
247-
} else {
248-
if compareReq.HeadOwner == ctx.Repo.Owner.Name && compareReq.HeadRepoName == ctx.Repo.Repository.Name {
249-
ci.HeadRepo = ctx.Repo.Repository
250-
} else {
251-
ci.HeadRepo, err = repo_model.GetRepositoryByName(ctx, ci.HeadOwner.ID, compareReq.HeadRepoName)
252-
if err != nil {
253-
if repo_model.IsErrRepoNotExist(err) {
254-
ctx.NotFound(nil)
255-
} else {
256-
ctx.ServerError("GetRepositoryByName", err)
257-
}
258-
return nil
259-
}
260-
}
261-
}
211+
ci.HeadOwner, ci.HeadRepo, err = common.GetHeadOwnerAndRepo(ctx, baseRepo, compareReq)
212+
switch {
213+
case errors.Is(err, util.ErrInvalidArgument):
214+
ctx.HTTPError(http.StatusBadRequest, err.Error())
215+
return nil
216+
case err != nil:
217+
ctx.ServerError("GetHeadOwnerAndRepo", err)
218+
return nil
219+
case ci.HeadOwner == nil || ci.HeadRepo == nil:
220+
ctx.NotFound(nil)
221+
return nil
262222
}
223+
263224
ci.BaseBranch = util.Iif(compareReq.BaseOriRef == "", baseRepo.DefaultBranch, compareReq.BaseOriRef)
264225
ci.HeadBranch = util.Iif(compareReq.HeadOriRef == "", ci.HeadRepo.DefaultBranch, compareReq.HeadOriRef)
265226
ci.DirectComparison = compareReq.DirectComparison()

0 commit comments

Comments
 (0)