Skip to content

Commit

Permalink
chore: add export by name option
Browse files Browse the repository at this point in the history
Dashboards and filters can now be exported by name using the --name
option
  • Loading branch information
b1zzu committed Feb 7, 2022
1 parent 29fc627 commit 964dde1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
18 changes: 10 additions & 8 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ type Test struct {
}

var (
exportFile string
exportProject string
exportDashboardID int
exportFilterID int
exportFile string
exportProject string
exportDashboardID int
exportDashboardName string
exportFilterID int
exportFilterName string

exportCmd = &cobra.Command{
Use: "export",
Expand All @@ -39,7 +41,7 @@ var (
}
r := rpdac.NewReportPortal(c)

return r.Export(rpdac.DashboardKind, exportProject, exportDashboardID, exportFile)
return r.Export(rpdac.DashboardKind, exportProject, exportDashboardID, exportDashboardName, exportFile)
},
}

Expand All @@ -54,7 +56,7 @@ var (
}
r := rpdac.NewReportPortal(c)

return r.Export(rpdac.DashboardKind, exportProject, exportFilterID, exportFile)
return r.Export(rpdac.DashboardKind, exportProject, exportFilterID, exportFilterName, exportFile)
},
}
)
Expand All @@ -81,14 +83,14 @@ func init() {

// Export Dashboard CMD
exportDashboardCmd.Flags().IntVar(&exportDashboardID, "id", -1, "ReportPortal Dashboard ID")
exportDashboardCmd.MarkFlagRequired("id")
exportDashboardCmd.Flags().StringVar(&exportDashboardName, "name", "", "ReportPortal Dashboard Name")
decorateCommonOptions(exportDashboardCmd)

exportCmd.AddCommand(exportDashboardCmd)

// Export Filter CMD
exportFilterCmd.Flags().IntVar(&exportFilterID, "id", -1, "ReportPortal Filter ID")
exportFilterCmd.MarkFlagRequired("id")
exportFilterCmd.Flags().StringVar(&exportFilterName, "name", "", "ReportPortal Filter Name")
decorateCommonOptions(exportFilterCmd)

exportCmd.AddCommand(exportFilterCmd)
Expand Down
31 changes: 24 additions & 7 deletions pkg/rpdac/reportportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,39 @@ func (r *ReportPortal) Service(kind ObjectKind) (ServiceInterface, error) {
}
}

// Export the ObjectKind with the passed id from the passed project to the passed file.
// Export the ObjectKind with the passed id or name from the passed project to the passed file.
//
// The file can be a relative or absoulte path to the file that will be written with the
// full content of the exported Object.
//
func (r *ReportPortal) Export(k ObjectKind, project string, id int, file string) error {
func (r *ReportPortal) Export(k ObjectKind, project string, id int, name string, file string) error {

if id == -1 && name == "" {
return fmt.Errorf("you need to specify the id (--id int) or name (--name string) of the %s to export", k)
}

s, err := r.Service(k)
if err != nil {
return err
}

// retrieve object from reportportal
o, err := s.Get(project, id)
if err != nil {
return fmt.Errorf("error retrieving '%s' with id '%d' in project '%s': %w", k.String(), id, project, err)
var o Object
if id != -1 {
// retrieve object from reportportal by id
o, err = s.Get(project, id)
if err != nil {
return fmt.Errorf("error retrieving '%s' with id '%d' in project '%s': %w", k, id, project, err)
}
} else {
// retrieve object from reportportal by name
o, err = s.GetByName(project, name)
if err != nil {
return fmt.Errorf("error retrieving '%s' with name '%s' in project '%s': %w", k, name, project, err)
}

if o == nil {
return fmt.Errorf("%s with name '%s' in project '%s' not found", k, name, project)
}
}

// convert object to YAML
Expand All @@ -95,7 +112,7 @@ func (r *ReportPortal) Export(k ObjectKind, project string, id int, file string)
return fmt.Errorf("error writing '%s' with id '%d' in project '%s' to file '%s': %w", k.String(), id, project, file, err)
}

log.Printf("%s with id '%d' in project '%s' exported to '%s'", k.String(), id, project, file)
log.Printf("%s with name '%s' in project '%s' exported to '%s'", k, o.GetName(), project, file)
return nil
}

Expand Down
37 changes: 35 additions & 2 deletions pkg/rpdac/reportportal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,40 @@ func TestExport_Dashboard(t *testing.T) {
},
}

err := r.Export(DashboardKind, "test_project", 3, file)
err := r.Export(DashboardKind, "test_project", 3, "", file)
if err != nil {
t.Errorf("Export returned error: %s", err)
}

want := `kind: Dashboard
name: MK E2E Tests Overview
description: ""
widgets: []
`

testFileContains(t, file, want)
}

func TestExport_DashboardByName(t *testing.T) {
file, cleanFile := tmpFile(t, "dashboard")
defer cleanFile()

r := NewReportPortal(nil)

r.Dashboard = &MockService{
GetByNameM: func(project string, name string) (Object, error) {
testEqual(t, project, "test_project")
testEqual(t, name, "MK E2E Tests Overview")
return &Dashboard{
Kind: DashboardKind,
Name: "MK E2E Tests Overview",
Description: "",
Widgets: []*Widget{},
}, nil
},
}

err := r.Export(DashboardKind, "test_project", -1, "MK E2E Tests Overview", file)
if err != nil {
t.Errorf("Export returned error: %s", err)
}
Expand Down Expand Up @@ -134,7 +167,7 @@ func TestExport_Filter(t *testing.T) {
},
}

err := r.Export(FilterKind, "test_project", 3, file)
err := r.Export(FilterKind, "test_project", 3, "", file)
if err != nil {
t.Errorf("Export returned error: %s", err)
}
Expand Down

0 comments on commit 964dde1

Please sign in to comment.