Skip to content

Commit ef229b1

Browse files
Merge pull request #69628 from gaurav-nelson/fix-previewscript-logic
Add conditional checks for PR and slug in autopreview.sh
2 parents 018796d + 8d95985 commit ef229b1

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

autopreview.sh

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,69 @@ GREEN='\033[0;32m'
88
YELLOW='\033[1;33m'
99
NC='\033[0m' # No Color
1010

11-
USERNAME=${TRAVIS_PULL_REQUEST_SLUG::-15}
11+
# Check if it is a PR
12+
if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
13+
echo -e "${YELLOW}❗🙅‍♀️ Not a Pull request. Skipping the preview.${NC}"
14+
exit 0
15+
fi
1216

13-
if [[ "$USERNAME" == "openshift-cherrypick-robot" ]]; then
17+
# Check if slug is empty
18+
if [[ -z "$TRAVIS_PULL_REQUEST_SLUG" ]]; then
19+
echo -e "${YELLOW}🤖 Slug is empty this is a push build. Skipping the preview.${NC}"
20+
exit 0
21+
fi
22+
23+
# Check if the slug is "openshift-cherrypick-robot"
24+
if [[ "${TRAVIS_PULL_REQUEST_SLUG::-15}" == "openshift-cherrypick-robot" ]]; then
1425
echo -e "${YELLOW}🤖 PR by openshift-cherrypick-robot. Skipping the preview.${NC}"
1526
exit 0
1627
fi
1728

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
29+
# Check if modified files meet the conditions
30+
COMMIT_HASH="$(git rev-parse @~)"
31+
modified_files=$(git diff --name-only "$COMMIT_HASH")
2332

33+
should_send_request() {
2434
for file in $modified_files; do
2535
if [[ $file == *.adoc || $file == "_topic_map.yml" || $file == "_distro_map.yml" || $file == "_topic_maps/"* ]]; then
26-
send_request=true
27-
break
36+
return 0
2837
fi
2938
done
39+
return 1
40+
}
3041

31-
if [ "$send_request" = true ]; then
32-
# Build the JSON
33-
json_data=$(
34-
cat <<EOF
42+
if should_send_request; then
43+
# Build the JSON
44+
json_data=$(
45+
cat <<EOF
3546
{
3647
"PR_BRANCH": "${TRAVIS_PULL_REQUEST_BRANCH}",
3748
"BASE_REPO": "${TRAVIS_REPO_SLUG}",
3849
"PR_NUMBER": "${TRAVIS_PULL_REQUEST}",
39-
"USER_NAME": "${USERNAME}",
50+
"USER_NAME": "${TRAVIS_PULL_REQUEST_SLUG::-15}",
4051
"BASE_REF": "${TRAVIS_BRANCH}",
4152
"REPO_NAME": "${TRAVIS_PULL_REQUEST_SLUG}",
4253
"SHA": "${TRAVIS_PULL_REQUEST_SHA}"
4354
}
4455
EOF
45-
)
56+
)
4657

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
53-
else
54-
echo -e "${GREEN}✅🥳 $response${NC}"
55-
fi
56-
else
57-
echo -e "${RED}❌😬 Curl request failed: $response${NC}"
58+
# Send the curl request
59+
if response=$(curl -s -X POST -H "Content-Type: application/json" --data "$json_data" https://ocpdocs-preview-receiver.vercel.app/api/buildPreview); then
60+
if echo "$response" | jq -e '.message == "Invalid data!"' >/dev/null; then
61+
echo -e "${RED}❌😔 Curl request failed: Invalid data!${NC}"
5862
echo -e "${YELLOW}$json_data${NC}"
5963
exit 1
64+
else
65+
echo -e "${GREEN}✅🥳 $response${NC}"
6066
fi
61-
62-
echo -e "${GREEN}🚀🎉 Request sent successfully!${NC}"
6367
else
64-
echo -e "${YELLOW}⚠️🤔 No .adoc files, _topic_map.yml, or _distro_map.yml modified. Skipping the preview.${NC}"
68+
echo -e "${RED}❌😬 Curl request failed: $response${NC}"
69+
echo -e "${YELLOW}$json_data${NC}"
70+
exit 1
6571
fi
72+
73+
echo -e "${GREEN}🚀🎉 Request sent successfully!${NC}"
6674
else
67-
echo -e "${YELLOW}❗🙅‍♀️ Not a Pull request. Skipping the preview.${NC}"
68-
fi
75+
echo -e "${YELLOW}⚠️🤔 No .adoc files, _topic_map.yml, or _distro_map.yml modified. Skipping the preview.${NC}"
76+
fi

0 commit comments

Comments
 (0)