-
Notifications
You must be signed in to change notification settings - Fork 6
/
diffAgainstLatestReleases.sh
executable file
·162 lines (132 loc) · 5.08 KB
/
diffAgainstLatestReleases.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
set -eo pipefail
API_URL="https://api.github.com/repos/gchq/event-logging-schema"
#Shell Colour constants for use in 'echo -e'
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color
pushd() {
command pushd "$@" > /dev/null
}
popd() {
command popd > /dev/null
}
debug_value() {
local name="$1"; shift
local value="$1"; shift
if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${name}: [${value}]${NC}" >&2
fi
}
debug() {
local str="$1"; shift
if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${str}${NC}" >&2
fi
}
check_for_installed_binary() {
local -r binary_name=$1
command -v "${binary_name}" 1>/dev/null \
|| error_exit "Binary ${binary_name} is not installed"
}
check_for_installed_binary "jq"
main() {
#Check script arguments
if [ "$#" -ne 1 ] || ! [ -d "$1" ]; then
echo -e "${RED}INVALID ARGS!${NC}"
echo -e "Usage: ${GREEN}$0 workingDir${NC}"
echo -e "Where workingDir is normally ${BLUE}event-logging-transformer-main/pipelines${NC}"
exit 1
fi
workingDir=$1
pushd "${workingDir}"
local curl_auth_args=()
if [[ -n "${GIHUB_TOKEN}" ]]; then
curl_auth_args+=( --header "authorization: Bearer ${GITHUB_TOKEN}" )
fi
latest_schema_release_tag="$( \
curl \
--silent \
"${curl_auth_args[@]}" \
"${API_URL}/releases" \
| jq -r '.[].tag_name | select(. | test("^v"))' \
| head -n1 )"
debug_value "latest_schema_release_tag" "${latest_schema_release_tag}"
#Call the github API to git the json for the latest release, then extract the sources jar binary url from it#GH_USER_AND_TOKEN decalred in .travis.yml env:/global/:secure
sedScript='s/\s*"browser_download_url":.*"\(http.*event-logging-v.*\.xsd\)"/\1/p'
if [ "${GH_USER_AND_TOKEN}x" = "x" ]; then
#running locally so do it unauthenticated
latestUrls=$(curl -s ${API_URL} | sed -ne "${sedScript}")
else
latestUrls=$(curl -s --user "${GH_USER_AND_TOKEN}" ${API_URL} | sed -ne "${sedScript}")
fi
local latest_schema_release_url="${API_URL}/releases/tags/${latest_schema_release_tag}"
local latest_schema_urls
latest_schema_urls="$( \
curl \
--silent \
"${curl_auth_args[@]}" \
"${latest_schema_release_url}" \
| jq -r '.assets[] | select(.browser_download_url | test(".*\\.xsd")) | .browser_download_url' )"
echo
echo -e "${YELLOW}Comparing the latest released schemas from github against the ones just generated${NC}"
echo
echo -e "Working directory: [${BLUE}${PWD}${NC}]"
#echo "PWD=${PWD}"
echo
echo -e "${GREEN}Latest release URLs${NC}:\n${latestUrls}"
if [ -z "${latest_schema_urls}" ]; then
echo
echo -e "${RED}ERROR${NC} Latest schema urls could not be found from ${API_URL} json content:"
#dump out all the download urls
curl \
--silent \
"${curl_auth_args[@]}" \
"${latest_schema_release_url}" \
| jq -r '.assets[] | .browser_download_url'
exit 1
fi
mkdir -p downloaded
rm -rf ./downloaded/*
#loop over each of the download urls on github
while read -r url; do
echo
echo -e "Downloading schema ${BLUE}${url}${NC}"
pushd downloaded
#Download the released schema from github
#Need -L to follow re-directs as github will redirect the request
curl -L -s -O "${url}"
popd
#extract the filename part from the url
downloadedFilename="$(echo "${url}" | sed -ne 's#http.*/\([^/]*\.xsd\)#\1#p')"
downloadedFile="./downloaded/${downloadedFilename}"
#capture the suffix part from the filename, if there is one
suffix="$(echo "${downloadedFilename}" | sed -ne 's/event-logging-v[0-9]*\(.*\)\.xsd/\1/p')"
echo -e "Downloaded file: [${GREEN}${downloadedFile}${NC}]"
echo -e "Suffix: [${GREEN}${suffix}${NC}]"
#localFile=$(ls ../event-logging-v*${suffix}.xsd)
localFile="$(find ./generated/ -maxdepth 1 -regextype posix-extended -regex ".*/event-logging-v([0-9]+|SNAPSHOT)${suffix}.xsd")"
if [ -z "${localFile}" ]; then
echo -e "${RED}Warning${NC}: no local file found to match downloaded file ${BLUE}${downloadedFile}${NC}"
else
echo -e "Local file: [${GREEN}${localFile}${NC}]"
echo -e "Diffing ${BLUE}${localFile}${NC} against ${BLUE}${downloadedFile}${NC}"
diffFile="${downloadedFile}.diff"
#OR with true to stop the exit code from diff stopping the script
diff "${downloadedFile}" "${localFile}" > "${diffFile}" || true
if [ "$( wc -l < "${diffFile}" )" -gt 0 ]; then
diff --color "${downloadedFile}" "${localFile}" || true
echo -e "\n${RED}Warning${NC}: Local schema ${BLUE}${localFile}${NC} differs from the latest release ${BLUE}${downloadedFile}${NC}"
echo -e "This will likely be as intended but serves as a confirmation of what has changed since the latest release"
echo -e "(see changes in $PWD/${diffFile})${NC}"
else
echo -e "\n${GREEN}Local schema ${BLUE}${localFile}${NC} is identical to ${BLUE}${downloadedFile}${NC}"
fi
fi
done <<< "${latest_schema_urls}"
exit 0
}
main "$@"
# vim: set tabstop=2 shiftwidth=2 expandtab: