fix(notify): open configured web run pages from ntfy

This commit is contained in:
2026-10-21 13:05:10 +01:00
parent 870c70aefa
commit 9ee2e003bf
12 changed files with 201 additions and 14 deletions
+25
View File
@@ -0,0 +1,25 @@
package server
import "testing"
func TestRunNotificationWebClick(t *testing.T) {
for _, tc := range []struct{ base, want string }{
{"https://pipelines.example.com", "https://pipelines.example.com/runs/20"},
{"http://10.0.0.75:8095/", "http://10.0.0.75:8095/runs/20"},
{"", "pipelines://run/20"}, // Existing Android-only installations.
{"javascript:alert(1)", ""},
{"https://user:password@example.com", ""},
{"https://example.com?token=secret", ""},
{"https://example.com/#fragment", ""},
{"https://example.com/subpath", ""}, // Web app uses origin-root routes.
{"/runs", ""},
{"https://example.com\r\nClick:bad", ""},
} {
t.Run(tc.base, func(t *testing.T) {
t.Setenv("PIPELINES_WEB_PUBLIC_URL", tc.base)
if got := runAttentionClick(20); got != tc.want {
t.Fatalf("click = %q, want %q", got, tc.want)
}
})
}
}
+12 -4
View File
@@ -73,7 +73,7 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N
return
}
receipt := &receipts[len(receipts)-1]
result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data, "")
result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data, runAttentionClick(id))
if err != nil {
receipt.Status = "failed"
receipt.Diagnostic = err.Error()
@@ -294,10 +294,18 @@ func (r *runner) forgetAttention(id int64) {
delete(r.attentionSent, id)
}
// runAttentionClick is the deep link a run notification carries so tapping it
// opens the run in the Android app, whose intent filter registers this scheme
// and host.
// Prefer the operator's authenticated web origin. Never infer a public address
// from a request Host, the unauthenticated API listener or the ntfy server URL.
// Older Android-only installations retain their deep link until configured.
func runAttentionClick(id int64) string {
if raw := os.Getenv("PIPELINES_WEB_PUBLIC_URL"); raw != "" {
base := publicNtfyBase(raw)
if base == "" {
log.Printf("notification web link unavailable: PIPELINES_WEB_PUBLIC_URL must be an HTTP(S) origin without credentials, query or fragment")
return ""
}
return fmt.Sprintf("%s/runs/%d", base, id)
}
return fmt.Sprintf("pipelines://run/%d", id)
}
+7 -2
View File
@@ -22,6 +22,7 @@ import (
)
func TestStageNotificationEventsAndArtifactAudit(t *testing.T) {
t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com/")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "notify.db"))
@@ -36,6 +37,9 @@ func TestStageNotificationEventsAndArtifactAudit(t *testing.T) {
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count := calls.Add(1)
if click := r.Header.Get("Click"); !strings.HasPrefix(click, "https://pipelines.example.com/runs/") {
t.Errorf("stage notification lacks web run link: %q", click)
}
filename, _ := new(mime.WordDecoder).DecodeHeader(r.Header.Get("Filename"))
body, _ := io.ReadAll(r.Body)
if filename == "report.txt" && string(body) != "created file" {
@@ -287,6 +291,7 @@ func TestRunAttentionEventMapping(t *testing.T) {
// the run and its pipeline, the click action opens the run, only waiting/failed
// publish, and a run that stays waiting is never republished.
func TestRunAttentionNotificationDelivery(t *testing.T) {
t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "attention.db"))
@@ -336,7 +341,7 @@ func TestRunAttentionNotificationDelivery(t *testing.T) {
if first.title != "Run needs your decision" || !strings.Contains(first.message, "Run #7") || !strings.Contains(first.message, "release") || !strings.Contains(first.message, "decision") || !strings.Contains(first.message, "approve") {
t.Fatalf("waiting notification does not identify the run/pipeline/need: %+v", first)
}
if first.click != "pipelines://run/7" {
if first.click != "https://pipelines.example.com/runs/7" {
t.Fatalf("waiting notification click does not open the run: %q", first.click)
}
if first.authorization != "" {
@@ -346,7 +351,7 @@ func TestRunAttentionNotificationDelivery(t *testing.T) {
if second.title != "Run failed" || !strings.Contains(second.message, "Run #7") || !strings.Contains(second.message, "release") || !strings.Contains(second.message, "deploy") || !strings.Contains(second.message, "provider returned 500") {
t.Fatalf("failed notification is not actionable: %+v", second)
}
if second.click != "pipelines://run/7" {
if second.click != "https://pipelines.example.com/runs/7" {
t.Fatalf("failed notification click does not open the run: %q", second.click)
}
select {