Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added branch as option when git is configured. #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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) && \
Expand Down
7 changes: 4 additions & 3 deletions conf/git_reader.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions conf/git_reader_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
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) {

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)
Expand Down
10 changes: 6 additions & 4 deletions conf/reader.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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")

Expand Down
21 changes: 15 additions & 6 deletions conf/reader_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -83,7 +85,8 @@ var mockJsonFileContent = []byte(`{
"sourceType": "git",
"gitOptions" : {
"url" : "testUrl",
"privateKeyFilepath" : "testKeyPath"
"privateKeyFilepath" : "testKeyPath",
"branch" : "main"
},
"env": [
"e1=v1", "e2=v2"
Expand Down Expand Up @@ -122,6 +125,7 @@ actionMappings:
gitOptions:
url: testUrl
privateKeyFilepath: testKeyPath
branch: main
env:
- e1=v1
- e2=v2
Expand All @@ -130,17 +134,17 @@ 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
`)

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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions conf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
5 changes: 3 additions & 2 deletions conf/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions git/clone.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
}

Expand Down
14 changes: 10 additions & 4 deletions git/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions git/repository.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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 {
Expand Down