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
@@ -98,9 +98,16 @@ fails. Nothing else notifies — a queued, running, succeeded, canceled or inter
run sends nothing. You do not author these; they are the operator default, so a run
you never wired still reaches the operator.
The payload names the run and its pipeline and carries a `click` action
`pipelines://run/<id>` that opens that run in the Android app (the app declares the
scheme). A run that stays waiting is not republished on a read or restart, and a
The payload names the run and its pipeline. When the operator configures
`PIPELINES_WEB_PUBLIC_URL` on the server, run notifications and stage hooks carry
the authenticated web link `<origin>/runs/<id>`. Native installs default this to
their advertised web address; custom/VPN domains require the operator's actual
phone-reachable URL. Never use the API listener or ntfy URL, nor put credentials
in the click link. Existing Android-only installations without that setting keep
`pipelines://run/<id>`. Invalid origins omit the link and log a warning. Existing
delivered alerts are not rewritten. Do not change service settings or send a test
notification merely to author a workflow. A run that stays waiting is not
republished on a read or restart, and a
stage `notify` policy that already covers the same event (`on=waiting`, or a failure
hook) suppresses the run-level default, so one genuine event means one notification.
A delivery failure is logged, never fails the run, and is not retried.
@@ -110,4 +117,3 @@ a run waiting for a decision, then a failed run, then a run that finished since
operator last looked. It is derived from the run events the server already pushes
(no extra request), an entry clears when the run is resolved or the operator opens
it, and it can be deliberately dismissed. It is not a new place to start a run.
+1 -1
View File
@@ -78,7 +78,7 @@ func (d serviceDefinition) render(platform string) (string, error) {
// Only explicitly supported settings are captured, never the complete environment
// or provider credentials. File-based notification credentials remain references.
var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_OPENCODE_ENDPOINT_USERNAME", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE"}
var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_OPENCODE_ENDPOINT_USERNAME", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE", "PIPELINES_WEB_PUBLIC_URL"}
func envValues(env map[string]string) []string {
var values []string
+15
View File
@@ -111,6 +111,21 @@ func TestServiceWritesRefuseUnmanagedFilesAndSymlinks(t *testing.T) {
}
}
func TestServicePreservesNotificationWebOrigin(t *testing.T) {
d := serviceDefinition{Binary: "/bin/server", Directory: "/repo", Environment: map[string]string{
"PIPELINES_WEB_PUBLIC_URL": "https://pipelines.example.com",
}}
for _, platform := range []string{"linux", "darwin"} {
text, err := d.render(platform)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(text, "PIPELINES_WEB_PUBLIC_URL") || !strings.Contains(text, "https://pipelines.example.com") {
t.Fatalf("%s lost notification web origin", platform)
}
}
}
func TestServiceConfigurationPreviewAndLifecycleWithMockOSManager(t *testing.T) {
t.Setenv("PIPELINES_PREFIX", "")
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
+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 {