Skip to content

Commit

Permalink
feat: add delete branch feature
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Jan 20, 2024
1 parent 46f8b36 commit 6b4387c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
39 changes: 38 additions & 1 deletion api/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,42 @@ func (h *Handler) CreateBranch(c *gin.Context) {
return
}

c.JSON(200, branch)
c.JSON(201, branch)
}

type DeleteBranchUri struct {
RepoURI
Branch string `uri:"branch" binding:"required"`
}

// Delete a branch by name
// @Summary Delete a branch by name.
func (h *Handler) DeleteBranch(c *gin.Context) {
var uri DeleteBranchUri
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

hsting, err := getHostingFromContext(c)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

err = hsting.DeleteBranch(
c.Request.Context(),
&hosting.Repository{Owner: uri.Owner, Name: uri.Name},
&hosting.Branch{
Name: uri.Branch,
},
)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{
"error": err.Error(),
})
return
}

c.Status(204)
}
1 change: 1 addition & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Routes(r *gin.Engine, h *Handler) {
gitRepoApi := r.Group("/repos/:hosting/:owner/:name")
gitRepoApi.GET("/branches", h.GetBranches)
gitRepoApi.POST("/branches", h.CreateBranch)
gitRepoApi.DELETE("/branches/:branch", h.DeleteBranch)
gitRepoApi.GET("/commits", h.GetCommits)
gitRepoApi.GET("/files", h.GetFiles)
gitRepoApi.GET("/files/*path", h.GetFiles)
Expand Down
1 change: 1 addition & 0 deletions hosting/hosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Hosting interface {
GetRepository(ctx context.Context, owner string, repo string) (*Repository, error)
GetBranches(ctx context.Context, repo *Repository) ([]Branch, error)
CreateBranch(ctx context.Context, repo *Repository, opts *CreateBranchOpts) (*Branch, error)
DeleteBranch(ctx context.Context, repo *Repository, branch *Branch) error
GetCommits(ctx context.Context, repo *Repository, opts *GetCommitsOpts) ([]Commit, error)
GetFiles(ctx context.Context, repo *Repository, path string) (*File, []File, error)
GetRawFile(ctx context.Context, repo *Repository, path string) ([]byte, error)
Expand Down
15 changes: 13 additions & 2 deletions internal/hosting/github/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func mapBranchRef(r *github.Reference) *hosting.Branch {
return &branch
}

func getBranchRefFromName(name string) string {
return fmt.Sprintf("heads/%v", strings.TrimPrefix(name, "heads/"))
}

func (h *HostingGithub) GetBranches(ctx context.Context, repo *hosting.Repository) ([]hosting.Branch, error) {
githubBranches, _, err := h.client.Repositories.ListBranches(ctx, repo.Owner, repo.Name, &github.BranchListOptions{})
if err != nil {
Expand Down Expand Up @@ -60,7 +64,7 @@ func (h *HostingGithub) CreateBranch(ctx context.Context, repo *hosting.Reposito
return nil, err
}

ref = fmt.Sprintf("heads/%v", githubRepo.GetDefaultBranch())
ref = getBranchRefFromName(githubRepo.GetDefaultBranch())
}

commit, _, err := h.client.Git.GetRef(ctx, repo.Owner, repo.Name, ref)
Expand All @@ -71,7 +75,7 @@ func (h *HostingGithub) CreateBranch(ctx context.Context, repo *hosting.Reposito
githubRef.Object = commit.Object
}

branchRef := fmt.Sprintf("heads/%v", strings.TrimPrefix(*opts.Branch, "heads/"))
branchRef := getBranchRefFromName(*opts.Branch)
githubRef.Ref = &branchRef

githubBranchRef, _, err := h.client.Git.CreateRef(ctx, repo.Owner, repo.Name, &githubRef)
Expand All @@ -81,3 +85,10 @@ func (h *HostingGithub) CreateBranch(ctx context.Context, repo *hosting.Reposito

return mapBranchRef(githubBranchRef), nil
}

func (h *HostingGithub) DeleteBranch(ctx context.Context, repo *hosting.Repository, branch *hosting.Branch) error {
branchRef := getBranchRefFromName(branch.Name)
_, err := h.client.Git.DeleteRef(ctx, repo.Owner, repo.Name, branchRef)

return err
}
6 changes: 6 additions & 0 deletions internal/hosting/gitlab/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ func (h *HostingGitlab) CreateBranch(ctx context.Context, repo *hosting.Reposito

return mapBranch(gitlabBranch), nil
}

func (h *HostingGitlab) DeleteBranch(ctx context.Context, repo *hosting.Repository, branch *hosting.Branch) error {
_, err := h.client.Branches.DeleteBranch(createPid(repo), branch.Name)

return err
}

0 comments on commit 6b4387c

Please sign in to comment.