# This builds only track 1 SDKs. See eng\pipelines\templates\steps\build.yml for track 2.

trigger:
  paths:
    exclude:
    - sdk/
    - eng/tools/
    - eng/config.json

pr:
  paths:
    exclude:
    - sdk/
    - eng/tools/
    - eng/config.json

jobs:
  - job: Build_Test
    strategy:
      matrix:
        Linux_Go117:
          pool.name: azsdk-pool-mms-ubuntu-2004-general
          go.version: '1.17.8'
        Linux_Go118:
          pool.name: azsdk-pool-mms-ubuntu-2004-general
          go.version: '1.18.2'

    pool:
      name: $(pool.name)

    variables:
      - template: /eng/pipelines/templates/variables/globals.yml
      - name: GOPATH
        value: '$(system.defaultWorkingDirectory)/work'
      - name: sdkPath
        value: '$(GOPATH)/src/github.com/$(build.repository.name)'
      - name: GO111MODULE
        value: 'off'
      - name: IGNORE_BREAKING_CHANGES
        value: 'true'
      - name: go.list.filter
        value: 'profiles services storage'
      - name: go.test.filter
        value: '-path ./vendor -prune -o -path ./sdk -prune -o -path ./eng -prune -o'

    steps:
    - task: GoTool@0
      inputs:
        version: '$(go.version)'
      displayName: "Select Go Version"

    - script: |
        set -e
        mkdir -p '$(GOPATH)/bin'
        mkdir -p '$(sdkPath)'
        shopt -s dotglob extglob
        mv !(work) '$(sdkPath)'
        echo '##vso[task.prependpath]$(GOROOT)/bin'
        echo '##vso[task.prependpath]$(GOPATH)/bin'
      displayName: 'Create Go Workspace'

    - script: |
        set -e
        go version
        curl -sSL https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
        dep ensure -v
        go get -u golang.org/x/lint/golint
      workingDirectory: '$(sdkPath)'
      displayName: 'Install Dependencies'

    - script: |
        for dd in $(go.list.filter); do
          cd $(sdkPath)/$dd
          go vet -v ./...
        done
      workingDirectory: '$(sdkPath)'
      displayName: 'Vet'

    - script: |
        for dd in $(go.list.filter); do
          cd $(sdkPath)/$dd
          go build -v ./...
        done
      workingDirectory: '$(sdkPath)'
      displayName: 'Build'

    - script: go test $(dirname $(find . $(go.test.filter) -name '*_test.go' -print) | sort -u)
      workingDirectory: '$(sdkPath)'
      displayName: 'Run Tests'

    - template: /eng/common/pipelines/templates/steps/verify-links.yml
      parameters:
        ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
          Urls: ($(sdkPath)/eng/common/scripts/get-markdown-files-from-changed-files.ps1)
        ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
          Urls: $(Get-ChildItem -Path '$(sdkPath)/*.md' -Recurse | Where {$_.FullName -notlike "*/vendor/*" -and $_.FullName -notlike "*/sdk/*"})
        Directory: ''
        ScriptDirectory: '$(sdkPath)/eng/common/scripts'
        WorkingDirectory: '$(sdkPath)'

    - script: go run ./eng/tools/apidiff/main.go packages ./services FETCH_HEAD~1 FETCH_HEAD --copyrepo --breakingchanges || $IGNORE_BREAKING_CHANGES
      workingDirectory: '$(sdkPath)'
      displayName: 'Display Breaking Changes'

    - script: go run ./eng/tools/pkgchk/main.go ./services --exceptions ./eng/tools/pkgchk/exceptions.txt
      workingDirectory: '$(sdkPath)'
      displayName: 'Verify Package Directory'

    - script: grep -L -r --include *.go --exclude-dir vendor -P "Copyright (\d{4}|\(c\)) Microsoft" ./ | tee >&2
      workingDirectory: '$(sdkPath)'
      displayName: 'Copyright Header Check'
      failOnStderr: true
      condition: succeededOrFailed()

    - script: |
        for dd in $(go.list.filter); do
          cd $(sdkPath)/$dd
          gofmt -s -l -d $(find . -name '*.go' -print) >&2
        done
      workingDirectory: '$(sdkPath)'
      displayName: 'Format Check'
      failOnStderr: true
      condition: and(succeededOrFailed(), startsWith(variables['go.version'], '1.16'))

    - script: |
        golint ./storage/... >&2
      workingDirectory: '$(sdkPath)'
      displayName: 'Linter Check'
      failOnStderr: true
      condition: succeededOrFailed()
