From 3b310af7548daa73ea26a0095ebbe2e098bf8d41 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 31 Jan 2024 17:26:12 +0000 Subject: [PATCH] ci: add workflow to update on pact-ffi-released events --- .github/workflows/update-ffi.yml | 21 ++++++++++++++ scripts/create-pr-to-update-pact-ffi.sh | 27 ++++++++++++++++++ scripts/dispatch-ffi-released.sh | 38 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 .github/workflows/update-ffi.yml create mode 100755 scripts/create-pr-to-update-pact-ffi.sh create mode 100755 scripts/dispatch-ffi-released.sh diff --git a/.github/workflows/update-ffi.yml b/.github/workflows/update-ffi.yml new file mode 100644 index 000000000..7119f884e --- /dev/null +++ b/.github/workflows/update-ffi.yml @@ -0,0 +1,21 @@ +name: Update Pact FFI Library + +on: + repository_dispatch: + types: + - pact-ffi-released + +jobs: + update: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - run: | + git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" + git config --global user.name "${GITHUB_ACTOR}" + git config pull.ff only + + - run: script/create-pr-to-update-pact-ffi.sh ${{ github.event.client_payload.version }} + env: + GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}' diff --git a/scripts/create-pr-to-update-pact-ffi.sh b/scripts/create-pr-to-update-pact-ffi.sh new file mode 100755 index 000000000..5f0e8476f --- /dev/null +++ b/scripts/create-pr-to-update-pact-ffi.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +set -e + +: "${1?Please supply the pact-ffi version to upgrade to}" + +FFI_VERSION=$1 +TYPE=${2:-fix} +DASHERISED_VERSION=$(echo "${FFI_VERSION}" | sed 's/\./\-/g') +BRANCH_NAME="chore/upgrade-to-pact-ffi-${DASHERISED_VERSION}" + +git checkout master +git checkout installer/installer.go +git pull origin master + +git checkout -b ${BRANCH_NAME} + +cat installer/installer.go | sed "s/version:.*/version: \"${FFI_VERSION}\",/" > tmp-install +mv tmp-install installer/installer.go + +git add installer/installer.go +git commit -m "${TYPE}: update pact-ffi to ${FFI_VERSION}" +git push --set-upstream origin ${BRANCH_NAME} + +gh pr create --title "${TYPE}: update pact-ffi to ${FFI_VERSION}" --fill + +git checkout master diff --git a/scripts/dispatch-ffi-released.sh b/scripts/dispatch-ffi-released.sh new file mode 100755 index 000000000..3ed226ed7 --- /dev/null +++ b/scripts/dispatch-ffi-released.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# Script to trigger an update of the pact ffi from pact-foundation/pact-reference to listening repos +# Requires a Github API token with repo scope stored in the +# environment variable GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES + +: "${GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES:?Please set environment variable GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES}" + +if [ -n "$1" ]; then + name="\"${1}\"" +else + echo "name not provided as first param" + exit 1 +fi + +if [ -n "$2" ]; then + version="\"${2}\"" +else + echo "name not provided as second param" + exit 1 +fi + +repository_slug=$(git remote get-url origin | cut -d':' -f2 | sed 's/\.git//') + +output=$(curl -v https://api.github.com/repos/${repository_slug}/dispatches \ + -H 'Accept: application/vnd.github.everest-preview+json' \ + -H "Authorization: Bearer $GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES" \ + -d "{\"event_type\": \"pact-ffi-released\", \"client_payload\": {\"name\": ${name}, \"version\" : ${version}}}" 2>&1) + +if ! echo "${output}" | grep "HTTP\/.* 204" > /dev/null; then + echo "$output" | sed "s/${GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES}/********/g" + echo "Failed to trigger update" + exit 1 +else + echo "Update workflow triggered" +fi + +echo "See https://github.com/${repository_slug}/actions?query=workflow%3A%22Update+Pact+FFI+Library%22" \ No newline at end of file