From 54dcc853546028ee3e10485e876dea8c5756eae7 Mon Sep 17 00:00:00 2001 From: Joshua Casey Date: Thu, 8 Sep 2022 17:28:00 -0500 Subject: [PATCH] Add update-github-config workflow to sync from github-config/library --- .github/.patch_files | 1 + .github/workflows/create-draft-release.yml | 2 +- .github/workflows/test-pull-request.yml | 2 +- .github/workflows/update-github-config.yml | 62 +++++++++++++++++ scripts/.util/git.sh | 21 ++++++ scripts/.util/print.sh | 51 ++++++++++++++ scripts/.util/tools.sh | 17 +++++ scripts/integration.sh | 80 ++++++++++++++++++++++ scripts/unit.sh | 62 +++++++++++++++++ 9 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/update-github-config.yml create mode 100644 scripts/.util/git.sh create mode 100644 scripts/.util/print.sh create mode 100644 scripts/.util/tools.sh create mode 100755 scripts/integration.sh create mode 100755 scripts/unit.sh diff --git a/.github/.patch_files b/.github/.patch_files index 65270276..346d63b3 100644 --- a/.github/.patch_files +++ b/.github/.patch_files @@ -5,6 +5,7 @@ .github/workflows/approve-bot-pr.yml .github/workflows/codeql-analysis.yml .github/workflows/lint.yml +.github/workflows/update-github-config.yml .github/workflows/create-draft-release.yml .github/workflows/test-pull-request.yml .github/workflows/lint-yaml.yml diff --git a/.github/workflows/create-draft-release.yml b/.github/workflows/create-draft-release.yml index 7692bc6d..ba56ab13 100644 --- a/.github/workflows/create-draft-release.yml +++ b/.github/workflows/create-draft-release.yml @@ -27,7 +27,7 @@ jobs: - name: Checkout uses: actions/checkout@v3 - name: Run Unit Tests - run: go test -v -count=1 ./... + run: ./scripts/unit.sh release: name: Release diff --git a/.github/workflows/test-pull-request.yml b/.github/workflows/test-pull-request.yml index daa81c56..6e25eebb 100644 --- a/.github/workflows/test-pull-request.yml +++ b/.github/workflows/test-pull-request.yml @@ -20,7 +20,7 @@ jobs: uses: actions/checkout@v3 - name: Run Unit Tests - run: go test -v -count=1 ./... + run: ./scripts/unit.sh upload: name: Upload Workflow Event Payload diff --git a/.github/workflows/update-github-config.yml b/.github/workflows/update-github-config.yml new file mode 100644 index 00000000..e413e28c --- /dev/null +++ b/.github/workflows/update-github-config.yml @@ -0,0 +1,62 @@ +name: Update shared github-config + +on: + schedule: + - cron: '30 1 * * *' + workflow_dispatch: {} + +concurrency: github_config_update + +jobs: + build: + name: Create PR to update shared files + runs-on: ubuntu-latest + steps: + + - name: Checkout + uses: actions/checkout@v3 + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + + - name: Checkout github-config + uses: actions/checkout@v3 + with: + repository: paketo-buildpacks/github-config + path: github-config + + - name: Checkout Branch + uses: paketo-buildpacks/github-config/actions/pull-request/checkout-branch@main + with: + branch: automation/github-config/update + + - name: Run the sync action + uses: paketo-buildpacks/github-config/actions/sync@main + with: + workspace: /github/workspace + config: /github/workspace/github-config/library + + - name: Cleanup + run: rm -rf github-config + + - name: Commit + id: commit + uses: paketo-buildpacks/github-config/actions/pull-request/create-commit@main + with: + message: "Updating github-config" + pathspec: "." + keyid: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY_ID }} + key: ${{ secrets.PAKETO_BOT_GPG_SIGNING_KEY }} + + - name: Push Branch + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/push-branch@main + with: + branch: automation/github-config/update + + - name: Open Pull Request + if: ${{ steps.commit.outputs.commit_sha != '' }} + uses: paketo-buildpacks/github-config/actions/pull-request/open@main + with: + token: ${{ secrets.PAKETO_BOT_GITHUB_TOKEN }} + title: "Updates github-config" + branch: automation/github-config/update diff --git a/scripts/.util/git.sh b/scripts/.util/git.sh new file mode 100644 index 00000000..71965bc4 --- /dev/null +++ b/scripts/.util/git.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +# shellcheck source=SCRIPTDIR/print.sh +source "$(dirname "${BASH_SOURCE[0]}")/print.sh" + +function util::git::token::fetch() { + if [[ -z "${GIT_TOKEN:-""}" ]]; then + util::print::title "Fetching GIT_TOKEN" + + GIT_TOKEN="$( + lpass show Shared-CF\ Buildpacks/concourse-private.yml \ + | grep buildpacks-github-token \ + | cut -d ' ' -f 2 + )" + fi + + printf "%s" "${GIT_TOKEN}" +} diff --git a/scripts/.util/print.sh b/scripts/.util/print.sh new file mode 100644 index 00000000..0c5a49e8 --- /dev/null +++ b/scripts/.util/print.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +function util::print::title() { + local blue reset message + blue="\033[0;34m" + reset="\033[0;39m" + message="${1}" + + echo -e "\n${blue}${message}${reset}" >&2 +} + +function util::print::info() { + local message + message="${1}" + + echo -e "${message}" >&2 +} + +function util::print::error() { + local message red reset + message="${1}" + red="\033[0;31m" + reset="\033[0;39m" + + echo -e "${red}${message}${reset}" >&2 + exit 1 +} + +function util::print::success() { + local message green reset + message="${1}" + green="\033[0;32m" + reset="\033[0;39m" + + echo -e "${green}${message}${reset}" >&2 + exitcode="${2:-0}" + exit "${exitcode}" +} + +function util::print::warn() { + local message yellow reset + message="${1}" + yellow="\033[0;33m" + reset="\033[0;39m" + + echo -e "${yellow}${message}${reset}" >&2 + exit 0 +} diff --git a/scripts/.util/tools.sh b/scripts/.util/tools.sh new file mode 100644 index 00000000..53abd49e --- /dev/null +++ b/scripts/.util/tools.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +# shellcheck source=SCRIPTDIR/print.sh +source "$(dirname "${BASH_SOURCE[0]}")/print.sh" + +function util::tools::tests::checkfocus() { + testout="${1}" + if grep -q 'Focused: [1-9]' "${testout}"; then + echo "Detected Focused Test(s) - setting exit code to 197" + rm "${testout}" + util::print::success "** GO Test Succeeded **" 197 + fi + rm "${testout}" +} diff --git a/scripts/integration.sh b/scripts/integration.sh new file mode 100755 index 00000000..bcf77839 --- /dev/null +++ b/scripts/integration.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +readonly PROGDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +readonly BUILDPACKDIR="$(cd "${PROGDIR}/.." && pwd)" + +# shellcheck source=SCRIPTDIR/.util/tools.sh +source "${PROGDIR}/.util/tools.sh" + +# shellcheck source=SCRIPTDIR/.util/print.sh +source "${PROGDIR}/.util/print.sh" + +# shellcheck source=SCRIPTDIR/.util/git.sh +source "${PROGDIR}/.util/git.sh" + +function main() { + while [[ "${#}" != 0 ]]; do + case "${1}" in + --use-token|-t) + shift 1 + token::fetch + ;; + + --help|-h) + shift 1 + usage + exit 0 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + util::print::error "unknown argument \"${1}\"" + esac + done + + if [[ ! -d "${BUILDPACKDIR}/integration" ]]; then + util::print::warn "** WARNING No Integration tests **" + fi + + tests::run +} + +function usage() { + cat <<-USAGE +integration.sh [OPTIONS] + +Runs the integration test suite. + +OPTIONS + --help -h prints the command usage + --use-token -t use GIT_TOKEN from lastpass +USAGE +} + +function token::fetch() { + GIT_TOKEN="$(util::git::token::fetch)" + export GIT_TOKEN +} + +function tests::run() { + util::print::title "Run Library Integration Tests" + + testout=$(mktemp) + pushd "${BUILDPACKDIR}" > /dev/null + if GOMAXPROCS="${GOMAXPROCS:-4}" go test -count=1 -timeout 0 ./integration/... -v -run Integration | tee "${testout}"; then + util::tools::tests::checkfocus "${testout}" + util::print::success "** GO Test Succeeded **" + else + util::print::error "** GO Test Failed **" + fi + popd > /dev/null +} + +main "${@:-}" diff --git a/scripts/unit.sh b/scripts/unit.sh new file mode 100755 index 00000000..9d9bbbab --- /dev/null +++ b/scripts/unit.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash + +set -eu +set -o pipefail + +readonly PROGDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +readonly BUILDPACKDIR="$(cd "${PROGDIR}/.." && pwd)" + +# shellcheck source=SCRIPTDIR/.util/tools.sh +source "${PROGDIR}/.util/tools.sh" + +# shellcheck source=SCRIPTDIR/.util/print.sh +source "${PROGDIR}/.util/print.sh" + +function main() { + while [[ "${#}" != 0 ]]; do + case "${1}" in + --help|-h) + shift 1 + usage + exit 0 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + util::print::error "unknown argument \"${1}\"" + esac + done + + unit::run +} + +function usage() { + cat <<-USAGE +unit.sh [OPTIONS] + +Runs the unit test suite. + +OPTIONS + --help -h prints the command usage +USAGE +} + +function unit::run() { + util::print::title "Run Library pack Unit and Example Tests" + + testout=$(mktemp) + pushd "${BUILDPACKDIR}" > /dev/null + if go test ./... -v -run "Unit|Example" | tee "${testout}"; then + util::tools::tests::checkfocus "${testout}" + util::print::success "** GO Test Succeeded **" + else + util::print::error "** GO Test Failed **" + fi + popd > /dev/null +} + +main "${@:-}"