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

enhance: allow glob pattern for files in create/upload #68

Merged
merged 1 commit into from
Aug 23, 2024
Merged
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
32 changes: 32 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions cmd/vela-github-release/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os/exec"
"path/filepath"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -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))
Expand Down
110 changes: 108 additions & 2 deletions cmd/vela-github-release/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions cmd/vela-github-release/testdata/file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing
1 change: 1 addition & 0 deletions cmd/vela-github-release/testdata/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions cmd/vela-github-release/testdata/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
24 changes: 18 additions & 6 deletions cmd/vela-github-release/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
80 changes: 78 additions & 2 deletions cmd/vela-github-release/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

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

Expand Down
Loading