Enhance PR comment handling in entrypoint.sh to update existing comments or create new ones, and improve markdown report generation in markdown.go by adding optional root path and duration details.

This commit is contained in:
Luke Hagar
2025-09-11 20:00:18 +00:00
parent a0598ac3e9
commit 9148a9108c
2 changed files with 52 additions and 13 deletions

View File

@@ -87,17 +87,41 @@ if [ "${COMMENT_PR_ARG}" = "true" ]; then
echo "[slinky] GITHUB_REPOSITORY not set; skipping PR comment."
else
BODY_CONTENT="$(cat "${MD_OUT_ARG}")"
COMMENT_BODY="<!-- slinky-report -->\n${BODY_CONTENT}"
# Try to find an existing slinky comment to update
COMMENTS_JSON=$(curl -sS -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100") || COMMENTS_JSON="[]"
EXISTING_ID=$(printf '%s' "${COMMENTS_JSON}" | jq -r '[.[] | select((.body // "") | contains("<!-- slinky-report -->"))][0].id // empty')
if [ -n "${EXISTING_ID}" ]; then
# Update existing comment
curl -sS -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-X PATCH "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${EXISTING_ID}" \
-d "$(printf '{"body": %s}' "$(jq -Rs . <<EOF
${COMMENT_BODY}
EOF
)" )" >/dev/null || true
echo "[slinky] Updated existing PR comment #${EXISTING_ID}."
else
# Create new comment
curl -sS -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-X POST "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
-d "$(printf '{"body": %s}' "$(jq -Rs . <<EOF
${BODY_CONTENT}
${COMMENT_BODY}
EOF
)" )" >/dev/null || true
echo "[slinky] Posted PR comment to #${PR_NUMBER}."
fi
fi
fi
fi

View File

@@ -50,15 +50,30 @@ func WriteMarkdown(path string, results []web.Result, s Summary) (string, error)
var buf bytes.Buffer
// Title and summary
buf.WriteString("## Slinky Test Report\n\n")
// Optional root
if strings.TrimSpace(s.RootPath) != "." && strings.TrimSpace(s.RootPath) != "" && s.RootPath != string(filepath.Separator) {
buf.WriteString(fmt.Sprintf("- **Root**: %s\n", escapeMD(s.RootPath)))
buf.WriteString(fmt.Sprintf("- **Started**: %s\n", s.StartedAt.Format("2006-01-02 15:04:05 MST")))
buf.WriteString(fmt.Sprintf("- **Finished**: %s\n", s.FinishedAt.Format("2006-01-02 15:04:05 MST")))
buf.WriteString(fmt.Sprintf("- **Processed**: %d • **OK**: %d • **Fail**: %d\n", s.Processed, s.OK, s.Fail))
buf.WriteString(fmt.Sprintf("- **Rates**: avg %.1f/s • peak %.1f/s • low %.1f/s\n", s.AvgRPS, s.PeakRPS, s.LowRPS))
if s.JSONPath != "" {
base := filepath.Base(s.JSONPath)
buf.WriteString(fmt.Sprintf("- **JSON**: %s\n", escapeMD(base)))
}
// Last run and duration
buf.WriteString(fmt.Sprintf("- **Last Run**: %s\n", s.StartedAt.Format("2006-01-02 15:04:05 MST")))
dur := s.FinishedAt.Sub(s.StartedAt)
if dur < 0 {
dur = 0
}
buf.WriteString(fmt.Sprintf("- **Duration**: %s\n", dur.Truncate(time.Millisecond)))
// Totals
buf.WriteString(fmt.Sprintf("- **Total**: %d\n", s.Processed))
buf.WriteString(fmt.Sprintf("- **Pass**: %d\n", s.OK))
buf.WriteString(fmt.Sprintf("- **Fail**: %d\n", s.Fail))
// Rates only if non-zero
if !(s.AvgRPS == 0 && s.PeakRPS == 0 && s.LowRPS == 0) {
buf.WriteString(fmt.Sprintf("- **Rates**: avg %.1f/s • peak %.1f/s • low %.1f/s\n", s.AvgRPS, s.PeakRPS, s.LowRPS))
}
buf.WriteString("\n")
// Failures by URL