Monorepo for Tangled tangled.org

spindle: add environment variables to pipeline workflows #843

merged opened by evan.jarrett.net targeting master from evan.jarrett.net/core: spindle-env
Labels

None yet.

assignee

None yet.

Participants 2
AT URI
at://did:plc:pddp4xt5lgnv2qsegbzzs4xg/sh.tangled.repo.pull/3m72mdgh4sy22
+9 -63
Interdiff #0 โ†’ #1
spindle/engines/nixery/engine.go

This file has not been changed.

spindle/models/clone.go

This file has not been changed.

spindle/models/pipeline.go

This file has not been changed.

+9 -23
spindle/models/pipeline_env.go
··· 3 3 import ( 4 4 "strings" 5 5 6 + "github.com/go-git/go-git/v5/plumbing" 6 7 "tangled.org/core/api/tangled" 7 8 "tangled.org/core/workflow" 8 9 ) ··· 33 34 switch workflow.TriggerKind(tr.Kind) { 34 35 case workflow.TriggerKindPush: 35 36 if tr.Push != nil { 37 + refName := plumbing.ReferenceName(tr.Push.Ref) 38 + refType := "branch" 39 + if refName.IsTag() { 40 + refType = "tag" 41 + } 42 + 36 43 env["TANGLED_REF"] = tr.Push.Ref 37 - env["TANGLED_REF_NAME"] = extractRefName(tr.Push.Ref) 38 - env["TANGLED_REF_TYPE"] = extractRefType(tr.Push.Ref) 44 + env["TANGLED_REF_NAME"] = refName.Short() 45 + env["TANGLED_REF_TYPE"] = refType 39 46 env["TANGLED_SHA"] = tr.Push.NewSha 40 47 env["TANGLED_COMMIT_SHA"] = tr.Push.NewSha 41 48 } ··· 67 74 } 68 75 69 76 return env 70 - } 71 - 72 - // extractRefName extracts the branch or tag name from a full git ref. 73 - // e.g., "refs/heads/main" -> "main", "refs/tags/v1.0.0" -> "v1.0.0" 74 - func extractRefName(ref string) string { 75 - if branch, ok := strings.CutPrefix(ref, "refs/heads/"); ok { 76 - return branch 77 - } 78 - if tag, ok := strings.CutPrefix(ref, "refs/tags/"); ok { 79 - return tag 80 - } 81 - // Fallback: return as-is if not a standard ref format 82 - return ref 83 - } 84 - 85 - // extractRefType determines if a ref is a branch or tag. 86 - func extractRefType(ref string) string { 87 - if strings.HasPrefix(ref, "refs/tags/") { 88 - return "tag" 89 - } 90 - return "branch" 91 77 }
-40
spindle/models/pipeline_env_test.go
··· 258 258 t.Error("Should not have TANGLED_REF when push data is nil") 259 259 } 260 260 } 261 - 262 - func TestExtractRefName(t *testing.T) { 263 - tests := []struct { 264 - ref string 265 - expected string 266 - }{ 267 - {"refs/heads/main", "main"}, 268 - {"refs/heads/feature/my-feature", "feature/my-feature"}, 269 - {"refs/tags/v1.0.0", "v1.0.0"}, 270 - {"refs/tags/release-2023.01", "release-2023.01"}, 271 - {"some-other-ref", "some-other-ref"}, 272 - } 273 - 274 - for _, tt := range tests { 275 - result := extractRefName(tt.ref) 276 - if result != tt.expected { 277 - t.Errorf("extractRefName(%q) = %q, want %q", tt.ref, result, tt.expected) 278 - } 279 - } 280 - } 281 - 282 - func TestExtractRefType(t *testing.T) { 283 - tests := []struct { 284 - ref string 285 - expected string 286 - }{ 287 - {"refs/heads/main", "branch"}, 288 - {"refs/heads/feature/my-feature", "branch"}, 289 - {"refs/tags/v1.0.0", "tag"}, 290 - {"refs/tags/release-2023.01", "tag"}, 291 - {"some-other-ref", "branch"}, // defaults to branch 292 - } 293 - 294 - for _, tt := range tests { 295 - result := extractRefType(tt.ref) 296 - if result != tt.expected { 297 - t.Errorf("extractRefType(%q) = %q, want %q", tt.ref, result, tt.expected) 298 - } 299 - } 300 - }
spindle/server.go

This file has not been changed.

History

2 rounds 2 comments
sign up or login to add to the discussion
1 commit
expand
spindle: add environment variables to pipeline workflows
expand 1 comment

lgtm thanks! looks pretty clean.

pull request successfully merged
1 commit
expand
spindle: add environment variables to pipeline workflows
expand 1 comment

largely lgtm! thanks for picking this up. nits: extractRefName and extractRefType are available in go-git's plumbing module:

refName := plumbing.ReferenceName(r)

extractRefName(r) == refName.Short()

refName.IsTag()
refName.IsBranch()