Skip to content

Commit

Permalink
gateway: add api to redelivery a commit status delivery
Browse files Browse the repository at this point in the history
add an api to redelivery a commit status delivery by project ref and commit status delivery id.
  • Loading branch information
alessandro-sorint committed Jan 8, 2024
1 parent 6cd419c commit 504b39f
Show file tree
Hide file tree
Showing 5 changed files with 554 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/services/gateway/action/commitstatusdelivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,29 @@ func (h *ActionHandler) GetProjectCommitStatusDeliveries(ctx context.Context, re

return res, nil
}

type ProjectCommitStatusRedeliveryRequest struct {
ProjectRef string
CommitStatusDeliveryID string
}

func (h *ActionHandler) ProjectCommitStatusRedelivery(ctx context.Context, req *ProjectCommitStatusRedeliveryRequest) error {
project, _, err := h.configstoreClient.GetProject(ctx, req.ProjectRef)
if err != nil {
return util.NewAPIError(util.KindFromRemoteError(err), err)
}
isUserOwner, err := h.IsAuthUserProjectOwner(ctx, project.OwnerType, project.OwnerID)
if err != nil {
return errors.Wrapf(err, "failed to determine permissions")
}
if !isUserOwner {
return util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized"))
}

_, err = h.notificationClient.CommitStatusRedelivery(ctx, project.ID, req.CommitStatusDeliveryID)
if err != nil {
return util.NewAPIError(util.KindFromRemoteError(err), err)
}

return nil
}
40 changes: 40 additions & 0 deletions internal/services/gateway/api/commitstatusdelivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,43 @@ func (h *ProjectCommitStatusDeliveries) do(w http.ResponseWriter, r *http.Reques

return commitStatusDeliveries, nil
}

type ProjectCommitStatusRedelivery struct {
log zerolog.Logger
ah *action.ActionHandler
}

func NewProjectCommitStatusRedeliveryHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectCommitStatusRedelivery {
return &ProjectCommitStatusRedelivery{log: log, ah: ah}
}

func (h *ProjectCommitStatusRedelivery) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h.do(w, r)
if util.HTTPError(w, err) {
h.log.Err(err).Send()
return
}

if err := util.HTTPResponse(w, http.StatusOK, nil); err != nil {
h.log.Err(err).Send()
}
}

func (h *ProjectCommitStatusRedelivery) do(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()

vars := mux.Vars(r)
projectRef := vars["projectref"]
commitStatusDeliveryID := vars["commitstatusdeliveryid"]

areq := &action.ProjectCommitStatusRedeliveryRequest{
ProjectRef: projectRef,
CommitStatusDeliveryID: commitStatusDeliveryID,
}
err := h.ah.ProjectCommitStatusRedelivery(ctx, areq)
if err != nil {
return errors.WithStack(err)
}

return nil
}
2 changes: 2 additions & 0 deletions internal/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (g *Gateway) Run(ctx context.Context) error {
projectRunWebhookDeliveriesHandler := api.NewProjectRunWebhookDeliveriesHandler(g.log, g.ah)
projectRunWebhookRedeliveryHandler := api.NewProjectRunWebhookRedeliveryHandler(g.log, g.ah)
projectCommitStatusDeliveriesHandler := api.NewProjectCommitStatusDeliveriesHandler(g.log, g.ah)
projectCommitStatusRedeliveryHandler := api.NewProjectCommitStatusRedeliveryHandler(g.log, g.ah)

secretHandler := api.NewSecretHandler(g.log, g.ah)
createSecretHandler := api.NewCreateSecretHandler(g.log, g.ah)
Expand Down Expand Up @@ -324,6 +325,7 @@ func (g *Gateway) Run(ctx context.Context) error {
apirouter.Handle("/projects/{projectref}/runwebhookdeliveries", authForcedHandler(projectRunWebhookDeliveriesHandler)).Methods("GET")
apirouter.Handle("/projects/{projectref}/runwebhookdeliveries/{runwebhookdeliveryid}/redelivery", authForcedHandler(projectRunWebhookRedeliveryHandler)).Methods("PUT")
apirouter.Handle("/projects/{projectref}/commitstatusdeliveries", authForcedHandler(projectCommitStatusDeliveriesHandler)).Methods("GET")
apirouter.Handle("/projects/{projectref}/commitstatusdeliveries/{commitstatusdeliveryid}/redelivery", authForcedHandler(projectCommitStatusRedeliveryHandler)).Methods("PUT")

apirouter.Handle("/projectgroups/{projectgroupref}/secrets", authForcedHandler(secretHandler)).Methods("GET")
apirouter.Handle("/projects/{projectref}/secrets", authForcedHandler(secretHandler)).Methods("GET")
Expand Down
4 changes: 4 additions & 0 deletions services/gateway/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,7 @@ func (c *Client) GetProjectCommitStatusDeliveries(ctx context.Context, projectRe
resp, err := c.getParsedResponse(ctx, "GET", fmt.Sprintf("/projects/%s/commitstatusdeliveries", url.PathEscape(projectRef)), q, common.JSONContent, nil, &commitStatusDeliveries)
return commitStatusDeliveries, resp, errors.WithStack(err)
}

func (c *Client) ProjectCommitStatusRedelivery(ctx context.Context, projectRef string, commitStatusDeliveryID string) (*Response, error) {
return c.getResponse(ctx, "PUT", fmt.Sprintf("/projects/%s/commitstatusdeliveries/%s/redelivery", projectRef, commitStatusDeliveryID), nil, jsonContent, nil)
}
Loading

0 comments on commit 504b39f

Please sign in to comment.