Skip to content

Commit

Permalink
feat: Add vcsBaseCommitSha to estimate metadata (#3059)
Browse files Browse the repository at this point in the history
* feat: Add vcsBaseCommitSha to estimate metadata

It's empty on breakdown, and base branch's HEAD SHA on diff/output.
NB: When combining several JSONs in output command latest item's
metadata will be used in the output.

* fixup! feat: Add vcsBaseCommitSha to estimate metadata

* chore: Remove deprecated Go linters
  • Loading branch information
vdmgolub committed May 6, 2024
1 parent ddf22ac commit 12aa5e9
Show file tree
Hide file tree
Showing 22 changed files with 2,282 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- errcheck
- gocritic
- gofmt
Expand All @@ -13,10 +12,8 @@ linters:
- misspell
- revive
- staticcheck
- structcheck
- typecheck
- unused
- varcheck

linters-settings:
gocritic:
Expand Down
20 changes: 20 additions & 0 deletions cmd/infracost/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ func TestDiffWithCompareToFormatJSON(t *testing.T) {
)
}

func TestDiffWithCompareToNoMetadataFormatJSON(t *testing.T) {
dir := path.Join("./testdata", testutil.CalcGoldenFileTestdataDirName())
GoldenFileCommandTest(
t,
testutil.CalcGoldenFileTestdataDirName(),
[]string{
"diff",
"--path",
dir,
"--compare-to",
path.Join(dir, "prior.json"),
"--format",
"json",
}, &GoldenFileOptions{
RunTerraformCLI: true,
IsJSON: true,
},
)
}

func TestDiffWithCompareToPreserveSummary(t *testing.T) {
dir := path.Join("./testdata", testutil.CalcGoldenFileTestdataDirName())
GoldenFileCommandTest(
Expand Down
6 changes: 6 additions & 0 deletions cmd/infracost/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func TestOutputFormatJSON(t *testing.T) {
GoldenFileCommandTest(t, testutil.CalcGoldenFileTestdataDirName(), []string{"output", "--format", "json", "--path", "./testdata/example_out.json", "--path", "./testdata/azure_firewall_out.json"}, opts)
}

func TestOutputDiffFormatJSON(t *testing.T) {
opts := DefaultOptions()
opts.IsJSON = true
GoldenFileCommandTest(t, testutil.CalcGoldenFileTestdataDirName(), []string{"output", "--format", "json", "--path", "./testdata/example_diff_out.json"}, opts)
}

func TestOutputFormatBitbucketCommentWithProjectNames(t *testing.T) {
testName := testutil.CalcGoldenFileTestdataDirName()
GoldenFileCommandTest(t, testName,
Expand Down
8 changes: 8 additions & 0 deletions cmd/infracost/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func runMain(cmd *cobra.Command, runCtx *config.RunContext) error {
logging.Logger.Debug().Err(err).Msgf("failed to fetch vcs metadata for path %s", wd)
}
runCtx.VCSMetadata = metadata
runCtx.VCSMetadata.BaseCommit = vcs.Commit{}

pr, err := newParallelRunner(cmd, runCtx)
if err != nil {
Expand Down Expand Up @@ -126,6 +127,13 @@ func runMain(cmd *cobra.Command, runCtx *config.RunContext) error {
if err != nil {
return err
}
runCtx.VCSMetadata.BaseCommit = vcs.Commit{
SHA: pr.prior.Metadata.CommitSHA,
AuthorName: pr.prior.Metadata.CommitAuthorName,
AuthorEmail: pr.prior.Metadata.CommitAuthorEmail,
Time: pr.prior.Metadata.CommitTimestamp,
Message: pr.prior.Metadata.CommitMessage,
}
}

r.IsCIRun = runCtx.IsCIRun()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"metadata": {
"infracostCommand": "diff",
"vcsBranch": "stub-branch",
"vcsBaseCommitSha": "71785af96bce822bd54f359d0883cfa4ca805432",
"vcsCommitSha": "stub-sha",
"vcsCommitAuthorName": "stub-author",
"vcsCommitAuthorEmail": "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"metadata": {
"infracostCommand": "diff",
"vcsBranch": "stub-branch",
"vcsBaseCommitSha": "71785af96bce822bd54f359d0883cfa4ca805432",
"vcsCommitSha": "stub-sha",
"vcsCommitAuthorName": "stub-author",
"vcsCommitAuthorEmail": "[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"metadata": {
"infracostCommand": "diff",
"vcsBranch": "stub-branch",
"vcsBaseCommitSha": "71785af96bce822bd54f359d0883cfa4ca805432",
"vcsCommitSha": "stub-sha",
"vcsCommitAuthorName": "stub-author",
"vcsCommitAuthorEmail": "[email protected]",
Expand Down
10 changes: 10 additions & 0 deletions cmd/infracost/testdata/diff_with_compare_to_format_json/prior.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"version": "0.2",
"metadata": {
"infracostCommand": "breakdown",
"vcsBranch": "master",
"vcsCommitSha": "71785af96bce822bd54f359d0883cfa4ca805432",
"vcsCommitAuthorName": "Hugo",
"vcsCommitAuthorEmail": "",
"vcsCommitTimestamp": "2024-02-23T11:04:26Z",
"vcsCommitMessage": "wip",
"vcsRepositoryUrl": "https://github.com/infracost/infracost.git"
},
"currency": "USD",
"projects": [
{
Expand Down

0 comments on commit 12aa5e9

Please sign in to comment.