This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
entrypoint.sh
executable file
·103 lines (80 loc) · 2.83 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
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
98
99
100
101
102
103
#!/bin/bash
. /action/lib-semver.sh
set -e
set -o pipefail
if [[ -z "$GITHUB_TOKEN" ]]; then
echo '$GITHUB_TOKEN does not exist.'
exit 1
fi
if [[ -z "$GITHUB_REF" ]]; then
echo '$GITHUB_REF does not exist. Make sure to configure on available action type'
exit 1
fi
API_URI=https://api.github.com
API_VERSION=v3
API_HEADER="Accept: application/vnd.github.${API_VERSION}+json"
AUTH_HEADER="Authorization: Token ${GITHUB_TOKEN}"
REF_FULL_REGEX='refs/heads/greenkeeper/(monorepo\.)?([-_@[:alnum:]]+)-([-_\.[:digit:]]+)'
filter_action() {
# Filter events only "merged pull requests"
local action=$(jq --raw-output .action "$GITHUB_EVENT_PATH")
local merged=$(jq --raw-output .pull_request.merged "$GITHUB_EVENT_PATH")
if [[ ! "$action" == 'closed' ]] || [[ ! "$merged" == 'true' ]]; then
exit 78 # netural
fi
}
numer_to_semver() {
if [[ "$1" =~ [[:digit:]]+ ]]; then
echo "${1}.0.0"
fi
echo "$1"
}
main() {
if ! [[ "$GITHUB_REF" =~ $REF_FULL_REGEX ]]; then
exit 78 # netural
fi
local ref_package_name="${BASH_REMATCH[2]}"
local ref_package_version="${BASH_REMATCH[3]}"
echo "package name: $ref_package_name"
echo "package version: $ref_package_version"
if semver_check "$ref_package_version"; then
echo "Determined $ref_package_version as semver format"
elif [[ "$ref_package_version" =~ [[:digit:]]+ ]]; then
echo "Determined $ref_package_version as numeric format"
# Transform numeric version to semver-like
ref_package_version="$(numer_to_semver $ref_package_version)"
else
echo "Only semver or numeric version format is supported"
exit 1
fi
local other_refs=$(
curl -XGET -fsSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${API_URI}/repos/${GITHUB_REPOSITORY}/git/refs?${PER_PAGE:-100}" | jq ".[].ref" | grep "$ref_package_name" \
)
for other_ref in $other_refs; do
echo "Processing $other_ref..."
# Remove quotation marks
other_ref=${other_ref%\"}
other_ref=${other_ref#\"}
[[ $other_ref =~ $REF_FULL_REGEX ]] && \
local other_ref_package_version="$(numer_to_semver ${BASH_REMATCH[3]})"
if ! semver_compare "$ref_package_version" "$other_ref_package_version"; then
echo "Merged ref has greater version than this, deleting..."
curl -XDELETE -sSL \
-H "${AUTH_HEADER}" \
-H "${API_HEADER}" \
"${API_URI}/repos/${GITHUB_REPOSITORY}/git/${other_ref%\"}"
if [[ "$?" == '0' ]]; then
echo "Succeed to delete $other_ref!"
else
echo "Failed to delete $other_ref (╯°□°)╯︵ ┻━┻"
fi
fi
echo
done
exit 0
}
filter_action
main "$@"