@@ -3,16 +3,46 @@ package controller
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "net/http"
8+ "regexp"
9+ "strings"
710 "time"
811
12+ "github.com/jiaozifs/jiaozifs/utils/hash"
13+
914 "github.com/jiaozifs/jiaozifs/auth"
1015
1116 "github.com/jiaozifs/jiaozifs/api"
1217 "github.com/jiaozifs/jiaozifs/models"
1318 "go.uber.org/fx"
1419)
1520
21+ var maxBranchNameLength = 20
22+ var branchNameRegex = regexp .MustCompile ("^[a-zA-Z0-9_]*$" )
23+
24+ func CheckBranchName (name string ) error {
25+ for _ , blackName := range RepoNameBlackList {
26+ if name == blackName {
27+ return errors .New ("repository name is black list" )
28+ }
29+ }
30+
31+ if len (name ) > maxBranchNameLength {
32+ return fmt .Errorf ("branch name is too long" )
33+ }
34+
35+ seg := strings .Split (name , "/" )
36+ if len (seg ) > 2 {
37+ return fmt .Errorf ("ref format must be <name> or <name>/<name>" )
38+ }
39+
40+ if ! branchNameRegex .Match ([]byte (seg [0 ])) || ! branchNameRegex .Match ([]byte (seg [1 ])) {
41+ return fmt .Errorf ("branch name must be combination of number and letter or combine with '/'" )
42+ }
43+ return nil
44+ }
45+
1646type BranchController struct {
1747 fx.In
1848
@@ -60,6 +90,11 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
6090}
6191
6292func (bct BranchController ) CreateBranch (ctx context.Context , w * api.JiaozifsResponse , _ * http.Request , body api.CreateBranchJSONRequestBody , ownerName string , repositoryName string ) {
93+ if err := CheckBranchName (body .Name ); err != nil {
94+ w .BadRequest (err .Error ())
95+ return
96+ }
97+
6398 operator , err := auth .GetOperator (ctx )
6499 if err != nil {
65100 w .Error (err )
@@ -84,18 +119,33 @@ func (bct BranchController) CreateBranch(ctx context.Context, w *api.JiaozifsRes
84119 return
85120 }
86121
87- // Get source ref
88- ref , err := bct .Repo .RefRepo ().Get (ctx , models .NewGetRefParams ().SetName (body .Name ).SetRepositoryID (repository .ID ))
122+ //check exit
123+ _ , err = bct .Repo .RefRepo ().Get (ctx , models .NewGetRefParams ().SetName (body .Name ).SetRepositoryID (repository .ID ))
124+ if err == nil {
125+ w .BadRequest (fmt .Sprintf ("%s already exit" , body .Name ))
126+ return
127+ }
89128 if err != nil && ! errors .Is (err , models .ErrNotFound ) {
90129 w .Error (err )
91130 return
92131 }
132+ //get source ref
133+ sourceRef , err := bct .Repo .RefRepo ().Get (ctx , models .NewGetRefParams ().SetName (body .Source ).SetRepositoryID (repository .ID ))
134+ if err != nil && ! errors .Is (err , models .ErrNotFound ) {
135+ w .Error (err )
136+ return
137+ }
138+
139+ commitHash := hash .EmptyHash
140+ if sourceRef != nil {
141+ commitHash = sourceRef .CommitHash
142+ }
143+
93144 // Create branch
94145 newRef := & models.Ref {
95146 RepositoryID : repository .ID ,
96- CommitHash : ref . CommitHash ,
147+ CommitHash : commitHash ,
97148 Name : body .Name ,
98- Description : ref .Description ,
99149 CreatorID : operator .ID ,
100150 CreatedAt : time .Now (),
101151 UpdatedAt : time .Now (),
@@ -142,6 +192,12 @@ func (bct BranchController) DeleteBranch(ctx context.Context, w *api.JiaozifsRes
142192 return
143193 }
144194
195+ _ , err = bct .Repo .RefRepo ().Get (ctx , models .NewGetRefParams ().SetName (params .RefName ).SetRepositoryID (repository .ID ))
196+ if err != nil {
197+ w .Error (err )
198+ return
199+ }
200+
145201 // Delete branch
146202 err = bct .Repo .RefRepo ().Delete (ctx , models .NewDeleteRefParams ().SetName (params .RefName ).SetRepositoryID (repository .ID ))
147203 if err != nil {
0 commit comments