Skip to content

Commit

Permalink
Add release get-latest command
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Nov 30, 2022
1 parent 0676ea1 commit 471ea15
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,17 @@ newreleases release get github golang/go go1.13.5
newreleases release get mdsbe60td5gwgzetyksdfeyxt4 go1.13.5
```

The last argument is the version which is requested.
### Get the latest non-excluded project release information

To get information about only one release, there is the `get-latest` sub-command:

```sh
newreleases release get github golang/go
```

```sh
newreleases release get mdsbe60td5gwgzetyksdfeyxt4
```

### Get a release note

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/spf13/viper v1.10.1
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba
newreleases.io/newreleases v1.9.0
newreleases.io/newreleases v1.10.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -815,8 +815,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba h1:3xhBI8FZepFq4YtdqlW6Z8YzdKM3nAV9xpOvgzWX+us=
jaytaylor.com/html2text v0.0.0-20211105163654-bc68cce691ba/go.mod h1:OxvTsCwKosqQ1q7B+8FwXqg4rKZ/UG9dUW+g/VL2xH4=
newreleases.io/newreleases v1.9.0 h1:5Nuk4UBQ5WZdiuwtNCUNzCeRa/1gyK4Dp+YneRdYcYI=
newreleases.io/newreleases v1.9.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
newreleases.io/newreleases v1.10.0 h1:ZkWO3z3NL/MyvidYGZ7WbDERoaRZLAfqbIETLq4AQR8=
newreleases.io/newreleases v1.10.0/go.mod h1:IFoaLRTd4ZNVIEIZflkjRt+0Z4BOgivteiLGs9vPobw=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
46 changes: 46 additions & 0 deletions newreleases/cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (c *command) initReleaseCmd() (err error) {
if err := c.initReleaseGetCmd(cmd); err != nil {
return err
}
if err := c.initReleaseGetLatestCmd(cmd); err != nil {
return err
}
if err := c.initReleaseNoteCmd(cmd); err != nil {
return err
}
Expand Down Expand Up @@ -135,6 +138,47 @@ func (c *command) initReleaseGetCmd(releaseCmd *cobra.Command) (err error) {
return addClientFlags(cmd)
}

func (c *command) initReleaseGetLatestCmd(releaseCmd *cobra.Command) (err error) {
cmd := &cobra.Command{
Use: "get-latest [provider project_name] | [project_id]",
Short: "Get the latest non-excluded project release",
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := newClientContext(c.config)
defer cancel()

var release *newreleases.Release
switch len(args) {
case 1:
release, err = c.releasesService.GetLatestByProjectID(ctx, args[0])
case 2:
release, err = c.releasesService.GetLatestByProjectName(ctx, args[0], args[1])
default:
return cmd.Help()
}
if err != nil {
return err
}

if release == nil {
cmd.Println("Release not found.")
return nil
}

printRelease(cmd, release)
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := addClientConfigOptions(cmd, c.config); err != nil {
return err
}
return c.setReleasesService(cmd, args)
},
}

releaseCmd.AddCommand(cmd)
return addClientFlags(cmd)
}

func (c *command) initReleaseNoteCmd(releaseCmd *cobra.Command) (err error) {
cmd := &cobra.Command{
Use: "note [PROVIDER PROJECT_NAME] | [PROJECT_ID] version",
Expand Down Expand Up @@ -193,6 +237,8 @@ type releasesService interface {
ListByProjectName(ctx context.Context, provider, projectName string, page int) (releases []newreleases.Release, lastPage int, err error)
GetByProjectID(ctx context.Context, projectID, version string) (release *newreleases.Release, err error)
GetByProjectName(ctx context.Context, provider, projectName, version string) (release *newreleases.Release, err error)
GetLatestByProjectID(ctx context.Context, projectID string) (release *newreleases.Release, err error)
GetLatestByProjectName(ctx context.Context, provider, projectName string) (release *newreleases.Release, err error)
GetNoteByProjectID(ctx context.Context, projectID string, version string) (release *newreleases.ReleaseNote, err error)
GetNoteByProjectName(ctx context.Context, provider, projectName string, version string) (release *newreleases.ReleaseNote, err error)
}
Expand Down
86 changes: 86 additions & 0 deletions newreleases/cmd/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,78 @@ func TestReleaseCmd_Get(t *testing.T) {
}
}

func TestReleaseCmd_GetLatest(t *testing.T) {
for _, tc := range []struct {
name string
args []string
releasesService cmd.ReleasesService
wantOutputFunc func() string
wantError error
}{
{
name: "no releases",
args: []string{"github", "golang/go"},
releasesService: newMockReleasesService(nil, nil, 1, nil),
wantOutputFunc: func() string { return "Release not found.\n" },
},
{
name: "release",
args: []string{"github", "golang/go"},
releasesService: newMockReleasesService([]newreleases.Release{
{Version: "v0.1.0", Date: newTime(t, "2019-10-22T01:45:55Z")},
}, nil, 1, nil),
wantOutputFunc: func() string {
dateLen := len(newTime(t, "2019-10-22T01:45:55Z").Local().String())
return fmt.Sprintf("Version: v0.1.0%s \nDate: %s \n",
strings.Repeat(" ", dateLen-6),
newTime(t, "2019-10-22T01:45:55Z").Local(),
)
},
},
{
name: "release with optional flags",
args: []string{"github", "golang/go"},
releasesService: newMockReleasesService([]newreleases.Release{
{Version: "v0.1.0", Date: newTime(t, "2019-10-22T01:45:55Z"), IsPrerelease: true, HasNote: true, IsUpdated: true, IsExcluded: true},
}, nil, 1, nil),
wantOutputFunc: func() string {
dateLen := len(newTime(t, "2019-10-22T01:45:55Z").Local().String())
return fmt.Sprintf("Version: v0.1.0%s \nDate: %s \nPre-Release: yes%s \nHas Note: yes%s \nUpdated: yes%s \nExcluded: yes%s \n",
strings.Repeat(" ", dateLen-6),
newTime(t, "2019-10-22T01:45:55Z").Local(),
strings.Repeat(" ", dateLen-3),
strings.Repeat(" ", dateLen-3),
strings.Repeat(" ", dateLen-3),
strings.Repeat(" ", dateLen-3),
)
},
},
{
name: "error",
args: []string{"github", "golang/go"},
releasesService: newMockReleasesService(nil, nil, 1, errTest),
wantOutputFunc: func() string { return "" },
wantError: errTest,
},
} {
t.Run(tc.name, func(t *testing.T) {
var outputBuf bytes.Buffer
if err := newCommand(t,
cmd.WithArgs(append([]string{"release", "get-latest"}, tc.args...)...),
cmd.WithOutput(&outputBuf),
cmd.WithReleasesService(tc.releasesService),
).Execute(); err != tc.wantError {
t.Fatalf("got error %v, want %v", err, tc.wantError)
}

gotOutput := outputBuf.String()
if wantOutput := tc.wantOutputFunc(); gotOutput != wantOutput {
t.Errorf("got output %q, want %q", gotOutput, wantOutput)
}
})
}
}

func TestReleaseCmd_Note(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down Expand Up @@ -244,6 +316,20 @@ func (s mockReleasesService) GetByProjectName(ctx context.Context, provider, pro
return &s.releases[0], s.err
}

func (s mockReleasesService) GetLatestByProjectID(ctx context.Context, projectID string) (release *newreleases.Release, err error) {
if len(s.releases) == 0 {
return nil, s.err
}
return &s.releases[0], s.err
}

func (s mockReleasesService) GetLatestByProjectName(ctx context.Context, provider, projectName string) (release *newreleases.Release, err error) {
if len(s.releases) == 0 {
return nil, s.err
}
return &s.releases[0], s.err
}

func (s mockReleasesService) GetNoteByProjectID(ctx context.Context, projectID string, version string) (release *newreleases.ReleaseNote, err error) {
return s.note, s.err
}
Expand Down

0 comments on commit 471ea15

Please sign in to comment.