name: Tests
"on":
    merge_group:
        types:
            - checks_requested
        branches:
            - main
    pull_request: {}
    push:
        branches:
            - main
jobs:
    create-package:
        name: Create Package Test
        runs-on:
            - ubuntu-latest
        steps:
            - uses: actions/setup-go@v4
              with:
                go-version: "1.20"
            - name: Install create-package
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                go install -ldflags="-s -w" github.com/paketo-buildpacks/libpak/cmd/create-package@${PAKETO_LIBPAK_COMMIT:=latest}
              env:
                PAKETO_LIBPAK_COMMIT: ${{ vars.PAKETO_LIBPAK_COMMIT }}
            - name: Install pack
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                echo "Installing pack ${PACK_VERSION}"

                mkdir -p "${HOME}"/bin
                echo "${HOME}/bin" >> "${GITHUB_PATH}"

                curl \
                  --location \
                  --show-error \
                  --silent \
                  "https://github.com/buildpacks/pack/releases/download/v${PACK_VERSION}/pack-v${PACK_VERSION}-linux.tgz" \
                | tar -C "${HOME}"/bin -xz pack
              env:
                PACK_VERSION: 0.30.0-pre2
            - name: Enable pack Experimental
              if: ${{ false }}
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                echo "Enabling pack experimental features"

                mkdir -p "${HOME}"/.pack
                echo "experimental = true" >> "${HOME}"/.pack/config.toml
            - uses: actions/checkout@v3
            - uses: actions/cache@v3
              with:
                key: ${{ runner.os }}-go-${{ hashFiles('**/extension.toml', '**/package.toml') }}
                path: |-
                    ${{ env.HOME }}/.pack
                    ${{ env.HOME }}/carton-cache
                restore-keys: ${{ runner.os }}-go-
            - name: Compute Version
              id: version
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                if [ -z "${GITHUB_REF+set}" ]; then
                  echo "GITHUB_REF set to [${GITHUB_REF-<unset>}], but should never be empty or unset"
                  exit 255
                fi

                if [[ ${GITHUB_REF} =~ refs/tags/v([0-9]+\.[0-9]+\.[0-9]+) ]]; then
                  VERSION=${BASH_REMATCH[1]}

                  MAJOR_VERSION="$(echo "${VERSION}" | awk -F '.' '{print $1 }')"
                  MINOR_VERSION="$(echo "${VERSION}" | awk -F '.' '{print $1 "." $2 }')"

                  echo "version-major=${MAJOR_VERSION}" >> "$GITHUB_OUTPUT"
                  echo "version-minor=${MINOR_VERSION}" >> "$GITHUB_OUTPUT"
                elif [[ ${GITHUB_REF} =~ refs/heads/(.+) ]]; then
                  VERSION=${BASH_REMATCH[1]}
                else
                  VERSION=$(git rev-parse --short HEAD)
                fi

                echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
                echo "Selected ${VERSION} from
                  * ref: ${GITHUB_REF}
                  * sha: ${GITHUB_SHA}
                "
            - name: Create Package
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                # With Go 1.20, we need to set this so that we produce statically compiled binaries
                #
                # Starting with Go 1.20, Go will produce binaries that are dynamically linked against libc
                #   which can cause compatibility issues. The compiler links against libc on the build system
                #   but that may be newer than on the stacks we support.
                export CGO_ENABLED=0

                if [[ "${EXTENSION:-x}" == "true" ]]; then
                  export PACK_MODULE_TYPE=extension
                else
                  export PACK_MODULE_TYPE=buildpack
                fi

                if [[ "${INCLUDE_DEPENDENCIES}" == "true" ]]; then
                  create-package \
                    --source ${SOURCE_PATH:-.} \
                    --cache-location "${HOME}"/carton-cache \
                    --destination "${HOME}"/${PACK_MODULE_TYPE} \
                    --include-dependencies \
                    --version "${VERSION}"
                else
                  create-package \
                    --source ${SOURCE_PATH:-.} \
                    --destination "${HOME}"/${PACK_MODULE_TYPE} \
                    --version "${VERSION}"
                fi

                PACKAGE_FILE=${SOURCE_PATH:-.}/package.toml
                [[ -e ${PACKAGE_FILE} ]] && cp ${PACKAGE_FILE} "${HOME}"/package.toml
                printf '['${PACK_MODULE_TYPE}']\nuri = "%s"\n\n[platform]\nos = "%s"\n' "${HOME}"/${PACK_MODULE_TYPE} "${OS}" >> "${HOME}"/package.toml
              env:
                EXTENSION: "true"
                INCLUDE_DEPENDENCIES: "true"
                OS: linux
                VERSION: ${{ steps.version.outputs.version }}
            - name: Package Buildpack / Extension
              run: |-
                #!/usr/bin/env bash

                set -euo pipefail

                PACKAGE_LIST=($PACKAGES)
                # Extract first repo (Docker Hub) as the main to package & register
                PACKAGE=${PACKAGE_LIST[0]}

                if [[ "${EXTENSION:-x}" == "true" ]]; then
                  export PACK_MODULE_TYPE=extension
                else
                  export PACK_MODULE_TYPE=buildpack
                fi

                if [[ "${PUBLISH:-x}" == "true" ]]; then
                  pack ${PACK_MODULE_TYPE} package \
                    "${PACKAGE}:${VERSION}" \
                    --config "${HOME}"/package.toml \
                    --publish

                  if [[ -n ${VERSION_MINOR:-} && -n ${VERSION_MAJOR:-} ]]; then
                    crane tag "${PACKAGE}:${VERSION}" "${VERSION_MINOR}"
                    crane tag "${PACKAGE}:${VERSION}" "${VERSION_MAJOR}"
                  fi
                  crane tag "${PACKAGE}:${VERSION}" latest
                  echo "digest=$(crane digest "${PACKAGE}:${VERSION}")" >> "$GITHUB_OUTPUT"

                  # copy to other repositories specified
                  for P in "${PACKAGE_LIST[@]}"
                    do
                      if [ "$P" != "$PACKAGE" ]; then
                        crane copy "${PACKAGE}:${VERSION}" "${P}:${VERSION}"
                        if [[ -n ${VERSION_MINOR:-} && -n ${VERSION_MAJOR:-} ]]; then
                           crane tag "${P}:${VERSION}" "${VERSION_MINOR}"
                           crane tag "${P}:${VERSION}" "${VERSION_MAJOR}"
                        fi
                        crane tag "${P}:${VERSION}" latest
                      fi
                    done

                else
                  pack ${PACK_MODULE_TYPE} package \
                    "${PACKAGE}:${VERSION}" \
                    --config "${HOME}"/package.toml \
                    --format "${FORMAT}"
                fi
              env:
                EXTENSION: "true"
                FORMAT: image
                PACKAGES: test
                VERSION: ${{ steps.version.outputs.version }}
    unit:
        name: Unit Test
        runs-on:
            - ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - uses: actions/cache@v3
              with:
                key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
                path: ${{ env.HOME }}/go/pkg/mod
                restore-keys: ${{ runner.os }}-go-
            - uses: actions/setup-go@v4
              with:
                go-version: "1.20"
            - name: Install richgo
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                echo "Installing richgo ${RICHGO_VERSION}"

                mkdir -p "${HOME}"/bin
                echo "${HOME}/bin" >> "${GITHUB_PATH}"

                curl \
                  --location \
                  --show-error \
                  --silent \
                  "https://github.com/kyoh86/richgo/releases/download/v${RICHGO_VERSION}/richgo_${RICHGO_VERSION}_linux_amd64.tar.gz" \
                | tar -C "${HOME}"/bin -xz richgo
              env:
                RICHGO_VERSION: 0.3.10
            - name: Run Tests
              run: |
                #!/usr/bin/env bash

                set -euo pipefail

                richgo test ./...
              env:
                RICHGO_FORCE_COLOR: "1"