Skip to content

Commit 4144983

Browse files
committed
Updated autopreview script, git clone depth fix
1 parent 5b6b04f commit 4144983

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

autopreview.sh

100644100755
Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,68 @@
11
#!/bin/bash
2-
set -ev
32

4-
#ALLOWED_USERS=("aireilly" "mburke5678" "vikram-redhat" "abrennan89" "ahardin-rh" "kalexand-rh" "adellape" "bmcelvee" "ousleyp" "lamek" "JStickler" "rh-max" "bergerhoffer" "sheriff-rh" "jboxman" "bobfuru" "aburdenthehand" "boczkowska" "Preeticp" "neal-timpe" "codyhoag" "apinnick" "bgaydosrh" "lmandavi" "maxwelldb" "pneedle-rh" "lbarbeevargas" "jeana-redhat" "RichardHoch" "johnwilkins" "sjhala-ccs" "mgarrellRH" "SNiemann15" "sfortner-RH" "jonquilwilliams" "ktania46" "wking" "
5-
#jc-berger" "rishumehra" "iranzo" "abhatt-rh" "@mohit-sheth" "stoobie" "emarcusRH" "kquinn1204" "mikemckiernan" "skrthomas" "sagidlow" "rolfedh")
3+
set -e
64

7-
if [ "$TRAVIS_PULL_REQUEST" != "false" ] ; then #to make sure it only runs on PRs and not all merges
8-
USERNAME=${TRAVIS_PULL_REQUEST_SLUG::-15}
9-
# COMMIT_HASH="$(git rev-parse @~)"
10-
# mapfile -t FILES_CHANGED < <(git diff --name-only "$COMMIT_HASH")
5+
# Define colors
6+
RED='\033[0;31m'
7+
GREEN='\033[0;32m'
8+
YELLOW='\033[1;33m'
9+
NC='\033[0m' # No Color
1110

12-
if [ "${TRAVIS_PULL_REQUEST_BRANCH}" != "main" ] ; then # to make sure it does not run for direct main changes
13-
# if [[ " ${FILES_CHANGED[*]} " = *".adoc"* ]] || [[ " ${FILES_CHANGED[*]} " = *"_topic_map.yml"* ]] || [[ " ${FILES_CHANGED[*]} " = *"_distro_map.yml"* ]] ; then # to make sure this doesn't run for general modifications
14-
if [ "$USERNAME" != "openshift-cherrypick-robot" ] ; then # to make sure it doesn't run for USERNAME openshift-cherrypick-robot
15-
echo "{\"PR_BRANCH\":\"${TRAVIS_PULL_REQUEST_BRANCH}\",\"BASE_REPO\":\"${TRAVIS_REPO_SLUG}\",\"PR_NUMBER\":\"${TRAVIS_PULL_REQUEST}\",\"USER_NAME\":\"${USERNAME}\",\"BASE_REF\":\"${TRAVIS_BRANCH}\",\"REPO_NAME\":\"${TRAVIS_PULL_REQUEST_SLUG}\",\"SHA\":\"${TRAVIS_PULL_REQUEST_SHA}\"}" > buildset.json
11+
USERNAME=${TRAVIS_PULL_REQUEST_SLUG::-15}
1612

17-
curl -H 'Content-Type: application/json' --request POST --data @buildset.json "https://ocpdocs-preview-receiver.vercel.app/api/buildPreview"
13+
if [[ "$USERNAME" == "openshift-cherrypick-robot" ]]; then
14+
echo -e "${YELLOW}🤖 PR by openshift-cherrypick-robot. Skipping the preview.${NC}"
15+
exit 0
16+
fi
17+
18+
if [[ "$TRAVIS_PULL_REQUEST" ]]; then
19+
# Check if modified files meet the conditions
20+
COMMIT_HASH="$(git rev-parse @~)"
21+
modified_files=$(git diff --name-only "$COMMIT_HASH")
22+
send_request=false
23+
24+
for file in $modified_files; do
25+
if [[ $file == *.adoc || $file == "_topic_map.yml" || $file == "_distro_map.yml" ]]; then
26+
send_request=true
27+
break
28+
fi
29+
done
1830

19-
echo -e "\\n\\033[0;32m[✓] Sent request for building a preview.\\033[0m"
31+
if [ "$send_request" = true ]; then
32+
# Build the JSON
33+
json_data=$(
34+
cat <<EOF
35+
{
36+
"PR_BRANCH": "${TRAVIS_PULL_REQUEST_BRANCH}",
37+
"BASE_REPO": "${TRAVIS_REPO_SLUG}",
38+
"PR_NUMBER": "${TRAVIS_PULL_REQUEST}",
39+
"USER_NAME": "${USERNAME}",
40+
"BASE_REF": "${TRAVIS_BRANCH}",
41+
"REPO_NAME": "${TRAVIS_PULL_REQUEST_SLUG}",
42+
"SHA": "${TRAVIS_PULL_REQUEST_SHA}"
43+
}
44+
EOF
45+
)
46+
47+
# Send the curl request
48+
if response=$(curl -s -X POST -H "Content-Type: application/json" --data "$json_data" https://ocpdocs-preview-receiver.vercel.app/api/buildPreview); then
49+
if echo "$response" | jq -e '.message == "Invalid data!"' >/dev/null; then
50+
echo -e "${RED}❌😔 Curl request failed: Invalid data!${NC}"
51+
echo -e "${YELLOW}$json_data${NC}"
52+
exit 1
2053
else
21-
echo -e "\\n\\033[0;32m[✓] Skipping preview build for openshift-cherrypick-robot.\\033[0m"
54+
echo -e "${GREEN}✅🥳 $response${NC}"
2255
fi
23-
# else
24-
# echo -e "\\n\\033[1;33m[!] No .adoc files modified, not building a preview.\\033[0m"
25-
# fi
56+
else
57+
echo -e "${RED}❌😬 Curl request failed: $response${NC}"
58+
echo -e "${YELLOW}$json_data${NC}"
59+
exit 1
60+
fi
61+
62+
echo -e "${GREEN}🚀🎉 Request sent successfully!${NC}"
2663
else
27-
echo -e "\\n\\033[1;33m[!] Direct PR for main branch, not building a preview.\\033[0m"
64+
echo -e "${YELLOW}⚠️🤔 No .adoc files, _topic_map.yml, or _distro_map.yml modified. Skipping the preview.${NC}"
2865
fi
2966
else
30-
echo -e "\\n\\033[1;33m[!] Not a PR, not building a preview.\\033[0m"
67+
echo -e "${YELLOW}❗🙅‍♀️ Not a Pull request. Skipping the preview.${NC}"
3168
fi

0 commit comments

Comments
 (0)