flammenwehrfer.sh 4.64 KB
Newer Older
uxwlu's avatar
uxwlu committed
1
2
#!/bin/bash

3
# This script maintains an issue in a repository informing its maintainers of failing tests
4
# It is supposed to be run by CI
uxwlu's avatar
uxwlu committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#  _   _
# | | | |___  __ _  __ _  ___
# | | | / __|/ _` |/ _` |/ _ \
# | |_| \__ \ (_| | (_| |  __/
#  \___/|___/\__,_|\__, |\___|
#                  |___/
if [[ "$GITHUB_PATH" == "" ]] || [[ "$AUTH" == "" ]]; then
  echo "GITHUB_PATH ('user/repo') and AUTH ('user:PAT') env vars must be set"
  exit 1
fi


#   ____                _              _
#  / ___|___  _ __  ___| |_ __ _ _ __ | |_ ___
# | |   / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
# | |__| (_) | | | \__ \ || (_| | | | | |_\__ \
#  \____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
#
BODY="Greetings traveller,\n\nsticks and stones won't break your bones but https://git.scc.kit.edu/IPDSnelting/mjtest-tests broke your compiler :(\n\nHave fun and take good care of it :lady_beetle:"
25
SUCCESS_CLOSING_BODY="Greetings traveller,\n\nall tests succeeded. Well done!\n\n<img src=\"https://c.tenor.com/EHpQyFzthQ4AAAAd/kalm-calm.gif\" width=\"300px\">\n\nEnjoy your day :lady_beetle:"
uxwlu's avatar
uxwlu committed
26
27
28
29
30
31
32
33
34
GITHUB_URL="https://api.github.com/repos/$GITHUB_PATH"


#  _____         _   _
# |_   _|__  ___| |_(_)_ __   __ _
#   | |/ _ \/ __| __| | '_ \ / _` |
#   | |  __/\__ \ |_| | | | | (_| |  _   _   _
#   |_|\___||___/\__|_|_| |_|\__, | (_) (_) (_)
#                            |___/
35
# Create copy of stdout to capture and stream stdout in subshell
36
# Set pipefail to get mjtest exit code instead of tee exit code
37
exec 3>&1
38
set -o pipefail
39
MJ_OUTPUT="$(MJ_TIMEOUT=30 MJ_BIG_TIMEOUT=180 /mjtest/mjt.py lexer syntax ast semantic compile-firm-only compile-only compile-firm compile /compiler-under-test/run 2>&1 | tee /dev/fd/3)"
uxwlu's avatar
uxwlu committed
40
MJ_EXIT_CODE="$?"
41
# Close copy, unset pipefail
42
exec 3>&-
43
set +o pipefail
uxwlu's avatar
uxwlu committed
44

45
46
47
48
if [[ "$MJ_EXIT_CODE" == "0" ]]; then
    echo "Test successful"
fi

uxwlu's avatar
uxwlu committed
49
50
if [[ "$USE_FLAMMENWEHRFER" != 1 ]] || [[ "$CI_COMMIT_BRANCH" != "master" ]]; then
  echo "Flammenwehrfer disabled, checked for branch '$CI_COMMIT_BRANCH'"
51
  echo -e "Output:\n$MJ_OUTPUT"
uxwlu's avatar
uxwlu committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  exit $MJ_EXIT_CODE
fi


#  ____                        _        _
# |  _ \ ___ _ __   ___    ___| |_ __ _| |_ ___
# | |_) / _ \ '_ \ / _ \  / __| __/ _` | __/ _ \
# |  _ <  __/ |_) | (_) | \__ \ || (_| | ||  __/
# |_| \_\___| .__/ \___/  |___/\__\__,_|\__\___|
#           |_|
FETCHED_ISSUES="$(curl -Ss \
                      -H "Accept: application/vnd.github.v3+json" \
                      https://api.github.com/repos/$GITHUB_PATH/issues\?creator\=Flammenwehrfer\&state\=all)"
ISSUE_COUNT="$(echo "$FETCHED_ISSUES" | jq "length")"
echo "Found $ISSUE_COUNT Github issue(s)"


