-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
791 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package user | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// Update modifies a dashboard based off the provided configuration. | ||
func (c *Config) Update(client *vela.Client) error { | ||
logrus.Debug("executing update for dashboard configuration") | ||
|
||
var ( | ||
user *api.User | ||
err error | ||
) | ||
|
||
if len(c.Name) > 0 { | ||
user, _, err = client.User.Get(c.Name) | ||
} else { | ||
user, _, err = client.User.GetCurrent() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// drop specified dashboards from the user | ||
if len(c.DropDashboards) > 0 { | ||
newDashboards := []string{} | ||
|
||
for _, d := range user.GetDashboards() { | ||
if !slices.Contains(c.DropDashboards, d) { | ||
newDashboards = append(newDashboards, d) | ||
} | ||
} | ||
|
||
user.SetDashboards(newDashboards) | ||
} | ||
|
||
// add specified repositories to the dashboard | ||
if len(c.AddDashboards) > 0 { | ||
dashboards := user.GetDashboards() | ||
|
||
for _, d := range c.AddDashboards { | ||
_, _, err := client.Dashboard.Get(d) | ||
if err != nil { | ||
return fmt.Errorf("unable to get dashboard %s: %w", d, err) | ||
} | ||
|
||
dashboards = append(dashboards, d) | ||
} | ||
|
||
user.SetDashboards(dashboards) | ||
} | ||
|
||
// drop specified favorites from the user | ||
if len(c.DropFavorites) > 0 { | ||
newFavorites := []string{} | ||
|
||
for _, f := range user.GetFavorites() { | ||
if !slices.Contains(c.DropFavorites, f) { | ||
newFavorites = append(newFavorites, f) | ||
} | ||
} | ||
|
||
user.SetFavorites(newFavorites) | ||
} | ||
|
||
// add specified favorites to the user | ||
if len(c.AddFavorites) > 0 { | ||
favorites := user.GetFavorites() | ||
|
||
for _, f := range c.AddFavorites { | ||
splitRepo := strings.Split(f, "/") | ||
|
||
if len(splitRepo) != 2 { | ||
return fmt.Errorf("invalid format for repository: %s (valid format: <org>/<repo>)", f) | ||
} | ||
|
||
_, _, err := client.Repo.Get(splitRepo[0], splitRepo[1]) | ||
if err != nil { | ||
return fmt.Errorf("unable to get repo %s: %w", f, err) | ||
} | ||
|
||
favorites = append(favorites, f) | ||
} | ||
|
||
user.SetFavorites(favorites) | ||
} | ||
|
||
// send API call to modify the user | ||
if len(c.Name) > 0 { | ||
user, _, err = client.User.Update(c.Name, user) | ||
} else { | ||
user, _, err = client.User.UpdateCurrent(user) | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return outputUser(user, c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package user | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestRepo_Config_Update(t *testing.T) { | ||
// setup test server | ||
s := httptest.NewServer(server.FakeHandler()) | ||
|
||
// create a vela client | ||
client, err := vela.NewClient(s.URL, "vela", nil) | ||
if err != nil { | ||
t.Errorf("unable to create client: %v", err) | ||
} | ||
|
||
// setup tests | ||
tests := []struct { | ||
failure bool | ||
config *Config | ||
}{ | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Name: "octocat", | ||
AddFavorites: []string{"foo/bar", "foo/baz"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Name: "octocat", | ||
DropFavorites: []string{"foo/bar", "foo/baz"}, | ||
DropDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "dump", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar", "foo/baz"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
DropFavorites: []string{"foo/bar", "foo/baz"}, | ||
DropDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "dump", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar", "foo/baz"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "json", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar", "foo/baz"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "spew", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar", "foo/baz"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "yaml", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar", "not-found"}, | ||
AddDashboards: []string{"c8da1302-07d6-11ea-882f-4893bca275b8"}, | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: true, | ||
config: &Config{ | ||
AddFavorites: []string{"foo/bar"}, | ||
AddDashboards: []string{"0"}, | ||
Output: "", | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Update(client) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Update should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Update returned err: %v", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package user | ||
|
||
import ( | ||
"github.com/go-vela/cli/internal/output" | ||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// Config represents the configuration necessary | ||
// to perform user related requests with Vela. | ||
type Config struct { | ||
Name string | ||
AddFavorites []string | ||
DropFavorites []string | ||
AddDashboards []string | ||
DropDashboards []string | ||
Output string | ||
Color output.ColorOptions | ||
} | ||
|
||
func outputUser(user *api.User, c *Config) error { | ||
// handle the output based off the provided configuration | ||
switch c.Output { | ||
case output.DriverDump: | ||
// output the user in dump format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump | ||
return output.Dump(user) | ||
case output.DriverJSON: | ||
// output the user in JSON format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON | ||
return output.JSON(user, c.Color) | ||
case output.DriverSpew: | ||
// output the user in spew format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew | ||
return output.Spew(user) | ||
case output.DriverYAML: | ||
// output the user in YAML format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML | ||
return output.YAML(user, c.Color) | ||
default: | ||
// output the user in stdout format | ||
// | ||
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout | ||
return output.Stdout(user) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package user | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// View inspects a user based off the provided configuration. | ||
func (c *Config) View(client *vela.Client) error { | ||
logrus.Debug("executing view for user configuration") | ||
|
||
var ( | ||
user *api.User | ||
err error | ||
) | ||
|
||
// send API call to capture user | ||
if len(c.Name) > 0 { | ||
user, _, err = client.User.Get(c.Name) | ||
} else { | ||
user, _, err = client.User.GetCurrent() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return outputUser(user, c) | ||
} |
Oops, something went wrong.