-
Notifications
You must be signed in to change notification settings - Fork 16
/
reports.sh
187 lines (165 loc) · 5.77 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
#!/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"
FILENAME_WITH_COMMENTS_FROM_GITHUB="comments"
#################### 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="$(echo "${directory}"| awk -F"/" '{print $1}')".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
for result in $(find jdi-dark*tests/target/allure-results -maxdepth 1 -type d)
do
echo RESULT: ${result}
archiveFile="$(archive "${result}")"
echo ARCHIVE: "${archiveFile}"
ls -lah *.tar.gz
uploadedTo="$(uploadFile "${archiveFile}")"
echo UPLOAD TO KEY: "${uploadedTo}"
sendComment "$(aboutTransfer "${uploadedTo}")"
done
fi
}
function uploadFile() {
file="$1"
# TODO : make an if depending of boolean variable to switch between transfer or between https://www.file.io/#one
#url=$(curl --upload-file "${file}" https://transfer.sh/${file})
response="$(curl -F "file=@${file}" https://file.io/)"
url="$(echo "${response}" |jq -j '.link')"
urlKey="$(echo "${url}"| awk -F/ '{print $4}')"
echo "${urlKey}" #return
}
###################### 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})"
}
function downloadAllureResults() {
urlExistence=false
echo "TRAVIS_BUILD_NUMBER: ${TRAVIS_BUILD_NUMBER}"
for urlKey in $(collectRelevantComments "${TRAVIS_BUILD_NUMBER}")
do
urlExistence=true
echo "Found: ${urlKey}"
# TODO: $4 for file.io, #5 for transfer.sh, add an IF
#fileName="$(echo "${url}.tar.gz"| awk -F/ '{print $5}')"
fileName="${urlKey}.tar.gz"
curl https://file.io/"${urlKey}" --output "${fileName}"
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
for report in $(ls -d1 jdi-dark*tests/target/)
do
allureDirExistence=true
allureDir="${report}allure-results"
if [[ -d "$allureDir" ]] ; then
echo "Results found for ${report}"
reportDirList="${reportDirList} ${allureDir}"
else
echo "RESULTS NOT FOUND FOR ${report}"
fi
done
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
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`