# Copyright 2020 The Knative Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file is automagically synced here from github.com/knative-sandbox/.github
# repo by knobots: https://github.com/knative-sandbox/knobots and will be overwritten.

name: Verify

on:
  pull_request:
    branches: [ 'master', 'release-*' ]

jobs:

  verify:
    name: Verify Deps and Codegen
    strategy:
      matrix:
        go-version: [1.15.x]
        platform: [ubuntu-latest]

    runs-on: ${{ matrix.platform }}

    env:
      GOPATH: ${{ github.workspace }}

    steps:

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

    - name: Install Dependencies
      run: |
        go get github.com/google/ko/cmd/ko
        go get github.com/google/go-licenses

    - name: Check out code onto GOPATH
      uses: actions/checkout@v2
      with:
        path: ./src/knative.dev/${{ github.event.repository.name }}

    - name: Update Codegen
      shell: bash
      run: |
        pushd ./src/knative.dev/${{ github.event.repository.name }}
        [[ ! -f hack/update-codegen.sh ]] || ./hack/update-codegen.sh
        popd

    - name: Verify
      shell: bash
      run: |
        # From: https://backreference.org/2009/12/23/how-to-match-newlines-in-sed/
        # This is to leverage this workaround:
        # https://github.com/actions/toolkit/issues/193#issuecomment-605394935
        function urlencode() {
          sed ':begin;$!N;s/\n/%0A/;tbegin'
        }

        pushd ./src/knative.dev/${{ github.event.repository.name }}
        if [[ -z "$(git status --porcelain)" ]]; then
            echo "${{ github.repository }} up to date."
        else
            # Print it both ways because sometimes we haven't modified the file (e.g. zz_generated),
            # and sometimes we have (e.g. configmap checksum).
            echo "Found diffs in: $(git diff-index --name-only HEAD --)"
            for x in $(git diff-index --name-only HEAD --); do
                echo "::error file=$x::Please run ./hack/update-codegen.sh.%0A$(git diff $x | urlencode)"
            done
            echo "${{ github.repository }} is out of date. Please run hack/update-codegen.sh"
            exit 1
        fi
        popd
