Skip to content

Commit

Permalink
Fix GetPipelineFiles to handle both pipelines and path to pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jul 31, 2024
1 parent 2030b29 commit 8a21375
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 76 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ local.build: local.check ## Generates the artifact with the help of 'go build'
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} goreleaser build --rm-dist

publish: local.check ## Builds and publishes the app
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --rm-dist
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --clean

mock.publish: local.check ## Builds and mocks app release
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --skip-publish --rm-dist
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --skip=publish --clean

lint: ## Lint's application for errors, it is a linters aggregator (https://github.com/golangci/golangci-lint).
if [ -z "${DEV}" ]; then golangci-lint run --color always ; else docker run --rm -v $(APP_DIR):/app -w /app golangci/golangci-lint:v1.46.2-alpine golangci-lint run --color always ; fi
Expand Down
40 changes: 19 additions & 21 deletions configrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,41 +335,39 @@ func (conf *client) SetPipelineFiles(pipelines []PipelineFiles) map[string]strin

// GetPipelineFiles reads the pipeline file or recursively read the directory to get all the pipelines matching the pattern and transforms to []PipelineFiles
// So that SetPipelineFiles can convert them to format that ConfigRepoPreflightCheck understands.
func (conf *client) GetPipelineFiles(pathAndPattern ...string) ([]PipelineFiles, error) {
path := pathAndPattern[0]
patterns := pathAndPattern[1:]

func (conf *client) GetPipelineFiles(path string, pipelines []string, patterns ...string) ([]PipelineFiles, error) {
var pipelineFiles []PipelineFiles

fileInfo, err := os.Stat(pathAndPattern[0])
if err != nil {
return nil, err
}
if len(pipelines) != 0 {
for _, goCDPipeline := range pipelines {
conf.logger.Debugf("finding absolute path of the pipeline '%s'", goCDPipeline)
_, err := os.Stat(goCDPipeline)
if err != nil {
return nil, err
}

if !fileInfo.IsDir() {
conf.logger.Debugf("pipeline files path '%s' is a file finding absolute path of the same", path)
absFilePath, err := filepath.Abs(goCDPipeline)
if err != nil {
return pipelineFiles, err
}

absFilePath, err := filepath.Abs(path)
if err != nil {
return pipelineFiles, err
_, fileName := filepath.Split(absFilePath)
pipelineFiles = append(pipelineFiles, PipelineFiles{
Name: fileName,
Path: absFilePath,
})
}

_, fileName := filepath.Split(absFilePath)
pipelineFiles = append(pipelineFiles, PipelineFiles{
Name: fileName,
Path: absFilePath,
})

return pipelineFiles, nil
}

if len(pathAndPattern) <= 1 {
if len(patterns) == 0 {
return nil, &errors.GoCDSDKError{Message: "pipeline files pattern not passed (ex: *.gocd.yaml)"}
}

conf.logger.Debugf("pipeline files path '%s' is a directory, finding all the files matching the pattern '%s'", path, strings.Join(patterns, ","))

if err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions configrepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {

client := gocd.NewClient(server.URL, auth, "debug", nil)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -601,7 +601,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {

client := gocd.NewClient(server.URL, auth, "info", nil)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -617,7 +617,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {
nil, false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -634,7 +634,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -649,7 +649,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {
false, nil)
client := gocd.NewClient(server.URL, auth, "info", nil)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -665,7 +665,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {
client.SetRetryCount(1)
client.SetRetryWaitTime(1)

pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
pipelineFiles, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)

pipelineMap := client.SetPipelineFiles(pipelineFiles)
Expand All @@ -687,7 +687,7 @@ func Test_client_ConfigRepoPreflightCheck(t *testing.T) {
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)

pipelineFiles, err := client.GetPipelineFiles(filepath.Join(homeDir, "opensource/gocd-git-path-sample"), "*.gocd.yaml")
pipelineFiles, err := client.GetPipelineFiles(filepath.Join(homeDir, "opensource/gocd-git-path-sample"), nil, "*.gocd.yaml")
assert.NoError(t, err)

pipeliness := client.SetPipelineFiles(pipelineFiles)
Expand Down Expand Up @@ -762,7 +762,7 @@ func Test_client_GetPipelineFiles(t *testing.T) {
},
}

actual, err := client.GetPipelineFiles("internal/fixtures", "*_config.json")
actual, err := client.GetPipelineFiles("internal/fixtures", nil, "*_config.json")
assert.NoError(t, err)
assert.Equal(t, len(expected), len(actual))
assert.Equal(t, expected, actual)
Expand All @@ -771,15 +771,15 @@ func Test_client_GetPipelineFiles(t *testing.T) {
t.Run("should error out while parsing directory to fetch the pipelines since pattern is missing", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "debug", nil)

actual, err := client.GetPipelineFiles("internal/fixture", "*_config.json")
assert.EqualError(t, err, "stat internal/fixture: no such file or directory")
actual, err := client.GetPipelineFiles("internal/fixture", nil, "*_config.json")
assert.EqualError(t, err, "lstat internal/fixture: no such file or directory")
assert.Nil(t, actual)
})

t.Run("should error out while parsing directory due to wrong path", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "debug", nil)

actual, err := client.GetPipelineFiles("internal/fixtures")
actual, err := client.GetPipelineFiles("internal/fixtures", nil)
assert.EqualError(t, err, "pipeline files pattern not passed (ex: *.gocd.yaml)")
assert.Nil(t, actual)
})
Expand All @@ -794,15 +794,15 @@ func Test_client_GetPipelineFiles(t *testing.T) {
},
}

actual, err := client.GetPipelineFiles("internal/fixtures/mail_server_config.json")
actual, err := client.GetPipelineFiles("", []string{"internal/fixtures/mail_server_config.json"})
assert.NoError(t, err)
assert.Equal(t, expected, actual)
})

t.Run("should error out while identifying the pipeline files due to wrong path", func(t *testing.T) {
client := gocd.NewClient("http://localhost:8156/go", auth, "debug", nil)

actual, err := client.GetPipelineFiles("internal/fixture/mail_server_config.json")
actual, err := client.GetPipelineFiles("", []string{"internal/fixture/mail_server_config.json"})
assert.EqualError(t, err, "stat internal/fixture/mail_server_config.json: no such file or directory")
assert.Nil(t, actual)
})
Expand Down
2 changes: 1 addition & 1 deletion gocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GoCd interface {
ConfigRepoTriggerUpdate(name string) (map[string]string, error)
ConfigRepoPreflightCheck(pipelines map[string]string, pluginID string, repoID string) (bool, error)
SetPipelineFiles(pipelines []PipelineFiles) map[string]string
GetPipelineFiles(pathAndPattern ...string) ([]PipelineFiles, error)
GetPipelineFiles(path string, pipelines []string, patterns ...string) ([]PipelineFiles, error)
EnableMaintenanceMode() error
DisableMaintenanceMode() error
GetMaintenanceModeInfo() (Maintenance, error)
Expand Down
Loading

0 comments on commit 8a21375

Please sign in to comment.