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

[Rajat] Add Github client to fetch latest release of proctor #78

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gojektech/proctor/io"

"github.com/spf13/cobra"
"github.com/gojektech/proctor/cmd/version/github"
)

var (
Expand All @@ -28,8 +29,8 @@ var (
}
)

func Execute(printer io.Printer, proctorDClient daemon.Client) {
versionCmd := version.NewCmd(printer)
func Execute(printer io.Printer, proctorDClient daemon.Client, githubClient github.LatestReleaseFetcher) {
versionCmd := version.NewCmd(printer, githubClient)
rootCmd.AddCommand(versionCmd)

descriptionCmd := description.NewCmd(printer, proctorDClient)
Expand Down
5 changes: 3 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/gojektech/proctor/io"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/gojektech/proctor/cmd/version/github"
)

func TestRootCmdUsage(t *testing.T) {
Execute(&io.MockPrinter{}, &daemon.MockClient{})
Execute(&io.MockPrinter{}, &daemon.MockClient{}, &github.MockClient{})

assert.Equal(t, "proctor", rootCmd.Use)
assert.Equal(t, "A command-line interface to run procs", rootCmd.Short)
Expand All @@ -27,7 +28,7 @@ func contains(commands []*cobra.Command, commandName string) bool {
}

func TestRootCmdSubCommands(t *testing.T) {
Execute(&io.MockPrinter{}, &daemon.MockClient{})
Execute(&io.MockPrinter{}, &daemon.MockClient{}, &github.MockClient{})

assert.True(t, contains(rootCmd.Commands(), "describe"))
assert.True(t, contains(rootCmd.Commands(), "execute"))
Expand Down
23 changes: 23 additions & 0 deletions cmd/version/github/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package github

import (
"context"
"github.com/google/go-github/github"
)

type LatestReleaseFetcher interface {
LatestRelease(owner, repository string) (*github.RepositoryRelease, error)
}

type client struct {
client *github.Client
}

func NewClient() *client {
return &client{github.NewClient(nil)}
}

func (gc *client) LatestRelease(owner, repository string) (*github.RepositoryRelease, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning '*github.RepositoryRelease' in the LatestRelease function, you can return string directly. Then in the caller you can directly use the value instead of '*release.TagName' in version.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

release, _, err := gc.client.Repositories.GetLatestRelease(context.Background(), owner, repository)
return release, err
}
15 changes: 15 additions & 0 deletions cmd/version/github/client_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package github

import (
"github.com/google/go-github/github"
"github.com/stretchr/testify/mock"
)

type MockClient struct {
mock.Mock
}

func (m *MockClient) LatestRelease(owner, repository string) (release *github.RepositoryRelease, err error) {
args := m.Called(owner, repository)
return args.Get(0).(*github.RepositoryRelease), args.Error(1)
}
7 changes: 6 additions & 1 deletion cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import (
"fmt"

"github.com/fatih/color"
"github.com/gojektech/proctor/cmd/version/github"
"github.com/gojektech/proctor/io"
"github.com/spf13/cobra"
)

const ClientVersion = "v0.6.0"

func NewCmd(printer io.Printer) *cobra.Command {
func NewCmd(printer io.Printer, fetcher github.LatestReleaseFetcher) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version of Proctor command-line tool",
Long: `Example: proctor version`,
Run: func(cmd *cobra.Command, args []string) {
printer.Println(fmt.Sprintf("Proctor: A Developer Friendly Automation Orchestrator %s", ClientVersion), color.Reset)
release, e := fetcher.LatestRelease("gojektech", "proctor")
if e == nil && *release.TagName != ClientVersion {
printer.Println(fmt.Sprintf("Your version of Proctor client is out of date! The latest version is %s You can update by either running brew upgrade proctor or downloading a release for your OS here: https://github.com/gojektech/proctor/releases", *release.TagName), color.Reset)
}
},
}
}
30 changes: 27 additions & 3 deletions cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,49 @@ import (
"testing"

"github.com/fatih/color"
gh "github.com/gojektech/proctor/cmd/version/github"
"github.com/gojektech/proctor/io"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestVersionCmdUsage(t *testing.T) {
versionCmd := NewCmd(&io.MockPrinter{})
githubClient := &gh.MockClient{}
versionCmd := NewCmd(&io.MockPrinter{}, githubClient)
assert.Equal(t, "version", versionCmd.Use)
assert.Equal(t, "Print version of Proctor command-line tool", versionCmd.Short)
assert.Equal(t, "Example: proctor version", versionCmd.Long)
}

func TestVersionCmd(t *testing.T) {
func TestLatestVersionCmd(t *testing.T) {
mockPrinter := &io.MockPrinter{}
versionCmd := NewCmd(mockPrinter)
githubClient := &gh.MockClient{}
versionCmd := NewCmd(mockPrinter, githubClient)
version := "v0.6.0"

mockPrinter.On("Println", fmt.Sprintf("Proctor: A Developer Friendly Automation Orchestrator %s", ClientVersion), color.Reset).Once()
githubClient.On("LatestRelease", "gojektech", "proctor").Return(&github.RepositoryRelease{TagName: &version}, nil)

versionCmd.Run(&cobra.Command{}, []string{})

mockPrinter.AssertExpectations(t)
}

func TestOldVersionCmd(t *testing.T) {
mockPrinter := &io.MockPrinter{}
githubClient := &gh.MockClient{}
version := "v0.9.0"
RajatVaryani marked this conversation as resolved.
Show resolved Hide resolved
versionCmd := NewCmd(mockPrinter, githubClient)

mockPrinter.On("Println", fmt.Sprintf("Proctor: A Developer Friendly Automation Orchestrator %s", ClientVersion), color.Reset).Once()
mockPrinter.On("Println", fmt.Sprintf("Your version of Proctor client is out of date!" +
" The latest version is %s You can update by either running brew upgrade proctor or downloading a release for your OS here:" +
" https://github.com/gojektech/proctor/releases", version), color.Reset).Once()
githubClient.On("LatestRelease", "gojektech", "proctor").Return(&github.RepositoryRelease{TagName: &version}, nil)

versionCmd.Run(&cobra.Command{}, []string{})

mockPrinter.AssertExpectations(t)
githubClient.AssertExpectations(t)
}
22 changes: 19 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ import:
version: v0.0.1
- package: github.com/thingful/httpmock
version: 0.0.2
- package: github.com/google/go-github
version: v24.0.0
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"github.com/gojektech/proctor/config"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/cmd/version/github"
)

func main() {
printer := io.GetPrinter()
proctorConfigLoader := config.NewLoader()
proctorDClient := daemon.NewClient(printer, proctorConfigLoader)
githubClient := github.NewClient()

cmd.Execute(printer, proctorDClient)
cmd.Execute(printer, proctorDClient, githubClient)
}