Skip to content
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
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ jobs:
uses: golangci/golangci-lint-action@v9
with:
version: v2.10

mocks:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v6
with:
cache: false
go-version-file: go.mod
- uses: infracost/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.INFRACOST_CI_APP_ID }}
private-key: ${{ secrets.INFRACOST_CI_APP_PRIVATE_KEY }}
owner: infracost
repositories: config
- run: |
git config --global url."https://x-access-token:${{ steps.app-token.outputs.token }}@github.com/".insteadOf "https://github.com/"
- run: make mocks
- name: Check for changes
run: |
if [ -n "$(git diff --name-only)" ]; then
echo "Mock files are out of date. Please run 'make mock' and commit the changes."
git diff --name-only
exit 1
fi
40 changes: 40 additions & 0 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
all: false
dir: '{{.InterfaceDir}}/mocks'
filename: mocks.go
force-file-write: true
formatter: goimports
formatter-options:
goimports:
all-errors: false
comments: true
format-only: true
fragment: false
local-prefix: ""
tab-indent: true
tab-width: 8
generate: true
include-auto-generated: true
log-level: info
structname: '{{.Mock}}{{.InterfaceName}}'
pkgname: mocks
recursive: false
require-template-schema-exists: true
template: testify
template-schema: '{{.Template}}.schema.json'
packages:
github.com/infracost/cli/internal/api/dashboard:
interfaces:
Client:
github.com/infracost/cli/internal/api/events:
interfaces:
Client:
github.com/infracost/proto/gen/go/infracost/parser/api:
config:
dir: pkg/plugins/parser/mocks
interfaces:
ParserServiceClient:
github.com/infracost/proto/gen/go/infracost/provider:
config:
dir: pkg/plugins/providers/mocks
interfaces:
ProviderServiceClient:
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ lint_install: ## Install golangci-lint
lint: lint_install ## Run linting operations
golangci-lint run ./...

.PHONY: mockery_install
mockery_install: ## Install mockery
go install github.com/vektra/mockery/v3@latest

.PHONY: mocks
mocks: mockery_install ## Generate mocks
mockery

.PHONY: fmt
fmt: ## Format the code
@gofmt -l . | while read -r f; do \
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/Masterminds/semver/v3 v3.4.0
github.com/MicahParks/keyfunc/v3 v3.7.0
github.com/golang-jwt/jwt/v5 v5.3.0
github.com/google/go-cmp v0.7.0
github.com/google/go-github/v83 v83.0.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-hclog v1.6.3
Expand Down Expand Up @@ -52,6 +51,7 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/soongo/path-to-regexp v1.6.4 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tklauser/go-sysconf v0.3.16 // indirect
github.com/tklauser/numcpus v0.11.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiT
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
Expand Down
20 changes: 14 additions & 6 deletions internal/api/dashboard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import (
"github.com/infracost/cli/internal/api/dashboard/graphql"
)

type Client struct {
client *http.Client
config *Config
}

type RunParameters struct {
OrganizationID string `json:"organizationId"`
RepositoryName string `json:"repositoryName"`
Expand All @@ -26,7 +21,20 @@ type RunParameters struct {
FinopsPolicies []json.RawMessage `json:"finopsPolicies"`
}

func (c *Client) RunParameters(ctx context.Context, repoURL, branchName string) (RunParameters, error) {
type Client interface {
RunParameters(ctx context.Context, repoURL, branchName string) (RunParameters, error)
}

var (
_ Client = (*client)(nil)
)

type client struct {
client *http.Client
config *Config
}

func (c *client) RunParameters(ctx context.Context, repoURL, branchName string) (RunParameters, error) {
const query = `query RunParameters($repoUrl: String, $branchName: String) {
runParameters(repoUrl: $repoUrl, branchName: $branchName) {
organizationId
Expand Down
13 changes: 8 additions & 5 deletions internal/api/dashboard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ var (
type Config struct {
Environment string `flagvalue:"environment"`
Endpoint string `env:"INFRACOST_CLI_DASHBOARD_ENDPOINT" flag:"dashboard-endpoint;hidden" usage:"The endpoint for the Infracost dashboard"`

// Can override this in tests.
Client func(httpClient *http.Client) Client
}

func (c *Config) Process() {
if c.Endpoint == "" {
c.Endpoint = defaultValues[c.Environment]["endpoint"]
}
}

func (c *Config) Client(client *http.Client) *Client {
return &Client{
client: client,
config: c,
c.Client = func(httpClient *http.Client) Client {
return &client{
client: httpClient,
config: c,
}
}
}
111 changes: 111 additions & 0 deletions internal/api/dashboard/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions internal/api/events/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import (
"github.com/infracost/cli/internal/logging"
)

type Client struct {
var (
_ Client = (*client)(nil)
)

type Client interface {
Push(ctx context.Context, event string, extra ...interface{})
}

type client struct {
client *http.Client
config *Config
}

func (c *Client) Push(ctx context.Context, event string, extra ...interface{}) {
func (c *client) Push(ctx context.Context, event string, extra ...interface{}) {
if isTest, ok := metadata["isTest"].(bool); ok && isTest {
return
}
Expand Down
26 changes: 17 additions & 9 deletions internal/api/events/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@ import (
"net/http"
)

var (
_ Config
)

type Config struct {
Endpoint string `env:"INFRACOST_CLI_EVENTS_ENDPOINT" flag:"events-endpoint;hidden" usage:"The endpoint for the Infracost events service" default:"https://pricing.api.infracost.io"`

Client func(httpClient *http.Client) Client
}

func (c *Config) Client(client *http.Client) *Client {
// The events client may be used before config defaults are applied (e.g.
// to report early errors), so ensure the endpoint is always set.
if c.Endpoint == "" {
c.Endpoint = "https://pricing.api.infracost.io"
}
return &Client{
client: client,
config: c,
func (c *Config) Process() {
c.Client = func(httpClient *http.Client) Client {
// The events client may be used before config defaults are applied (e.g.
// to report early errors), so ensure the endpoint is always set.
if c.Endpoint == "" {
c.Endpoint = "https://pricing.api.infracost.io"
}
return &client{
client: httpClient,
config: c,
}
}
}
Loading
Loading