github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/generic-autobumper/helper.go (about)

     1  /*
     2  Copyright 2021 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  	"strings"
    22  
    23  	"sigs.k8s.io/prow/cmd/generic-autobumper/imagebumper"
    24  )
    25  
    26  // Extract image from image name
    27  func imageFromName(name string) string {
    28  	parts := strings.Split(name, ":")
    29  	if len(parts) < 2 {
    30  		return ""
    31  	}
    32  	return parts[0]
    33  }
    34  
    35  // Extract image tag from image name
    36  func tagFromName(name string) string {
    37  	parts := strings.Split(name, ":")
    38  	if len(parts) < 2 {
    39  		return ""
    40  	}
    41  	return parts[1]
    42  }
    43  
    44  // Extract prow component name from image
    45  func componentFromName(name string) string {
    46  	s := strings.SplitN(strings.Split(name, ":")[0], "/", 3)
    47  	return s[len(s)-1]
    48  }
    49  
    50  func formatTagDate(d string) string {
    51  	if len(d) != 8 {
    52  		return d
    53  	}
    54  	// &#x2011; = U+2011 NON-BREAKING HYPHEN, to prevent line wraps.
    55  	return fmt.Sprintf("%s&#x2011;%s&#x2011;%s", d[0:4], d[4:6], d[6:8])
    56  }
    57  
    58  // commitToRef converts git describe part of a tag to a ref (commit or tag).
    59  //
    60  // v0.0.30-14-gdeadbeef => deadbeef
    61  // v0.0.30 => v0.0.30
    62  // deadbeef => deadbeef
    63  func commitToRef(commit string) string {
    64  	tag, _, commit := imagebumper.DeconstructCommit(commit)
    65  	if commit != "" {
    66  		return commit
    67  	}
    68  	return tag
    69  }
    70  
    71  // Format variant for PR summary
    72  func formatVariant(variant string) string {
    73  	if variant == "" {
    74  		return ""
    75  	}
    76  	return fmt.Sprintf("(%s)", strings.TrimPrefix(variant, "-"))
    77  }
    78  
    79  // Check whether the path is under the given path
    80  func isUnderPath(name string, paths []string) bool {
    81  	for _, p := range paths {
    82  		if p != "" && strings.HasPrefix(name, p) {
    83  			return true
    84  		}
    85  	}
    86  	return false
    87  }
    88  
    89  // isBumpedPrefix takes a prefix and a map of new tags resulted from bumping
    90  // : the images using those tags and itterates over the map to find if the
    91  // prefix is found. If it is, this means it has been bumped.
    92  func isBumpedPrefix(prefix prefix, versions map[string][]string) (string, bool) {
    93  	for tag, imageList := range versions {
    94  		for _, image := range imageList {
    95  			if strings.HasPrefix(image, prefix.Prefix) {
    96  				return tag, true
    97  			}
    98  		}
    99  	}
   100  	return "", false
   101  }