Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions pkg/attestation/crafter/runners/azurepipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ 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 used to build the run link.
const (
azureCollectionURIEnv = "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"
azureServerURIEnv = "SYSTEM_TEAMFOUNDATIONSERVERURI"
azureTeamProjectEnv = "SYSTEM_TEAMPROJECT"
)

type AzurePipeline struct {
*Generic
}
Expand Down Expand Up @@ -64,23 +70,39 @@ 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(azureTeamProjectEnv)
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")
// 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()
Expand Down
104 changes: 100 additions & 4 deletions pkg/attestation/crafter/runners/azurepipeline_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -109,7 +109,102 @@ 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{
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{
"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{
azureTeamProjectEnv: "",
},
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() {
Expand Down Expand Up @@ -137,8 +232,9 @@ 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("SYSTEM_TEAMFOUNDATIONSERVERURI", "https://dev.azure.com/chainloop-test/")
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")
t.Setenv("SYSTEM_STAGEID", "96ac2280-0000-0000-99de-dd2da759617d")
t.Setenv("BUILD_REQUESTEDFORID", "4962d626-0000-0000-ae45-95ea268aa3e8")
Expand Down
Loading