Skip to content

Commit

Permalink
feat: create bug ticket (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
vLia committed Aug 5, 2022
1 parent 0f6f369 commit b1e42aa
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 19 deletions.
23 changes: 23 additions & 0 deletions cmd/kubectl-testkube/commands/createticket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package commands

import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
debuginfo "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/debug"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/github"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)

func NewCreateTicketCmd() *cobra.Command {
return &cobra.Command{
Use: "create-ticket",
Short: "Create bug ticket",
Long: "Create an issue of type bug in the Testkube repository",
Run: func(cmd *cobra.Command, args []string) {
client, _ := common.GetClient(cmd)
debug, err := debuginfo.GetDebugInfo(client)
ui.ExitOnError("get debug info", err)
ui.ExitOnError("opening GitHub ticket", github.OpenTicket(debug))
},
}
}
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common/validator"
debuginfo "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/debug"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/debug"
"github.com/kubeshop/testkube/pkg/ui"
)

Expand All @@ -22,7 +22,7 @@ func NewDebugCmd() *cobra.Command {
validator.PersistentPreRunVersionCheck(cmd, common.Version)
}}

cmd.AddCommand(debuginfo.NewShowDebugInfoCmd())
cmd.AddCommand(debug.NewShowDebugInfoCmd())

return cmd
}
29 changes: 21 additions & 8 deletions cmd/kubectl-testkube/commands/debug/info.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package debuginfo
package debug

import (
"fmt"
"os"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/common"
"github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
Expand All @@ -18,20 +19,32 @@ func NewShowDebugInfoCmd() *cobra.Command {
Long: "Get all the necessary information to debug an issue in Testkube",
Run: func(cmd *cobra.Command, args []string) {
client, _ := common.GetClient(cmd)
debug, err := client.GetDebugInfo()
debug, err := GetDebugInfo(client)
ui.ExitOnError("get debug info", err)

info, err := client.GetServerInfo()
ui.ExitOnError("get server info", err)

debug.ClientVersion = common.Version
debug.ServerVersion = info.Version

printDebugInfo(debug)
},
}
}

// GetDebugInfo returns information on the current Testkube environment
func GetDebugInfo(apiClient client.Client) (testkube.DebugInfo, error) {
debug, err := apiClient.GetDebugInfo()
if err != nil {
return testkube.DebugInfo{}, err
}

info, err := apiClient.GetServerInfo()
if err != nil {
return testkube.DebugInfo{}, err
}

debug.ClientVersion = common.Version
debug.ServerVersion = info.Version

return debug, nil
}

