mirror of
https://github.com/argoproj/argo-cd
synced 2026-05-23 17:28:44 +00:00
fix: avoid panic in badge handler (#3741)
This commit is contained in:
parent
e143fb4cb2
commit
460f6653dc
2 changed files with 21 additions and 1 deletions
|
|
@ -123,7 +123,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
badge = svgWidthPattern.ReplaceAllString(badge, fmt.Sprintf(`<svg width="%d" $2`, svgWidthWithRevision))
|
||||
badge = displayNonePattern.ReplaceAllString(badge, `display="inline"`)
|
||||
badge = revisionRectColorPattern.ReplaceAllString(badge, fmt.Sprintf(`id="revisionRect" fill="%s" $2`, rightColorString))
|
||||
badge = replaceFirstGroupSubMatch(revisionTextPattern, badge, fmt.Sprintf("(%s)", revision[:7]))
|
||||
shortRevision := revision
|
||||
if len(shortRevision) > 7 {
|
||||
shortRevision = shortRevision[:7]
|
||||
}
|
||||
badge = replaceFirstGroupSubMatch(revisionTextPattern, badge, fmt.Sprintf("(%s)", shortRevision))
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
|
|
|
|||
|
|
@ -111,6 +111,22 @@ func TestHandlerRevisionIsEnabledNoOperationState(t *testing.T) {
|
|||
assert.NotContains(t, response, "(aa29b85)")
|
||||
}
|
||||
|
||||
func TestHandlerRevisionIsEnabledShortCommitSHA(t *testing.T) {
|
||||
app := testApp.DeepCopy()
|
||||
app.Status.OperationState.SyncResult.Revision = "abc"
|
||||
|
||||
settingsMgr := settings.NewSettingsManager(context.Background(), fake.NewSimpleClientset(&argoCDCm, &argoCDSecret), "default")
|
||||
handler := NewHandler(appclientset.NewSimpleClientset(app), settingsMgr, "default")
|
||||
req, err := http.NewRequest("GET", "/api/badge?name=testApp&revision=true", nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
response := rr.Body.String()
|
||||
assert.Contains(t, response, "(abc)")
|
||||
}
|
||||
|
||||
func TestHandlerFeatureIsDisabled(t *testing.T) {
|
||||
|
||||
argoCDCmDisabled := argoCDCm.DeepCopy()
|
||||
|
|
|
|||
Loading…
Reference in a new issue