Stitch any CI into Tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

tekton: merge user params with built-in params instead of overwriting

User-defined params (from workflow YAML) were silently replacing the
built-in commit/branch/actor params. Now both are merged into a single
sorted slice, with user params taking precedence on name collisions so
callers can still override the defaults when needed.

authored by

Vincent Demeester and committed by
Tangled
e0094490 27078622

+65 -5
+24 -3
provider_tekton.go
··· 370 370 if cfg.ServiceAccount != "" { 371 371 spec["serviceAccountName"] = cfg.ServiceAccount 372 372 } 373 + 374 + // Merge user-defined params with the built-in ones (commit, 375 + // branch, actor). User params that collide with a built-in name 376 + // win, so callers can override the defaults when the upstream 377 + // Tekton Pipeline expects a different shape. 373 378 if len(cfg.Params) > 0 { 374 - keys := make([]string, 0, len(cfg.Params)) 375 - for key := range cfg.Params { 379 + builtins := map[string]string{ 380 + "commit": commit, 381 + "branch": branch, 382 + "actor": actor, 383 + } 384 + 385 + // Collect all param names, user params override built-ins. 386 + merged := make(map[string]string, len(builtins)+len(cfg.Params)) 387 + for k, v := range builtins { 388 + merged[k] = v 389 + } 390 + for k, v := range cfg.Params { 391 + merged[k] = v 392 + } 393 + 394 + keys := make([]string, 0, len(merged)) 395 + for key := range merged { 376 396 keys = append(keys, key) 377 397 } 378 398 sort.Strings(keys) 399 + 379 400 params := make([]any, 0, len(keys)) 380 401 for _, key := range keys { 381 402 params = append(params, map[string]any{ 382 403 "name": key, 383 - "value": cfg.Params[key], 404 + "value": merged[key], 384 405 }) 385 406 } 386 407 spec["params"] = params
+41 -2
provider_tekton_test.go
··· 117 117 if !ok || sa != "runner" { 118 118 t.Fatalf("serviceAccountName = %q", sa) 119 119 } 120 + // With cfg.Params={"image": "example/app"}, the merged params 121 + // should contain the 3 built-ins (actor, branch, commit) plus 122 + // the user-supplied "image" — 4 total, sorted alphabetically. 120 123 params, ok := obj.NestedSlice("spec", "params") 121 - if !ok || len(params) != 1 { 122 - t.Fatalf("params = %+v", params) 124 + if !ok || len(params) != 4 { 125 + t.Fatalf("params count = %d, want 4: %+v", len(params), params) 123 126 } 124 127 if obj.GetAnnotations()[tektonAnnotationActor] != "did:plc:actor" || 125 128 obj.GetAnnotations()[tektonAnnotationCommit] != "abcdef" { 126 129 t.Fatalf("annotations missing identity: %+v", obj.GetAnnotations()) 130 + } 131 + } 132 + 133 + func TestTektonBuildPipelineRunParamsOverride(t *testing.T) { 134 + // When a user param collides with a built-in name, the user's 135 + // value should win so callers can customize what the upstream 136 + // Tekton Pipeline receives. 137 + cfg := &tektonWorkflowConfig{ 138 + Pipeline: "repo-ci", 139 + Params: map[string]string{ 140 + "commit": "user-override-sha", 141 + "extra": "bonus", 142 + }, 143 + } 144 + obj := buildTektonPipelineRun("ci", "run-1", cfg, 145 + "knot.example.com", "rkey-1", "did:plc:actor", 146 + "original-sha", "main", 147 + &tangled.Pipeline_Workflow{Name: "ci.yml"}, 148 + ) 149 + params, ok := obj.NestedSlice("spec", "params") 150 + if !ok { 151 + t.Fatal("params missing") 152 + } 153 + // Expect 4: actor, branch, commit (overridden), extra. 154 + if len(params) != 4 { 155 + t.Fatalf("params count = %d, want 4: %+v", len(params), params) 156 + } 157 + // Verify the user override took effect. 158 + for _, raw := range params { 159 + p, _ := raw.(map[string]any) 160 + if p["name"] == "commit" && p["value"] != "user-override-sha" { 161 + t.Fatalf( 162 + "commit param = %q, want user-override-sha", 163 + p["value"], 164 + ) 165 + } 127 166 } 128 167 } 129 168