diff --git a/cmd/docker-compose-webhook/main.go b/cmd/docker-compose-webhook/main.go index 02ab943..5e0435b 100644 --- a/cmd/docker-compose-webhook/main.go +++ b/cmd/docker-compose-webhook/main.go @@ -130,7 +130,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -156,7 +155,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -171,7 +169,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -193,7 +190,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) } @@ -209,7 +205,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -229,7 +224,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -258,7 +252,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -277,7 +270,6 @@ func main() { utils.JSONError(w, errMsg, err.Error(), - event.Repository.FullName, jobID, http.StatusInternalServerError) @@ -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 diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 68672f5..0445d87 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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 }