Skip to content

Commit

Permalink
fix: adjust http response (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimdre authored Jul 31, 2024
1 parent 2df43bf commit a4e651c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
16 changes: 3 additions & 13 deletions cmd/docker-compose-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -156,7 +155,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -171,7 +169,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -193,7 +190,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)
}
Expand All @@ -209,7 +205,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -229,7 +224,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand Down Expand Up @@ -258,7 +252,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -277,7 +270,6 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

Expand All @@ -295,17 +287,15 @@ func main() {
utils.JSONError(w,
errMsg,
err.Error(),
event.Repository.FullName,
jobID,
http.StatusInternalServerError)

return
}

jobLog.Info("project deployment successful")

// Respond with a 204 No Content status
w.WriteHeader(http.StatusNoContent)
msg := "project deployment successful"
jobLog.Info(msg)
utils.JSONResponse(w, msg, jobID, http.StatusCreated)

case gitlab.PushEventPayload:
// TODO: Implement GitLab webhook handling
Expand Down
48 changes: 35 additions & 13 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,55 @@ import (
"net/http"
)

type jsonResponse struct {
Code int `json:"code"`
JobID string `json:"job_id,omitempty"`
Details string `json:"details,omitempty"`
}

// jsonError inherits from jsonResponse and adds an error message
type jsonError struct {
Code int `json:"code"`
Error string `json:"error"`
Details string `json:"details,omitempty"`
Repository string `json:"repository,omitempty"`
JobID string `json:"job_id,omitempty"`
jsonResponse
Error string `json:"error"`
}

// JSONError writes an error response to the client in JSON format
func JSONError(w http.ResponseWriter, err interface{}, details, repo, jobId string, code int) {
func JSONError(w http.ResponseWriter, err interface{}, details, jobId string, code int) {
if _, ok := err.(error); ok {
err = fmt.Sprintf("%v", err)
}

err = jsonError{
Error: err.(string),
Code: code,
Details: details,
Repository: repo,
JobID: jobId,
resp := jsonError{
Error: err.(string),
jsonResponse: jsonResponse{
Code: code,
JobID: jobId,
Details: details,
},
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)

err = json.NewEncoder(w).Encode(resp)
if err != nil {
return
}
}

func JSONResponse(w http.ResponseWriter, details, jobId string, code int) {
resp := jsonResponse{
Code: code,
JobID: jobId,
Details: details,
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)

err = json.NewEncoder(w).Encode(err)
err := json.NewEncoder(w).Encode(resp)
if err != nil {
return
}
Expand Down

0 comments on commit a4e651c

Please sign in to comment.