From 588b48c046cf3805c0f41ab5790618a4e706e3a0 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:59:17 -0500 Subject: [PATCH] enhance: allow glob pattern in create/upload closes https://github.com/go-vela/community/issues/724 --- DOCS.md | 32 ++++++ cmd/vela-github-release/create.go | 18 +++- cmd/vela-github-release/create_test.go | 110 ++++++++++++++++++++- cmd/vela-github-release/testdata/file | 1 + cmd/vela-github-release/testdata/test1.txt | 1 + cmd/vela-github-release/testdata/test2.txt | 1 + cmd/vela-github-release/upload.go | 24 +++-- cmd/vela-github-release/upload_test.go | 80 ++++++++++++++- 8 files changed, 255 insertions(+), 12 deletions(-) create mode 100644 cmd/vela-github-release/testdata/file create mode 100644 cmd/vela-github-release/testdata/test1.txt create mode 100644 cmd/vela-github-release/testdata/test2.txt diff --git a/DOCS.md b/DOCS.md index 639c8bb..3eb2c82 100644 --- a/DOCS.md +++ b/DOCS.md @@ -27,6 +27,22 @@ steps: tag: v0.1.0 ``` +Sample of creating a GitHub release, attaching all `.pdf` files in the current directory: + +```yaml +steps: + - name: gh + image: target/vela-github-release:latest + pull: always + parameters: + action: create + files: [ "*.pdf" ] + tag: v0.1.0 +``` + +> [!IMPORTANT] +> This uses [Go's implementation of glob patterns](https://pkg.go.dev/path/filepath#Match) + Sample of deleting release files: ```yaml @@ -75,6 +91,22 @@ steps: tag: v0.1.0 ``` +Sample of uploading assets using glob pattern to a gh release: + +```yaml +steps: + - name: gh + image: target/vela-github-release:latest + pull: always + parameters: + action: upload + files: [ "*.pdf" ] + tag: v0.1.0 +``` + +> [!IMPORTANT] +> This uses [Go's implementation of glob patterns](https://pkg.go.dev/path/filepath#Match) + Sample of viewing information about a gh release: ```yaml diff --git a/cmd/vela-github-release/create.go b/cmd/vela-github-release/create.go index 78b007c..e4b4188 100644 --- a/cmd/vela-github-release/create.go +++ b/cmd/vela-github-release/create.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os/exec" + "path/filepath" "github.com/sirupsen/logrus" ) @@ -60,8 +61,21 @@ func (c *Create) Command() *exec.Cmd { flags = append(flags, c.Tag) } - // add flag for files provided by create files - flags = append(flags, c.Files...) + // iterate through the files and add them as parameters + for _, file := range c.Files { + f, err := filepath.Glob(file) + if err != nil { + logrus.Warnf("bad file pattern: %v", err) + } + + if f == nil { + logrus.Warnf("no file matches found for %s", file) + + continue + } + + flags = append(flags, f...) + } // add flag for draft from provided create draft flags = append(flags, fmt.Sprintf("--draft=%t", c.Draft)) diff --git a/cmd/vela-github-release/create_test.go b/cmd/vela-github-release/create_test.go index 9e040f1..f514f57 100644 --- a/cmd/vela-github-release/create_test.go +++ b/cmd/vela-github-release/create_test.go @@ -14,7 +14,112 @@ func TestGithubRelease_Create_Command(t *testing.T) { // setup types c := &Create{ Draft: false, - Files: []string{"file"}, + Files: []string{"testdata/file"}, + Notes: "notes", + NotesFile: "notes_file", + Prerelease: false, + Tag: "tag", + Target: "target", + Title: "title", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + createAction, + "tag", + "testdata/file", + fmt.Sprintf("--draft=%t", false), + fmt.Sprintf("--notes=%s", c.Notes), + fmt.Sprintf("--notes-file=%s", c.NotesFile), + fmt.Sprintf("--prerelease=%t", false), + fmt.Sprintf("--target=%s", c.Target), + fmt.Sprintf("--title=%s", c.Title), + ) + + got := c.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Create_Command_FileMissing(t *testing.T) { + // setup types + c := &Create{ + Draft: false, + Files: []string{"testdata/file_missing"}, + Notes: "notes", + NotesFile: "notes_file", + Prerelease: false, + Tag: "tag", + Target: "target", + Title: "title", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + createAction, + "tag", + fmt.Sprintf("--draft=%t", false), + fmt.Sprintf("--notes=%s", c.Notes), + fmt.Sprintf("--notes-file=%s", c.NotesFile), + fmt.Sprintf("--prerelease=%t", false), + fmt.Sprintf("--target=%s", c.Target), + fmt.Sprintf("--title=%s", c.Title), + ) + + got := c.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Create_Command_MultipleFiles(t *testing.T) { + // setup types + c := &Create{ + Draft: false, + Files: []string{"testdata/test1.txt", "testdata/test2.txt"}, + Notes: "notes", + NotesFile: "notes_file", + Prerelease: false, + Tag: "tag", + Target: "target", + Title: "title", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + createAction, + "tag", + "testdata/test1.txt", + "testdata/test2.txt", + fmt.Sprintf("--draft=%t", false), + fmt.Sprintf("--notes=%s", c.Notes), + fmt.Sprintf("--notes-file=%s", c.NotesFile), + fmt.Sprintf("--prerelease=%t", false), + fmt.Sprintf("--target=%s", c.Target), + fmt.Sprintf("--title=%s", c.Title), + ) + + got := c.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Create_Command_MultipleFilesGlob(t *testing.T) { + // setup types + c := &Create{ + Draft: false, + Files: []string{"testdata/*.txt"}, Notes: "notes", NotesFile: "notes_file", Prerelease: false, @@ -29,7 +134,8 @@ func TestGithubRelease_Create_Command(t *testing.T) { releaseCmd, createAction, "tag", - "file", + "testdata/test1.txt", + "testdata/test2.txt", fmt.Sprintf("--draft=%t", false), fmt.Sprintf("--notes=%s", c.Notes), fmt.Sprintf("--notes-file=%s", c.NotesFile), diff --git a/cmd/vela-github-release/testdata/file b/cmd/vela-github-release/testdata/file new file mode 100644 index 0000000..038d718 --- /dev/null +++ b/cmd/vela-github-release/testdata/file @@ -0,0 +1 @@ +testing diff --git a/cmd/vela-github-release/testdata/test1.txt b/cmd/vela-github-release/testdata/test1.txt new file mode 100644 index 0000000..a5bce3f --- /dev/null +++ b/cmd/vela-github-release/testdata/test1.txt @@ -0,0 +1 @@ +test1 diff --git a/cmd/vela-github-release/testdata/test2.txt b/cmd/vela-github-release/testdata/test2.txt new file mode 100644 index 0000000..180cf83 --- /dev/null +++ b/cmd/vela-github-release/testdata/test2.txt @@ -0,0 +1 @@ +test2 diff --git a/cmd/vela-github-release/upload.go b/cmd/vela-github-release/upload.go index 0207d5b..f1b1378 100644 --- a/cmd/vela-github-release/upload.go +++ b/cmd/vela-github-release/upload.go @@ -6,16 +6,15 @@ import ( "errors" "fmt" "os/exec" + "path/filepath" "github.com/sirupsen/logrus" ) const uploadAction = "upload" -var ( - // ErrorNoUploadTag is returned when the plugin is missing the upload tag. - ErrorNoUploadTag = errors.New("no upload tag provided") -) +// ErrorNoUploadTag is returned when the plugin is missing the upload tag. +var ErrorNoUploadTag = errors.New("no upload tag provided") // Upload represents the plugin configuration for Upload config information. type Upload struct { @@ -47,8 +46,21 @@ func (u *Upload) Command() *exec.Cmd { flags = append(flags, u.Tag) } - // add flag for files provided by upload files - flags = append(flags, u.Files...) + // iterate through the files and add them as parameters + for _, file := range u.Files { + f, err := filepath.Glob(file) + if err != nil { + logrus.Warnf("bad file pattern: %v", err) + } + + if f == nil { + logrus.Warnf("no file matches found for %s", file) + + continue + } + + flags = append(flags, f...) + } // add flag for upload from provided upload flags = append(flags, fmt.Sprintf("--clobber=%t", u.Clobber)) diff --git a/cmd/vela-github-release/upload_test.go b/cmd/vela-github-release/upload_test.go index 6969b39..bea693b 100644 --- a/cmd/vela-github-release/upload_test.go +++ b/cmd/vela-github-release/upload_test.go @@ -14,7 +14,82 @@ func TestGithubRelease_Upload_Command(t *testing.T) { // setup types u := &Upload{ Clobber: false, - Files: []string{"files"}, + Files: []string{"testdata/file"}, + Tag: "tag", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + uploadAction, + "tag", + "testdata/file", + fmt.Sprintf("--clobber=%t", u.Clobber), + ) + + got := u.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Upload_Command_FileMissing(t *testing.T) { + // setup types + u := &Upload{ + Clobber: false, + Files: []string{"testdata/file_missing"}, + Tag: "tag", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + uploadAction, + "tag", + fmt.Sprintf("--clobber=%t", u.Clobber), + ) + + got := u.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Upload_Command_MultipleFiles(t *testing.T) { + // setup types + u := &Upload{ + Clobber: false, + Files: []string{"testdata/test1.txt", "testdata/test2.txt"}, + Tag: "tag", + } + + //nolint:gosec // ignore for testing purposes + want := exec.Command( + _gh, + releaseCmd, + uploadAction, + "tag", + "testdata/test1.txt", + "testdata/test2.txt", + fmt.Sprintf("--clobber=%t", u.Clobber), + ) + + got := u.Command() + + if !reflect.DeepEqual(got, want) { + t.Errorf("execCmd is %v, want %v", got, want) + } +} + +func TestGithubRelease_Upload_Command_MultipleFilesGlob(t *testing.T) { + // setup types + u := &Upload{ + Clobber: false, + Files: []string{"testdata/*.txt"}, Tag: "tag", } @@ -24,7 +99,8 @@ func TestGithubRelease_Upload_Command(t *testing.T) { releaseCmd, uploadAction, "tag", - "files", + "testdata/test1.txt", + "testdata/test2.txt", fmt.Sprintf("--clobber=%t", u.Clobber), )