From 50e07f1e61aa58062f575753b6f3439f407fa4ca Mon Sep 17 00:00:00 2001 From: Miguel Martinez Trivino Date: Mon, 23 Nov 2026 12:13:20 +0100 Subject: [PATCH 1/2] fix(runners): build Azure Pipelines run link from the collection URI Use SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, falling back to SYSTEM_TEAMFOUNDATIONSERVERURI, so the run link also works on Azure DevOps Server, where the server URI has no collection segment. Return an empty link when the base URI, project or build ID is missing, and only add the job logs deep link when SYSTEM_JOBID is set. Fixes #3845 Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino Chainloop-Trace-Sessions: 349d093c-9e39-4126-b6b2-b2b6e878ba09 --- .../crafter/runners/azurepipeline.go | 44 ++++++--- .../crafter/runners/azurepipeline_test.go | 95 ++++++++++++++++++- 2 files changed, 124 insertions(+), 15 deletions(-) diff --git a/pkg/attestation/crafter/runners/azurepipeline.go b/pkg/attestation/crafter/runners/azurepipeline.go index 3b4ecf3df..2556c27c9 100644 --- a/pkg/attestation/crafter/runners/azurepipeline.go +++ b/pkg/attestation/crafter/runners/azurepipeline.go @@ -17,14 +17,19 @@ package runners import ( "context" - neturl "net/url" + "net/url" "os" - "path" schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners/commitverification" ) +// Azure Pipelines predefined variables that point to the organization or collection. +const ( + azureCollectionURIEnv = "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" + azureServerURIEnv = "SYSTEM_TEAMFOUNDATIONSERVERURI" +) + type AzurePipeline struct { *Generic } @@ -64,23 +69,38 @@ func (r *AzurePipeline) ListEnvVars() []*EnvVarDefinition { } } -func (r *AzurePipeline) RunURI() (url string) { - teamFoundationServerURI := os.Getenv("SYSTEM_TEAMFOUNDATIONSERVERURI") - definitionName := os.Getenv("SYSTEM_TEAMPROJECT") +// RunURI returns a browser link to the pipeline run, in the form +// {collectionURI}{project}/_build/results?buildId={buildID}. It returns an +// empty string if the required variables are missing. +func (r *AzurePipeline) RunURI() string { + // The collection URI includes the collection segment on Azure DevOps Server + // (https://host/tfs/DefaultCollection/), which the server URI does not have. + // On Azure DevOps Services both point to https://dev.azure.com/{org}/. + baseURI := os.Getenv(azureCollectionURIEnv) + if baseURI == "" { + baseURI = os.Getenv(azureServerURIEnv) + } + + project := os.Getenv("SYSTEM_TEAMPROJECT") buildID := os.Getenv("BUILD_BUILDID") - jobID := os.Getenv("SYSTEM_JOBID") + if baseURI == "" || project == "" || buildID == "" { + return "" + } - uri, err := neturl.Parse(teamFoundationServerURI) - if err != nil { + uri, err := url.Parse(baseURI) + if err != nil || uri.Scheme == "" || uri.Host == "" { return "" } - query := neturl.Values{} + query := url.Values{} query.Set("buildId", buildID) - query.Set("view", "logs") - query.Set("j", jobID) + // Link to the logs of this job when its ID is known. + if jobID := os.Getenv("SYSTEM_JOBID"); jobID != "" { + query.Set("view", "logs") + query.Set("j", jobID) + } - uri.Path = path.Join(uri.Path, definitionName, "_build/results") + uri = uri.JoinPath(project, "_build", "results") uri.RawQuery = query.Encode() return uri.String() diff --git a/pkg/attestation/crafter/runners/azurepipeline_test.go b/pkg/attestation/crafter/runners/azurepipeline_test.go index e0fd970dd..6b4b4c132 100644 --- a/pkg/attestation/crafter/runners/azurepipeline_test.go +++ b/pkg/attestation/crafter/runners/azurepipeline_test.go @@ -1,5 +1,5 @@ // -// Copyright 2023 The Chainloop Authors. +// Copyright 2023-2026 The Chainloop Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -109,7 +109,95 @@ func (s *azurePipelineSuite) TestResolveEnvVars() { } func (s *azurePipelineSuite) TestRunURI() { - s.Equal("https://dev.azure.com/chainloop-test/chainloop-tests/_build/results?buildId=6&j=12f1170f-0000-0000-20dd-22fc7dff55f9&view=logs", s.runner.RunURI()) + const jobID = "12f1170f-0000-0000-20dd-22fc7dff55f9" + + testCases := []struct { + name string + env map[string]string + want string + }{ + { + name: "azure devops services", + env: map[string]string{}, + want: "https://dev.azure.com/chainloop-test/chainloop-tests/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, + { + name: "azure devops server uses the collection uri, not the server uri", + env: map[string]string{ + azureServerURIEnv: "https://tfs.example.com/tfs/", + azureCollectionURIEnv: "https://tfs.example.com/tfs/DefaultCollection/", + }, + want: "https://tfs.example.com/tfs/DefaultCollection/chainloop-tests/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, + { + name: "falls back to the server uri when the collection uri is missing", + env: map[string]string{ + azureServerURIEnv: "https://dev.azure.com/fallback-org/", + azureCollectionURIEnv: "", + }, + want: "https://dev.azure.com/fallback-org/chainloop-tests/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, + { + name: "collection uri without trailing slash", + env: map[string]string{ + azureCollectionURIEnv: "https://tfs.example.com/tfs/DefaultCollection", + }, + want: "https://tfs.example.com/tfs/DefaultCollection/chainloop-tests/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, + { + name: "project name with spaces is escaped", + env: map[string]string{ + "SYSTEM_TEAMPROJECT": "My Project", + }, + want: "https://dev.azure.com/chainloop-test/My%20Project/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, + { + name: "no job id links to the run page", + env: map[string]string{ + "SYSTEM_JOBID": "", + }, + want: "https://dev.azure.com/chainloop-test/chainloop-tests/_build/results?buildId=6", + }, + { + name: "missing base uri", + env: map[string]string{ + azureServerURIEnv: "", + azureCollectionURIEnv: "", + }, + want: "", + }, + { + name: "relative base uri", + env: map[string]string{ + azureCollectionURIEnv: "not-a-url", + }, + want: "", + }, + { + name: "missing project", + env: map[string]string{ + "SYSTEM_TEAMPROJECT": "", + }, + want: "", + }, + { + name: "missing build id", + env: map[string]string{ + "BUILD_BUILDID": "", + }, + want: "", + }, + } + + for _, tc := range testCases { + s.Run(tc.name, func() { + for k, v := range tc.env { + s.T().Setenv(k, v) + } + + s.Equal(tc.want, s.runner.RunURI()) + }) + } } func (s *azurePipelineSuite) TestRunnerName() { @@ -138,7 +226,8 @@ func (s *azurePipelineSuite) SetupTest() { t.Setenv("SYSTEM_COLLECTIONID", "e2dadf5b-9a6d-0000-0000-89ad0786f16e") t.Setenv("SYSTEM_TEAMPROJECTID", "e0730109-da00-0000-0000-80abab2033a2") t.Setenv("SYSTEM_TEAMPROJECT", "chainloop-tests") - t.Setenv("SYSTEM_TEAMFOUNDATIONSERVERURI", "https://dev.azure.com/chainloop-test/") + t.Setenv(azureServerURIEnv, "https://dev.azure.com/chainloop-test/") + t.Setenv(azureCollectionURIEnv, "https://dev.azure.com/chainloop-test/") t.Setenv("SYSTEM_DEFINITIONNAME", "chainloop-tests") t.Setenv("SYSTEM_STAGEID", "96ac2280-0000-0000-99de-dd2da759617d") t.Setenv("BUILD_REQUESTEDFORID", "4962d626-0000-0000-ae45-95ea268aa3e8") From 61a37b1e503b1f8f0d1d22df41138aca69e71ff7 Mon Sep 17 00:00:00 2001 From: Miguel Martinez Trivino Date: Mon, 23 Nov 2026 12:23:02 +0100 Subject: [PATCH 2/2] fix(runners): escape the Azure project name before joining the path URL.JoinPath expects escaped elements and silently keeps the base path when an element does not unescape, so a project name with a percent sign dropped the project and results route from the run link. Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino Chainloop-Trace-Sessions: 349d093c-9e39-4126-b6b2-b2b6e878ba09 --- pkg/attestation/crafter/runners/azurepipeline.go | 8 +++++--- .../crafter/runners/azurepipeline_test.go | 13 ++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/attestation/crafter/runners/azurepipeline.go b/pkg/attestation/crafter/runners/azurepipeline.go index 2556c27c9..7f18fd668 100644 --- a/pkg/attestation/crafter/runners/azurepipeline.go +++ b/pkg/attestation/crafter/runners/azurepipeline.go @@ -24,10 +24,11 @@ import ( "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners/commitverification" ) -// Azure Pipelines predefined variables that point to the organization or collection. +// Azure Pipelines predefined variables used to build the run link. const ( azureCollectionURIEnv = "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI" azureServerURIEnv = "SYSTEM_TEAMFOUNDATIONSERVERURI" + azureTeamProjectEnv = "SYSTEM_TEAMPROJECT" ) type AzurePipeline struct { @@ -81,7 +82,7 @@ func (r *AzurePipeline) RunURI() string { baseURI = os.Getenv(azureServerURIEnv) } - project := os.Getenv("SYSTEM_TEAMPROJECT") + project := os.Getenv(azureTeamProjectEnv) buildID := os.Getenv("BUILD_BUILDID") if baseURI == "" || project == "" || buildID == "" { return "" @@ -100,7 +101,8 @@ func (r *AzurePipeline) RunURI() string { query.Set("j", jobID) } - uri = uri.JoinPath(project, "_build", "results") + // JoinPath expects escaped elements and drops the path if one does not unescape. + uri = uri.JoinPath(url.PathEscape(project), "_build", "results") uri.RawQuery = query.Encode() return uri.String() diff --git a/pkg/attestation/crafter/runners/azurepipeline_test.go b/pkg/attestation/crafter/runners/azurepipeline_test.go index 6b4b4c132..1bac06719 100644 --- a/pkg/attestation/crafter/runners/azurepipeline_test.go +++ b/pkg/attestation/crafter/runners/azurepipeline_test.go @@ -147,10 +147,17 @@ func (s *azurePipelineSuite) TestRunURI() { { name: "project name with spaces is escaped", env: map[string]string{ - "SYSTEM_TEAMPROJECT": "My Project", + azureTeamProjectEnv: "My Project", }, want: "https://dev.azure.com/chainloop-test/My%20Project/_build/results?buildId=6&j=" + jobID + "&view=logs", }, + { + name: "project name with percent sign is escaped", + env: map[string]string{ + azureTeamProjectEnv: "100% done", + }, + want: "https://dev.azure.com/chainloop-test/100%25%20done/_build/results?buildId=6&j=" + jobID + "&view=logs", + }, { name: "no job id links to the run page", env: map[string]string{ @@ -176,7 +183,7 @@ func (s *azurePipelineSuite) TestRunURI() { { name: "missing project", env: map[string]string{ - "SYSTEM_TEAMPROJECT": "", + azureTeamProjectEnv: "", }, want: "", }, @@ -225,7 +232,7 @@ func (s *azurePipelineSuite) SetupTest() { t.Setenv("AGENT_VERSION", "3.220.5") t.Setenv("SYSTEM_COLLECTIONID", "e2dadf5b-9a6d-0000-0000-89ad0786f16e") t.Setenv("SYSTEM_TEAMPROJECTID", "e0730109-da00-0000-0000-80abab2033a2") - t.Setenv("SYSTEM_TEAMPROJECT", "chainloop-tests") + t.Setenv(azureTeamProjectEnv, "chainloop-tests") t.Setenv(azureServerURIEnv, "https://dev.azure.com/chainloop-test/") t.Setenv(azureCollectionURIEnv, "https://dev.azure.com/chainloop-test/") t.Setenv("SYSTEM_DEFINITIONNAME", "chainloop-tests")