From a4e361ccd29345d9d7230ee9f75cf4cd192ac25c Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 4 Nov 2022 11:54:51 -0700 Subject: [PATCH] weldr: Add /blueprints/change/NAME/COMMIT route This allows clients to request a specific blueprint change. This can then be used to locally implement diff support. --- internal/weldr/api.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 032f786c2..8091a44e1 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -236,6 +236,7 @@ func setupRouter(api *API) *API { api.router.GET("/api/v:version/blueprints/depsolve/*blueprints", api.blueprintsDepsolveHandler) api.router.GET("/api/v:version/blueprints/freeze/*blueprints", api.blueprintsFreezeHandler) api.router.GET("/api/v:version/blueprints/diff/:blueprint/:from/:to", api.blueprintsDiffHandler) + api.router.GET("/api/v:version/blueprints/change/:blueprint/:commit", api.blueprintsChangeHandler) api.router.GET("/api/v:version/blueprints/changes/*blueprints", api.blueprintsChangesHandler) api.router.POST("/api/v:version/blueprints/new", api.blueprintsNewHandler) api.router.POST("/api/v:version/blueprints/workspace", api.blueprintsWorkspaceHandler) @@ -1805,6 +1806,70 @@ func (api *API) blueprintsDiffHandler(writer http.ResponseWriter, request *http. common.PanicOnError(err) } +// blueprintsChangeHandler returns a specific change to a blueprint +func (api *API) blueprintsChangeHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + if !verifyRequestVersion(writer, params, 1) { + return + } + name := params.ByName("blueprint") + if !verifyStringsWithRegex(writer, []string{name}, ValidBlueprintName) { + return + } + + commit := params.ByName("commit") + if !verifyStringsWithRegex(writer, []string{commit}, ValidBlueprintName) { + return + } + + bpChange, err := api.store.GetBlueprintChange(name, commit) + if err != nil { + errors := responseError{ + ID: "UnknownCommit", + Msg: err.Error(), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } + + bp := bpChange.Blueprint + if len(bpChange.Blueprint.Name) == 0 { + errors := responseError{ + ID: "BlueprintsError", + Msg: fmt.Sprintf("no blueprint found for commit %s", commit), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } + + query, err := url.ParseQuery(request.URL.RawQuery) + if err != nil { + errors := responseError{ + ID: "InvalidChars", + Msg: fmt.Sprintf("invalid query string: %v", err), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } + + format := query.Get("format") + if format == "json" || format == "" { + err := json.NewEncoder(writer).Encode(bp) + common.PanicOnError(err) + } else if format == "toml" { + encoder := toml.NewEncoder(writer) + encoder.Indent = "" + err := encoder.Encode(bp) + common.PanicOnError(err) + } else { + errors := responseError{ + ID: "InvalidChars", + Msg: fmt.Sprintf("invalid format parameter: %s", format), + } + statusResponseError(writer, http.StatusBadRequest, errors) + return + } +} + func (api *API) blueprintsChangesHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { if !verifyRequestVersion(writer, params, 0) { return