Skip to content

Commit

Permalink
Merge pull request #1 from b1zzu/filters
Browse files Browse the repository at this point in the history
Add import and export cmd for filters
  • Loading branch information
b1zzu authored Jan 19, 2022
2 parents 09a1579 + c7eabc4 commit 2f561b5
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 49 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Dependency directories (remove the comment below to include it)
# vendor/

.rpdac.toml
rpdac
/.rpdac.toml
/rpdac

/dist
/dist/
42 changes: 37 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/rpdac"
"github.com/spf13/cobra"
)
Expand All @@ -11,24 +13,54 @@ var (

createCmd = &cobra.Command{
Use: "create",
Short: "create ReportPortal dashboard from a YAML definition",
Short: "create ReportPortal object from a YAML definition",
RunE: func(cmd *cobra.Command, args []string) error {

d, err := rpdac.LoadDashboardFromFile(createFile)
file, err := rpdac.LoadFile(createFile)
if err != nil {
return err
}

object, err := rpdac.LoadObjectFromFile(file)
if err != nil {
return fmt.Errorf("error loading '%s': %w", createFile, err)
}

c, err := requireReportPortalClient()
if err != nil {
return err
}

r := rpdac.NewReportPortal(c)

err = r.CreateDashboard(createProject, d)
if err != nil {
return err
switch object.Kind {
case rpdac.DashboardKind:

d, err := rpdac.LoadDashboardFromFile(file)
if err != nil {
return err
}

err = r.CreateDashboard(createProject, d)
if err != nil {
return err
}

case rpdac.FilterKind:

f, err := rpdac.LoadFilterFromFile(file)
if err != nil {
return err
}

err = r.CreateFilter(createProject, f)
if err != nil {
return err
}

default:
return fmt.Errorf("unknown Kind '%s' in file '%s'", object.Kind, createFile)

}

return nil
Expand Down
80 changes: 71 additions & 9 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ type Test struct {
}

var (
exportFile string
exportProject string
exportDashboard int
exportFile string
exportProject string
exportDashboardID int
exportFilterID int

exportCmd = &cobra.Command{
Use: "export",
Use: "export",
RunE: func(cmd *cobra.Command, args []string) error {

log.Printf("warning: 'rpdac export' is deprecated, please use 'rpdac export dashboard' instead")
return exportDashboardCmd.RunE(cmd, args)
},
}

exportDashboardCmd = &cobra.Command{
Use: "dashboard",
Short: "Exprt a ReportPortal dashboard to YAML",
RunE: func(cmd *cobra.Command, args []string) error {

Expand All @@ -31,13 +41,13 @@ var (
r := rpdac.NewReportPortal(c)

// retrieve the Dashboard and Widgets in a single reusable object
d, err := r.GetDashboard(exportProject, exportDashboard)
d, err := r.GetDashboard(exportProject, exportDashboardID)
if err != nil {
return err
}

// write the Dashboard object to file in YAML
err = d.WriteToFile(exportFile)
err = rpdac.WriteToFile(d, exportFile)
if err != nil {
return err
}
Expand All @@ -46,16 +56,68 @@ var (
return nil
},
}

exportFilterCmd = &cobra.Command{
Use: "filter",
Short: "Export a ReportPortal filter to YAML",
RunE: func(cmd *cobra.Command, args []string) error {

c, err := requireReportPortalClient()
if err != nil {
return err
}

r := rpdac.NewReportPortal(c)

// retrieve the Filter object
f, err := r.GetFilter(exportProject, exportFilterID)
if err != nil {
return err
}

// write the Filter object to file in YAML
err = rpdac.WriteToFile(f, exportFile)
if err != nil {
return err
}

log.Printf("Filter \"%s\" exported to \"%s\"", f.Name, exportFile)
return nil
},
}
)

func decorateCommonOptions(cmd *cobra.Command) {
cmd.Flags().StringVarP(&exportFile, "file", "f", "", "YAML File")
cmd.Flags().StringVarP(&exportProject, "project", "p", "", "ReportPortal Project")

cmd.MarkFlagRequired("file")
cmd.MarkFlagRequired("project")
}

func init() {
exportCmd.Flags().StringVarP(&exportFile, "file", "f", "", "YAML File")
exportCmd.Flags().StringVarP(&exportProject, "project", "p", "", "ReportPortal Project")
exportCmd.Flags().IntVarP(&exportDashboard, "dashboard", "d", -1, "ReportPortal Dashboard ID")
// Export CMD
exportCmd.Flags().StringVarP(&exportFile, "file", "f", "", "(Deprecated) YAML File")
exportCmd.Flags().StringVarP(&exportProject, "project", "p", "", "(Deprecated) ReportPortal Project")
exportCmd.Flags().IntVarP(&exportDashboardID, "dashboard", "d", -1, "(Deprecated) ReportPortal Dashboard ID")

exportCmd.MarkFlagRequired("file")
exportCmd.MarkFlagRequired("project")
exportCmd.MarkFlagRequired("dashboard")

rootCmd.AddCommand(exportCmd)

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

exportCmd.AddCommand(exportDashboardCmd)

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

exportCmd.AddCommand(exportFilterCmd)
}
1 change: 1 addition & 0 deletions examples/dashone.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: MK E2E Tests Overview
kind: Dashboard
widgets:
- name: Failed/Skipped/Passed [Last 7 days]
description: ""
Expand Down
13 changes: 13 additions & 0 deletions examples/filterone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: mk-e2e-test-suite-sandbox
kind: Filter
type: Launch
description: ""
conditions:
- filteringfield: name
condition: eq
value: mk-e2e-test-suite-sandbox
orders:
- sortingcolumn: startTime
isasc: false
- sortingcolumn: number
isasc: false
66 changes: 62 additions & 4 deletions pkg/reportportal/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,40 @@ type FilterList struct {
}

type Filter struct {
Share bool `json:"share"`
ID int `json:"id"`
Name string `json:"name"`
// incomplete
Share bool `json:"share"`
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Owner string `json:"onwer"`
Conditions []*FilterCondition `json:"conditions"`
Orders []*FilterOrder `json:"orders"`
}

type FilterCondition struct {
Condition string `json:"condition"`
FilteringField string `json:"filteringField"`
Value string `json:"value"`
}

type FilterOrder struct {
IsAsc bool `json:"isAsc"`
SortingColumn string `json:"sortingColumn"`
}

type FilterNotFoundError struct {
Message string
}

type NewFilter struct {
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Share bool `json:"share"`
Conditions []*FilterCondition `json:"conditions"`
Orders []*FilterOrder `json:"orders"`
}

func NewFilterNotFoundError(projectName, filterName string) *DashboardNotFoundError {
return &DashboardNotFoundError{Message: fmt.Sprintf("error filter with name \"%s\" in project \"%s\" not found", filterName, projectName)}
}
Expand All @@ -30,6 +54,23 @@ func (e *FilterNotFoundError) Error() string {
return e.Message
}

func (s *FilterService) GetByID(projectName string, id int) (*Filter, *Response, error) {
u := fmt.Sprintf("v1/%s/filter/%d", projectName, id)

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

f := new(Filter)
resp, err := s.client.Do(req, f)
if err != nil {
return nil, resp, err
}

return f, resp, nil
}

func (s *FilterService) GetByName(projectName, name string) (*Filter, *Response, error) {
u := fmt.Sprintf("v1/%s/filter?%s", projectName, url.Values{"filter.eq.name": []string{name}}.Encode())

Expand All @@ -50,3 +91,20 @@ func (s *FilterService) GetByName(projectName, name string) (*Filter, *Response,

return fl.Content[0], resp, nil
}

func (s *FilterService) Create(projectName string, f *NewFilter) (int, *Response, error) {
u := fmt.Sprintf("v1/%v/filter", projectName)

req, err := s.client.NewRequest("POST", u, f)
if err != nil {
return 0, nil, err
}

e := new(EntryCreated)
resp, err := s.client.Do(req, e)
if err != nil {
return 0, resp, err
}

return e.ID, resp, nil
}
38 changes: 10 additions & 28 deletions pkg/rpdac/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/reportportal"
"gopkg.in/yaml.v2"
)

const DashboardKind = "Dashboard"

type Dashboard struct {
Name string `json:"name"`
Kind string `json:"kind"`
Widgets []*Widget `json:"widgets"`
}

Expand Down Expand Up @@ -44,7 +46,7 @@ type WidgetContentParameters struct {
}

func ToDashboard(d *reportportal.Dashboard, widgets []*Widget) *Dashboard {
return &Dashboard{Name: d.Name, Widgets: widgets}
return &Dashboard{Name: d.Name, Kind: DashboardKind, Widgets: widgets}
}

// convert 'statistics$defects$system_issue$xx_xxxxxxxxxxx' fields to 'statistics$defects$system_issue$shortname`
Expand Down Expand Up @@ -137,42 +139,22 @@ func FromWidget(dashboardHash string, w *Widget, filtersMap map[string]int, enco
return nw, dw, nil
}

func LoadDashboardFromFile(file string) (*Dashboard, error) {

b, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
func LoadDashboardFromFile(file []byte) (*Dashboard, error) {

d := new(Dashboard)
err = yaml.Unmarshal(b, d)
err := yaml.Unmarshal(file, d)
if err != nil {
return nil, err
}

return d, nil
}

func (d *Dashboard) ToYaml() ([]byte, error) {
b, err := yaml.Marshal(d)
if err != nil {
return []byte{}, fmt.Errorf("error marshal dashboard %s: %w", d.Name, err)
}
return b, nil
func (d *Dashboard) GetName() string {
return d.Name
}

func (d *Dashboard) WriteToFile(file string) error {

y, err := d.ToYaml()
if err != nil {
return err
}

err = ioutil.WriteFile(file, y, 0660)
if err != nil {
return fmt.Errorf("error writing yaml dashboard %s to file %s: %w", d.Name, file, err)
}
return nil
func (d *Dashboard) GetKind() string {
return d.Kind
}

func (d *Dashboard) HashName() string {
Expand Down
Loading

0 comments on commit 2f561b5

Please sign in to comment.