Skip to content

Commit

Permalink
Merge pull request #14025 from gabemontero/new-app-clone-pr-ref
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot authored May 8, 2017
2 parents 34cda05 + b944e48 commit 17ebd78
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions pkg/build/builder/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type GitClient interface {
CloneWithOptions(dir string, url string, args ...string) error
Fetch(dir string, url string, ref string) error
Checkout(dir string, ref string) error
PotentialPRRetryAsFetch(dir string, url string, ref string, err error) error
SubmoduleUpdate(dir string, init, recursive bool) error
TimedListRemote(timeout time.Duration, url string, args ...string) (string, string, error)
GetInfo(location string) (*git.SourceInfo, []error)
Expand Down
9 changes: 1 addition & 8 deletions pkg/build/builder/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,10 @@ func extractGitSource(ctx context.Context, gitClient GitClient, gitSource *api.G
}

if err := gitClient.Checkout(dir, commit); err != nil {
glog.V(4).Infof("Checkout after clone failed for ref %s with error: %v, attempting fetch", commit, err)
err = gitClient.Fetch(dir, gitSource.URI, commit)
err = gitClient.PotentialPRRetryAsFetch(dir, gitSource.URI, commit, err)
if err != nil {
return true, err
}

err = gitClient.Checkout(dir, "FETCH_HEAD")
if err != nil {
return true, err
}
glog.V(4).Infof("Fetch / checkout for %s successful", commit)
}

// Recursively update --init
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/cli/cmd/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,10 @@ func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client
return nil, err
}
if err := repo.Checkout(tempDirectory, commit); err != nil {
return nil, err
err = repo.PotentialPRRetryAsFetch(tempDirectory, path, commit, err)
if err != nil {
return nil, err
}
}

// We'll continue to use tar on the temp directory
Expand Down
5 changes: 4 additions & 1 deletion pkg/generate/app/sourcelookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,10 @@ func CloneAndCheckoutSources(repo git.Repository, remote, ref, localDir, context
}
if len(ref) > 0 {
if err := repo.Checkout(localDir, ref); err != nil {
return "", fmt.Errorf("unable to checkout ref %q in %q repository: %v", ref, remote, err)
err = repo.PotentialPRRetryAsFetch(localDir, remote, ref, err)
if err != nil {
return "", fmt.Errorf("unable to checkout ref %q in %q repository: %v", ref, remote, err)
}
}
}
if len(contextDir) > 0 {
Expand Down
18 changes: 18 additions & 0 deletions pkg/generate/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Repository interface {
CloneMirror(dir string, url string) error
Fetch(dir string, url string, ref string) error
Checkout(dir string, ref string) error
PotentialPRRetryAsFetch(dir string, url string, ref string, err error) error
SubmoduleUpdate(dir string, init, recursive bool) error
Archive(dir, ref, format string, w io.Writer) error
Init(dir string, bare bool) error
Expand Down Expand Up @@ -127,6 +128,23 @@ func IsBareRoot(path string) (bool, error) {
return true, nil
}

// PotentialPRRetryAsFetch is used on checkout errors after a clone where the possibility
// that a fetch or a PR ref is needed between the clone and checkout operations
func (r *repository) PotentialPRRetryAsFetch(dir, remote, ref string, err error) error {
glog.V(4).Infof("Checkout after clone failed for ref %s with error: %v, attempting fetch", ref, err)
err = r.Fetch(dir, remote, ref)
if err != nil {
return err
}

err = r.Checkout(dir, "FETCH_HEAD")
if err != nil {
return err
}
glog.V(4).Infof("Fetch / checkout for %s successful", ref)
return nil
}

// GetRootDir obtains the directory root for a Git repository
func (r *repository) GetRootDir(location string) (string, error) {
dir, _, err := r.git(location, "rev-parse", "--git-dir")
Expand Down
3 changes: 3 additions & 0 deletions test/cmd/newapp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ os::cmd::expect_failure_and_text 'oc new-app --dry-run mysq' 'error: only a part
os::cmd::expect_failure_and_text 'oc new-app --dry-run mysq' 'The argument "mysq" only partially matched'
os::cmd::expect_failure_and_text 'oc new-app --dry-run mysq' "Image stream \"mysql\" \\(tag \"5.7\"\\) in project"

# ensure new-app with pr ref does not fail
os::cmd::expect_success 'oc new-app https://github.com/openshift/ruby-hello-world#refs/pull/58/head --dry-run'

# verify image streams with no tags are reported correctly and that --allow-missing-imagestream-tags works
# new-app
os::cmd::expect_success 'printf "apiVersion: v1\nkind: ImageStream\nmetadata:\n name: emptystream\n" | oc create -f -'
Expand Down

0 comments on commit 17ebd78

Please sign in to comment.