// printDebugInfo prints the debugging data to the CLI
func printDebugInfo(info testkube.DebugInfo) {
ui.Table(info, os.Stdout)
Expand Down
91 changes: 91 additions & 0 deletions cmd/kubectl-testkube/commands/github/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package github

import (
"bytes"
"errors"
"fmt"
"html/template"

prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
"github.com/cli/cli/v2/utils"
"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/skratchdot/open-golang/open"
)

const (
BaseURL = "https://github.com/kubeshop/testkube/issues/new"
BugType = "bug 🐛"
Template = `
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Run '...'
2. Specify '...'
3. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Version / Cluster**
- Testkube CLI version: {{ .ClientVersion }}
- Testkube API server version: {{ .ServerVersion }}
- Kubernetes cluster version: {{ .ClusterVersion }}
**Screenshots**
If applicable, add CLI commands/output to help explain your problem.
**Additional context**
Add any other context about the problem here.
Attach the output of the **testkube debug info** command to provide more details.
`
)

// OpenTicket opens up a browser to create a Bug issue in the Testkube GitHub repository
func OpenTicket(d testkube.DebugInfo) error {
title, body, err := buildTicket(d)
if err != nil {
return fmt.Errorf("could not build issue: %w", err)
}
issue := prShared.IssueMetadataState{
Type: prShared.IssueMetadata,
Body: body,
Title: title,
Labels: []string{BugType},
}

openURL, err := prShared.WithPrAndIssueQueryParams(nil, nil, BaseURL, issue)
if err != nil {
return err
}

if !utils.ValidURL(openURL) {
return fmt.Errorf("cannot open in browser: maximum URL length exceeded")
}

ui.Info(fmt.Sprintf("Opening %s in your browser.\n", utils.DisplayURL(openURL)))

return open.Start(openURL)
}

// buildTicket builds up the title and the body of the ticket, completing the version numbers with data from the environment
func buildTicket(d testkube.DebugInfo) (string, string, error) {
if d.ClientVersion == "" || d.ClusterVersion == "" {
return "", "", errors.New("client version and cluster version must be populated to create debug message")
}
t, err := template.New("debug").Parse(Template)
if err != nil {
return "", "", fmt.Errorf("cannot create template: %w", err)
}

var result bytes.Buffer
err = t.Execute(&result, d)
if err != nil {
return "", "", fmt.Errorf("cannot parse template: %w", err)
}

return "New bug report", result.String(), nil
}
80 changes: 80 additions & 0 deletions cmd/kubectl-testkube/commands/github/issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package github

import (
"testing"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
)

func TestBuildTicket(t *testing.T) {
tests := []struct {
name string
debugInfo testkube.DebugInfo
wantTitle string
wantBody string
wantErr bool
}{
{
name: "Empty DebugInfo",
debugInfo: testkube.DebugInfo{},
wantErr: true,
},
{
name: "Debug info populated",
debugInfo: testkube.DebugInfo{
ClientVersion: "v0.test",
ServerVersion: "v1.test",
ClusterVersion: "v2.test",
ApiLogs: []string{"api logline1", "api logline2"},
OperatorLogs: []string{"operator logline1", "operator logline2", "operator logline3"},
ExecutionLogs: map[string][]string{
"execution1": {"execution logline1"},
"execution2": {"execution logline1", "execution logline2"},
},
},
wantTitle: "New bug report",
wantBody: `
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Run '...'
2. Specify '...'
3. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Version / Cluster**
- Testkube CLI version: v0.test
- Testkube API server version: v1.test
- Kubernetes cluster version: v2.test
**Screenshots**
If applicable, add CLI commands/output to help explain your problem.
**Additional context**
Add any other context about the problem here.
Attach the output of the **testkube debug info** command to provide more details.
`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTitle, gotBody, err := buildTicket(tt.debugInfo)
if (err != nil) != tt.wantErr {
t.Errorf("BuildTicket() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotTitle != tt.wantTitle {
t.Errorf("BuildTicket() title = %v, want %v", gotTitle, tt.wantTitle)
}
if gotBody != tt.wantBody {
t.Errorf("BuildTicket() body = %v, want %v", gotBody, tt.wantBody)
}
})
}
}
1 change: 1 addition & 0 deletions cmd/kubectl-testkube/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func init() {

RootCmd.AddCommand(NewConfigCmd())
RootCmd.AddCommand(NewDebugCmd())
RootCmd.AddCommand(NewCreateTicketCmd())
}

var RootCmd = &cobra.Command{
Expand Down
33 changes: 33 additions & 0 deletions docs/cli/kubectl-testkube_create_ticket.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## kubectl-testkube debug create-ticket

Create bug ticket

### Synopsis

Create an issue of type bug in the Testkube repository

```
kubectl-testkube debug create-ticket [flags]
```

### Options

```
-h, --help help for info
```

### Options inherited from parent commands

```
-a, --api-uri string api uri, default value read from config if set (default "http://localhost:8088")
-c, --client string client used for connecting to Testkube API one of proxy|direct (default "proxy")
--namespace string Kubernetes namespace, default value read from config if set (default "testkube")
--oauth-enabled enable oauth
--telemetry-enabled enable collection of anonumous telemetry data
--verbose show additional debug messages
```

### SEE ALSO

* [kubectl-testkube](kubectl-testkube.md) - Testkube entrypoint for kubectl plugin
* [kubectl-testkube debug](kubectl-testkube_debug.md) - Debug Testkube
2 changes: 1 addition & 1 deletion docs/cli/kubectl-testkube_debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Print environment information for debugging

### Synopsis

Print environment information for debugging
Debug Testkube

```
kubectl-testkube debug [flags]
Expand Down
4 changes: 2 additions & 2 deletions docs/cli/kubectl-testkube_debug_info.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## kubectl-testkube debug info

Testkube entrypoint for kubectl plugin
Show Testkube environment information

### Synopsis

Expand Down Expand Up @@ -30,4 +30,4 @@ kubectl-testkube debug info [flags]
### SEE ALSO

* [kubectl-testkube](kubectl-testkube.md) - Testkube entrypoint for kubectl plugin
* [kubectl-testkube debug](kubectl-testkube_debug.md) - Print environment information for debugging
* [kubectl-testkube debug](kubectl-testkube_debug.md) - Debug Testkube

0 comments on commit b1e42aa

Please sign in to comment.