forked from transferwise/actions-pr-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparison.sh
executable file
·97 lines (81 loc) · 2.42 KB
/
comparison.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
shopt -s nocasematch
body_comparison() {
if [[ ! "$GITHUB_PULL_REQUEST_EVENT_BODY" =~ $PR_CONTAINS_PATTERN ]]
then
echo "PR description does not match pattern \"${PR_CONTAINS_PATTERN}\""
return 1
fi
if [[ "$GITHUB_PULL_REQUEST_EVENT_BODY" =~ $PR_NOT_CONTAINS_PATTERN ]]
then
echo "PR description should not contain pattern \"${PR_NOT_CONTAINS_PATTERN}\""
return 1
fi
return 0
}
title_comparison() {
if [[ ! "$GITHUB_PULL_REQUEST_EVENT_TITLE" =~ $PR_TITLE_CONTAINS_PATTERN ]]
then
echo "PR title \"${GITHUB_PULL_REQUEST_EVENT_TITLE}\" does not match pattern \"${PR_TITLE_CONTAINS_PATTERN}\""
return 1
fi
if [[ "$GITHUB_PULL_REQUEST_EVENT_TITLE" =~ $PR_TITLE_NOT_CONTAINS_PATTERN ]]
then
echo "PR title \"${GITHUB_PULL_REQUEST_EVENT_TITLE}\" should not contain pattern \"${PR_TITLE_NOT_CONTAINS_PATTERN}\""
return 1
fi
return 0
}
tags_comparison() {
# Disable globbing, remember current -f flag value
[[ "$-" == *f* ]] || globbing_disabled=1
set -f
IFSSS=$IFS
IFS=$'\n'
TAGS_LIST=( $(echo "$GITHUB_PULL_REQUEST_EVENT_LABELS" | jq -r '.[].name') )
TAGS_MANDATORY=( $(echo "$PR_TAGS_MANDATORY" | jq -r '.[]') )
TAGS_RESTRICTED=( $(echo "$PR_TAGS_RESTRICTED" | jq -r '.[]') )
# Restore globbing
test -n "$globbing_disabled" && set +f
IFS=$IFSSS
# check tags count
TAG_COUNT="${#TAGS_LIST[@]}"
if [[ $TAG_COUNT -lt $PR_TAGS_MIN_COUNT ]]; then
echo "PR tags count is ${TAG_COUNT}, minimum is ${PR_TAGS_MIN_COUNT}"
return 1
fi
# check mandatory tags
HAVE_MANDATORY_TAG=0
for TAG in "${TAGS_LIST[@]}" ; do
for S in "${TAGS_MANDATORY[@]}" ; do
if [[ $S == "$TAG" ]]; then
HAVE_MANDATORY_TAG=1
fi
done
done
if [[ -n $PR_TAGS_MANDATORY && $HAVE_MANDATORY_TAG != 1 ]]; then
echo "PR tags don't have any of required tags: ${PR_TAGS_MANDATORY}"
return 1
fi
# check restricted tags
for TAG in "${TAGS_LIST[@]}" ; do
HAVE_FORBIDDEN_TAG=1
for S in "${TAGS_RESTRICTED[@]}" ; do
if [[ $S == "$TAG" ]]; then
HAVE_FORBIDDEN_TAG=0
fi
done
if [[ $HAVE_FORBIDDEN_TAG == 1 ]]; then
break
fi
done
if [[ -n $PR_TAGS_RESTRICTED && $HAVE_FORBIDDEN_TAG == 1 ]]; then
echo "PR tags should be only: ${PR_TAGS_RESTRICTED}"
return 1
fi
return 0
}
no_comparison() {
# Last operation should return success, otherwise Github Actions will fail the whole run
return 0
}