From adbf153be8ea40a089489cebd0cd031764abc833 Mon Sep 17 00:00:00 2001 From: Gaurav Nelson Date: Wed, 20 Dec 2023 09:49:12 +1000 Subject: [PATCH] Add conditional checks for PR and slug in autopreview.sh --- autopreview.sh | 70 ++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/autopreview.sh b/autopreview.sh index 188aa5727c..428c712821 100755 --- a/autopreview.sh +++ b/autopreview.sh @@ -8,61 +8,69 @@ GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color -USERNAME=${TRAVIS_PULL_REQUEST_SLUG::-15} +# Check if it is a PR +if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then + echo -e "${YELLOW}❗🙅‍♀️ Not a Pull request. Skipping the preview.${NC}" + exit 0 +fi -if [[ "$USERNAME" == "openshift-cherrypick-robot" ]]; then +# Check if slug is empty +if [[ -z "$TRAVIS_PULL_REQUEST_SLUG" ]]; then + echo -e "${YELLOW}🤖 Slug is empty this is a push build. Skipping the preview.${NC}" + exit 0 +fi + +# Check if the slug is "openshift-cherrypick-robot" +if [[ "${TRAVIS_PULL_REQUEST_SLUG::-15}" == "openshift-cherrypick-robot" ]]; then echo -e "${YELLOW}🤖 PR by openshift-cherrypick-robot. Skipping the preview.${NC}" exit 0 fi -if [[ "$TRAVIS_PULL_REQUEST" ]]; then - # Check if modified files meet the conditions - COMMIT_HASH="$(git rev-parse @~)" - modified_files=$(git diff --name-only "$COMMIT_HASH") - send_request=false +# Check if modified files meet the conditions +COMMIT_HASH="$(git rev-parse @~)" +modified_files=$(git diff --name-only "$COMMIT_HASH") +should_send_request() { for file in $modified_files; do if [[ $file == *.adoc || $file == "_topic_map.yml" || $file == "_distro_map.yml" || $file == "_topic_maps/"* ]]; then - send_request=true - break + return 0 fi done + return 1 +} - if [ "$send_request" = true ]; then - # Build the JSON - json_data=$( - cat </dev/null; then - echo -e "${RED}❌😔 Curl request failed: Invalid data!${NC}" - echo -e "${YELLOW}$json_data${NC}" - exit 1 - else - echo -e "${GREEN}✅🥳 $response${NC}" - fi - else - echo -e "${RED}❌😬 Curl request failed: $response${NC}" + # Send the curl request + if response=$(curl -s -X POST -H "Content-Type: application/json" --data "$json_data" https://ocpdocs-preview-receiver.vercel.app/api/buildPreview); then + if echo "$response" | jq -e '.message == "Invalid data!"' >/dev/null; then + echo -e "${RED}❌😔 Curl request failed: Invalid data!${NC}" echo -e "${YELLOW}$json_data${NC}" exit 1 + else + echo -e "${GREEN}✅🥳 $response${NC}" fi - - echo -e "${GREEN}🚀🎉 Request sent successfully!${NC}" else - echo -e "${YELLOW}⚠️🤔 No .adoc files, _topic_map.yml, or _distro_map.yml modified. Skipping the preview.${NC}" + echo -e "${RED}❌😬 Curl request failed: $response${NC}" + echo -e "${YELLOW}$json_data${NC}" + exit 1 fi + + echo -e "${GREEN}🚀🎉 Request sent successfully!${NC}" else - echo -e "${YELLOW}❗🙅‍♀️ Not a Pull request. Skipping the preview.${NC}" -fi + echo -e "${YELLOW}⚠️🤔 No .adoc files, _topic_map.yml, or _distro_map.yml modified. Skipping the preview.${NC}" +fi \ No newline at end of file