istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/prow/util.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package prow
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"istio.io/istio/pkg/env"
    23  )
    24  
    25  var (
    26  	runningInCI   = env.Register("CI", false, "If true, indicates we are running in CI").Get()
    27  	artifactsBase = env.Register("PROW_ARTIFACTS_BASE", "https://gcsweb.istio.io/gcs/istio-prow", "the base url for prow artifacts").Get()
    28  	// https://github.com/kubernetes/test-infra/blob/master/prow/jobs.md#job-environment-variables
    29  	jobType    = env.Register("JOB_TYPE", "presubmit", "type of job").Get()
    30  	jobName    = env.Register("JOB_NAME", "", "name of job").Get()
    31  	pullNumber = env.Register("PULL_NUMBER", "", "PR of job").Get()
    32  	repoName   = env.Register("REPO_NAME", "istio", "repo name").Get()
    33  	repoOwner  = env.Register("REPO_OWNER", "istio", "repo owner").Get()
    34  	buildID    = env.Register("BUILD_ID", "", "build id").Get()
    35  	artifacts  = env.Register("ARTIFACTS", "", "artifacts base").Get()
    36  )
    37  
    38  func ArtifactsURL(filename string) string {
    39  	if !runningInCI {
    40  		return filename
    41  	}
    42  	name := "artifacts/" + strings.TrimPrefix(filename, artifacts+"/")
    43  	if jobType == "presubmit" {
    44  		return join(artifactsBase, "pr-logs/pull", fmt.Sprintf("%s_%s", repoOwner, repoName), pullNumber, jobName, buildID, name)
    45  	}
    46  	return join(artifactsBase, "logs", jobName, buildID, name)
    47  }
    48  
    49  func join(base string, elem ...string) string {
    50  	res, _ := url.JoinPath(base, elem...)
    51  	return res
    52  }