From 7d09d97adf5811177bf90da5dfc2e6d3f77dbaa8 Mon Sep 17 00:00:00 2001 From: JulesFaucherre Date: Thu, 20 Apr 2023 17:31:46 +0200 Subject: [PATCH] style: Added a log when going the legacy path --- config/config.go | 1 + config/legacy_test.go | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 61b770c65..abc451b87 100644 --- a/config/config.go +++ b/config/config.go @@ -111,6 +111,7 @@ func (c *ConfigCompiler) ConfigQuery( configCompilationResp := &ConfigResponse{} statusCode, originalErr := c.compileRestClient.DoRequest(req, configCompilationResp) if statusCode == 404 { + fmt.Fprintf(os.Stderr, "You are using a old version of CircleCI Server, please consider updating") legacyResponse, err := c.legacyConfigQueryByOrgID(configString, orgID, params, values, c.cfg) if err != nil { return nil, err diff --git a/config/legacy_test.go b/config/legacy_test.go index 516232273..5175a136f 100644 --- a/config/legacy_test.go +++ b/config/legacy_test.go @@ -71,6 +71,41 @@ func TestLegacyFlow(t *testing.T) { }) _, err := compiler.ConfigQuery("testdata/config.yml", "1234", Parameters{}, Values{}) assert.Error(t, err) - assert.Contains(t, "failed to validate", err.Error()) + assert.Contains(t, err.Error(), "failed to validate") + }) + + t.Run("tests that the compiler fails out completely when a non-404 is returned from the http endpoint", func(t *testing.T) { + mux := http.NewServeMux() + gqlHitCounter := 0 + + mux.HandleFunc("/compile-config-with-defaults", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + + }) + + mux.HandleFunc("/me/collaborations", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `[{"vcs_type":"circleci","slug":"gh/test","id":"2345"}]`) + }) + + mux.HandleFunc("/graphql-unstable", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `{"data":{"buildConfig":{"errors":[{"message": "failed to validate"}]}}}`) + gqlHitCounter++ + }) + + svr := httptest.NewServer(mux) + defer svr.Close() + + compiler := New(&settings.Config{ + Host: svr.URL, + Endpoint: "/graphql-unstable", + HTTPClient: http.DefaultClient, + Token: "", + }) + _, err := compiler.ConfigQuery("testdata/config.yml", "1234", Parameters{}, Values{}) + assert.Error(t, err) + assert.Contains(t, err.Error(), "config compilation request returned an error:") + assert.Equal(t, 0, gqlHitCounter) }) }