From a13b4ee17b642d14a1f2b4414c91b1bc8cd1b569 Mon Sep 17 00:00:00 2001 From: Gamunu Balagalla Date: Thu, 18 Jul 2024 00:35:31 +0530 Subject: [PATCH] fix: remove telementry hooks (#14) Signed-off-by: Gamunu Balagalla --- .github/workflows/add-copyright-headers.yml | 41 ---- .github/workflows/build-pr.yml | 59 ----- .github/workflows/build.yml | 1 + .github/workflows/check-changelog.yml | 36 ---- go.sum | 2 - internal/features/modules/hooks/hooks.go | 2 - .../modules/hooks/module_source_registry.go | 50 +---- .../hooks/module_source_registry_test.go | 201 +----------------- internal/features/modules/modules_feature.go | 13 -- .../rootmodules/root_modules_feature.go | 39 ---- .../langserver/handlers/completion_hooks.go | 21 -- internal/langserver/handlers/hooks_module.go | 1 - 12 files changed, 4 insertions(+), 462 deletions(-) delete mode 100644 .github/workflows/add-copyright-headers.yml delete mode 100644 .github/workflows/build-pr.yml delete mode 100644 .github/workflows/check-changelog.yml delete mode 100644 internal/langserver/handlers/completion_hooks.go diff --git a/.github/workflows/add-copyright-headers.yml b/.github/workflows/add-copyright-headers.yml deleted file mode 100644 index 536ecb6f2..000000000 --- a/.github/workflows/add-copyright-headers.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: "Add Copyright Headers" - -on: - pull_request_target: - types: - - opened - - reopened - - synchronize - - ready_for_review - workflow_dispatch: {} - -jobs: - add-copyright-headers: - runs-on: ubuntu-latest - env: - HEAD_REF: ${{ github.event.pull_request.head.ref }} - permissions: - contents: write - steps: - - name: Checkout - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - with: - ref: ${{ github.event.pull_request.head.ref }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - - name: Set git identity - run: |- - git config user.name "hashicorp-copywrite[bot]" - git config user.email "110428419+hashicorp-copywrite[bot]@users.noreply.github.com" - - name: Setup Copywrite tool - uses: hashicorp/setup-copywrite@32638da2d4e81d56a0764aa1547882fc4d209636 # v1.1.3 - - name: Add headers using Copywrite tool - run: copywrite headers - - name: Check if there are any changes - id: get_changes - run: echo "changed=$(git status --porcelain | wc -l)" >> $GITHUB_OUTPUT - - name: Push changes - if: steps.get_changes.outputs.changed != 0 - run: |- - git add . - git commit -s -m "[COMPLIANCE] Add required copyright headers" - git push origin HEAD:$HEAD_REF diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml deleted file mode 100644 index 4ee7c4513..000000000 --- a/.github/workflows/build-pr.yml +++ /dev/null @@ -1,59 +0,0 @@ -name: build-pr - -on: - pull_request: - branches-ignore: - - pre-release - -env: - GOPROXY: https://proxy.golang.org/ - -jobs: - generate-provider-schemas: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Set up Go - uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 - with: - go-version-file: ".go-version" - - name: Generate provider schemas - run: | - go generate ./internal/schemas - du -h -s ./internal/schemas/data - - build: - runs-on: ubuntu-latest - strategy: - matrix: - include: - - { goos: "linux", goarch: "386" } - - { goos: "linux", goarch: "amd64" } - - { goos: "linux", goarch: "arm" } - - { goos: "linux", goarch: "arm64" } - - { goos: "freebsd", goarch: "386" } - - { goos: "freebsd", goarch: "amd64" } - - { goos: "freebsd", goarch: "arm" } - - { goos: "openbsd", goarch: "386" } - - { goos: "openbsd", goarch: "amd64" } - - { goos: "solaris", goarch: "amd64" } - - { goos: "windows", goarch: "386" } - - { goos: "windows", goarch: "amd64" } - - { goos: "windows", goarch: "arm64" } - - { goos: "darwin", goarch: "arm64" } - - { goos: "darwin", goarch: "amd64" } - timeout-minutes: 25 - steps: - - name: Checkout - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Set up Go - uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 - with: - go-version-file: ".go-version" - - name: ${{ matrix.goos }} ${{ matrix.goarch }} build - run: |- - go build \ - -trimpath \ - -buildvcs=false \ - -ldflags "-s -w" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cff58d91e..085590f3e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,6 +28,7 @@ jobs: build: runs-on: ubuntu-latest + needs: [generate-provider-schemas] strategy: matrix: include: diff --git a/.github/workflows/check-changelog.yml b/.github/workflows/check-changelog.yml deleted file mode 100644 index a108af916..000000000 --- a/.github/workflows/check-changelog.yml +++ /dev/null @@ -1,36 +0,0 @@ -# Checks if a file has been committed under the .changes/unreleased directory -# -# Skip PRs labeled with 'dependencies' - -on: - pull_request: - branches: - - main - - pre-release - -name: Check if changelog entry exists - -jobs: - changelog_existence: - name: Check if changelog entry exists - if: "!contains(github.event.pull_request.labels.*.name, 'dependencies')" - runs-on: ubuntu-latest - - steps: - - name: Check if changelog file was added - # https://github.com/marketplace/actions/paths-changes-filter - # For each filter, it sets output variable named by the filter to the text: - # 'true' - if any of changed files matches any of filter rules - # 'false' - if none of changed files matches any of filter rules - uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2 - id: changelog_check - with: - filters: | - exists: - - '.changes/unreleased/**.yaml' - - - name: Fail job if changelog entry is missing and required - if: steps.changelog_check.outputs.exists == 'false' - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 - with: - script: core.setFailed('Changelog entry required to merge.') diff --git a/go.sum b/go.sum index 594b08d43..7826554b0 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,6 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chigopher/pathlib v0.19.1 h1:RoLlUJc0CqBGwq239cilyhxPNLXTK+HXoASGyGznx5A= github.com/chigopher/pathlib v0.19.1/go.mod h1:tzC1dZLW8o33UQpWkNkhvPwL5n4yyFRFm/jL1YGWFvY= -github.com/chigopher/pathlib v1.0.0 h1:SbsCrFX4vDf4M2d8mT/RTzuVlKOjTKoPHK0HidsQFak= -github.com/chigopher/pathlib v1.0.0/go.mod h1:3+YPPV21mU9vyw8Mjp+F33CyCfE6iOzinpiqBcccv7I= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= diff --git a/internal/features/modules/hooks/hooks.go b/internal/features/modules/hooks/hooks.go index 6584d2449..42a6e2cf2 100644 --- a/internal/features/modules/hooks/hooks.go +++ b/internal/features/modules/hooks/hooks.go @@ -9,7 +9,6 @@ package hooks import ( "log" - "github.com/algolia/algoliasearch-client-go/v3/algolia/search" "github.com/hashicorp/terraform-ls/internal/features/modules/state" "github.com/hashicorp/terraform-ls/internal/registry" ) @@ -17,6 +16,5 @@ import ( type Hooks struct { ModStore *state.ModuleStore RegistryClient registry.Client - AlgoliaClient *search.Client Logger *log.Logger } diff --git a/internal/features/modules/hooks/module_source_registry.go b/internal/features/modules/hooks/module_source_registry.go index bd39b9aee..c1fb049d1 100644 --- a/internal/features/modules/hooks/module_source_registry.go +++ b/internal/features/modules/hooks/module_source_registry.go @@ -5,12 +5,9 @@ package hooks import ( "context" - "strings" - - "github.com/algolia/algoliasearch-client-go/v3/algolia/opt" "github.com/hashicorp/hcl-lang/decoder" - "github.com/hashicorp/hcl-lang/lang" "github.com/zclconf/go-cty/cty" + "strings" ) type RegistryModule struct { @@ -18,32 +15,6 @@ type RegistryModule struct { Description string `json:"description"` } -const algoliaModuleIndex = "tf-registry:prod:modules" - -func (h *Hooks) fetchModulesFromAlgolia(ctx context.Context, term string) ([]RegistryModule, error) { - modules := make([]RegistryModule, 0) - - index := h.AlgoliaClient.InitIndex(algoliaModuleIndex) - params := []interface{}{ - ctx, // transport.Request will magically extract the context from here - opt.AttributesToRetrieve("full-name", "description"), - opt.HitsPerPage(10), - } - - res, err := index.Search(term, params...) - if err != nil { - return modules, err - } - - err = res.UnmarshalHits(&modules) - if err != nil { - return modules, err - - } - - return modules, nil -} - func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]decoder.Candidate, error) { candidates := make([]decoder.Candidate, 0) prefix := value.AsString() @@ -54,24 +25,7 @@ func (h *Hooks) RegistryModuleSources(ctx context.Context, value cty.Value) ([]d return candidates, nil } - if h.AlgoliaClient == nil { - return candidates, nil - } - - modules, err := h.fetchModulesFromAlgolia(ctx, prefix) - if err != nil { - h.Logger.Printf("Error fetching modules from Algolia: %#v", err) - return candidates, err - } - - for _, mod := range modules { - c := decoder.ExpressionCompletionCandidate(decoder.ExpressionCandidate{ - Value: cty.StringVal(mod.FullName), - Detail: "registry", - Description: lang.PlainText(mod.Description), - }) - candidates = append(candidates, c) - } + //TODO: figure out how we can do module autocompletion return candidates, nil } diff --git a/internal/features/modules/hooks/module_source_registry_test.go b/internal/features/modules/hooks/module_source_registry_test.go index d5e5bf4db..7aa8c545c 100644 --- a/internal/features/modules/hooks/module_source_registry_test.go +++ b/internal/features/modules/hooks/module_source_registry_test.go @@ -4,26 +4,13 @@ package hooks import ( - "context" "crypto/tls" - "fmt" - "io" - "log" - "net" "net/http" "net/http/httptest" "net/url" - "strings" "testing" - "time" "github.com/algolia/algoliasearch-client-go/v3/algolia/search" - "github.com/google/go-cmp/cmp" - "github.com/hashicorp/hcl-lang/decoder" - "github.com/hashicorp/hcl-lang/lang" - "github.com/hashicorp/terraform-ls/internal/features/modules/state" - globalState "github.com/hashicorp/terraform-ls/internal/state" - "github.com/zclconf/go-cty/cty" ) const responseAWS = `{ @@ -79,193 +66,7 @@ func (r *testRequester) Request(req *http.Request) (*http.Response, error) { return r.client.Do(req) } -func TestHooks_RegistryModuleSources(t *testing.T) { - ctx := context.Background() - - s, err := globalState.NewStateStore() - if err != nil { - t.Fatal(err) - } - store, err := state.NewModuleStore(s.ProviderSchemas, s.RegistryModules, s.ChangeStore) - if err != nil { - t.Fatal(err) - } - - searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.RequestURI == "/1/indexes/tf-registry%3Aprod%3Amodules/query" { - b, _ := io.ReadAll(r.Body) - - if strings.Contains(string(b), "query=aws") { - w.Write([]byte(responseAWS)) - return - } else if strings.Contains(string(b), "query=err") { - http.Error(w, responseErr, http.StatusForbidden) - return - } - - w.Write([]byte(responseEmpty)) - return - } - http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400) - })) - - h := &Hooks{ - ModStore: store, - AlgoliaClient: searchClient, - Logger: log.New(io.Discard, "", 0), - } - - tests := []struct { - name string - value cty.Value - want []decoder.Candidate - wantErr bool - }{ - { - "simple search", - cty.StringVal("aws"), - []decoder.Candidate{ - { - Label: `"terraform-aws-modules/vpc/aws"`, - Detail: "registry", - Kind: lang.StringCandidateKind, - Description: lang.PlainText("Terraform module which creates VPC resources on AWS"), - RawInsertText: `"terraform-aws-modules/vpc/aws"`, - }, - { - Label: `"terraform-aws-modules/eks/aws"`, - Detail: "registry", - Kind: lang.StringCandidateKind, - Description: lang.PlainText("Terraform module to create an Elastic Kubernetes (EKS) cluster and associated resources"), - RawInsertText: `"terraform-aws-modules/eks/aws"`, - }, - }, - false, - }, - { - "empty result", - cty.StringVal("foo"), - []decoder.Candidate{}, - false, - }, - { - "auth error", - cty.StringVal("err"), - []decoder.Candidate{}, - true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - candidates, err := h.RegistryModuleSources(ctx, tt.value) - - if (err != nil) != tt.wantErr { - t.Errorf("Hooks.RegistryModuleSources() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if diff := cmp.Diff(tt.want, candidates); diff != "" { - t.Fatalf("mismatched candidates: %s", diff) - } - }) - } -} - -func TestHooks_RegistryModuleSourcesCtxCancel(t *testing.T) { - ctx := context.Background() - ctx, cancelFunc := context.WithTimeout(ctx, 50*time.Millisecond) - t.Cleanup(cancelFunc) - - s, err := globalState.NewStateStore() - if err != nil { - t.Fatal(err) - } - store, err := state.NewModuleStore(s.ProviderSchemas, s.RegistryModules, s.ChangeStore) - if err != nil { - t.Fatal(err) - } - - searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - time.Sleep(500 * time.Millisecond) - http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400) - })) - - h := &Hooks{ - ModStore: store, - AlgoliaClient: searchClient, - Logger: log.New(io.Discard, "", 0), - } - - _, err = h.RegistryModuleSources(ctx, cty.StringVal("aws")) - e, ok := err.(net.Error) - if !ok { - t.Fatalf("expected error, got %#v", err) - } - - if !strings.Contains(e.Error(), "context deadline exceeded") { - t.Fatalf("expected error with: %q, given: %q", "context deadline exceeded", e.Error()) - } -} - -func TestHooks_RegistryModuleSourcesIgnore(t *testing.T) { - ctx := context.Background() - - s, err := globalState.NewStateStore() - if err != nil { - t.Fatal(err) - } - store, err := state.NewModuleStore(s.ProviderSchemas, s.RegistryModules, s.ChangeStore) - if err != nil { - t.Fatal(err) - } - - searchClient := buildSearchClientMock(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - http.Error(w, fmt.Sprintf("unexpected request: %q", r.RequestURI), 400) - })) - - h := &Hooks{ - ModStore: store, - AlgoliaClient: searchClient, - Logger: log.New(io.Discard, "", 0), - } - - tests := []struct { - name string - value cty.Value - want []decoder.Candidate - }{ - { - "search dot", - cty.StringVal("."), - []decoder.Candidate{}, - }, - { - "search dot dot", - cty.StringVal(".."), - []decoder.Candidate{}, - }, - { - "local module", - cty.StringVal("../aws"), - []decoder.Candidate{}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - candidates, err := h.RegistryModuleSources(ctx, tt.value) - - if err != nil { - t.Fatal(err) - } - - if diff := cmp.Diff(tt.want, candidates); diff != "" { - t.Fatalf("mismatched candidates: %s", diff) - } - }) - } -} +// TODO: implement test cases for module search func buildSearchClientMock(t *testing.T, handler http.HandlerFunc) *search.Client { searchServer := httptest.NewTLSServer(handler) diff --git a/internal/features/modules/modules_feature.go b/internal/features/modules/modules_feature.go index 72e7ec5c2..16aec4803 100644 --- a/internal/features/modules/modules_feature.go +++ b/internal/features/modules/modules_feature.go @@ -9,11 +9,9 @@ import ( "io" "log" - "github.com/algolia/algoliasearch-client-go/v3/algolia/search" "github.com/hashicorp/go-version" "github.com/hashicorp/hcl-lang/decoder" "github.com/hashicorp/hcl-lang/lang" - "github.com/hashicorp/terraform-ls/internal/algolia" "github.com/hashicorp/terraform-ls/internal/document" "github.com/hashicorp/terraform-ls/internal/eventbus" fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder" @@ -23,7 +21,6 @@ import ( "github.com/hashicorp/terraform-ls/internal/langserver/diagnostics" "github.com/hashicorp/terraform-ls/internal/registry" globalState "github.com/hashicorp/terraform-ls/internal/state" - "github.com/hashicorp/terraform-ls/internal/telemetry" "github.com/hashicorp/terraform-schema/backend" tfmod "github.com/hashicorp/terraform-schema/module" ) @@ -170,11 +167,6 @@ func (f *ModulesFeature) AppendCompletionHooks(srvCtx context.Context, decoderCo Logger: f.logger, } - credentials, ok := algolia.CredentialsFromContext(srvCtx) - if ok { - h.AlgoliaClient = search.NewClient(credentials.AppID, credentials.APIKey) - } - decoderContext.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources decoderContext.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources decoderContext.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions @@ -247,11 +239,6 @@ func (f *ModulesFeature) Telemetry(path string) map[string]interface{} { if len(mod.Meta.ProviderRequirements) > 0 { reqs := make(map[string]string, 0) for pAddr, cons := range mod.Meta.ProviderRequirements { - if telemetry.IsPublicProvider(pAddr) { - reqs[pAddr.String()] = cons.String() - continue - } - // anonymize any unknown providers or the ones not publicly listed id, err := f.stateStore.ProviderSchemas.GetProviderID(pAddr) if err != nil { diff --git a/internal/features/rootmodules/root_modules_feature.go b/internal/features/rootmodules/root_modules_feature.go index c51bcc0a1..ae02ca452 100644 --- a/internal/features/rootmodules/root_modules_feature.go +++ b/internal/features/rootmodules/root_modules_feature.go @@ -5,7 +5,6 @@ package rootmodules import ( "context" - "fmt" "io" "log" @@ -14,7 +13,6 @@ import ( "github.com/hashicorp/terraform-ls/internal/features/rootmodules/jobs" "github.com/hashicorp/terraform-ls/internal/features/rootmodules/state" globalState "github.com/hashicorp/terraform-ls/internal/state" - "github.com/hashicorp/terraform-ls/internal/telemetry" "github.com/hashicorp/terraform-ls/internal/terraform/exec" tfaddr "github.com/hashicorp/terraform-registry-address" tfmod "github.com/hashicorp/terraform-schema/module" @@ -152,43 +150,6 @@ func (f *RootModulesFeature) CallersOfModule(modPath string) ([]string, error) { return f.Store.CallersOfModule(modPath) } -func (f *RootModulesFeature) Telemetry(path string) map[string]interface{} { - properties := make(map[string]interface{}) - - record, err := f.Store.RootRecordByPath(path) - if err != nil { - return properties - } - - if record.TerraformVersion != nil { - properties["tfVersion"] = record.TerraformVersion.String() - } - if len(record.InstalledProviders) > 0 { - installedProviders := make(map[string]string, 0) - for pAddr, pv := range record.InstalledProviders { - if telemetry.IsPublicProvider(pAddr) { - versionString := "" - if pv != nil { - versionString = pv.String() - } - installedProviders[pAddr.String()] = versionString - continue - } - - // anonymize any unknown providers or the ones not publicly listed - id, err := f.stateStore.ProviderSchemas.GetProviderID(pAddr) - if err != nil { - continue - } - addr := fmt.Sprintf("unlisted/%s", id) - installedProviders[addr] = "" - } - properties["installedProviders"] = installedProviders - } - - return properties -} - // InstalledModulePath checks the installed modules in the given root module // for the given normalized source address. // diff --git a/internal/langserver/handlers/completion_hooks.go b/internal/langserver/handlers/completion_hooks.go deleted file mode 100644 index 26f7b9e55..000000000 --- a/internal/langserver/handlers/completion_hooks.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package handlers - -import ( - "github.com/hashicorp/hcl-lang/decoder" - "github.com/hashicorp/terraform-ls/internal/hooks" -) - -func (s *service) AppendCompletionHooks(decoderContext decoder.DecoderContext) { - h := hooks.Hooks{ - ModStore: s.modStore, - RegistryClient: s.registryClient, - Logger: s.logger, - } - - decoderContext.CompletionHooks["CompleteLocalModuleSources"] = h.LocalModuleSources - decoderContext.CompletionHooks["CompleteRegistryModuleSources"] = h.RegistryModuleSources - decoderContext.CompletionHooks["CompleteRegistryModuleVersions"] = h.RegistryModuleVersions -} diff --git a/internal/langserver/handlers/hooks_module.go b/internal/langserver/handlers/hooks_module.go index e4dc55bb2..00958f8a0 100644 --- a/internal/langserver/handlers/hooks_module.go +++ b/internal/langserver/handlers/hooks_module.go @@ -12,7 +12,6 @@ import ( "github.com/hashicorp/terraform-ls/internal/state" ) - func callRefreshClientCommand(clientRequester session.ClientCaller, commandId string) notifier.Hook { return func(ctx context.Context, changes state.Changes) error { // TODO: avoid triggering if module calls/providers did not change