-
Notifications
You must be signed in to change notification settings - Fork 1
/
reports.sh
201 lines (179 loc) · 6.25 KB
/
reports.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/bin/bash
#################### ABOUT
# grubAllureResults - to send allure results
# deployAllureResults - to deploy allure reports
#################### VARS
BRANCH_ERROR_MESSAGE="IF YOU DON'T SEE THE PULL REQUEST BUILD, THEN BRANCH CANNOT BE MERGED, YOU SHOULD FIX IT FIRST"
URL_NOT_FOUND_ERROR_MESSAGE="NONE OF THE ALLURE REPORTS WERE FOUND"
TEST_FAILED_ERROR_MESSAGE="SOME OF THE TESTS IS NOT PASSED. PLEASE CHECK ALLURE REPORT, FOR GETTING MORE DETAILS"
FILENAME_WITH_COMMENTS_FROM_GITHUB="comments"
FASTER_FILE_SHARING="true"
#################### UTILS
function getCommentsLastPageIndex(){
url="https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments"
index="$(curl -I -H "Authorization: token ${GIT_COMMENT_USER}"\
-X GET "${url}" | grep Link: | awk '{print $4}' | egrep -o 'page=[0-9]{1,10}' | awk -F"=" '{print $2}')"
reInteger='[0-9]+'
if ! [[ $index =~ $reInteger ]] ;
then
index=1
fi
echo ${index}
}
function collectRelevantComments(){
lastPageIndex=$(getCommentsLastPageIndex)
matchPattern="$1"
fileName="${FILENAME_WITH_COMMENTS_FROM_GITHUB}"
for (( pageIndex=1; pageIndex<=lastPageIndex; pageIndex++ ))
do
url="https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments?page=${pageIndex}"
curl -H "Authorization: token ${GIT_COMMENT_USER}"\
-X GET "${url}"\
>> ${fileName}
done
jq ".[].body" ${fileName} | grep "${matchPattern}"| awk '{print $3}' | sed "s/\"//g" | sort | uniq #return list
}
function sendComment() {
body="$1"
url="https://api.github.com/repos/${TRAVIS_REPO_SLUG}/issues/${TRAVIS_PULL_REQUEST}/comments";
echo Body: "${body}"
echo URL: "${url}"
curl -H "Authorization: token ${GIT_COMMENT_USER}" \
-X POST "${url}" \
-d "{
\"body\": \"${body}\"
}";
}
function archive() {
directory="$1"
archiveName=jdi-light-mobile-demo-platform-report.tar.gz
tar -czf "${archiveName}" "${directory}" > /dev/null
echo "${archiveName}" #return
}
function extractArchive() {
file="$1"
tar -zxvf "${file}"
}
function aboutTransfer() {
url="$1"
echo "[${TRAVIS_BUILD_NUMBER}] - ${url}" #return
}
function aboutNetlify() {
url="$1"
echo "[${TRAVIS_BUILD_NUMBER}] - Allure report on Netlify: ${url}" #return
}
function checkBranchIsOk() {
if [[ "x${TRAVIS_PULL_REQUEST}" == "xfalse" ]] ; then
echo "${BRANCH_ERROR_MESSAGE}"
sleep 3
exit 0
fi
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
######################### PART 1: send allure results into web to collect it later
function grubAllureResults() {
echo "Stage was: ${TRAVIS_BUILD_STAGE_NAME}"
checkBranchIsOk #there is an exit inside
if [[ "x${TRAVIS_BUILD_STAGE_NAME}" == "xtest" ]] ; then #don't remove x, it's useful
result="./jdi-light-mobile-demo-tests/target/allure-results"
echo RESULT: ${result}
archiveFile="$(archive "${result}")"
echo ARCHIVE: "${archiveFile}"
ls -lah ./"${archiveFile}"
uploadedTo="$(uploadFile "${archiveFile}")"
echo UPLOAD TO KEY: "${uploadedTo}"
sendComment "$(aboutTransfer "${uploadedTo}")"
fi
}
function uploadFile() {
file="$1"
if [[ "x${FASTER_FILE_SHARING}" == "xfalse" ]] ; then
urlKey="$(curl --upload-file "${file}" https://transfer.sh/"${file}")"
else
response="$(curl -F "file=@${file}" https://file.io/)"
url="$(echo "${response}" |jq -j '.link')"
urlKey="$(echo "${url}"| awk -F/ '{print $4}')"
fi
echo "${urlKey}" #return
}
function checkThatAllTestsPassed() {
content=$(<.*/allure-report/widgets/summary.json) #file system request
broken="$(echo "${content}"| jq '.statistic.broken')"
echo "${content}"
if [[ ${broken} -gt 0 ]]; then
echo "${TEST_FAILED_ERROR_MESSAGE}"
sleep 5
exit 1
fi
}
###################### PART 2: Deploy allure results as allure reports to netlify
function deployAllureResults() {
checkBranchIsOk #there is an exit inside
downloadAllureResults
extractAllureResults
generateAllureReports
echo "LOG1"
url="$(deployToNetlify "allure-report")"
echo "LOG2"
sendComment "$(aboutNetlify "${url}")"
checkThatAllTestsPassed #there is an exit with exception inside
}
function downloadAllureResults() {
urlExistence=false
echo "TRAVIS_BUILD_NUMBER: ${TRAVIS_BUILD_NUMBER}"
echo $(collectRelevantComments "${TRAVIS_BUILD_NUMBER}")
for urlKey in $(collectRelevantComments "${TRAVIS_BUILD_NUMBER}")
do
urlExistence=true
echo "Found: ${urlKey}"
if [[ "x${FASTER_FILE_SHARING}" == "xfalse" ]] ; then
fileName="$(echo "${urlKey}"| awk -F/ '{print $5}')"
curl "${urlKey}" --output "${fileName}"
else
fileName="${urlKey}.tar.gz"
curl https://file.io/"${urlKey}" --output "${fileName}"
fi
done
if [[ "x${urlExistence}" == "xfalse" ]] ; then
echo "Failed inside downloadAllureResults()"
echo "Comments recieved from github:"
cat "${FILENAME_WITH_COMMENTS_FROM_GITHUB}"
exitWithError
fi
}
function extractAllureResults() {
for archiveFile in $(ls -1 ./*.tar.gz)
do
extractArchive "${archiveFile}"
done
}
function generateAllureReports() {
reportDirList="";
allureDirExistence=false
allureDir="jdi-light-mobile-demo-tests/target/allure-results"
if [[ -d "$allureDir" ]] ; then
allureDirExistence=true
echo "Results found"
reportDirList="${reportDirList} ${allureDir}"
else
echo "RESULTS NOT FOUND"
fi
if [[ "x${allureDirExistence}" == "xfalse" ]] ; then
echo "Failed inside generateAllureReports()"
exitWithError
fi
echo "${reportDirList}"
allure generate --clean "${reportDirList}"
}
function deployToNetlify() {
directory="$1"
result="$(netlify deploy --dir "${directory}" --json)"
deployUrl="$(echo "${result}"r |jq '.deploy_url' |sed 's/"//g')"
echo "${deployUrl}"
}
function exitWithError() {
echo "${URL_NOT_FOUND_ERROR_MESSAGE}"
sleep 3
exit 1
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`