Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: add golangci-lint (linter) support #972

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
name: CI

on:
pull_request:
types: [opened, synchronize]

env:
ENV0_API_ENDPOINT: ${{ secrets.ENV0_API_ENDPOINT }}
ENV0_API_KEY: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_KEY }} # API Key for organization 'TF-provider-integration-tests' @ dev
ENV0_API_SECRET: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_SECRET }}
GO_VERSION: "1.21"
TERRAFORM_VERSION: 1.1.7

jobs:
unit-tests:
name: Unit Tests
timeout-minutes: 10
runs-on: ubuntu-20.04
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v4
- name: Generate mocks
run: |
go install go.uber.org/mock/[email protected]
go generate client/api_client.go
- name: Go fmt
run: |
! go fmt ./... | read
- name: Go vet
run: |
! go vet ./... | read
- name: Go Test
run: go test -v ./...

# See terraform-provider-env0 README for integration tests prerequisites
integration-tests:
name: Integration Tests
runs-on: ubuntu-20.04
container: golang:1.21-alpine3.18
timeout-minutes: 20
steps:
- name: Install Terraform
run: apk add terraform
- name: Checkout code
uses: actions/checkout@v4
- name: Run Harness tests
run: go run tests/harness.go
name: CI

on:
pull_request:
types: [opened, synchronize]

env:
ENV0_API_ENDPOINT: ${{ secrets.ENV0_API_ENDPOINT }}
ENV0_API_KEY: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_KEY }} # API Key for organization 'TF-provider-integration-tests' @ dev
ENV0_API_SECRET: ${{ secrets.TF_PROVIDER_INTEGRATION_TEST_API_SECRET }}
GO_VERSION: "1.21"
TERRAFORM_VERSION: 1.1.7

jobs:
unit-tests:
name: Unit Tests
timeout-minutes: 10
runs-on: ubuntu-20.04
steps:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout code
uses: actions/checkout@v4
- name: Generate mocks
run: |
go install go.uber.org/mock/[email protected]
go generate client/api_client.go
- name: Go fmt
run: |
! go fmt ./... | read
- name: Go vet
run: |
! go vet ./... | read
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
- name: Go Test
run: go test -v ./...

# See terraform-provider-env0 README for integration tests prerequisites
integration-tests:
name: Integration Tests
runs-on: ubuntu-20.04
container: golang:1.21-alpine3.18
timeout-minutes: 20
steps:
- name: Install Terraform
run: apk add terraform
- name: Checkout code
uses: actions/checkout@v4
- name: Run Harness tests
run: go run tests/harness.go
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ linters:
- errname
- errorlint
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- goconst
- gocritic
Expand Down
4 changes: 2 additions & 2 deletions client/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func (client *ApiClient) Agents() ([]Agent, error) {
}

var result []Agent
err = client.http.Get("/agents", map[string]string{"organizationId": organizationId}, &result)
if err != nil {

if err := client.http.Get("/agents", map[string]string{"organizationId": organizationId}, &result); err != nil {
return nil, err
}

Expand Down
1 change: 1 addition & 0 deletions client/agent_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (client *ApiClient) ProjectsAgentsAssignments() (*ProjectsAgentsAssignments
}

var result ProjectsAgentsAssignments

err = client.http.Get("/agents/projects-assignments", map[string]string{"organizationId": organizationId}, &result)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions client/agent_project_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var _ = Describe("Agent Project Assignment", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId).Times(1)
mockOrganizationIdCall().Times(1)

httpCall = mockHttpClient.EXPECT().
Post("/agents/projects-assignments?organizationId="+organizationId, mapping, gomock.Any()).Times(1).
Expand All @@ -59,7 +59,7 @@ var _ = Describe("Agent Project Assignment", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId).Times(1)
mockOrganizationIdCall().Times(1)

httpCall = mockHttpClient.EXPECT().
Post("/agents/projects-assignments?organizationId="+organizationId, mapping, gomock.Any()).Times(1).Return(errorMock)
Expand All @@ -83,7 +83,7 @@ var _ = Describe("Agent Project Assignment", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId).Times(1)
mockOrganizationIdCall().Times(1)

httpCall = mockHttpClient.EXPECT().
Get("/agents/projects-assignments", map[string]string{"organizationId": organizationId}, gomock.Any()).Times(1).
Expand All @@ -107,7 +107,7 @@ var _ = Describe("Agent Project Assignment", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId).Times(1)
mockOrganizationIdCall().Times(1)

httpCall = mockHttpClient.EXPECT().
Get("/agents/projects-assignments", map[string]string{"organizationId": organizationId}, gomock.Any()).Times(1).
Expand Down
4 changes: 2 additions & 2 deletions client/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var _ = Describe("Agent Client", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()

httpCall = mockHttpClient.EXPECT().
Get("/agents", gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -56,7 +56,7 @@ var _ = Describe("Agent Client", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()

httpCall = mockHttpClient.EXPECT().
Get("/agents", gomock.Any(), gomock.Any()).
Expand Down
17 changes: 12 additions & 5 deletions client/api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("ApiKey Client", func() {
mockApiKeys := []ApiKey{mockApiKey}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()
httpCall = mockHttpClient.EXPECT().
Get("/api-keys", map[string]string{"organizationId": organizationId}, gomock.Any()).
Do(func(path string, request interface{}, response *[]ApiKey) {
Expand All @@ -48,10 +48,11 @@ var _ = Describe("ApiKey Client", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()

createApiKeyPayload := ApiKeyCreatePayload{}
copier.Copy(&createApiKeyPayload, &mockApiKey)

_ = copier.Copy(&createApiKeyPayload, &mockApiKey)

expectedCreateRequest := ApiKeyCreatePayloadWith{
ApiKeyCreatePayload: createApiKeyPayload,
Expand Down Expand Up @@ -85,14 +86,20 @@ var _ = Describe("ApiKey Client", func() {
})

Describe("Delete ApiKey", func() {
var err error

BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete("/api-keys/"+mockApiKey.Id, nil)
apiClient.ApiKeyDelete(mockApiKey.Id)
err = apiClient.ApiKeyDelete(mockApiKey.Id)
})

It("Should send DELETE request with ApiKey id", func() {
httpCall.Times(1)
})

It("Should not return error", func() {
Expect(err).To(BeNil())
})
})

Describe("Get Oidc Sub", func() {
Expand All @@ -101,7 +108,7 @@ var _ = Describe("ApiKey Client", func() {
mockedOidcSub := "oidc sub 1234"

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()
httpCall = mockHttpClient.EXPECT().
Get("/api-keys/oidc-sub", map[string]string{"organizationId": organizationId}, gomock.Any()).
Do(func(path string, request interface{}, response *string) {
Expand Down
8 changes: 4 additions & 4 deletions client/approval_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("Approval Policy Client", func() {
mockApprovalPolicies := []ApprovalPolicy{mockApprovalPolicy}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()
httpCall = mockHttpClient.EXPECT().
Get("/approval-policy", map[string]string{"organizationId": organizationId, "name": mockApprovalPolicy.Name}, gomock.Any()).
Do(func(path string, request interface{}, response *[]ApprovalPolicy) {
Expand Down Expand Up @@ -125,10 +125,10 @@ var _ = Describe("Approval Policy Client", func() {
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId).Times(1)
mockOrganizationIdCall().Times(1)

createApprovalPolicyPayload := ApprovalPolicyCreatePayload{}
copier.Copy(&createApprovalPolicyPayload, &mockApprovalPolicy)
_ = copier.Copy(&createApprovalPolicyPayload, &mockApprovalPolicy)

expectedCreateRequest := struct {
ApprovalPolicyCreatePayload
Expand Down Expand Up @@ -162,7 +162,7 @@ var _ = Describe("Approval Policy Client", func() {

BeforeEach(func() {
updateApprovalPolicyPayload := ApprovalPolicyUpdatePayload{}
copier.Copy(&updateApprovalPolicyPayload, &mockApprovalPolicy)
_ = copier.Copy(&updateApprovalPolicyPayload, &mockApprovalPolicy)

httpCall = mockHttpClient.EXPECT().
Put("/approval-policy", &updateApprovalPolicyPayload, gomock.Any()).
Expand Down
2 changes: 1 addition & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = AfterSuite(func() {
ctrl.Finish()
})

func mockOrganizationIdCall(organizationId string) *gomock.Call {
func mockOrganizationIdCall() *gomock.Call {
organizations := []Organization{{
Id: organizationId,
}}
Expand Down
4 changes: 2 additions & 2 deletions client/cloud_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("CloudAccount", func() {

Describe("create", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()

payload := CloudAccountCreatePayload{
Provider: account1.Provider,
Expand Down Expand Up @@ -151,7 +151,7 @@ var _ = Describe("CloudAccount", func() {
}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
mockOrganizationIdCall()

httpCall = mockHttpClient.EXPECT().
Get("/cloud/configurations", map[string]string{"organizationId": organizationId}, gomock.Any()).
Expand Down
4 changes: 2 additions & 2 deletions client/cloud_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func (client *ApiClient) CloudCredentialsList() ([]Credentials, error) {
}

var credentials []Credentials
err = client.http.Get("/credentials", map[string]string{"organizationId": organizationId}, &credentials)
if err != nil {

if err := client.http.Get("/credentials", map[string]string{"organizationId": organizationId}, &credentials); err != nil {
return []Credentials{}, err
}

Expand Down
8 changes: 4 additions & 4 deletions client/cloud_credentials_project_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type CloudCredentialsProjectAssignment struct {
func (client *ApiClient) AssignCloudCredentialsToProject(projectId string, credentialId string) (CloudCredentialsProjectAssignment, error) {
var result CloudCredentialsProjectAssignment

err := client.http.Put("/credentials/deployment/"+credentialId+"/project/"+projectId, nil, &result)
if err != nil {
if err := client.http.Put("/credentials/deployment/"+credentialId+"/project/"+projectId, nil, &result); err != nil {
return result, err
}

return result, nil
}

Expand All @@ -26,10 +26,10 @@ func (client *ApiClient) RemoveCloudCredentialsFromProject(projectId string, cre

func (client *ApiClient) CloudCredentialIdsInProject(projectId string) ([]string, error) {
var result CloudCredentialIdsInProjectResponse
err := client.http.Get("/credentials/deployment/project/"+projectId, nil, &result)

if err != nil {
if err := client.http.Get("/credentials/deployment/project/"+projectId, nil, &result); err != nil {
return nil, err
}

return result.CredentialIds, nil
}
Loading
Loading