Skip to content

Commit

Permalink
chore: add git workflow to delete old branches in pactflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 19, 2024
1 parent 4612b79 commit 0604800
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/delete_branch_in_pactflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Delete application branch in Pactflow

on:
delete:
branches:
- "*"

jobs:
delete-branch-in-pactflow:
name: delete
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
# Requires Ruby to encode the URL path correctly - could use any other scripting language here
- uses: ruby/setup-ruby@v1

- name: Delete branch ${GIT_BRANCH} for application ${PACTICIPANT} in Pactflow
run: script/ci/delete-branch-in-pactflow.sh
env:
GIT_BRANCH: ${{ github.event.ref }}
PACTICIPANT: "Pact Broker Client"
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN_PACT_FOUNDATION }}
PACT_BROKER_BASE_URL=https://pact-foundation.pactflow.io
22 changes: 22 additions & 0 deletions script/ci/delete-branch-in-pactflow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -Eeuo pipefail

echo "Deleting branch ${GIT_BRANCH} for ${PACTICIPANT} in Pactflow..."
ENCODED_GIT_BRANCH=$(echo "$GIT_BRANCH" | ruby -e "require 'erb'; puts ERB::Util.url_encode(ARGF.read.chomp)")
ENCODED_PACTICIPANT=$(echo "$PACTICIPANT" | ruby -e "require 'erb'; puts ERB::Util.url_encode(ARGF.read.chomp)")
BRANCH_URL="${PACT_BROKER_BASE_URL}/pacticipants/${ENCODED_PACTICIPANT}/branches/${ENCODED_GIT_BRANCH}"

output_file=$(mktemp)

status=$(curl -v -X DELETE "${BRANCH_URL}" -H "Authorization: Bearer ${PACT_BROKER_TOKEN}" 2>&1 | tee "${output_file}" | awk '/^< HTTP/{print $3}')

if [ "$status" = "404" ]; then
echo "Branch ${GIT_BRANCH} for ${PACTICIPANT} does not exist in Pactflow"
elif [ $status -ge 400 ]; then
cat "${output_file}"
echo "Error deleting branch in Pactflow"
exit 1
else
echo "Deleted branch ${GIT_BRANCH} for ${PACTICIPANT} in Pactflow"
fi

0 comments on commit 0604800

Please sign in to comment.