github.com/buildtool/build-tools@v0.2.29-0.20240322150259-6a1d0a553c23/pkg/ci/ci.go (about)

     1  // MIT License
     2  //
     3  // Copyright (c) 2018 buildtool
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package ci
    24  
    25  import (
    26  	"os"
    27  	"path/filepath"
    28  	"strings"
    29  
    30  	"github.com/apex/log"
    31  	"github.com/buildtool/build-tools/pkg/vcs"
    32  )
    33  
    34  type CI interface {
    35  	Name() string
    36  	// BuildName returns the name of the current build in lowercase
    37  	BuildName() string
    38  	Branch() string
    39  	BranchReplaceSlash() string
    40  	Commit() string
    41  	SetVCS(vcs vcs.VCS)
    42  	SetImageName(imageName string)
    43  	Configured() bool
    44  }
    45  
    46  type Common struct {
    47  	VCS       vcs.VCS
    48  	ImageName string
    49  }
    50  
    51  func (c *Common) SetVCS(vcs vcs.VCS) {
    52  	c.VCS = vcs
    53  }
    54  
    55  func (c *Common) SetImageName(imageName string) {
    56  	c.ImageName = imageName
    57  }
    58  
    59  func (c *Common) BuildName(name string) string {
    60  	if c.ImageName != "" {
    61  		log.Infof("Using %s as BuildName\n", c.ImageName)
    62  		return c.ImageName
    63  	}
    64  	if name != "" {
    65  		return strings.ToLower(name)
    66  	}
    67  	dir, _ := os.Getwd()
    68  	return strings.ToLower(filepath.Base(dir))
    69  }
    70  
    71  func (c *Common) Branch(name string) string {
    72  	if name != "" {
    73  		return name
    74  	}
    75  	return c.VCS.Branch()
    76  }
    77  
    78  func (c *Common) Commit(name string) string {
    79  	if name != "" {
    80  		return name
    81  	}
    82  	return c.VCS.Commit()
    83  }
    84  
    85  func branchReplaceSlash(name string) string {
    86  	return strings.ReplaceAll(strings.ReplaceAll(name, "/", "_"), " ", "_")
    87  }
    88  
    89  func IsValid(c CI) bool {
    90  	return len(c.Commit()) != 0 || len(c.Branch()) != 0
    91  }