From 965d2aaf6f57365d5bd3d7051d2e1680dbc28af7 Mon Sep 17 00:00:00 2001 From: Ismael Puerto Freire Date: Sun, 27 Oct 2024 12:51:01 +0100 Subject: [PATCH] Added branch as option when git is configured. --- Dockerfile | 2 +- conf/git_reader.go | 7 ++++--- conf/git_reader_test.go | 7 ++++--- conf/reader.go | 10 ++++++---- conf/reader_test.go | 21 +++++++++++++++------ conf/util.go | 9 +++++---- conf/util_test.go | 5 +++-- git/clone.go | 17 +++++++++++------ git/pull.go | 14 ++++++++++---- git/repository.go | 10 ++++++---- 10 files changed, 65 insertions(+), 37 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3fe3a802..ad9d6700 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.14 as builder +FROM golang:1.21 as builder ADD . $GOPATH/src/github.com/opsgenie/oec WORKDIR $GOPATH/src/github.com/opsgenie/oec/main RUN export GIT_COMMIT=$(git rev-list -1 HEAD) && \ diff --git a/conf/git_reader.go b/conf/git_reader.go index 97f03ed8..6346c8d9 100644 --- a/conf/git_reader.go +++ b/conf/git_reader.go @@ -1,21 +1,22 @@ package conf import ( - "github.com/opsgenie/oec/git" "os" fpath "path/filepath" + + "github.com/opsgenie/oec/git" ) var cloneMasterFunc = git.CloneMaster -func readFileFromGit(url, privateKeyFilepath, passPhrase, filepath string) (*Configuration, error) { +func readFileFromGit(url, privateKeyFilepath, passPhrase, filepath, branch string) (*Configuration, error) { err := checkFileExtension(filepath) if err != nil { return nil, err } - repoFilepath, err := cloneMasterFunc(url, privateKeyFilepath, passPhrase) + repoFilepath, err := cloneMasterFunc(url, privateKeyFilepath, passPhrase, branch) if err != nil { return nil, err } diff --git a/conf/git_reader_test.go b/conf/git_reader_test.go index 2fd6e830..ad3d29c8 100644 --- a/conf/git_reader_test.go +++ b/conf/git_reader_test.go @@ -1,10 +1,11 @@ package conf import ( + "testing" + "github.com/opsgenie/oec/git" "github.com/opsgenie/oec/util" "github.com/stretchr/testify/assert" - "testing" ) func TestReadFileFromGit(t *testing.T) { @@ -12,11 +13,11 @@ func TestReadFileFromGit(t *testing.T) { defer func() { cloneMasterFunc = git.CloneMaster }() confPath, err := util.CreateTempTestFile(mockJsonFileContent, ".json") - cloneMasterFunc = func(url, privateKeyFilepath, passPhrase string) (repositoryPath string, err error) { + cloneMasterFunc = func(url, privateKeyFilepath, passPhrase, branch string) (repositoryPath string, err error) { return "", nil } - config, err := readFileFromGit("", "", "", confPath) + config, err := readFileFromGit("", "", "", confPath, "") assert.Nil(t, err) assert.Equal(t, mockConf, config) diff --git a/conf/reader.go b/conf/reader.go index fdef8e00..280df234 100644 --- a/conf/reader.go +++ b/conf/reader.go @@ -1,12 +1,13 @@ package conf import ( - "github.com/opsgenie/oec/git" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "os" "path/filepath" "strings" + + "github.com/opsgenie/oec/git" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) const ( @@ -54,6 +55,7 @@ func readFileFromSource(confSourceType string) (*Configuration, error) { privateKeyFilepath := os.Getenv("OEC_CONF_GIT_PRIVATE_KEY_FILEPATH") passphrase := os.Getenv("OEC_CONF_GIT_PASSPHRASE") confFilepath := os.Getenv("OEC_CONF_GIT_FILEPATH") + branch := os.Getenv("OEC_CONF_GIT_BRANCH") if privateKeyFilepath != "" { privateKeyFilepath = addHomeDirPrefix(privateKeyFilepath) @@ -63,7 +65,7 @@ func readFileFromSource(confSourceType string) (*Configuration, error) { return nil, errors.New("Git configuration filepath could not be empty.") } - return readFileFromGitFunc(url, privateKeyFilepath, passphrase, confFilepath) + return readFileFromGitFunc(url, privateKeyFilepath, passphrase, confFilepath, branch) case LocalSourceType: confFilepath := os.Getenv("OEC_CONF_LOCAL_FILEPATH") diff --git a/conf/reader_test.go b/conf/reader_test.go index 0ba49c74..4ad14937 100644 --- a/conf/reader_test.go +++ b/conf/reader_test.go @@ -1,12 +1,13 @@ package conf import ( + "os" + "testing" + "github.com/opsgenie/oec/git" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" - "os" - "testing" ) var readFileFromGitCalled = false @@ -33,6 +34,7 @@ var mockActionMappings = ActionMappings{ GitOptions: git.Options{ Url: "testUrl", PrivateKeyFilepath: "testKeyPath", + Branch: "main", }, Env: []string{"e1=v1", "e2=v2"}, Filepath: "/path/to/action.bin", @@ -83,7 +85,8 @@ var mockJsonFileContent = []byte(`{ "sourceType": "git", "gitOptions" : { "url" : "testUrl", - "privateKeyFilepath" : "testKeyPath" + "privateKeyFilepath" : "testKeyPath", + "branch" : "main" }, "env": [ "e1=v1", "e2=v2" @@ -122,6 +125,7 @@ actionMappings: gitOptions: url: testUrl privateKeyFilepath: testKeyPath + branch: main env: - e1=v1 - e2=v2 @@ -130,9 +134,9 @@ actionMappings: type: "http" filepath: "/path/to/http-executor" url: "https://opsgenie.com" - headers: + headers: Authentication: "Basic JNjDkNsKaMs" - params: + params: Key1: Value1 method: PUT sourceType: local @@ -140,7 +144,7 @@ actionMappings: const testLocalConfFilePath = "/path/to/test/conf/file.json" -func mockReadFileFromGit(owner, repo, filepath, token string) (*Configuration, error) { +func mockReadFileFromGit(owner, repo, filepath, token, branch string) (*Configuration, error) { readFileFromGitCalled = true if len(owner) <= 0 { @@ -159,6 +163,10 @@ func mockReadFileFromGit(owner, repo, filepath, token string) (*Configuration, e return nil, errors.New("Token was empty.") } + if len(branch) <= 0 { + return nil, errors.New("Branch was empty.") + } + conf := *mockConf conf.ActionMappings = copyActionMappings(mockActionMappings) @@ -222,6 +230,7 @@ func testReadFileFromGit(t *testing.T) { os.Setenv("OEC_CONF_GIT_PRIVATE_KEY_FILEPATH", "/test_id_rsa") os.Setenv("OEC_CONF_GIT_FILEPATH", "oec/testConf.json") os.Setenv("OEC_CONF_GIT_PASSPHRASE", "pass") + os.Setenv("OEC_CONF_GIT_BRANCH", "main") readFileFromGitFunc = mockReadFileFromGit configuration, err := Read() diff --git a/conf/util.go b/conf/util.go index ca91398c..02f8d314 100644 --- a/conf/util.go +++ b/conf/util.go @@ -2,16 +2,17 @@ package conf import ( "encoding/json" - "github.com/opsgenie/oec/git" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "gopkg.in/yaml.v2" "io/ioutil" "os" fpath "path/filepath" "runtime" "strings" "time" + + "github.com/opsgenie/oec/git" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) const unknownFileExtErrMessage = "Unknown configuration file extension[%s]. Only \".json\" and \".yml(.yaml)\" types are allowed." diff --git a/conf/util_test.go b/conf/util_test.go index 08fd7879..7e7712cb 100644 --- a/conf/util_test.go +++ b/conf/util_test.go @@ -2,10 +2,11 @@ package conf import ( "fmt" - "github.com/opsgenie/oec/util" - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/opsgenie/oec/util" + "github.com/stretchr/testify/assert" ) func TestReadJsonFile(t *testing.T) { diff --git a/git/clone.go b/git/clone.go index ff0ca417..a8476621 100644 --- a/git/clone.go +++ b/git/clone.go @@ -1,25 +1,26 @@ package git import ( + "io/ioutil" + "os" + "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/transport/ssh" - "io/ioutil" - "os" ) var cloneMasterFunc = cloneMaster const repositoryDirPrefix = "oec" -func CloneMaster(url, privateKeyFilepath, passPhrase string) (repositoryPath string, err error) { +func CloneMaster(url, privateKeyFilepath, passPhrase, branch string) (repositoryPath string, err error) { tmpDir, err := ioutil.TempDir("", repositoryDirPrefix) if err != nil { return "", err } - err = cloneMasterFunc(tmpDir, url, privateKeyFilepath, passPhrase) + err = cloneMasterFunc(tmpDir, url, privateKeyFilepath, passPhrase, branch) if err != nil { os.RemoveAll(tmpDir) return "", err @@ -28,12 +29,16 @@ func CloneMaster(url, privateKeyFilepath, passPhrase string) (repositoryPath str return tmpDir, nil } -func cloneMaster(tmpDir, gitUrl, privateKeyFilepath, passPhrase string) error { +func cloneMaster(tmpDir, gitUrl, privateKeyFilepath, passPhrase, branch string) error { + + if branch == "" { + branch = "master" + } options := &git.CloneOptions{ URL: gitUrl, RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, // todo restrict max depth - ReferenceName: plumbing.Master, + ReferenceName: plumbing.NewBranchReferenceName(branch), SingleBranch: true, } diff --git a/git/pull.go b/git/pull.go index 5c7fabe7..f981df87 100644 --- a/git/pull.go +++ b/git/pull.go @@ -7,7 +7,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/ssh" ) -func PullMaster(repositoryPath, privateKeyFilePath, passPhrase string) error { +func Pull(repositoryPath, privateKeyFilePath, passPhrase string) error { r, err := git.PlainOpen(repositoryPath) if err != nil { return err @@ -38,15 +38,20 @@ func PullMaster(repositoryPath, privateKeyFilePath, passPhrase string) error { return w.Pull(options) } -func FetchAndReset(repositoryPath, privateKeyFilePath, passPhrase string) error { +func FetchAndReset(repositoryPath, privateKeyFilePath, passPhrase, branch string) error { r, err := git.PlainOpen(repositoryPath) if err != nil { return err } + if branch == "" { + branch = "master" + } + + refSpec := config.RefSpec("+refs/heads/" + branch + ":refs/remotes/origin/" + branch) options := &git.FetchOptions{ - RefSpecs: []config.RefSpec{"refs/heads/master:refs/heads/master"}, + RefSpecs: []config.RefSpec{refSpec}, } if privateKeyFilePath != "" { @@ -64,7 +69,8 @@ func FetchAndReset(repositoryPath, privateKeyFilePath, passPhrase string) error return err } - ref, err := r.Reference(plumbing.Master, true) + remoteRef := plumbing.NewRemoteReferenceName("origin", branch) + ref, err := r.Reference(remoteRef, true) if err != nil { return err } diff --git a/git/repository.go b/git/repository.go index d3cf1057..3a63adec 100644 --- a/git/repository.go +++ b/git/repository.go @@ -1,18 +1,20 @@ package git import ( + "os" + "sync" + "github.com/go-git/go-git/v5" "github.com/opsgenie/oec/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "os" - "sync" ) type Options struct { Url string `json:"url" yaml:"url"` PrivateKeyFilepath string `json:"privateKeyFilepath" yaml:"privateKeyFilepath"` Passphrase string `json:"passphrase" yaml:"passphrase"` + Branch string `json:"branch" yaml:"branch"` } type Url string @@ -49,7 +51,7 @@ func (r Repositories) DownloadAll(optionsList []Options) (err error) { func (r Repositories) Download(options *Options) (err error) { if _, contains := r[Url(options.Url)]; !contains { - repositoryPath, err := CloneMaster(options.Url, options.PrivateKeyFilepath, options.Passphrase) + repositoryPath, err := CloneMaster(options.Url, options.PrivateKeyFilepath, options.Passphrase, options.Branch) if err != nil { return errors.Errorf("Git repository[%s] could not be downloaded: %s", options.Url, err.Error()) } @@ -120,7 +122,7 @@ func (r *Repository) Pull() error { logrus.Warnf("Git repository[%s] chmod failed: %s", r.Options.Url, err) } }() - return FetchAndReset(r.Path, r.Options.PrivateKeyFilepath, r.Options.Passphrase) + return FetchAndReset(r.Path, r.Options.PrivateKeyFilepath, r.Options.Passphrase, r.Options.Branch) } func (r *Repository) Remove() error {