Skip to content

Commit e703f69

Browse files
QinYuuuuDev Agent
andauthored
Add CreateBranch method (#623)
Co-authored-by: Dev Agent <dev-agent@example.com>
1 parent ca754fd commit e703f69

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

_mocks/opencsg.com/csghub-server/builder/git/gitserver/mock_GitServer.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/git/gitserver/gitaly/branch.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,49 @@ func (c *Client) DeleteRepoBranch(ctx context.Context, req gitserver.DeleteBranc
130130

131131
return nil
132132
}
133+
134+
func (c *Client) CreateBranch(ctx context.Context, req gitserver.CreateBranchReq) error {
135+
repoType := fmt.Sprintf("%ss", string(req.RepoType))
136+
relativePath, err := c.BuildRelativePath(ctx, req.RepoType, req.Namespace, req.Name)
137+
if err != nil {
138+
return err
139+
}
140+
client, err := c.refClient.UpdateReferences(ctx)
141+
if err != nil {
142+
return err
143+
}
144+
createBranchReq := &gitalypb.UpdateReferencesRequest{
145+
Repository: &gitalypb.Repository{
146+
StorageName: c.config.GitalyServer.Storage,
147+
RelativePath: relativePath,
148+
GlRepository: filepath.Join(repoType, req.Namespace, req.Name),
149+
},
150+
Updates: []*gitalypb.UpdateReferencesRequest_Update{
151+
{
152+
Reference: []byte("refs/heads/" + req.BranchName),
153+
NewObjectId: []byte(req.CommitID),
154+
},
155+
},
156+
}
157+
158+
err = client.Send(createBranchReq)
159+
if err != nil {
160+
return errorx.CreateBranchFailed(err, errorx.Ctx().
161+
Set("repo_type", req.RepoType).
162+
Set("path", relativePath).
163+
Set("branch", req.BranchName).
164+
Set("commit_id", req.CommitID),
165+
)
166+
}
167+
168+
_, err = client.CloseAndRecv()
169+
if err != nil {
170+
return errorx.CreateBranchFailed(err, errorx.Ctx().
171+
Set("repo_type", req.RepoType).
172+
Set("path", relativePath).
173+
Set("branch", req.BranchName).
174+
Set("commit_id", req.CommitID),
175+
)
176+
}
177+
return nil
178+
}

builder/git/gitserver/gitea/branch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ func (c *Client) GetRepoBranchByName(ctx context.Context, req gitserver.GetBranc
6262
func (c *Client) DeleteRepoBranch(ctx context.Context, req gitserver.DeleteBranchReq) error {
6363
return nil
6464
}
65+
66+
func (c *Client) CreateBranch(ctx context.Context, req gitserver.CreateBranchReq) error {
67+
return nil
68+
}

builder/git/gitserver/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type GitServer interface {
2626
CreateRepo(ctx context.Context, req CreateRepoReq) (*CreateRepoResp, error)
2727
UpdateRepo(ctx context.Context, req UpdateRepoReq) (*CreateRepoResp, error)
2828
DeleteRepo(ctx context.Context, relativePath string) error
29+
CreateBranch(ctx context.Context, req CreateBranchReq) error
2930
GetRepoBranches(ctx context.Context, req GetBranchesReq) ([]types.Branch, error)
3031
GetRepoBranchByName(ctx context.Context, req GetBranchReq) (*types.Branch, error)
3132
DeleteRepoBranch(ctx context.Context, req DeleteBranchReq) error

builder/git/gitserver/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ type GetBranchesReq struct {
100100
RepoType types.RepositoryType `json:"repo_type"`
101101
}
102102

103+
type CreateBranchReq struct {
104+
Namespace string `json:"namespace"`
105+
Name string `json:"name"`
106+
BranchName string `json:"branch_name"`
107+
CommitID string `json:"commit_id"`
108+
RepoType types.RepositoryType `json:"repo_type"`
109+
}
110+
103111
type GetBranchReq struct {
104112
Namespace string `json:"namespace"`
105113
Name string `json:"name"`

common/errorx/error_git.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
gitFindBranchFailed
2020
gitBranchNotFound
2121
gitDeleteBranchFailed
22+
gitCreateBranchFailed
2223
gitFileNotFound
2324
gitUploadFailed
2425
gitDownloadFailed
@@ -555,6 +556,15 @@ func DeleteBranchFailed(err error, ctx context) error {
555556
}
556557
}
557558

559+
func CreateBranchFailed(err error, ctx context) error {
560+
return CustomError{
561+
prefix: errGitPrefix,
562+
code: gitCreateBranchFailed,
563+
err: err,
564+
context: ctx,
565+
}
566+
}
567+
558568
func GitFileNotFound(err error, ctx context) error {
559569
return CustomError{
560570
prefix: errGitPrefix,

component/checker/wire_gen.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)