-
3
#!/usr/bin/env sh
-
9
set -eu
-
-
9
SUITE_PATH="${1:-}"
-
-
11
[ -n "$SUITE_PATH" ] || { echo "ERROR: suite_path argument required" >&2; exit 1; }
-
10
[ -n "${GITEA_API_URL:-}" ] || { echo "ERROR: GITEA_API_URL is not set" >&2; exit 1; }
-
9
[ -n "${GIT_PAGES_URL:-}" ] || { echo "ERROR: GIT_PAGES_URL is not set" >&2; exit 1; }
-
8
[ -n "${GIT_PAGES_PUBLISH_TOKEN:-}" ] || { echo "ERROR: GIT_PAGES_PUBLISH_TOKEN is not set" >&2; exit 1; }
-
7
[ -n "${GITHUB_REPOSITORY:-}" ] || { echo "ERROR: GITHUB_REPOSITORY is not set" >&2; exit 1; }
-
4
[ -n "${GITHUB_SHA:-}" ] || { echo "ERROR: GITHUB_SHA is not set" >&2; exit 1; }
-
-
4
OWNER="${GITHUB_REPOSITORY%%/*}"
-
4
REPO="${GITHUB_REPOSITORY##*/}"
-
12
SHA8=$(echo "$GITHUB_SHA" | cut -c1-8)
-
4
PAGES_USER="${GIT_PAGES_PUBLISH_USER:-publish}"
-
4
REPORT_DIR="reports/${SHA8}/${SUITE_PATH%/}"
-
4
REPORT_BASE="${GIT_PAGES_URL}/${OWNER}/${REPO}/reports/${SHA8}"
-
-
6
[ -d "$REPORT_DIR" ] || { echo "ERROR: not a directory: $REPORT_DIR" >&2; exit 1; }
-
-
3
PUBLISH_SITE_URL="${GIT_PAGES_URL}/"
-
-
6
WORK=$(mktemp -d)
-
6
TAR=$(mktemp)
-
3
trap 'rm -rf "$WORK" "$TAR"' EXIT
-
-
3
RELPATH="${REPORT_DIR#reports/${SHA8}/}"
-
6
if [ "$RELPATH" != "$REPORT_DIR" ] && [ -n "$RELPATH" ]; then
-
3
TARGET="$WORK/${OWNER}/${REPO}/reports/${SHA8}/${RELPATH}"
-
else
-
TARGET="$WORK/${OWNER}/${REPO}/reports/${SHA8}"
-
fi
-
3
mkdir -p "$TARGET"
-
3
cp -a "$REPORT_DIR/." "$TARGET/"
-
-
3
if [ ! -f "$TARGET/index.html" ]; then
-
1
ITEM_LIST=""
-
1
ITEM_COUNT=0
-
-
1
for f in "$TARGET"/*; do
-
1
[ -f "$f" ] || continue
-
2
base=$(basename "$f")
-
1
[ "$base" = "index.html" ] && continue
-
1
ITEM_LIST="${ITEM_LIST}file:${base}
-
1
"
-
1
ITEM_COUNT=$((ITEM_COUNT + 1))
-
1
done
-
1
-
1
for d in "$TARGET"/*/; do
-
2
[ -d "$d" ] || continue
-
base=$(basename "$d")
-
[ -f "$d/index.html" ] || continue
-
1
ITEM_LIST="${ITEM_LIST}dir:${base}
-
1
"
-
1
ITEM_COUNT=$((ITEM_COUNT + 1))
-
1
done
-
1
-
1
if [ "$ITEM_COUNT" -gt 1 ]; then
-
{
-
echo '<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">'
-
echo "<title>Test report ${SHA8}</title>"
-
echo '<style>body{font-family:sans-serif;margin:2em;max-width:960px}'
-
echo 'h1{color:#1e293b}ul{list-style:none;padding:0}'
-
echo 'li{margin:.5em 0;padding:.5em;background:#f8fafc;border-radius:6px}'
-
echo 'a{color:#2563eb;text-decoration:none}a:hover{text-decoration:underline}'
-
echo '</style></head><body>'
-
echo "<h1>Test report <code>${SHA8}</code></h1><ul>"
-
-
echo "$ITEM_LIST" | while IFS= read -r item; do
-
[ -z "$item" ] && continue
-
item_type=$(echo "$item" | cut -d: -f1)
-
item_name=$(echo "$item" | cut -d: -f2-)
-
label=$(echo "$item_name" | sed -e 's/\.[^.]*$//' -e 's/[-_]/ /g')
-
first=$(echo "$label" | cut -c1 | tr '[:lower:]' '[:upper:]')
-
rest=$(echo "$label" | cut -c2-)
-
if [ "$item_type" = "file" ]; then
-
echo "<li><a href=\"$item_name\">${first}${rest}</a></li>"
-
else
-
echo "<li><a href=\"$item_name/index.html\">${first}${rest}</a></li>"
-
fi
-
done
-
-
echo '</ul></body></html>'
-
} > "$TARGET/index.html"
-
fi
-
fi
-
-
6
cat > "$TARGET/.meta" <<EOF
-
6
{"branch":"${GITHUB_REF_NAME:-}","sha":"${GITHUB_SHA}","published_at":"$(date -u +%Y-%m-%dT%H:%M:%SZ)"}
-
6
EOF
-
9
find "$WORK/$OWNER" \( -type f -o -type l \) -print | sed "s|^${WORK}/||" | tar -cf "$TAR" -C "$WORK" -T -
-
-
publish() {
-
3
method="$1"
-
3
curl -sS -X "$method" "$PUBLISH_SITE_URL" \
-
3
-u "${PAGES_USER}:${GIT_PAGES_PUBLISH_TOKEN}" \
-
3
-H "Content-Type: application/x-tar" \
-
3
-H "Atomic: no" \
-
3
-H "Create-Parents: yes" \
-
3
--data-binary @"$TAR" \
-
3
-o /tmp/git-pages-publish-response.txt \
-
3
-w "%{http_code}"
-
}
-
-
6
HTTP_CODE=$(publish PATCH)
-
-
3
case "$HTTP_CODE" in
-
200|201|204) ;;
-
*)
-
1
echo "ERROR: git-pages publish HTTP ${HTTP_CODE}" >&2
-
1
cat /tmp/git-pages-publish-response.txt >&2
-
1
exit 1
-
;;
-
esac
-
-
2
echo "$REPORT_BASE"