diff options
author | Nick Thomas <nick@gitlab.com> | 2019-03-15 17:16:17 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-03-15 17:16:17 +0000 |
commit | f237aba6df1c1873f1f9d5ba18c3b8924d85cb51 (patch) | |
tree | 22d69b9450693bb153e58dbe8b7cd6feb3f8e1e0 /go/internal/command | |
parent | 049beb74303a03d9fa598d23b150e0ccea3cd60d (diff) | |
parent | 83c0f18e1de04b3bad9c424084e738e911c47336 (diff) | |
download | gitlab-shell-f237aba6df1c1873f1f9d5ba18c3b8924d85cb51.tar.gz |
Merge branch 'bvl-discover-command' into 'master'
Call gitlab "/internal/discover" from go
Closes #175
See merge request gitlab-org/gitlab-shell!283
Diffstat (limited to 'go/internal/command')
-rw-r--r-- | go/internal/command/command.go | 3 | ||||
-rw-r--r-- | go/internal/command/discover/discover.go | 34 | ||||
-rw-r--r-- | go/internal/command/discover/discover_test.go | 131 | ||||
-rw-r--r-- | go/internal/command/fallback/fallback.go | 4 | ||||
-rw-r--r-- | go/internal/command/reporting/reporter.go | 8 |
5 files changed, 176 insertions, 4 deletions
diff --git a/go/internal/command/command.go b/go/internal/command/command.go index cb2acdc..d4649de 100644 --- a/go/internal/command/command.go +++ b/go/internal/command/command.go @@ -4,11 +4,12 @@ import ( "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/discover" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/fallback" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/reporting" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" ) type Command interface { - Execute() error + Execute(*reporting.Reporter) error } func New(arguments []string, config *config.Config) (Command, error) { diff --git a/go/internal/command/discover/discover.go b/go/internal/command/discover/discover.go index 63a7a32..8ad2868 100644 --- a/go/internal/command/discover/discover.go +++ b/go/internal/command/discover/discover.go @@ -4,7 +4,9 @@ import ( "fmt" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/reporting" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/discover" ) type Command struct { @@ -12,6 +14,34 @@ type Command struct { Args *commandargs.CommandArgs } -func (c *Command) Execute() error { - return fmt.Errorf("No feature is implemented yet") +func (c *Command) Execute(reporter *reporting.Reporter) error { + response, err := c.getUserInfo() + if err != nil { + return fmt.Errorf("Failed to get username: %v", err) + } + + if response.IsAnonymous() { + fmt.Fprintf(reporter.Out, "Welcome to GitLab, Anonymous!\n") + } else { + fmt.Fprintf(reporter.Out, "Welcome to GitLab, @%s!\n", response.Username) + } + + return nil +} + +func (c *Command) getUserInfo() (*discover.Response, error) { + client, err := discover.NewClient(c.Config) + if err != nil { + return nil, err + } + + if c.Args.GitlabKeyId != "" { + return client.GetByKeyId(c.Args.GitlabKeyId) + } else if c.Args.GitlabUsername != "" { + return client.GetByUsername(c.Args.GitlabUsername) + } else { + // There was no 'who' information, this matches the ruby error + // message. + return nil, fmt.Errorf("who='' is invalid") + } } diff --git a/go/internal/command/discover/discover_test.go b/go/internal/command/discover/discover_test.go new file mode 100644 index 0000000..ec6f931 --- /dev/null +++ b/go/internal/command/discover/discover_test.go @@ -0,0 +1,131 @@ +package discover + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/commandargs" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/reporting" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/testserver" +) + +var ( + testConfig = &config.Config{GitlabUrl: "http+unix://" + testserver.TestSocket} + requests = []testserver.TestRequestHandler{ + { + Path: "/api/v4/internal/discover", + Handler: func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("key_id") == "1" || r.URL.Query().Get("username") == "alex-doe" { + body := map[string]interface{}{ + "id": 2, + "username": "alex-doe", + "name": "Alex Doe", + } + json.NewEncoder(w).Encode(body) + } else if r.URL.Query().Get("username") == "broken_message" { + body := map[string]string{ + "message": "Forbidden!", + } + w.WriteHeader(http.StatusForbidden) + json.NewEncoder(w).Encode(body) + } else if r.URL.Query().Get("username") == "broken" { + w.WriteHeader(http.StatusInternalServerError) + } else { + fmt.Fprint(w, "null") + } + }, + }, + } +) + +func TestExecute(t *testing.T) { + cleanup, err := testserver.StartSocketHttpServer(requests) + require.NoError(t, err) + defer cleanup() + + testCases := []struct { + desc string + arguments *commandargs.CommandArgs + expectedOutput string + }{ + { + desc: "With a known username", + arguments: &commandargs.CommandArgs{GitlabUsername: "alex-doe"}, + expectedOutput: "Welcome to GitLab, @alex-doe!\n", + }, + { + desc: "With a known key id", + arguments: &commandargs.CommandArgs{GitlabKeyId: "1"}, + expectedOutput: "Welcome to GitLab, @alex-doe!\n", + }, + { + desc: "With an unknown key", + arguments: &commandargs.CommandArgs{GitlabKeyId: "-1"}, + expectedOutput: "Welcome to GitLab, Anonymous!\n", + }, + { + desc: "With an unknown username", + arguments: &commandargs.CommandArgs{GitlabUsername: "unknown"}, + expectedOutput: "Welcome to GitLab, Anonymous!\n", + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + cmd := &Command{Config: testConfig, Args: tc.arguments} + buffer := &bytes.Buffer{} + + err := cmd.Execute(&reporting.Reporter{Out: buffer}) + + assert.NoError(t, err) + assert.Equal(t, tc.expectedOutput, buffer.String()) + }) + } +} + +func TestFailingExecute(t *testing.T) { + cleanup, err := testserver.StartSocketHttpServer(requests) + require.NoError(t, err) + defer cleanup() + + testCases := []struct { + desc string + arguments *commandargs.CommandArgs + expectedError string + }{ + { + desc: "With missing arguments", + arguments: &commandargs.CommandArgs{}, + expectedError: "Failed to get username: who='' is invalid", + }, + { + desc: "When the API returns an error", + arguments: &commandargs.CommandArgs{GitlabUsername: "broken_message"}, + expectedError: "Failed to get username: Forbidden!", + }, + { + desc: "When the API fails", + arguments: &commandargs.CommandArgs{GitlabUsername: "broken"}, + expectedError: "Failed to get username: Internal API error (500)", + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + cmd := &Command{Config: testConfig, Args: tc.arguments} + buffer := &bytes.Buffer{} + + err := cmd.Execute(&reporting.Reporter{Out: buffer}) + + assert.Empty(t, buffer.String()) + assert.EqualError(t, err, tc.expectedError) + }) + } +} diff --git a/go/internal/command/fallback/fallback.go b/go/internal/command/fallback/fallback.go index a136657..a2c73ed 100644 --- a/go/internal/command/fallback/fallback.go +++ b/go/internal/command/fallback/fallback.go @@ -4,6 +4,8 @@ import ( "os" "path/filepath" "syscall" + + "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/reporting" ) type Command struct{} @@ -12,7 +14,7 @@ var ( binDir = filepath.Dir(os.Args[0]) ) -func (c *Command) Execute() error { +func (c *Command) Execute(_ *reporting.Reporter) error { rubyCmd := filepath.Join(binDir, "gitlab-shell-ruby") execErr := syscall.Exec(rubyCmd, os.Args, os.Environ()) return execErr diff --git a/go/internal/command/reporting/reporter.go b/go/internal/command/reporting/reporter.go new file mode 100644 index 0000000..74bca59 --- /dev/null +++ b/go/internal/command/reporting/reporter.go @@ -0,0 +1,8 @@ +package reporting + +import "io" + +type Reporter struct { + Out io.Writer + ErrOut io.Writer +} |