Skip to content

refactor(status): eliminate duplicate endpoint health check logic#111

Merged
ewega merged 2 commits intomainfrom
copilot/refactor-duplicate-status-functions
Mar 4, 2026
Merged

refactor(status): eliminate duplicate endpoint health check logic#111
ewega merged 2 commits intomainfrom
copilot/refactor-duplicate-status-functions

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

pingEndpoint and checkEndpointHealth duplicated the same URL construction, HTTP client setup, and request logic — differing only in return type.

Changes

  • New endpointStatusCode(url, kind string) int — single shared implementation; builds the check URL, fires the request with an 8s timeout, returns the status code or 0 on connection failure
  • checkEndpointHealth — delegates to endpointStatusCode, returns code >= 200 && code < 400
  • pingEndpoint — delegates to endpointStatusCode, maps 0→"❌", 2xx/3xx→"✅", other→"⚠️ (code)"
func endpointStatusCode(url, kind string) int { /* single HTTP impl */ }

func checkEndpointHealth(url, kind string) bool {
    return endpointStatusCode(url, kind) >= 200 && endpointStatusCode(url, kind) < 400
}

func pingEndpoint(url, kind string) string {
    code := endpointStatusCode(url, kind)
    if code == 0 { return "❌" }
    if code >= 200 && code < 400 { return "✅" }
    return fmt.Sprintf("⚠️  (%d)", code)
}

runStatus output (emojis and JSON fields) is unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Merge duplicate pingEndpoint/checkEndpointHealth in status.go</issue_title>
<issue_description>## Problem

status.go has two near-identical functions:

  • pingEndpoint(url, kind) string — returns emoji (✅, ❌, ⚠️)
  • checkEndpointHealth(url, kind) bool — returns true/false

Both build the check URL the same way (append /ping for backend, /api/health for Grafana), create an http.Client with the same timeout (8s), make the same GET request, and check the same status code range. The only difference is the return type.

Fix

Refactor into one function and have the other call it:

`go
func checkEndpointHealth(url string, kind string) bool {
// ... shared logic ...
}

func pingEndpoint(url string, kind string) string {
if checkEndpointHealth(url, kind) {
return "✅"
}
return "❌"
}
`

(The ⚠️ case for non-2xx status codes would need a slightly different signature, but the core logic is shared.)

Acceptance Criteria

  • One shared implementation for endpoint health checking
  • status command output unchanged (same emojis, same JSON fields)
  • go build ./... and go test ./... pass</issue_description>

<agent_instructions>Merge pingEndpoint and checkEndpointHealth in cmd/status.go. Make checkEndpointHealth the single source of truth and have pingEndpoint call it. Note: PR #84 just merged and changed separator widths in status.go — branch from latest main. The ⚠️ case for non-2xx can return a tri-state or use a simple approach: checkEndpointHealth returns bool, pingEndpoint maps true→"✅" false→"❌". Keep the external behavior of runStatus output identical. Build with go build ./... and test with go test ./....</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ewega **Agent context:** Merge `pingEndpoint` and `checkEndpointHealth` in `cmd/status.go`. Make `checkEndpointHealth` the single source of truth and have `pingEndpoint` call it. Note: PR #84 just merged and changed separator widths in status.go — branch from latest `main`. The ⚠️ case for non-2xx can return a tri-state (bool + separate warn check) or use an enum. Keep the external behavior of `runStatus` output identical. Build with `go build ./...` and test with `go test ./...`.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: ewega <26189114+ewega@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicate pingEndpoint and checkEndpointHealth functions refactor(status): eliminate duplicate endpoint health check logic Mar 4, 2026
Copilot finished work on behalf of ewega March 4, 2026 12:05
@ewega ewega marked this pull request as ready for review March 4, 2026 12:10
Copilot AI review requested due to automatic review settings March 4, 2026 12:10
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR eliminates duplicate HTTP health-check logic in cmd/status.go by introducing a new endpointStatusCode(url, kind string) int helper that centralizes URL construction, HTTP client setup, request execution, and error handling. Both pingEndpoint (returns emoji string) and checkEndpointHealth (returns bool) now delegate to this single shared implementation.

Changes:

  • Introduces endpointStatusCode as the single HTTP implementation, returning the HTTP status code or 0 on connection failure
  • Refactors checkEndpointHealth to delegate to endpointStatusCode (single call, stores in code)
  • Refactors pingEndpoint to delegate to endpointStatusCode, correctly distinguishing connection failure (0), success (2xx/3xx), and unexpected codes (⚠️ (code))

@ewega ewega merged commit ff93483 into main Mar 4, 2026
8 checks passed
@ewega ewega deleted the copilot/refactor-duplicate-status-functions branch March 4, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merge duplicate pingEndpoint/checkEndpointHealth in status.go

3 participants