-
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.
* dashboard add update get view * tests * comments and typos --------- Co-authored-by: david may <1301201+wass3r@users.noreply.github.com>
- Loading branch information
Showing
25 changed files
with
1,378 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// Add creates a dashboard based off the provided configuration. | ||
func (c *Config) Add(client *vela.Client) error { | ||
logrus.Debug("executing add for dashboard configuration") | ||
|
||
dashRepos := []*api.DashboardRepo{} | ||
dashAdmins := []*api.User{} | ||
|
||
// generate dashboard repos | ||
for _, r := range c.AddRepos { | ||
repo := new(api.DashboardRepo) | ||
repo.SetName(r) | ||
|
||
if len(c.Branches) > 0 { | ||
repo.SetBranches(c.Branches) | ||
} | ||
|
||
if len(c.Events) > 0 { | ||
repo.SetEvents(c.Events) | ||
} | ||
|
||
dashRepos = append(dashRepos, repo) | ||
} | ||
|
||
// generate dashboard admins | ||
for _, u := range c.AddAdmins { | ||
admin := new(api.User) | ||
admin.SetName(u) | ||
|
||
dashAdmins = append(dashAdmins, admin) | ||
} | ||
|
||
// create the dashboard object | ||
d := &api.Dashboard{ | ||
Name: vela.String(c.Name), | ||
Repos: &dashRepos, | ||
Admins: &dashAdmins, | ||
} | ||
|
||
logrus.Tracef("adding dashboard %s", c.Name) | ||
|
||
// send API call to add a dashboard | ||
dashboard, _, err := client.Dashboard.Add(d) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return outputDashboard(dashboard, 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,65 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestDashboard_Config_Add(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{ | ||
Action: "add", | ||
Name: "my-dashboard", | ||
AddRepos: []string{"github/octocat"}, | ||
AddAdmins: []string{"octocat"}, | ||
Branches: []string{"main", "dev"}, | ||
Events: []string{"push", "tag"}, | ||
Output: "json", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "add", | ||
Name: "my-dashboard", | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Add(client) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Add should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Add 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,20 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
// Config represents the configuration necessary | ||
// to perform dashboard related requests with Vela. | ||
type Config struct { | ||
Action string | ||
Name string | ||
ID string | ||
AddRepos []string | ||
TargetRepos []string | ||
DropRepos []string | ||
Branches []string | ||
Events []string | ||
AddAdmins []string | ||
DropAdmins []string | ||
Full bool | ||
Output string | ||
} |
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,8 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package dashboard provides the defined CLI dashboard actions for Vela. | ||
// | ||
// Usage: | ||
// | ||
// import "github.com/go-vela/cli/action/dashboard" | ||
package dashboard |
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,35 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// Get captures a list of dashboards based off the provided configuration. | ||
func (c *Config) Get(client *vela.Client) error { | ||
logrus.Debug("executing get for dashboard configuration") | ||
|
||
// send API call to capture a list of dashboards | ||
dashCards, _, err := client.Dashboard.GetAllUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Full { | ||
err = outputDashboard(dashCards, c) | ||
} else { | ||
dashboards := []*api.Dashboard{} | ||
|
||
for _, d := range *dashCards { | ||
dashboards = append(dashboards, d.Dashboard) | ||
} | ||
|
||
err = outputDashboard(dashboards, c) | ||
} | ||
|
||
return 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,62 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestDashboard_Config_Get(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{ | ||
Action: "get", | ||
Full: true, | ||
Output: "", | ||
}, | ||
}, | ||
{ | ||
failure: false, | ||
config: &Config{ | ||
Action: "get", | ||
Full: false, | ||
Output: "dump", | ||
}, | ||
}, | ||
} | ||
|
||
// run tests | ||
for _, test := range tests { | ||
err := test.config.Get(client) | ||
|
||
if test.failure { | ||
if err == nil { | ||
t.Errorf("Get should have returned err") | ||
} | ||
|
||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("Get 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,125 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dashboard | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/go-vela/sdk-go/vela" | ||
api "github.com/go-vela/server/api/types" | ||
"github.com/go-vela/server/constants" | ||
) | ||
|
||
// Update modifies a dashboard based off the provided configuration. | ||
func (c *Config) Update(client *vela.Client) error { | ||
logrus.Debug("executing update for dashboard configuration") | ||
|
||
dashCard, _, err := client.Dashboard.Get(c.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// pull dashboard metadata from the API response | ||
dashboard := dashCard.Dashboard | ||
|
||
// drop specified repositories from the dashboard | ||
if len(c.DropRepos) > 0 { | ||
newRepos := []*api.DashboardRepo{} | ||
|
||
for _, r := range dashboard.GetRepos() { | ||
if !slices.Contains(c.DropRepos, r.GetName()) { | ||
newRepos = append(newRepos, r) | ||
} | ||
} | ||
|
||
dashboard.SetRepos(newRepos) | ||
} | ||
|
||
// add specified repositories from the dashboard | ||
if len(c.AddRepos) > 0 { | ||
repos := dashboard.GetRepos() | ||
|
||
for _, r := range c.AddRepos { | ||
repo := new(api.DashboardRepo) | ||
repo.SetName(r) | ||
|
||
if len(c.Branches) > 0 { | ||
repo.SetBranches(c.Branches) | ||
} | ||
|
||
if len(c.Events) > 0 { | ||
repo.SetEvents(c.Events) | ||
} | ||
|
||
repos = append(repos, repo) | ||
} | ||
|
||
dashboard.SetRepos(repos) | ||
} | ||
|
||
// update specified repositories from the dashboard | ||
if len(c.TargetRepos) > 0 { | ||
repos := dashboard.GetRepos() | ||
for _, r := range repos { | ||
if slices.Contains(c.TargetRepos, r.GetName()) { | ||
if len(c.Branches) > 0 { | ||
r.SetBranches(c.Branches) | ||
} | ||
|
||
if len(c.Events) > 0 { | ||
r.SetEvents(c.Events) | ||
} | ||
} | ||
} | ||
|
||
dashboard.SetRepos(repos) | ||
} | ||
|
||
// drop specified admins from the dashboard | ||
if len(c.DropAdmins) > 0 { | ||
newAdmins := []*api.User{} | ||
|
||
for _, a := range dashboard.GetAdmins() { | ||
if !slices.Contains(c.DropAdmins, a.GetName()) { | ||
newAdmins = append(newAdmins, a) | ||
} | ||
} | ||
|
||
dashboard.SetAdmins(newAdmins) | ||
} | ||
|
||
// add specified admins from the dashboard | ||
if len(c.AddAdmins) > 0 { | ||
admins := dashboard.GetAdmins() | ||
|
||
for _, a := range c.AddAdmins { | ||
admin := new(api.User) | ||
admin.SetName(a) | ||
|
||
admins = append(admins, admin) | ||
} | ||
|
||
dashboard.SetAdmins(admins) | ||
} | ||
|
||
// update the name of the dashboard | ||
if len(c.Name) > 0 { | ||
dashboard.SetName(c.Name) | ||
} | ||
|
||
// verify the number of repositories for a dashboard | ||
if len(dashboard.GetRepos()) > constants.DashboardRepoLimit { | ||
return fmt.Errorf("maximum number of repositories for a dashboard is %d", constants.DashboardRepoLimit) | ||
} | ||
|
||
// send API call to modify a dashboard | ||
dashboard, _, err = client.Dashboard.Update(dashboard) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return outputDashboard(dashboard, c) | ||
} |
Oops, something went wrong.