Skip to content

Commit fbb5059

Browse files
committed
ugly but may work
1 parent e44ac4e commit fbb5059

2 files changed

Lines changed: 154 additions & 2 deletions

File tree

Jenkinsfile

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,86 @@ EOF
11091109
}
11101110
}
11111111
/* ######################
1112-
Send status to Discord
1112+
Comment on PR (failed test) and Send status to Discord
11131113
###################### */
11141114
post {
11151115
always {
1116+
if (env.EXIT_STATUS != "ABORTED"){
1117+
if (currentBuild.currentResult != "SUCCESS"){
1118+
if (env.GITHUBIMAGE =~ /lspipepr/){
1119+
if (env.CI_TEST_ATTEMPTED == "true"){
1120+
sh '''#! /bin/bash
1121+
# Function to retrieve JSON data from URL
1122+
get_json() {
1123+
local url="$1"
1124+
local response=$(curl -s "$url")
1125+
if [ $? -ne 0 ]; then
1126+
echo "Failed to retrieve JSON data from $url"
1127+
return 1
1128+
fi
1129+
local json=$(echo "$response" | jq .)
1130+
if [ $? -ne 0 ]; then
1131+
echo "Failed to parse JSON data from $url"
1132+
return 1
1133+
fi
1134+
echo "$json"
1135+
}
1136+
1137+
build_table() {
1138+
local data="$1"
1139+
1140+
# Get the keys in the JSON data
1141+
local keys=$(echo "$data" | jq -r 'to_entries | map(.key) | .[]')
1142+
1143+
# Check if keys are empty
1144+
if [ -z "$keys" ]; then
1145+
echo "JSON report data does not contain any keys or the report does not exist."
1146+
return 1
1147+
fi
1148+
1149+
# Build table header
1150+
local header="| Tag | Passed |\\n| --- | --- |\\n"
1151+
1152+
# Loop through the JSON data to build the table rows
1153+
local rows=""
1154+
for build in $keys; do
1155+
local status=$(echo "$data" | jq -r ".[\\"$build\\"].test_success")
1156+
if [ "$status" = "true" ]; then
1157+
status="✅"
1158+
else
1159+
status="❌"
1160+
fi
1161+
local row="| "$build" | "$status" |\\n"
1162+
rows="${rows}${row}"
1163+
done
1164+
1165+
local table="${header}${rows}"
1166+
local escaped_table=$(echo "$table" | sed 's/\"/\\\\"/g')
1167+
echo "$escaped_table"
1168+
}
1169+
1170+
if [[ "${CI}" = "true" ]]; then
1171+
# Retrieve JSON data from URL
1172+
data=$(get_json "$CI_JSON_URL")
1173+
# Create table from JSON data
1174+
table=$(build_table "$data")
1175+
echo -e "$table"
1176+
1177+
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \
1178+
-H "Accept: application/vnd.github.v3+json" \
1179+
"https://api.github.com/repos/$LS_USER/$LS_REPO/issues/$PULL_REQUEST/comments" \
1180+
-d "{\\"body\\": \\"I am a bot, here are the test results for this PR: \\n${CI_URL}\\n${SHELLCHECK_URL}\\n${table}\\"}"
1181+
else
1182+
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \
1183+
-H "Accept: application/vnd.github.v3+json" \
1184+
"https://api.github.com/repos/$LS_USER/$LS_REPO/issues/$PULL_REQUEST/comments" \
1185+
-d "{\\"body\\": \\"I am a bot, here is the pushed image/manifest for this PR: \\n\\n\\`${GITHUBIMAGE}:${META_TAG}\\`\\"}"
1186+
fi
1187+
'''
1188+
}
1189+
}
1190+
}
1191+
}
11161192
sh '''#!/bin/bash
11171193
rm -rf /config/.ssh/id_sign
11181194
rm -rf /config/.ssh/id_sign.pub

ansible/roles/repository/templates/Jenkinsfile.j2

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,10 +1541,86 @@ EOF
15411541
{% endif %}
15421542
}
15431543
/* ######################
1544-
Send status to Discord
1544+
Comment on PR (failed test) and Send status to Discord
15451545
###################### */
15461546
post {
15471547
always {
1548+
if (env.EXIT_STATUS != "ABORTED"){
1549+
if (currentBuild.currentResult != "SUCCESS"){
1550+
if (env.GITHUBIMAGE =~ /lspipepr/){
1551+
if (env.CI_TEST_ATTEMPTED == "true"){
1552+
sh '''#! /bin/bash
1553+
# Function to retrieve JSON data from URL
1554+
get_json() {
1555+
local url="$1"
1556+
local response=$(curl -s "$url")
1557+
if [ $? -ne 0 ]; then
1558+
echo "Failed to retrieve JSON data from $url"
1559+
return 1
1560+
fi
1561+
local json=$(echo "$response" | jq .)
1562+
if [ $? -ne 0 ]; then
1563+
echo "Failed to parse JSON data from $url"
1564+
return 1
1565+
fi
1566+
echo "$json"
1567+
}
1568+
1569+
build_table() {
1570+
local data="$1"
1571+
1572+
# Get the keys in the JSON data
1573+
local keys=$(echo "$data" | jq -r 'to_entries | map(.key) | .[]')
1574+
1575+
# Check if keys are empty
1576+
if [ -z "$keys" ]; then
1577+
echo "JSON report data does not contain any keys or the report does not exist."
1578+
return 1
1579+
fi
1580+
1581+
# Build table header
1582+
local header="| Tag | Passed |\\n| --- | --- |\\n"
1583+
1584+
# Loop through the JSON data to build the table rows
1585+
local rows=""
1586+
for build in $keys; do
1587+
local status=$(echo "$data" | jq -r ".[\\"$build\\"].test_success")
1588+
if [ "$status" = "true" ]; then
1589+
status="✅"
1590+
else
1591+
status="❌"
1592+
fi
1593+
local row="| "$build" | "$status" |\\n"
1594+
rows="${rows}${row}"
1595+
done
1596+
1597+
local table="${header}${rows}"
1598+
local escaped_table=$(echo "$table" | sed 's/\"/\\\\"/g')
1599+
echo "$escaped_table"
1600+
}
1601+
1602+
if [[ "${CI}" = "true" ]]; then
1603+
# Retrieve JSON data from URL
1604+
data=$(get_json "$CI_JSON_URL")
1605+
# Create table from JSON data
1606+
table=$(build_table "$data")
1607+
echo -e "$table"
1608+
1609+
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \
1610+
-H "Accept: application/vnd.github.v3+json" \
1611+
"https://api.github.com/repos/$LS_USER/$LS_REPO/issues/$PULL_REQUEST/comments" \
1612+
-d "{\\"body\\": \\"I am a bot, here are the test results for this PR: \\n${CI_URL}\\n${SHELLCHECK_URL}\\n${table}\\"}"
1613+
else
1614+
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \
1615+
-H "Accept: application/vnd.github.v3+json" \
1616+
"https://api.github.com/repos/$LS_USER/$LS_REPO/issues/$PULL_REQUEST/comments" \
1617+
-d "{\\"body\\": \\"I am a bot, here is the pushed image/manifest for this PR: \\n\\n\\`${GITHUBIMAGE}:${META_TAG}\\`\\"}"
1618+
fi
1619+
'''
1620+
}
1621+
}
1622+
}
1623+
}
15481624
sh '''#!/bin/bash
15491625
rm -rf /config/.ssh/id_sign
15501626
rm -rf /config/.ssh/id_sign.pub

0 commit comments

Comments
 (0)