#
# Copyright 2021 The Sigstore 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.

name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v2.4.0
      - name: Extract version of Go to use
        run: echo "GOVERSION=$(cat Dockerfile|grep golang | awk ' { print $2 } ' | sed -r 's/^.*://g'| uniq)" >> $GITHUB_ENV
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GOVERSION }}
      - name: Validate OpenAPI with Swagger
        run: make validate-openapi
      - name: Build
        run: make -C $GITHUB_WORKSPACE all
      - name: Fuzz-Build
        run: make -C $GITHUB_WORKSPACE fuzz
      - name: Test
        run: go test -v ./...
      - name: Ensure no files were modified as a result of the build
        run: git update-index --refresh && git diff-index --quiet HEAD -- || git diff --exit-code

  container-build:
    runs-on: ubuntu-20.04
    steps:
      - uses: actions/checkout@v2.4.0
      - name: Extract version of Go to use
        run: echo "GOVERSION=$(cat Dockerfile|grep golang | awk ' { print $2 } ' | sed -r 's/^.*://g'| uniq)" >> $GITHUB_ENV
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GOVERSION }}
      - uses: imjasonh/setup-ko@v0.4
        with:
          version: v0.9.3
      - name: container
        run: |
          KO_PREFIX=ko.local make ko 2>&1 | tee output.txt
          docker run --rm $(tail -1 output.txt) version

  e2e:
    runs-on: ubuntu-20.04
    needs: build

    steps:
      - name: download minisign
        run: sudo add-apt-repository ppa:dysfunctionalprogramming/minisign && sudo apt-get update && sudo apt-get install minisign
      - uses: actions/checkout@v2.4.0
      - name: Docker Build
        run: docker-compose build
      - name: Extract version of Go to use
        run: echo "GOVERSION=$(cat Dockerfile|grep golang | awk ' { print $2 } ' | sed -r 's/^.*://g'| uniq)" >> $GITHUB_ENV
      - uses: actions/setup-go@v2
        with:
          go-version: ${{ env.GOVERSION }}
      - name: CLI
        run: ./tests/e2e-test.sh
      - name: Upload logs if they exist
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: Docker Compose logs
          path: /tmp/docker-compose.log
