github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/jenkinsfile/gitresolver/buildpack.go (about)

     1  package gitresolver
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/jenkins-x/jx-logging/pkg/log"
    11  
    12  	"github.com/olli-ai/jx/v2/pkg/gits"
    13  	"github.com/olli-ai/jx/v2/pkg/util"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // InitBuildPack initialises the build pack URL and git ref returning the packs dir or an error
    18  func InitBuildPack(gitter gits.Gitter, packURL string, packRef string) (string, error) {
    19  	u, err := url.Parse(strings.TrimSuffix(packURL, ".git"))
    20  	if err != nil {
    21  		return "", fmt.Errorf("Failed to parse build pack URL: %s: %s", packURL, err)
    22  	}
    23  
    24  	draftDir, err := util.DraftDir()
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  	dir := filepath.Join(draftDir, "packs", u.Host, u.Path)
    29  	if err := os.MkdirAll(dir, 0755); err != nil {
    30  		return "", fmt.Errorf("Could not create %s: %s", dir, err)
    31  	}
    32  
    33  	err = ensureBranchTracksOrigin(dir, packRef, gitter)
    34  	if err != nil {
    35  		return "", errors.Wrapf(err, "there was a problem ensuring the branch %s has tracking info", packRef)
    36  	}
    37  
    38  	err = gitter.CloneOrPull(packURL, dir)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	if packRef != "master" && packRef != "" {
    43  		err = gitter.FetchTags(dir)
    44  		if err != nil {
    45  			return "", errors.Wrapf(err, "fetching tags from %s", packURL)
    46  		}
    47  		tags, err := gitter.FilterTags(dir, packRef)
    48  		if err != nil {
    49  			return "", errors.Wrapf(err, "filtering tags for %s", packRef)
    50  		}
    51  		if len(tags) == 0 {
    52  			tags, err = gitter.FilterTags(dir, fmt.Sprintf("v%s", packRef))
    53  			if err != nil {
    54  				return "", errors.Wrapf(err, "filtering tags for v%s", packRef)
    55  			}
    56  		}
    57  		if len(tags) == 1 {
    58  			tag := tags[0]
    59  			branchName := fmt.Sprintf("tag-%s", tag)
    60  			err = gitter.CreateBranchFrom(dir, branchName, tag)
    61  			if err != nil {
    62  				return "", errors.Wrapf(err, "creating branch %s from %s", branchName, tag)
    63  			}
    64  			err = gitter.Checkout(dir, branchName)
    65  			if err != nil {
    66  				return "", errors.Wrapf(err, "checking out branch %s", branchName)
    67  			}
    68  		} else {
    69  			if len(tags) > 1 {
    70  				log.Logger().Debugf("more than one tag matched %s or v%s, ignoring tags", packRef, packRef)
    71  			}
    72  			err = gitter.CheckoutRemoteBranch(dir, packRef)
    73  			if err != nil {
    74  				return "", errors.Wrapf(err, "checking out tracking branch %s", packRef)
    75  			}
    76  		}
    77  
    78  	}
    79  	return filepath.Join(dir, "packs"), nil
    80  }
    81  
    82  func ensureBranchTracksOrigin(dir string, packRef string, gitter gits.Gitter) error {
    83  	empty, err := util.IsEmpty(dir)
    84  	if err != nil {
    85  		return errors.Wrapf(err, "there was a problem checking if %s is empty", dir)
    86  	}
    87  
    88  	// The repository is cloned, before the pull, we have to make sure we fetch & checkout <packRef> and we are tracking origin/<packRef>
    89  	// This is due to a bug happening on old clones done by the old cloning func
    90  	if !empty {
    91  		err := gitter.FetchBranch(dir, "origin", packRef)
    92  		if err != nil {
    93  			return err
    94  		}
    95  		err = gitter.Checkout(dir, packRef)
    96  		if err != nil {
    97  			return err
    98  		}
    99  		err = gitter.SetUpstreamTo(dir, packRef)
   100  		if err != nil {
   101  			return errors.Wrapf(err, "there was a problem setting upstream to remote branch origin/%s", packRef)
   102  		}
   103  	}
   104  
   105  	return nil
   106  }