-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
49 lines (40 loc) · 1.51 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
shopt -s nocasematch
# Define the JIRA ticket pattern
JIRA_TICKET_PATTERN="[A-Z]{2,}-[0-9]+"
# Get the PR number and repo information from the environment variables
PR_NUMBER=${PR_NUMBER}
REPO=${REPO}
# Use the GitHub API to get the latest title and body of the PR
PR_DATA=$(curl --request GET \
--url "https://api.github.com/repos/$REPO/pulls/$PR_NUMBER" \
--header "Authorization: Bearer $GH_API_TOKEN" \
--header "Accept: application/vnd.github.v3+json" \
--silent)
# Extract the title and body from the API response
PR_TITLE=$(echo "$PR_DATA" | jq --raw-output .title)
PR_BODY=$(echo "$PR_DATA" | jq --raw-output .body)
# Check for a JIRA ticket in the title or body
if [[ $PR_TITLE =~ $JIRA_TICKET_PATTERN ]]; then
JIRA_TICKET=${BASH_REMATCH[0]^^}
echo "JIRA Ticket found in PR title: $JIRA_TICKET"
elif [[ $PR_BODY =~ $JIRA_TICKET_PATTERN ]]; then
JIRA_TICKET=${BASH_REMATCH[0]^^}
echo "JIRA Ticket found in PR body: $JIRA_TICKET"
else
echo "No match found."
exit 1
fi
shopt -u nocasematch
# Check if the JIRA ticket exists
response=$(curl --request GET \
--url "${JIRA_SERVER}/rest/api/3/issue/${JIRA_TICKET}" \
--user "${JIRA_SERVICE_USER}:${JIRA_SERVICE_API_TOKEN}" \
--header "Accept: application/json" \
--silent)
if [[ $(echo "$response" | jq -r '.key') == "$JIRA_TICKET" ]]; then
echo "Valid JIRA ticket!"
else
echo "The JIRA ticket does not exist."
exit 1
fi