gofr-dev/gofr

View on GitHub
.github/workflows/go.yml

Summary

Maintainability
Test Coverage
---
name: Workflow-Pipeline

on:
  push:
    branches:
      - main
      - development
    paths-ignore:
      - 'docs/**'
  pull_request:
    branches:
      - main
      - development
    paths-ignore:
      - 'docs/**'

jobs:
  Example-Unit-Testing:
    name: Example Unit Testing (v${{ matrix.go-version }})🛠
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: ['1.23', '1.22']

    services:
      kafka:
        image: bitnami/kafka:3.4
        ports:
          - "9092:9092"
        env:
          KAFKA_ENABLE_KRAFT: yes
          KAFKA_CFG_PROCESS_ROLES: broker,controller
          KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
          KAFKA_CFG_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
          KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
          KAFKA_CFG_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
          KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE: true
          KAFKA_BROKER_ID: 1
          KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@127.0.0.1:9093
          ALLOW_PLAINTEXT_LISTENER: yes
          KAFKA_CFG_NODE_ID: 1

      redis:
        image: redis:7.0.5
        ports:
          - "2002:6379"
        options: "--entrypoint redis-server"

      mysql:
        image: mysql:8.2.0
        ports:
          - "2001:3306"
        env:
          MYSQL_ROOT_PASSWORD: "password"
          MYSQL_DATABASE: "test"

    steps:
      - name: Checkout code into go module directory
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
        id: Go

      - name: Get dependencies
        run: |
          go mod download

      - name: Start Zipkin
        run: docker run -d -p 2005:9411 openzipkin/zipkin:latest

      - name: Test
        run: |
          export APP_ENV=test
          go test gofr.dev/examples/... -v -short -coverprofile packageWithpbgo.cov -coverpkg=gofr.dev/examples/...
          grep -vE '^gofr\.dev\/.*\.pb\.go' packageWithpbgo.cov > profile.cov
          go tool cover -func profile.cov

      - name: Upload Test Coverage
        if: ${{ matrix.go-version == '1.22'}}
        uses: actions/upload-artifact@v4
        with:
          name: Example-Test-Report
          path: profile.cov

  PKG-Unit-Testing:
    name: PKG Unit Testing (v${{ matrix.go-version }})🛠
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: ['1.22', '1.21']

    steps:
      - name: Checkout code into go module directory
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
        id: Go

      - name: Get dependencies
        run: |
          go mod download

      - name: Test
        run: |
          export APP_ENV=test
          go test gofr.dev/pkg/... -v -short -coverprofile package.cov -coverpkg=gofr.dev/pkg/...
          grep -v '/mock_' package.cov > profile.cov
          go tool cover -func profile.cov

      - name: Upload Test Coverage
        if: ${{ matrix.go-version == '1.22'}}
        uses: actions/upload-artifact@v4
        with:
          name: PKG-Coverage-Report
          path: profile.cov

  parse_coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    needs: [ Example-Unit-Testing,PKG-Unit-Testing]
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: Download Coverage Report
        uses: actions/download-artifact@v4
        with:
          path: artifacts


      - name: Merge Coverage Files
        working-directory: artifacts
        run: |
          awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
          tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov

      - name: Parse code-coverage value
        working-directory: artifacts
        run: |
          codeCoverage=$(go tool cover -func=merged_profile.cov | grep total | awk '{print $3}')
          codeCoverage=${codeCoverage%?}
          echo "CODE_COVERAGE=$codeCoverage" >> $GITHUB_ENV

#      - name: Check if code-coverage is greater than threshold
#        run: |
#          codeCoverage=${{ env.CODE_COVERAGE }}
#          codeCoverage=${codeCoverage%??}
#          if [[ $codeCoverage -lt 92 ]]; then echo "code coverage cannot be less than 92%, currently its ${{ env.CODE_COVERAGE }}%" && exit 1; fi;
  Submodule-Unit-Testing:
    name: Submodule Unit Testing (v${{ matrix.go-version }})🛠
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: [ '1.23', '1.22' ]

    steps:
      - name: Checkout code into go module directory
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
        id: Go

      - name: Detect and Test Submodules
        run: |
          # Find all directories containing a go.mod file within 'pkg'
          for module in $(find pkg -name "go.mod" -exec dirname {} \;); do
            echo "Testing module: $module"
            cd $module
          
            # Extract module name (replace '/' with '_')
            module_name=$(echo $module | tr '/' '_')

            # Download dependencies for the submodule
            go mod download
            go mod tidy

            # Run tests for the submodule and generate coverage
            export APP_ENV=test
            go test ./... -v -short -coverprofile=${module_name}.cov -coverpkg=./...

            # Return to the root directory
            cd -
          done

      - name: Upload Coverage Reports
        uses: actions/upload-artifact@v3
        with:
          name: coverage-reports
          path: pkg/**/*.cov


  upload_coverage:
    name: Upload Coverage📊
    runs-on: ubuntu-latest
    needs: [Example-Unit-Testing,PKG-Unit-Testing]
    if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/development'}}
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4

      - name: Download Coverage Report
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Merge Coverage Files
        working-directory: artifacts
        run: |
          awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov
          tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov

      - name: Upload
        uses: paambaati/codeclimate-action@v9.0.0
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
        with:
          coverageLocations: artifacts/merged_profile.cov:gocov
          prefix: gofr.dev

  code_quality:
    name: Code Quality🎖️
    runs-on: ubuntu-latest
    container: "golangci/golangci-lint:v1.62.0"
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4
      - name: Get dependencies
        run: go get -v -t -d ./...
      - name: Lint Root Module
        run: |
          echo "Linting root module..."
          golangci-lint run --timeout 9m0s
      - name: Lint Submodules
        run: |
          echo "Searching for submodules..."
          for module in $(find pkg -name "go.mod" -exec dirname {} \;); do
            echo "Linting submodule: $module"
            cd $module
            go mod tidy
            golangci-lint run --timeout 9m0s
            cd -
          done    

  linting_party:
    name: Linting Party🥳
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v4
      - name: Check for file names errors
        uses: ls-lint/action@v2.2.3
        with:
          config: .ls-lint.yml