Skip to content

Commit

Permalink
Add support for submodules in GitResource
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester committed Nov 7, 2019
1 parent 46d6d3e commit d3c8290
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 50 deletions.
3 changes: 3 additions & 0 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ Params that can be added are the following:
(branch, tag, commit SHA or ref) to clone. You can use this to control what
commit [or branch](#using-a-branch) is used. _If no revision is specified,
the resource will default to `latest` from `master`._
1. `submodules`: defines if the resource should initialize and
fetch the submodules, value is either `true` or `false`. _If note
specified, this will default to true_

When used as an input, the Git resource includes the exact commit fetched in the `resourceResults`
section of the `taskRun`'s status object:
Expand Down
23 changes: 19 additions & 4 deletions pkg/apis/pipeline/v1alpha1/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ type GitResource struct {
// Git revision (branch, tag, commit SHA or ref) to clone. See
// https://git-scm.com/docs/gitrevisions#_specifying_revisions for more
// information.
Revision string `json:"revision"`
Revision string `json:"revision"`
Submodules bool `json:"submodules"`

GitImage string `json:"-"`
}
Expand All @@ -50,16 +51,19 @@ func NewGitResource(gitImage string, r *PipelineResource) (*GitResource, error)
return nil, xerrors.Errorf("GitResource: Cannot create a Git resource from a %s Pipeline Resource", r.Spec.Type)
}
gitResource := GitResource{
Name: r.Name,
Type: r.Spec.Type,
GitImage: gitImage,
Name: r.Name,
Type: r.Spec.Type,
GitImage: gitImage,
Submodules: true,
}
for _, param := range r.Spec.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
gitResource.URL = param.Value
case strings.EqualFold(param.Name, "Revision"):
gitResource.Revision = param.Value
case strings.EqualFold(param.Name, "Submodules"):
gitResource.Submodules = toBool(param.Value, true)
}
}
// default revision to master if nothing is provided
Expand All @@ -69,6 +73,17 @@ func NewGitResource(gitImage string, r *PipelineResource) (*GitResource, error)
return &gitResource, nil
}

func toBool(s string, d bool) bool {
switch s {
case "true":
return true
case "false":
return false
default:
return d
}
}

// GetName returns the name of the resource
func (s GitResource) GetName() string {
return s.Name
Expand Down
55 changes: 45 additions & 10 deletions pkg/apis/pipeline/v1alpha1/git_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func Test_Valid_NewGitResource(t *testing.T) {
),
),
want: &v1alpha1.GitResource{
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "test",
GitImage: "override-with-git:latest",
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "test",
GitImage: "override-with-git:latest",
Submodules: true,
},
}, {
desc: "Without Revision",
Expand All @@ -60,11 +61,45 @@ func Test_Valid_NewGitResource(t *testing.T) {
),
),
want: &v1alpha1.GitResource{
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "master",
GitImage: "override-with-git:latest",
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "master",
GitImage: "override-with-git:latest",
Submodules: true,
},
}, {
desc: "With Submodules",
pipelineResource: tb.PipelineResource("git-resource", "default",
tb.PipelineResourceSpec(v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("URL", "git@github.com:test/test.git"),
tb.PipelineResourceSpecParam("Revision", "test"),
tb.PipelineResourceSpecParam("Submodules", "false"),
),
),
want: &v1alpha1.GitResource{
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "test",
GitImage: "override-with-git:latest",
Submodules: false,
},
}, {
desc: "Without Submodules",
pipelineResource: tb.PipelineResource("git-resource", "default",
tb.PipelineResourceSpec(v1alpha1.PipelineResourceTypeGit,
tb.PipelineResourceSpecParam("URL", "git@github.com:test/test.git"),
tb.PipelineResourceSpecParam("Revision", "test"),
),
),
want: &v1alpha1.GitResource{
Name: "git-resource",
Type: v1alpha1.PipelineResourceTypeGit,
URL: "git@github.com:test/test.git",
Revision: "test",
GitImage: "override-with-git:latest",
Submodules: true,
},
}} {
t.Run(tc.desc, func(t *testing.T) {
Expand Down
57 changes: 21 additions & 36 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,9 @@ func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error)

// Fetch fetches the specified git repository at the revision into path.
func Fetch(logger *zap.SugaredLogger, revision, path, url string) error {
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME
// env variable.
homepath, err := homedir.Dir()
if err != nil {
logger.Errorf("Unexpected error: getting the user home directory: %v", err)
if err := ensureHomeEnv(logger); err != nil {
return err
}
homeenv := os.Getenv("HOME")
euid := os.Geteuid()
// Special case the root user/directory
if euid == 0 {
if err := os.Symlink(homeenv+"/.ssh", "/root/.ssh"); err != nil {
// Only do a warning, in case we don't have a real home
// directory writable in our image
logger.Warnf("Unexpected error: creating symlink: %v", err)
}
} else if homeenv != "" && homeenv != homepath {
if _, err := os.Stat(homepath + "/.ssh"); os.IsNotExist(err) {
if err := os.Symlink(homeenv+"/.ssh", homepath+"/.ssh"); err != nil {
// Only do a warning, in case we don't have a real home
// directory writable in our image
logger.Warnf("Unexpected error: creating symlink: %v", err)
}
}
}

if revision == "" {
revision = "master"
Expand Down Expand Up @@ -113,6 +91,26 @@ func Commit(logger *zap.SugaredLogger, revision, path string) (string, error) {
}

func SubmoduleFetch(logger *zap.SugaredLogger, path string) error {
if err := ensureHomeEnv(logger); err != nil {
return err
}

if path != "" {
if err := os.Chdir(path); err != nil {
return xerrors.Errorf("Failed to change directory with path %s; err: %w", path, err)
}
}
if _, err := run(logger, "", "submodule", "init"); err != nil {
return err
}
if _, err := run(logger, "", "submodule", "update", "--recursive"); err != nil {
return err
}
logger.Infof("Successfully initialized and updated submodules in path %s", path)
return nil
}

func ensureHomeEnv(logger *zap.SugaredLogger) error {
// HACK: This is to get git+ssh to work since ssh doesn't respect the HOME
// env variable.
homepath, err := homedir.Dir()
Expand All @@ -138,18 +136,5 @@ func SubmoduleFetch(logger *zap.SugaredLogger, path string) error {
}
}
}

if path != "" {
if err := os.Chdir(path); err != nil {
return xerrors.Errorf("Failed to change directory with path %s; err: %w", path, err)
}
}
if _, err := run(logger, "", "submodule", "init"); err != nil {
return err
}
if _, err := run(logger, "", "submodule", "update", "--recursive"); err != nil {
return err
}
logger.Infof("Successfully initialized and updated submodules in path %s", path)
return nil
}

0 comments on commit d3c8290

Please sign in to comment.