From 9c9e6a0f4d6e375acff03fd862105e36ee2d6300 Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Mon, 18 Feb 2019 11:04:28 +0530 Subject: [PATCH 1/3] [Rajat] Add Github client to fetch latest release of proctor --- client/github.go | 24 ++++++++++++++++++++++++ client/github_test.go | 11 +++++++++++ glide.lock | 22 +++++++++++++++++++--- glide.yaml | 2 ++ 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 client/github.go create mode 100644 client/github_test.go diff --git a/client/github.go b/client/github.go new file mode 100644 index 00000000..a30d0e5c --- /dev/null +++ b/client/github.go @@ -0,0 +1,24 @@ +package client + +import ( + "github.com/google/go-github/github" + "context" + "github.com/gojektech/proctor/cmd/version" +) + +type LatestReleaseChecker interface { + IsLatestRelease() bool +} + +type GithubClient struct { + client *github.Client +} + +func NewGithubClient() *GithubClient { + return &GithubClient{github.NewClient(nil)} +} + +func (gc *GithubClient) IsLatestRelease() (bool, error) { + release, _, err := gc.client.Repositories.GetLatestRelease(context.Background(), "gojektech", "proctor") + return version.ClientVersion == *release.TagName, err +} diff --git a/client/github_test.go b/client/github_test.go new file mode 100644 index 00000000..f380f271 --- /dev/null +++ b/client/github_test.go @@ -0,0 +1,11 @@ +package client + +import ( + "testing" + "context" +) + +func TestGithubClient_IsLatestRelease(t *testing.T) { + githubClient := NewGithubClient() + _, _, _ = githubClient.client.Repositories.GetLatestRelease(context.Background(), "gojektech", "proctor") +} \ No newline at end of file diff --git a/glide.lock b/glide.lock index 6305865f..7dd48393 100644 --- a/glide.lock +++ b/glide.lock @@ -1,13 +1,17 @@ -hash: 99d5240e2827968d8fea8861618d19371699d856ce4634572dfab614345e80c7 -updated: 2018-11-21T15:16:40.295388+05:30 +hash: 19b9d4912ddf0523c6e59bb0df99744c09ffc76e9864340ad2c8764b11715583 +updated: 2019-02-18T11:00:01.471006+05:30 imports: - name: cloud.google.com/go version: 3b1ae45394a234c385be014e9a488f2bb6eef821 subpackages: - compute/metadata - internal +- name: github.com/badoux/checkmail + version: 9661bd69e9ad6fce1f7579022bdab0440807722a - name: github.com/briandowns/spinner version: 48dbb65d7bd5c74ab50d53d04c949f20e3d14944 +- name: github.com/certifi/gocertifi + version: 3fd9e1adb12b72d2f3f82191d49be9b93c69f67c - name: github.com/davecgh/go-spew version: 782f4967f2dc4564575ca782fe2d04090b5faca8 subpackages: @@ -21,6 +25,8 @@ imports: subpackages: - internal - redis +- name: github.com/getsentry/raven-go + version: 3f7439d3e74d88e21d196ba20eb61a5a958bc118 - name: github.com/ghodss/yaml version: 73d445a93680fa1a78ae23a5839bad48f32ba1ee - name: github.com/gogo/protobuf @@ -40,6 +46,14 @@ imports: - ptypes/timestamp - name: github.com/google/btree version: 7d79101e329e5a3adf994758c578dab82b90c017 +- name: github.com/google/go-github + version: 1c3f0aaee1ce742b655f1ba8f33740103e904c5c + subpackages: + - github +- name: github.com/google/go-querystring + version: 44c6ddd0a2342c386950e880b658017258da92fc + subpackages: + - query - name: github.com/google/gofuzz version: 44d81051d367757e1c7c6a5a86423ece9afcf63c - name: github.com/googleapis/gnostic @@ -123,9 +137,11 @@ imports: version: 792786c7400a136282c1664665ae0a8db921c6c2 subpackages: - difflib +- name: github.com/robfig/cron + version: b41be1df696709bb6395fe435af20370037c0b4c - name: github.com/satori/go.uuid version: f58768cc1a7a7e77a3bd49e98cdd21419399b6a3 -- name: github.com/Sirupsen/logrus +- name: github.com/sirupsen/logrus version: 44067abb194b1bc8b342e1f2120f8d3ea691b834 - name: github.com/spf13/afero version: d40851caa0d747393da1ffb28f7f9d8b4eeffebd diff --git a/glide.yaml b/glide.yaml index b93d0b40..d8a3ea13 100644 --- a/glide.yaml +++ b/glide.yaml @@ -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 From 3ef3ab565d5c3b55c31cc3b0e508c29c6430b733 Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Wed, 20 Feb 2019 18:54:04 +0530 Subject: [PATCH 2/3] [Rajat] Intimate user when obsolete version of proctor is used --- client/github.go | 24 ------------------------ client/github_test.go | 11 ----------- cmd/root.go | 5 +++-- cmd/root_test.go | 5 +++-- cmd/version/github/client.go | 23 +++++++++++++++++++++++ cmd/version/github/client_mock.go | 15 +++++++++++++++ cmd/version/version.go | 7 ++++++- cmd/version/version_test.go | 30 +++++++++++++++++++++++++++--- main.go | 4 +++- 9 files changed, 80 insertions(+), 44 deletions(-) delete mode 100644 client/github.go delete mode 100644 client/github_test.go create mode 100644 cmd/version/github/client.go create mode 100644 cmd/version/github/client_mock.go diff --git a/client/github.go b/client/github.go deleted file mode 100644 index a30d0e5c..00000000 --- a/client/github.go +++ /dev/null @@ -1,24 +0,0 @@ -package client - -import ( - "github.com/google/go-github/github" - "context" - "github.com/gojektech/proctor/cmd/version" -) - -type LatestReleaseChecker interface { - IsLatestRelease() bool -} - -type GithubClient struct { - client *github.Client -} - -func NewGithubClient() *GithubClient { - return &GithubClient{github.NewClient(nil)} -} - -func (gc *GithubClient) IsLatestRelease() (bool, error) { - release, _, err := gc.client.Repositories.GetLatestRelease(context.Background(), "gojektech", "proctor") - return version.ClientVersion == *release.TagName, err -} diff --git a/client/github_test.go b/client/github_test.go deleted file mode 100644 index f380f271..00000000 --- a/client/github_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package client - -import ( - "testing" - "context" -) - -func TestGithubClient_IsLatestRelease(t *testing.T) { - githubClient := NewGithubClient() - _, _, _ = githubClient.client.Repositories.GetLatestRelease(context.Background(), "gojektech", "proctor") -} \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index c166041f..aa870efb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,6 +18,7 @@ import ( "github.com/gojektech/proctor/io" "github.com/spf13/cobra" + "github.com/gojektech/proctor/cmd/version/github" ) var ( @@ -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) diff --git a/cmd/root_test.go b/cmd/root_test.go index b8ad5807..a0718286 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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) @@ -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")) diff --git a/cmd/version/github/client.go b/cmd/version/github/client.go new file mode 100644 index 00000000..1781a5ce --- /dev/null +++ b/cmd/version/github/client.go @@ -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) { + release, _, err := gc.client.Repositories.GetLatestRelease(context.Background(), owner, repository) + return release, err +} diff --git a/cmd/version/github/client_mock.go b/cmd/version/github/client_mock.go new file mode 100644 index 00000000..4a92ce76 --- /dev/null +++ b/cmd/version/github/client_mock.go @@ -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) +} diff --git a/cmd/version/version.go b/cmd/version/version.go index fd6474cb..d8f5d44b 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -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) + } }, } } diff --git a/cmd/version/version_test.go b/cmd/version/version_test.go index 41d5543b..e93ce707 100644 --- a/cmd/version/version_test.go +++ b/cmd/version/version_test.go @@ -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" + 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) +} diff --git a/main.go b/main.go index 2c377f31..1b34e926 100644 --- a/main.go +++ b/main.go @@ -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) } From ddcc0b5b7c7d1adffc413ed9fa03a70d41b2d56a Mon Sep 17 00:00:00 2001 From: Rajat Varyani Date: Mon, 25 Mar 2019 21:50:46 +0530 Subject: [PATCH 3/3] [Rajat] Refactor code based on feedback --- cmd/version/github/client.go | 6 +++--- cmd/version/github/client_mock.go | 5 ++--- cmd/version/version.go | 4 ++-- cmd/version/version_test.go | 11 +++++------ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/version/github/client.go b/cmd/version/github/client.go index 1781a5ce..20ee8d51 100644 --- a/cmd/version/github/client.go +++ b/cmd/version/github/client.go @@ -6,7 +6,7 @@ import ( ) type LatestReleaseFetcher interface { - LatestRelease(owner, repository string) (*github.RepositoryRelease, error) + LatestRelease(owner, repository string) (string, error) } type client struct { @@ -17,7 +17,7 @@ func NewClient() *client { return &client{github.NewClient(nil)} } -func (gc *client) LatestRelease(owner, repository string) (*github.RepositoryRelease, error) { +func (gc *client) LatestRelease(owner, repository string) (string, error) { release, _, err := gc.client.Repositories.GetLatestRelease(context.Background(), owner, repository) - return release, err + return *release.TagName, err } diff --git a/cmd/version/github/client_mock.go b/cmd/version/github/client_mock.go index 4a92ce76..45b641c1 100644 --- a/cmd/version/github/client_mock.go +++ b/cmd/version/github/client_mock.go @@ -1,7 +1,6 @@ package github import ( - "github.com/google/go-github/github" "github.com/stretchr/testify/mock" ) @@ -9,7 +8,7 @@ type MockClient struct { mock.Mock } -func (m *MockClient) LatestRelease(owner, repository string) (release *github.RepositoryRelease, err error) { +func (m *MockClient) LatestRelease(owner, repository string) (release string, err error) { args := m.Called(owner, repository) - return args.Get(0).(*github.RepositoryRelease), args.Error(1) + return args.Get(0).(string), args.Error(1) } diff --git a/cmd/version/version.go b/cmd/version/version.go index d8f5d44b..4b48d73d 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -19,8 +19,8 @@ func NewCmd(printer io.Printer, fetcher github.LatestReleaseFetcher) *cobra.Comm 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) + if e == nil && release != 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), color.Reset) } }, } diff --git a/cmd/version/version_test.go b/cmd/version/version_test.go index e93ce707..3eb16bb7 100644 --- a/cmd/version/version_test.go +++ b/cmd/version/version_test.go @@ -7,7 +7,6 @@ import ( "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" ) @@ -27,7 +26,7 @@ func TestLatestVersionCmd(t *testing.T) { 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) + githubClient.On("LatestRelease", "gojektech", "proctor").Return(version, nil) versionCmd.Run(&cobra.Command{}, []string{}) @@ -37,14 +36,14 @@ func TestLatestVersionCmd(t *testing.T) { func TestOldVersionCmd(t *testing.T) { mockPrinter := &io.MockPrinter{} githubClient := &gh.MockClient{} - version := "v0.9.0" + version := "v1000.0.0" 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:" + + 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) + githubClient.On("LatestRelease", "gojektech", "proctor").Return(version, nil) versionCmd.Run(&cobra.Command{}, []string{})