github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/bootstrap/paths.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  	"strings"
    23  )
    24  
    25  // Paths contains all of the upload/file paths used in a run of bootstrap
    26  type Paths struct {
    27  	Artifacts     string
    28  	BuildLog      string
    29  	PRPath        string
    30  	PRBuildLink   string
    31  	PRLatest      string
    32  	PRResultCache string
    33  	ResultCache   string
    34  	Started       string
    35  	Finished      string
    36  	Latest        string
    37  }
    38  
    39  // CIPaths returns a Paths for a CI Job
    40  func CIPaths(base, job, build string) *Paths {
    41  	return &Paths{
    42  		Artifacts:   filepath.Join(base, job, build, "artifacts"),
    43  		BuildLog:    filepath.Join(base, job, build, "build-log.txt"),
    44  		Finished:    filepath.Join(base, job, build, "finished.json"),
    45  		Latest:      filepath.Join(base, job, "latest-build.txt"),
    46  		ResultCache: filepath.Join(base, job, "jobResultsCache.json"),
    47  		Started:     filepath.Join(base, job, build, "started.json"),
    48  	}
    49  }
    50  
    51  // PRPaths returns a Paths for a Pull Request
    52  func PRPaths(base string, repos Repos, job, build string) (*Paths, error) {
    53  	if len(repos) == 0 {
    54  		return nil, fmt.Errorf("repos should not be empty")
    55  	}
    56  	repo := repos.Main()
    57  	// TODO(bentheelder): can we make this more generic?
    58  	var prefix string
    59  	if repo.Name == "k8s.io/kubernetes" || repo.Name == "kubernetes/kubernetes" {
    60  		prefix = ""
    61  	} else if strings.HasPrefix(repo.Name, "k8s.io/") {
    62  		prefix = repo.Name[len("k8s.io/"):]
    63  	} else if strings.HasPrefix(repo.Name, "kubernetes/") {
    64  		prefix = repo.Name[len("kubernetes/"):]
    65  	} else if strings.HasPrefix(repo.Name, "github.com/") {
    66  		prefix = strings.Replace(repo.Name[len("github.com/"):], "/", "_", -1)
    67  	} else {
    68  		prefix = strings.Replace(repo.Name, "/", "_", -1)
    69  	}
    70  	// Batch merges are those with more than one PR specified.
    71  	prNums := repo.PullNumbers()
    72  	var pull string
    73  	switch len(prNums) {
    74  	// TODO(bentheelder): jenkins/bootstrap.py would do equivalent to:
    75  	// `pull = filepath.Join(prefix, repo.Pull)` in this case, though we
    76  	// don't appear to ever have used this and probably shouldn't.
    77  	// Revisit if we want to error here or do the previous screwy behavior
    78  	case 0:
    79  		return nil, fmt.Errorf("expected at least one PR number")
    80  	case 1:
    81  		pull = filepath.Join(prefix, prNums[0])
    82  	default:
    83  		pull = filepath.Join(prefix, "batch")
    84  	}
    85  	prPath := filepath.Join(base, "pull", pull, job, build)
    86  	return &Paths{
    87  		Artifacts:     filepath.Join(prPath, "artifacts"),
    88  		BuildLog:      filepath.Join(prPath, "build-log.txt"),
    89  		PRPath:        prPath,
    90  		Finished:      filepath.Join(prPath, "finished.json"),
    91  		Latest:        filepath.Join(base, "directory", job, "latest-build.txt"),
    92  		PRBuildLink:   filepath.Join(base, "directory", job, build+".txt"),
    93  		PRLatest:      filepath.Join(base, "pull", pull, job, "latest-build.txt"),
    94  		PRResultCache: filepath.Join(base, "pull", pull, job, "jobResultsCache.json"),
    95  		ResultCache:   filepath.Join(base, "directory", job, "jobResultsCache.json"),
    96  		Started:       filepath.Join(prPath, "started.json"),
    97  	}, nil
    98  }
    99  
   100  // GubernatorBuildURL returns a Gubernator link for this build.
   101  func GubernatorBuildURL(paths *Paths) string {
   102  	logPath := filepath.Dir(paths.BuildLog)
   103  	if strings.HasPrefix(logPath, "gs:/") {
   104  		return strings.Replace(logPath, "gs:/", GubernatorBaseBuildURL, 1)
   105  	}
   106  	return logPath
   107  }