Skip to content

Commit

Permalink
feat: file content crud
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Jan 20, 2024
1 parent 6b4387c commit d2baf11
Show file tree
Hide file tree
Showing 19 changed files with 617 additions and 176 deletions.
58 changes: 26 additions & 32 deletions api/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ import (

// Get branches list
// @Summary Get branches list.
func (h *Handler) GetBranches(c *gin.Context) {
func (h *Handler) GetBranches(ctx *gin.Context) {
var uri RepoURI
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.ShouldBindUri(&uri); err != nil {
respondWithError(ctx, http.StatusBadRequest, err)
return
}

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

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

c.JSON(200, branches)
ctx.JSON(http.StatusOK, branches)
}

type CreateBranchForm struct {
Expand All @@ -39,40 +37,38 @@ type CreateBranchForm struct {

// Create a branch with name
// @Summary Create a branch with name.
func (h *Handler) CreateBranch(c *gin.Context) {
func (h *Handler) CreateBranch(ctx *gin.Context) {
var uri RepoURI
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.ShouldBindUri(&uri); err != nil {
respondWithError(ctx, http.StatusBadRequest, err)
return
}

var form CreateBranchForm
if err := c.ShouldBind(&form); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.ShouldBind(&form); err != nil {
respondWithError(ctx, http.StatusBadRequest, err)
return
}

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

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

c.JSON(201, branch)
ctx.JSON(http.StatusCreated, branch)
}

type DeleteBranchUri struct {
Expand All @@ -82,32 +78,30 @@ type DeleteBranchUri struct {

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

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

err = hsting.DeleteBranch(
c.Request.Context(),
ctx.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(),
})
respondWithError(ctx, http.StatusBadGateway, err)
return
}

c.Status(204)
ctx.Status(http.StatusNoContent)
}
22 changes: 10 additions & 12 deletions api/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@ import (

// Get commits list
// @Summary Get commits list.
func (h *Handler) GetCommits(c *gin.Context) {
func (h *Handler) GetCommits(ctx *gin.Context) {
var uri RepoURI
if err := c.ShouldBindUri(&uri); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.ShouldBindUri(&uri); err != nil {
respondWithError(ctx, http.StatusBadRequest, err)
return
}

var form RefForm
if err := c.ShouldBind(&form); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.ShouldBind(&form); err != nil {
respondWithError(ctx, http.StatusBadRequest, err)
}

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

commits, err := hsting.GetCommits(
c.Request.Context(),
ctx.Request.Context(),
&hosting.Repository{Owner: uri.Owner, Name: uri.Name},
&hosting.GetCommitsOpts{Ref: form.Ref},
)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadGateway, gin.H{
"error": err.Error(),
})
respondWithError(ctx, http.StatusBadGateway, err)
return
}

c.JSON(200, commits)
ctx.JSON(http.StatusOK, commits)
}
Loading

0 comments on commit d2baf11

Please sign in to comment.