Skip to content

Commit

Permalink
Initial project
Browse files Browse the repository at this point in the history
  • Loading branch information
b1zzu committed Dec 7, 2021
1 parent cc197e3 commit 6540a4d
Show file tree
Hide file tree
Showing 11 changed files with 1,097 additions and 0 deletions.
77 changes: 77 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"context"
"os"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/reportportal"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
)

type Test struct {
Dashboard *reportportal.Dashboard
Widgets []*reportportal.Widget
}

var (
exportFile string
exportProject string
exportDashboard int

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

c := oauth2.NewClient(context.TODO(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "TODO"}))

rp, err := reportportal.NewClient(c, reportportalBaseURL)
if err != nil {
return err
}

t := &Test{}

d, _, err := rp.Dashboard.Get(exportProject, exportDashboard)
if err != nil {
return err
}

t.Dashboard = d

// retrieve all widgets
ws := make([]*reportportal.Widget, len(d.Widgets))
for i, w := range d.Widgets {
wr, _, err := rp.Widget.Get(exportProject, w.WidgetId)
if err != nil {
return err
}

ws[i] = wr
}
t.Widgets = ws

b, err := yaml.Marshal(t)
if err != nil {
return err
}

err = os.WriteFile(exportFile, b, 0660)
if err != nil {
return err
}

return nil
},
}
)

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")

rootCmd.AddCommand(exportCmd)
}
42 changes: 42 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"fmt"
"io/ioutil"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
importFile string

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

byteFile, err := ioutil.ReadFile(importFile)
if err != nil {
return err
}

data := make(map[interface{}]interface{})

err = yaml.Unmarshal(byteFile, data)
if err != nil {
return err
}

fmt.Println(data)

return nil
},
}
)

func init() {
importCmd.Flags().StringVarP(&importFile, "file", "f", "", "YAML file")

rootCmd.AddCommand(importCmd)
}
27 changes: 27 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

const (
reportportalBaseURL = "TODO"
)

var rootCmd = &cobra.Command{
Use: "rpdac",
Short: "Import and export ReportPortal dashboards and widget in YAML",
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Empty file added examples/dashone.yaml
Empty file.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/b1zzu/reportportal-dashboards-as-code

go 1.16

require (
github.com/spf13/cobra v1.2.1
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
573 changes: 573 additions & 0 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/b1zzu/reportportal-dashboards-as-code/cmd"

func main() {
cmd.Execute()
}
51 changes: 51 additions & 0 deletions pkg/reportportal/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package reportportal

import (
"fmt"
)

type DashboardService service

type Dashboard struct {
Owner string `json:"owner"`
Share bool `json:"share"`
ID int `json:"id"`
Name string `json:"name"`
Widgets []*DashboardWidget `json:"widgets"`
}

type DashboardWidget struct {
WidgetName string `json:"widgetName"`
WidgetId int `json:"widgetId"`
WidgetType string `json:"widgetType"`
WidgetSize *DashboardWidgetSize `json:"widgetSize"`
WidgetPosition *DashboardWidgetPosition `json:"widgetPosition"`
Share bool `json:"share"`
}

type DashboardWidgetSize struct {
Width int `json:"width"`
Height int `json:"height"`
}

type DashboardWidgetPosition struct {
PositionX int `json:"positionX"`
PositionY int `json:"positionY"`
}

func (s *DashboardService) Get(projectName string, id int) (*Dashboard, *Response, error) {
u := fmt.Sprintf("v1/%v/dashboard/%v", projectName, id)

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

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

return d, resp, nil
}
Loading

0 comments on commit 6540a4d

Please sign in to comment.