#  ____
# / ___| _   _  ___ ___ ___  ___ ___
# \___ \| | | |/ __/ __/ _ \/ __/ __|
#  ___) | |_| | (_| (_|  __/\__ \__ \
# |____/ \__,_|\___\___\___||___/___/
#
if [[ "$MJ_EXIT_CODE" == "0" ]]; then
76
    echo "Test still successful"
uxwlu's avatar
uxwlu committed
77
78
79
80
81
82
83
84
    if [[ "$ISSUE_COUNT" > 0 ]] && [[ "$(echo "$FETCHED_ISSUES" | jq ".[0].state")" == '"open"' ]]; then
        ISSUE_NUMBER="$(echo "$FETCHED_ISSUES" | jq ".[0].number")"
        echo "Closing issue $ISSUE_NUMBER!"
        curl -Ss \
          -X PATCH \
          -H "Accept: application/vnd.github.v3+json" \
          -u "$AUTH" \
          "$GITHUB_URL/issues/$ISSUE_NUMBER" \
85
          -d "{\"state\":\"closed\", \"body\":$(echo -e "$SUCCESS_CLOSING_BODY" | jq -R -s)}" \
uxwlu's avatar
uxwlu committed
86
87
          -o /dev/null
    fi
88
    exit $MJ_EXIT_CODE
uxwlu's avatar
uxwlu committed
89
90
91
92
93
94
95
96
97
98
fi


#  _____     _ _
# |  ___|_ _(_) |_   _ _ __ ___
# | |_ / _` | | | | | | '__/ _ \
# |  _| (_| | | | |_| | | |  __/
# |_|  \__,_|_|_|\__,_|_|  \___|
#
echo "Test failed"
99
echo -e "Output:\n$MJ_OUTPUT"
uxwlu's avatar
uxwlu committed
100

101
MJ_OUTPUT="$(echo "$MJ_OUTPUT" | head -c 60000)"
uxwlu's avatar
uxwlu committed
102
103
CODE_BLOCK_HEADER='```'
BODY="$BODY\n\n----\n\n<details>\n<summary>mjtest output</summary>\n\n$CODE_BLOCK_HEADER\n$MJ_OUTPUT\n$CODE_BLOCK_HEADER\n\n</details>"
104
BODY="$BODY\n\n----\n\n$CODE_BLOCK_HEADER\n$(fortune | cowsay -n)\n$CODE_BLOCK_HEADER"
uxwlu's avatar
uxwlu committed
105
106
107
108
109
110
111
112
113
114
115

if [[ "$ISSUE_COUNT" == "0" ]]; then
    echo "Found no issues, creating new one"
    # Create new issue
    JSON="
      {
        \"title\":  \"Failing test detected\",
        \"body\" :  $(echo -e "$BODY" | jq -R -s),
        \"labels\": [\"test-failure-bot\"]
      }
    "
116
    echo "$JSON" | \
uxwlu's avatar
uxwlu committed
117
118
119
120
121
    curl -Ss \
        -X POST \
        -H "Accept: application/vnd.github.v3+json" \
        -u "$AUTH" \
        "$GITHUB_URL/issues" \
122
123
        --data-binary "@-"
    exit $MJ_EXIT_CODE
uxwlu's avatar
uxwlu committed
124
125
126
fi

ISSUE_NUMBER="$(echo "$FETCHED_ISSUES" | jq ".[0].number")"
127
echo -e "\n\n"
128
echo "Editing issue $ISSUE_NUMBER"
uxwlu's avatar
uxwlu committed
129

130
131
JSON="{\"state\":\"open\", \"body\":$(echo -e "$BODY" | jq -R -s)}"
echo "$JSON" | \
uxwlu's avatar
uxwlu committed
132
133
134
135
136
curl -Ss \
  -X PATCH \
  -H "Accept: application/vnd.github.v3+json" \
  -u "$AUTH" \
  "$GITHUB_URL/issues/$ISSUE_NUMBER" \
137
  --data-binary "@-" \
uxwlu's avatar
uxwlu committed
138
139
140
  -o /dev/null

exit $MJ_EXIT_CODE