Skip to content

Commit

Permalink
feat: user resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Sep 6, 2024
1 parent 32b3fc2 commit e944dd1
Show file tree
Hide file tree
Showing 18 changed files with 791 additions and 46 deletions.
2 changes: 1 addition & 1 deletion action/pipeline/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Config) Exec(client compiler.Engine) error {
WithLocal(true).
WithRepo(r).
WithLocalTemplates(c.TemplateFiles).
Compile(path)
Compile(context.Background(), path)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions action/pipeline/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package pipeline

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -150,15 +151,15 @@ func (c *Config) ValidateLocal(client compiler.Engine) error {
}

// compile the object into a pipeline with ruledata
p, _, err = client.CompileLite(path, ruleData, false)
p, _, err = client.CompileLite(context.Background(), path, ruleData, false)
if err != nil {
return err
}
} else {
logrus.Debugf("compiling pipeline")

// compile the object into a pipeline without ruledata
p, _, err = client.CompileLite(path, nil, false)
p, _, err = client.CompileLite(context.Background(), path, nil, false)
if err != nil {
return err
}
Expand Down
111 changes: 111 additions & 0 deletions action/user/update.go
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)
}
120 changes: 120 additions & 0 deletions action/user/update_test.go
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)
}
}
}
51 changes: 51 additions & 0 deletions action/user/user.go
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)
}
}
33 changes: 33 additions & 0 deletions action/user/view.go
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)
}
Loading

0 comments on commit e944dd1

Please sign in to comment.