Skip to content

Commit b7f2be5

Browse files
committed
Fix possible bug
1 parent 92e68ae commit b7f2be5

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

routers/api/v1/repo/pull.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,20 @@ func CreatePullRequest(ctx *context.APIContext) {
396396
}
397397

398398
var (
399-
repo = ctx.Repo.Repository
399+
baseRepo = ctx.Repo.Repository
400400
labelIDs []int64
401401
milestoneID int64
402402
)
403403

404+
baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, baseRepo)
405+
if err != nil {
406+
ctx.ServerError("OpenRepository", err)
407+
return
408+
}
409+
defer closer.Close()
410+
404411
// Get repo/branch information
405-
ci, err := common.ParseComparePathParams(ctx, form.Base+"..."+form.Head, repo, ctx.Repo.GitRepo)
412+
ci, err := common.ParseComparePathParams(ctx, form.Base+"..."+form.Head, baseRepo, baseGitRepo)
406413
if err != nil {
407414
switch {
408415
case user_model.IsErrUserNotExist(err):
@@ -446,7 +453,7 @@ func CreatePullRequest(ctx *context.APIContext) {
446453
}
447454

448455
// Check if another PR exists with the same targets
449-
existingPr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, ctx.Repo.Repository.ID, ci.HeadOriRef, ci.BaseOriRef, issues_model.PullRequestFlowGithub)
456+
existingPr, err := issues_model.GetUnmergedPullRequest(ctx, ci.HeadRepo.ID, baseRepo.ID, ci.HeadOriRef, ci.BaseOriRef, issues_model.PullRequestFlowGithub)
450457
if err != nil {
451458
if !issues_model.IsErrPullRequestNotExist(err) {
452459
ctx.Error(http.StatusInternalServerError, "GetUnmergedPullRequest", err)
@@ -466,7 +473,7 @@ func CreatePullRequest(ctx *context.APIContext) {
466473
}
467474

468475
if len(form.Labels) > 0 {
469-
labels, err := issues_model.GetLabelsInRepoByIDs(ctx, ctx.Repo.Repository.ID, form.Labels)
476+
labels, err := issues_model.GetLabelsInRepoByIDs(ctx, baseRepo.ID, form.Labels)
470477
if err != nil {
471478
ctx.Error(http.StatusInternalServerError, "GetLabelsInRepoByIDs", err)
472479
return
@@ -493,7 +500,7 @@ func CreatePullRequest(ctx *context.APIContext) {
493500
}
494501

495502
if form.Milestone > 0 {
496-
milestone, err := issues_model.GetMilestoneByRepoID(ctx, ctx.Repo.Repository.ID, form.Milestone)
503+
milestone, err := issues_model.GetMilestoneByRepoID(ctx, baseRepo.ID, form.Milestone)
497504
if err != nil {
498505
if issues_model.IsErrMilestoneNotExist(err) {
499506
ctx.NotFound()
@@ -512,7 +519,7 @@ func CreatePullRequest(ctx *context.APIContext) {
512519
}
513520

514521
prIssue := &issues_model.Issue{
515-
RepoID: repo.ID,
522+
RepoID: baseRepo.ID,
516523
Title: form.Title,
517524
PosterID: ctx.Doer.ID,
518525
Poster: ctx.Doer,
@@ -523,11 +530,11 @@ func CreatePullRequest(ctx *context.APIContext) {
523530
}
524531
pr := &issues_model.PullRequest{
525532
HeadRepoID: ci.HeadRepo.ID,
526-
BaseRepoID: repo.ID,
533+
BaseRepoID: baseRepo.ID,
527534
HeadBranch: ci.HeadOriRef,
528535
BaseBranch: ci.BaseOriRef,
529536
HeadRepo: ci.HeadRepo,
530-
BaseRepo: repo,
537+
BaseRepo: baseRepo,
531538
MergeBase: ci.CompareInfo.MergeBase,
532539
Type: issues_model.PullRequestGitea,
533540
}
@@ -550,19 +557,19 @@ func CreatePullRequest(ctx *context.APIContext) {
550557
return
551558
}
552559

553-
valid, err := access_model.CanBeAssigned(ctx, assignee, repo, true)
560+
valid, err := access_model.CanBeAssigned(ctx, assignee, baseRepo, true)
554561
if err != nil {
555562
ctx.Error(http.StatusInternalServerError, "canBeAssigned", err)
556563
return
557564
}
558565
if !valid {
559-
ctx.Error(http.StatusUnprocessableEntity, "canBeAssigned", repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: repo.Name})
566+
ctx.Error(http.StatusUnprocessableEntity, "canBeAssigned", repo_model.ErrUserDoesNotHaveAccessToRepo{UserID: aID, RepoName: baseRepo.Name})
560567
return
561568
}
562569
}
563570

564571
prOpts := &pull_service.NewPullRequestOptions{
565-
Repo: repo,
572+
Repo: baseRepo,
566573
Issue: prIssue,
567574
LabelIDs: labelIDs,
568575
PullRequest: pr,
@@ -586,7 +593,7 @@ func CreatePullRequest(ctx *context.APIContext) {
586593
return
587594
}
588595

589-
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
596+
log.Trace("Pull request created: %d/%d", baseRepo.ID, prIssue.ID)
590597
ctx.JSON(http.StatusCreated, convert.ToAPIPullRequest(ctx, pr, ctx.Doer))
591598
}
592599

0 commit comments

Comments
 (0)