Skip to content

Commit

Permalink
Add export cmd and general dashboard format
Browse files Browse the repository at this point in the history
  • Loading branch information
b1zzu committed Dec 9, 2021
1 parent 6540a4d commit bb4df5a
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.rpdac.toml
42 changes: 10 additions & 32 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package cmd

import (
"context"
"os"

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

type Test struct {
Expand All @@ -25,44 +21,22 @@ var (
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)
c, err := requireReportPortalClient()
if err != nil {
return err
}

t := &Test{}

d, _, err := rp.Dashboard.Get(exportProject, exportDashboard)
// retrieve the Dashboard and Widgets in a single reusable object
d, err := rpdac.LoadDashboardFromReportPortal(c, 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)
// write the Dashboard object to file in YAML
err = d.WriteToFile(exportFile)
if err != nil {
return err
}

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

return nil
},
}
Expand All @@ -73,5 +47,9 @@ func init() {
exportCmd.Flags().StringVarP(&exportProject, "project", "p", "", "ReportPortal Project")
exportCmd.Flags().IntVarP(&exportDashboard, "dashboard", "d", -1, "ReportPortal Dashboard ID")

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

rootCmd.AddCommand(exportCmd)
}
91 changes: 84 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,99 @@
package cmd

import (
"context"
"fmt"
"log"
"os"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/reportportal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)

const (
reportportalBaseURL = "TODO"
endpointKey = "endpoint"
tokenKey = "token"
)

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
},
var (
rootConfigFile string

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 init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&rootConfigFile, "config", ".rpdac.toml", "Config file (default: .rpdac.toml)")

rootCmd.PersistentFlags().StringP(endpointKey, "e", "", "ReportPortal endpoint (example: https://reportportal.example.com)")
rootCmd.PersistentFlags().StringP(tokenKey, "t", "", "ReportPortal access token")

viper.BindPFlag("endpoint", rootCmd.PersistentFlags().Lookup(endpointKey))
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup(tokenKey))
}

func initConfig() {
viper.SetConfigFile(rootConfigFile)

viper.AutomaticEnv()
viper.SetEnvPrefix("rpdac")

err := viper.ReadInConfig()
if err != nil {
log.Println(err)
} else {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

func requireValue(key string) (string, error) {
v := viper.GetString(key)
if v == "" {
return "", fmt.Errorf("required flag/env/conf \"%s\" is not set", key)
}
return v, nil
}

func requireEndpoint() (string, error) {
return requireValue(endpointKey)
}

func requireToken() (string, error) {
return requireValue(tokenKey)
}

func requireReportPortalClient() (*reportportal.Client, error) {

endpoint, err := requireEndpoint()
if err != nil {
return nil, err
}

token, err := requireToken()
if err != nil {
return nil, err
}

oc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))

log.Printf("endpoint: %s\n", endpoint)

// initizlie the ReportPortal client
rc, err := reportportal.NewClient(oc, endpoint)
if err != nil {
return nil, err
}

return rc, nil
}

func Execute() {
Expand Down

0 comments on commit bb4df5a

Please sign in to comment.