Skip to content

Commit

Permalink
feat: only check for tags on the main repository when creating the en…
Browse files Browse the repository at this point in the history
…vironments

Signed-off-by: Daniel Gozalo <[email protected]>

feat: added a test to the git helper

fix: fixed the variables positions

fix: fixed formatting on debug trace
  • Loading branch information
Daniel Gozalo authored and jenkins-x-bot committed Aug 1, 2019
1 parent ba44ed1 commit c90e349
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
27 changes: 22 additions & 5 deletions pkg/cmd/boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func NewCmdBoot(commonOpts *opts.CommonOptions) *cobra.Command {
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "d", ".", "the directory to look for the Jenkins X Pipeline, requirements and charts")
cmd.Flags().StringVarP(&options.GitURL, "git-url", "u", config.DefaultBootRepository, "override the Git clone URL for the JX Boot source to start from, ignoring the versions stream. Normally specified with git-ref as well")
cmd.Flags().StringVarP(&options.GitRef, "git-ref", "", "master", "override the Git ref for the JX Boot source to start from, ignoring the versions stream. Normally specified with git-url as well")
cmd.Flags().StringVarP(&options.GitURL, "git-url", "u", "", "override the Git clone URL for the JX Boot source to start from, ignoring the versions stream. Normally specified with git-ref as well")
cmd.Flags().StringVarP(&options.GitRef, "git-ref", "", "", "override the Git ref for the JX Boot source to start from, ignoring the versions stream. Normally specified with git-url as well")
cmd.Flags().StringVarP(&options.VersionStreamURL, "versions-repo", "", config.DefaultVersionsURL, "the bootstrap URL for the versions repo. Once the boot config is cloned, the repo will be then read from the jx-requirements.yaml")
cmd.Flags().StringVarP(&options.VersionStreamRef, "versions-ref", "", config.DefaultVersionsRef, "the bootstrap ref for the versions repo. Once the boot config is cloned, the repo will be then read from the jx-requirements.yaml")
return cmd
Expand All @@ -97,7 +97,23 @@ func (o *BootOptions) Run() error {
return err
}

gitURL := o.GitURL
gitURL, gitRef, err := gits.GetGitInfoFromDirectory(o.Dir, o.Git())
if err != nil {
log.Logger().Warn(fmt.Sprintf("there was a problem obtaining the boot config repository git configuration, falling back to defaults"))
gitURL = config.DefaultBootRepository
gitRef = "master"
}

if o.GitURL != "" {
log.Logger().Infof("GitURL provided, overriding the current value: %s", util.ColorInfo(gitURL))
gitURL = o.GitURL
}

if o.GitRef != "" {
log.Logger().Infof("GitRef provided, overriding the current value: %s", util.ColorInfo(gitRef))
gitRef = o.GitRef
}

