Skip to content

Commit 5c67694

Browse files
committed
add refs/for/<target-branch>/<topic-branch> support also
1 parent 9eeaf3d commit 5c67694

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

routers/private/hook.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
618618
description string
619619
)
620620

621+
topicBranch = opts.GitPushOptions["topic"]
622+
621623
for i := range opts.OldCommitIDs {
622624
if opts.NewCommitIDs[i] == git.EmptySHA {
623625
results = append(results, private.HockProcReceiveRefResult{
@@ -630,34 +632,58 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
630632
}
631633

632634
baseBranchName := opts.RefFullNames[i][len(git.PullRequestPrefix):]
635+
curentTopicBranch := ""
633636
if !gitRepo.IsBranchExist(baseBranchName) {
637+
// try match refs/for/<target-branch>/<topic-branch>
638+
splits := make([]int, 0, 6)
639+
for index, v := range baseBranchName {
640+
if v == '/' {
641+
if len(splits) >= 6 {
642+
break
643+
}
644+
splits = append(splits, index)
645+
}
646+
}
647+
if len(splits) > 5 {
648+
results = append(results, private.HockProcReceiveRefResult{
649+
OrignRef: opts.RefFullNames[i],
650+
OldOID: opts.OldCommitIDs[i],
651+
NewOID: opts.NewCommitIDs[i],
652+
Err: fmt.Sprintf("ref 'refs/for/%s' contain too many '/'. \n suggest use 'git push refs/for/<target-branch> -o topic='<topic-branch>'", baseBranchName),
653+
})
654+
continue
655+
}
656+
if len(splits) > 0 {
657+
for p := len(splits) - 1; p >= 0; p-- {
658+
if gitRepo.IsBranchExist(baseBranchName[:splits[p]]) && p != len(baseBranchName)-1 {
659+
curentTopicBranch = baseBranchName[splits[p]+1:]
660+
baseBranchName = baseBranchName[:splits[p]]
661+
break
662+
}
663+
}
664+
}
665+
}
666+
667+
if len(topicBranch) == 0 && len(curentTopicBranch) == 0 {
634668
results = append(results, private.HockProcReceiveRefResult{
635669
OrignRef: opts.RefFullNames[i],
636670
OldOID: opts.OldCommitIDs[i],
637671
NewOID: opts.NewCommitIDs[i],
638-
Err: fmt.Sprintf("target branch %s is not exist in %s/%s",
639-
baseBranchName, ownerName, repoName),
672+
Err: fmt.Sprintf("topic-branch is not set"),
640673
})
641-
continue
642-
}
643-
644-
if len(topicBranch) == 0 {
645-
has := false
646-
topicBranch, has = opts.GitPushOptions["topic"]
647-
if !has || len(topicBranch) == 0 {
648-
ctx.JSON(http.StatusForbidden, map[string]interface{}{
649-
"err": "push option 'topic' is requested",
650-
})
651-
return
652-
}
653674
}
654675

655676
headBranch := ""
656677
userName := strings.ToLower(opts.UserName)
657-
if !strings.HasPrefix(topicBranch, userName+"/") {
658-
headBranch = userName + "/" + topicBranch
678+
679+
if len(curentTopicBranch) == 0 {
680+
curentTopicBranch = topicBranch
681+
}
682+
683+
if !strings.HasPrefix(curentTopicBranch, userName+"/") {
684+
headBranch = userName + "/" + curentTopicBranch
659685
} else {
660-
headBranch = topicBranch
686+
headBranch = curentTopicBranch
661687
}
662688

663689
pr, err := models.GetUnmergedPullRequest(repo.ID, repo.ID, headBranch, baseBranchName, models.PullRequestStyleAGit)

0 commit comments

Comments
 (0)