name: Go

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Go 1.x
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Check Terraform format
        run: make tf_fmt_check

      - name: Run golangci-lint
        uses: golangci/golangci-lint-action@v6.1.0
        with:
          version: latest
          args: --timeout 5m

      - name: Install Terragrunt v0.31.8
        run: |
          sudo wget -q -O /usr/bin/terragrunt "https://github.com/gruntwork-io/terragrunt/releases/download/v0.31.8/terragrunt_linux_amd64"
          sudo chmod +x /usr/bin/terragrunt
          terragrunt --version

      - name: Test (unit and shared integration)
        run: make test_shared_int
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info
          INFRACOST_TERRAFORM_CLOUD_TOKEN: ${{ secrets.TERRAFORM_CLOUD_TOKEN }}

      - name: Parser benchmarks
        run: make parser_benchmarks

      - name: Upload benchmark artifacts
        uses: actions/upload-artifact@v4
        with:
          name: benchmarks
          path: |
            mem.out
            cpu.out
            trace.out

      - name: Test example works ok from CLI
        run: |
          make run --silent ARGS="breakdown --path examples/terraform --usage-file=examples/terraform/infracost-usage.yml --format json" > infracost-output.json
          [ -s infracost-output.json ] || (echo "file is empty" && exit 1)
          jq '' infracost-output.json
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info

      - name: Test example works ok from config file
        run: |
          make run --silent ARGS="breakdown --config-file=infracost-example.yml"
          [ -s infracost-output.json ] || (echo "file is empty" && exit 1)
          jq '' infracost-output.json
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info

  integration_aws:
    name: Integration tests (AWS)
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Go 1.x
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Test (AWS)
        run: make test_aws
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info

  integration_google:
    name: Integration tests (Google)
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Go 1.x
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Test (Google)
        run: make test_google
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info

  integration_azure:
    name: Integration tests (Azure)
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Go 1.x
        uses: actions/setup-go@v5
        with:
          go-version-file: go.mod

      - name: Test (Azure)
        run: make test_azure
        if: env.ARM_SUBSCRIPTION_ID != ''
        env:
          INFRACOST_API_KEY: "00000000000000000000000000000000"
          INFRACOST_LOG_LEVEL: info
          ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
          ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
          ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
          ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}