if config.LoadActiveInstallProfile() == config.CloudBeesProfile && o.GitURL == config.DefaultBootRepository {
gitURL = config.DefaultCloudBeesBootRepository
}
Expand Down Expand Up @@ -227,8 +243,9 @@ func (o *BootOptions) Run() error {
so.InterpretMode = true
so.NoReleasePrepare = true
so.AdditionalEnvVars = map[string]string{
"JX_NO_TILLER": "true",
"REPO_URL": gitURL,
"JX_NO_TILLER": "true",
"REPO_URL": gitURL,
"BASE_CONFIG_REF": gitRef,
}

so.VersionResolver, err = o.CreateVersionResolver(requirements.VersionStream.URL, requirements.VersionStream.Ref)
Expand Down
42 changes: 25 additions & 17 deletions pkg/cmd/step/verify/step_verify_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (o *StepVerifyEnvironmentsOptions) Run() error {

func (o *StepVerifyEnvironmentsOptions) prDevEnvironment(gitRepoName string, environmentsOrg string, privateRepo bool, user *auth.UserAuth, requirements *config.RequirementsConfig, server *auth.AuthServer) error {
fromGitURL := os.Getenv("REPO_URL")
gitRef := os.Getenv("BASE_CONFIG_REF")

log.Logger().Debugf("Defined REPO_URL env variable value: %s", fromGitURL)
log.Logger().Debugf("Defined BASE_CONFIG_REF env variable value: %s", gitRef)

gitInfo, err := gits.ParseGitURL(fromGitURL)
if err != nil {
return errors.Wrapf(err, "parsing %s", fromGitURL)
Expand All @@ -114,26 +119,29 @@ func (o *StepVerifyEnvironmentsOptions) prDevEnvironment(gitRepoName string, env
return errors.Wrapf(err, "resolving %s to absolute path", o.Dir)
}

vs := requirements.VersionStream
u := vs.URL
ref := vs.Ref

resolver, err := o.CreateVersionResolver(u, ref)
if err != nil {
return errors.Wrapf(err, "failed to create version resolver")
}

version, err := resolver.ResolveGitVersion("https://github.com/jenkins-x/jenkins-x-boot-config.git")
if err != nil {
return errors.Wrapf(err, "failed to resolve version for https://github.com/jenkins-x/jenkins-x-boot-config.git")
if fromGitURL == config.DefaultBootRepository && gitRef == "master" {
// If the GitURL is not overridden and the GitRef is set to it's default value then look up the version number
resolver, err := o.CreateVersionResolver(requirements.VersionStream.URL, requirements.VersionStream.Ref)
if err != nil {
return errors.Wrapf(err, "failed to create version resolver")
}
gitRef, err = resolver.ResolveGitVersion(fromGitURL)
if err != nil {
return errors.Wrapf(err, "failed to resolve version for https://github.com/jenkins-x/jenkins-x-boot-config.git")
}
if gitRef == "" {
log.Logger().Infof("Attempting to resolve version for upstream boot config %s", util.ColorInfo(config.DefaultBootRepository))
gitRef, err = resolver.ResolveGitVersion(config.DefaultBootRepository)
if err != nil {
return errors.Wrapf(err, "failed to resolve version for https://github.com/jenkins-x/jenkins-x-boot-config.git")
}
}
}

commitish, err := gits.FindTagForVersion(dir, version, o.Git())
commitish, err := gits.FindTagForVersion(dir, gitRef, o.Git())
if err != nil {
return errors.Wrapf(err, "finding tag for %s", version)
}
if commitish == "" {
commitish = "master"
log.Logger().Debugf(errors.Wrapf(err, "finding tag for %s", gitRef).Error())
commitish = fmt.Sprintf("%s/%s", "origin", gitRef)
}

// Duplicate the repo
Expand Down
24 changes: 23 additions & 1 deletion pkg/gits/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sort"
"strings"

uuid "github.com/satori/go.uuid"
"github.com/satori/go.uuid"

"github.com/jenkins-x/jx/pkg/util"

Expand Down Expand Up @@ -773,3 +773,25 @@ func DuplicateGitRepoFromCommitsh(toOrg string, toName string, fromGitURL string
}
return duplicateInfo, nil
}

// GetGitInfoFromDirectory obtains remote origin HTTPS and current branch of a given directory and fails if it's not a git repository
func GetGitInfoFromDirectory(dir string, gitter Gitter) (string, string, error) {
_, gitConfig, err := gitter.FindGitConfigDir(dir)
if err != nil {
return "", "", errors.Wrapf(err, "there was a problem obtaining the git config dir of directory %s", dir)
}
remoteGitURL, err := gitter.DiscoverRemoteGitURL(gitConfig)
if err != nil {
return "", "", errors.Wrapf(err, "there was a problem obtaining the remote Git URL of directory %s", dir)
}
currentBranch, err := gitter.Branch(dir)
if err != nil {
return "", "", errors.Wrapf(err, "there was a problem obtaining the current branch on directory %s", dir)
}
g, err := ParseGitURL(remoteGitURL)
if err != nil {
return "", "", errors.Wrapf(err, "there was a problem parsing the Git URL %s to HTTPS", remoteGitURL)
}

return g.HttpsURL(), currentBranch, nil
}
43 changes: 43 additions & 0 deletions pkg/gits/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,3 +1811,46 @@ func TestPushRepoAndCreatePullRequest(t *testing.T) {
})
}
}

func TestGetGitInfoFromDirectory(t *testing.T) {
t.Parallel()
gitter := gits.NewGitCLI()
owner := "fakeowner"
repo := "fakerepo"
originalRepo, err := gits.NewFakeRepository(owner, repo, func(dir string) error {
err := ioutil.WriteFile(filepath.Join(dir, "README"), []byte("Hello!"), 0655)
if err != nil {
return errors.Wrapf(err, "writing README")
}
return nil
}, gitter)
defer os.RemoveAll(originalRepo.BaseDir)

assert.NoError(t, err)
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir)
assert.NoError(t, err)
err = gitter.Clone(originalRepo.GitRepo.CloneURL, dir)
assert.NoError(t, err)
err = gitter.UpdateRemote(dir, fmt.Sprintf("[email protected]:%s/%s.git", owner, repo))
assert.NoError(t, err)

url, ref, err := gits.GetGitInfoFromDirectory(dir, gitter)
assert.NoError(t, err)

assert.Equal(t, fmt.Sprintf("https://github.com/%s/%s", owner, repo), url)
assert.Equal(t, "master", ref)
}

func TestGetGitInfoFromDirectoryNoGit(t *testing.T) {
t.Parallel()
gitter := gits.NewGitCLI()
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir)
assert.NoError(t, err)

_, _, err = gits.GetGitInfoFromDirectory(dir, gitter)
assert.Error(t, err)

assert.Equal(t, fmt.Sprintf("there was a problem obtaining the remote Git URL of directory %s: failed to unmarshal due to no GitConfDir defined", dir), err.Error())
}

0 comments on commit c90e349

Please sign in to comment.