|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Check if jq is installed |
| 6 | +hash jq 2>/dev/null || { echo >&2 "Error: jq is not installed. Please install jq before running this script."; exit 1; } |
| 7 | + |
| 8 | +# Check if gh is installed |
| 9 | +hash gh 2>/dev/null || { echo >&2 "Error: GitHub CLI (gh) is not installed. Please install gh before running this script."; exit 1; } |
| 10 | + |
| 11 | +# Check if inside a repository |
| 12 | +if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then |
| 13 | + echo "ERROR: you must execute this script from the OpenShift Git repository." 1>&2 |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +PR_BRANCH="$(git rev-parse --abbrev-ref HEAD)" |
| 18 | +PR_NUMBER="$(gh pr list -H "$PR_BRANCH" --json number | jq -r '.[0].number')" |
| 19 | +PR_COUNT="$(gh pr list -H $PR_BRANCH -s open | wc -l)" |
| 20 | +AUTHOR="$(gh pr view $PR_NUMBER --json author | jq -r '.author.login')" |
| 21 | +BASE_REF="$(gh pr view $PR_NUMBER --json baseRefName | jq -r '.baseRefName')" |
| 22 | +SHA=$(git rev-parse HEAD) |
| 23 | + |
| 24 | +# Check if there are more than one PRs associated with the branch |
| 25 | +if [ "$PR_COUNT" -gt 1 ]; then |
| 26 | + echo "ERROR: there are multiple PRs referencing your working branch." >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Function to create the data to send |
| 31 | +generate_pr_data() |
| 32 | +{ |
| 33 | + cat <<EOF |
| 34 | + { |
| 35 | + "PR_BRANCH": "$PR_BRANCH", |
| 36 | + "BASE_REPO": "openshift/openshift-docs", |
| 37 | + "PR_NUMBER": "$PR_NUMBER", |
| 38 | + "USER_NAME": "$AUTHOR", |
| 39 | + "BASE_REF": "$BASE_REF", |
| 40 | + "REPO_NAME": "$AUTHOR/openshift-docs", |
| 41 | + "SHA": "$SHA" |
| 42 | + } |
| 43 | +EOF |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +# Send the curl request |
| 48 | +if response=$(curl -s -X POST -H "Content-Type: application/json" -d "$(generate_pr_data)" https://eoa6vg2jiwjbnh6.m.pipedream.net); then |
| 49 | + if echo "$response" | grep -q -e "✅ Success! 🤖 Building the preview"; then |
| 50 | + echo "Preview build started successfully" |
| 51 | + echo "$response" |
| 52 | + else |
| 53 | + echo "Curl request failed: Invalid data!" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +else |
| 57 | + echo "Curl request failed: $response" |
| 58 | + exit 1 |
| 59 | +fi |
0 commit comments