From 494d2460a04cf0b8c4c438f283e67a1b147c10ba Mon Sep 17 00:00:00 2001 From: Prashant Shahi Date: Tue, 11 Mar 2025 17:24:19 +0530 Subject: [PATCH] ci: build/test/lint GH workflows for Go (#7266) build/test/lint GH workflows for Go --- .github/workflows/build.yaml | 4 --- .github/workflows/ci.yaml | 36 +++++++++++++++++++ .github/workflows/postci.yaml | 18 ++++++++++ ee/query-service/app/api/cloudIntegrations.go | 4 +-- .../integrations/signozio/signozio.go | 4 +-- ee/query-service/model/license_test.go | 8 ++--- pkg/types/alertmanagertypes/config_test.go | 19 +++++----- 7 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/postci.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 275f0e9599..27da9b415e 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -65,10 +65,6 @@ jobs: uses: actions/setup-go@v4 with: go-version: "1.21" - - name: Run tests - shell: bash - run: | - make test - name: Build query-service image shell: bash run: | diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000000..e347425698 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,36 @@ +name: ci + +on: + pull_request: + branches: + - main + pull_request_target: + types: + - labeled + +jobs: + test: + if: | + (github.event_name == 'pull_request' && ! github.event.pull_request.head.repo.fork && github.event.pull_request.user.login != 'dependabot[bot]' && ! contains(github.event.pull_request.labels.*.name, 'safe-to-test')) || + (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'safe-to-test')) + uses: signoz/primus.workflows/.github/workflows/go-test.yaml@main + secrets: inherit + with: + PRIMUS_REF: main + GO_TEST_CONTEXT: ./... + fmt: + if: | + (github.event_name == 'pull_request' && ! github.event.pull_request.head.repo.fork && github.event.pull_request.user.login != 'dependabot[bot]' && ! contains(github.event.pull_request.labels.*.name, 'safe-to-test')) || + (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'safe-to-test')) + uses: signoz/primus.workflows/.github/workflows/go-fmt.yaml@main + secrets: inherit + with: + PRIMUS_REF: main + lint: + if: | + (github.event_name == 'pull_request' && ! github.event.pull_request.head.repo.fork && github.event.pull_request.user.login != 'dependabot[bot]' && ! contains(github.event.pull_request.labels.*.name, 'safe-to-test')) || + (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'safe-to-test')) + uses: signoz/primus.workflows/.github/workflows/go-lint.yaml@main + secrets: inherit + with: + PRIMUS_REF: main diff --git a/.github/workflows/postci.yaml b/.github/workflows/postci.yaml new file mode 100644 index 0000000000..118db89e1d --- /dev/null +++ b/.github/workflows/postci.yaml @@ -0,0 +1,18 @@ +name: postci + +on: + pull_request_target: + branches: + - main + types: + - synchronize + +jobs: + remove: + if: github.event.pull_request.head.repo.fork || github.event.pull_request.user.login == 'dependabot[bot]' + uses: signoz/primus.workflows/.github/workflows/github-label.yaml@main + secrets: inherit + with: + PRIMUS_REF: main + GITHUB_LABEL_ACTION: remove + GITHUB_LABEL_NAME: safe-to-test diff --git a/ee/query-service/app/api/cloudIntegrations.go b/ee/query-service/app/api/cloudIntegrations.go index 8b2e38bb12..2739f48350 100644 --- a/ee/query-service/app/api/cloudIntegrations.go +++ b/ee/query-service/app/api/cloudIntegrations.go @@ -121,7 +121,7 @@ func (ah *APIHandler) getOrCreateCloudIntegrationPAT(ctx context.Context, orgId allPats, err := ah.AppDao().ListPATs(ctx) if err != nil { return "", basemodel.InternalError(fmt.Errorf( - "couldn't list PATs: %w", err.Error(), + "couldn't list PATs: %w", err, )) } for _, p := range allPats { @@ -147,7 +147,7 @@ func (ah *APIHandler) getOrCreateCloudIntegrationPAT(ctx context.Context, orgId integrationPAT, err := ah.AppDao().CreatePAT(ctx, newPAT) if err != nil { return "", basemodel.InternalError(fmt.Errorf( - "couldn't create cloud integration PAT: %w", err.Error(), + "couldn't create cloud integration PAT: %w", err, )) } return integrationPAT.Token, nil diff --git a/ee/query-service/integrations/signozio/signozio.go b/ee/query-service/integrations/signozio/signozio.go index a3a5cad414..9c39a9d955 100644 --- a/ee/query-service/integrations/signozio/signozio.go +++ b/ee/query-service/integrations/signozio/signozio.go @@ -47,7 +47,7 @@ func ValidateLicenseV3(licenseKey string) (*model.LicenseV3, *model.ApiError) { req, err := http.NewRequest("GET", C.GatewayUrl+"/v2/licenses/me", nil) if err != nil { - return nil, model.BadRequest(errors.Wrap(err, fmt.Sprintf("failed to create request: %w", err))) + return nil, model.BadRequest(errors.Wrap(err, "failed to create request")) } // Setting the custom header @@ -55,7 +55,7 @@ func ValidateLicenseV3(licenseKey string) (*model.LicenseV3, *model.ApiError) { response, err := client.Do(req) if err != nil { - return nil, model.BadRequest(errors.Wrap(err, fmt.Sprintf("failed to make post request: %w", err))) + return nil, model.BadRequest(errors.Wrap(err, "failed to make post request")) } body, err := io.ReadAll(response.Body) diff --git a/ee/query-service/model/license_test.go b/ee/query-service/model/license_test.go index 1c6150c8ac..24c74eee09 100644 --- a/ee/query-service/model/license_test.go +++ b/ee/query-service/model/license_test.go @@ -97,8 +97,8 @@ func TestNewLicenseV3(t *testing.T) { }, }, { - name: "Fallback to basic plan if license status is inactive", - data: []byte(`{"id":"does-not-matter","key":"does-not-matter-key","category":"FREE","status":"INACTIVE","plan":{"name":"TEAMS"},"valid_from": 1730899309,"valid_until": -1}`), + name: "Fallback to basic plan if license status is invalid", + data: []byte(`{"id":"does-not-matter","key":"does-not-matter-key","category":"FREE","status":"INVALID","plan":{"name":"TEAMS"},"valid_from": 1730899309,"valid_until": -1}`), pass: true, expected: &LicenseV3{ ID: "does-not-matter", @@ -108,14 +108,14 @@ func TestNewLicenseV3(t *testing.T) { "name": "TEAMS", }, "category": "FREE", - "status": "INACTIVE", + "status": "INVALID", "valid_from": float64(1730899309), "valid_until": float64(-1), }, PlanName: PlanNameBasic, ValidFrom: 1730899309, ValidUntil: -1, - Status: "INACTIVE", + Status: "INVALID", IsCurrent: false, Features: model.FeatureSet{}, }, diff --git a/pkg/types/alertmanagertypes/config_test.go b/pkg/types/alertmanagertypes/config_test.go index c9c832c8dc..326d018f19 100644 --- a/pkg/types/alertmanagertypes/config_test.go +++ b/pkg/types/alertmanagertypes/config_test.go @@ -17,7 +17,7 @@ func TestCreateRuleIDMatcher(t *testing.T) { name string orgID string receivers []config.Receiver - ruleIDToReceivers map[string][]string + ruleIDToReceivers []map[string][]string expectedRoutes []map[string]any }{ { @@ -34,7 +34,7 @@ func TestCreateRuleIDMatcher(t *testing.T) { }, }, }, - ruleIDToReceivers: map[string][]string{"test-rule": {"slack-receiver"}}, + ruleIDToReceivers: []map[string][]string{{"test-rule": {"slack-receiver"}}}, expectedRoutes: []map[string]any{{"receiver": "slack-receiver", "continue": true, "matchers": []any{"ruleId=~\"-1|test-rule\""}}}, }, { @@ -59,7 +59,7 @@ func TestCreateRuleIDMatcher(t *testing.T) { }, }, }, - ruleIDToReceivers: map[string][]string{"test-rule": {"slack-receiver", "email-receiver"}}, + ruleIDToReceivers: []map[string][]string{{"test-rule": {"slack-receiver", "email-receiver"}}}, expectedRoutes: []map[string]any{{"receiver": "slack-receiver", "continue": true, "matchers": []any{"ruleId=~\"-1|test-rule\""}}, {"receiver": "email-receiver", "continue": true, "matchers": []any{"ruleId=~\"-1|test-rule\""}}}, }, { @@ -76,7 +76,7 @@ func TestCreateRuleIDMatcher(t *testing.T) { }, }, }, - ruleIDToReceivers: map[string][]string{"test-rule": {"does-not-exist"}}, + ruleIDToReceivers: []map[string][]string{{"test-rule": {"does-not-exist"}}}, expectedRoutes: []map[string]any{{"receiver": "slack-receiver", "continue": true, "matchers": []any{"ruleId=~\"-1\""}}}, }, { @@ -93,7 +93,7 @@ func TestCreateRuleIDMatcher(t *testing.T) { }, }, }, - ruleIDToReceivers: map[string][]string{"test-rule-1": {"slack-receiver", "does-not-exist"}, "test-rule-2": {"slack-receiver"}}, + ruleIDToReceivers: []map[string][]string{{"test-rule-1": {"slack-receiver", "does-not-exist"}}, {"test-rule-2": {"slack-receiver"}}}, expectedRoutes: []map[string]any{{"receiver": "slack-receiver", "continue": true, "matchers": []any{"ruleId=~\"-1|test-rule-1|test-rule-2\""}}}, }, } @@ -112,9 +112,12 @@ func TestCreateRuleIDMatcher(t *testing.T) { require.NoError(t, err) } - for ruleID, receiverNames := range tc.ruleIDToReceivers { - err = cfg.CreateRuleIDMatcher(ruleID, receiverNames) - assert.NoError(t, err) + for _, ruleIDToReceiversMap := range tc.ruleIDToReceivers { + for ruleId, receiverNames := range ruleIDToReceiversMap { + err = cfg.CreateRuleIDMatcher(ruleId, receiverNames) + assert.NoError(t, err) + } + } routes, err := json.Marshal(cfg.alertmanagerConfig.Route.Routes)