github.com/kubeshop/testkube@v1.17.23/cmd/kubectl-testkube/commands/github/issue.go (about)

     1  package github
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"fmt"
     7  
     8  	prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared"
     9  	"github.com/skratchdot/open-golang/open"
    10  
    11  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    12  	"github.com/kubeshop/testkube/pkg/ui"
    13  	"github.com/kubeshop/testkube/pkg/utils"
    14  )
    15  
    16  const (
    17  	BaseURL  = "https://github.com/kubeshop/testkube/issues/new"
    18  	BugType  = "bug 🐛"
    19  	Template = `
    20  **Describe the bug**
    21  A clear and concise description of what the bug is.
    22  
    23  **To Reproduce**
    24  Steps to reproduce the behavior:
    25  1. Run '...'
    26  2. Specify '...'
    27  3. See error
    28  
    29  **Expected behavior**
    30  A clear and concise description of what you expected to happen.
    31  
    32  **Version / Cluster**
    33  - Testkube CLI version: {{ .ClientVersion }}
    34  - Testkube API server version: {{ .ServerVersion }}
    35  - Kubernetes cluster version: {{ .ClusterVersion }}
    36  
    37  **Screenshots**
    38  If applicable, add CLI commands/output to help explain your problem.
    39  
    40  **Additional context**
    41  Add any other context about the problem here.
    42  
    43  Attach the output of the **testkube debug info** command to provide more details.
    44  `
    45  )
    46  
    47  // OpenTicket opens up a browser to create a Bug issue in the Testkube GitHub repository
    48  func OpenTicket(d testkube.DebugInfo) error {
    49  	title, body, err := buildTicket(d)
    50  	if err != nil {
    51  		return fmt.Errorf("could not build issue: %w", err)
    52  	}
    53  	issue := prShared.IssueMetadataState{
    54  		Type:   prShared.IssueMetadata,
    55  		Body:   body,
    56  		Title:  title,
    57  		Labels: []string{BugType},
    58  	}
    59  
    60  	openURL, err := prShared.WithPrAndIssueQueryParams(nil, nil, BaseURL, issue)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if !prShared.ValidURL(openURL) {
    66  		return fmt.Errorf("cannot open in browser: maximum URL length exceeded")
    67  	}
    68  
    69  	ui.Info(fmt.Sprintf("Opening %s in your browser.\n", BaseURL))
    70  
    71  	return open.Start(openURL)
    72  }
    73  
    74  // buildTicket builds up the title and the body of the ticket, completing the version numbers with data from the environment
    75  func buildTicket(d testkube.DebugInfo) (string, string, error) {
    76  	if d.ClientVersion == "" || d.ClusterVersion == "" {
    77  		return "", "", errors.New("client version and cluster version must be populated to create debug message")
    78  	}
    79  	t, err := utils.NewTemplate("debug").Parse(Template)
    80  	if err != nil {
    81  		return "", "", fmt.Errorf("cannot create template: %w", err)
    82  	}
    83  
    84  	var result bytes.Buffer
    85  	err = t.Execute(&result, d)
    86  	if err != nil {
    87  		return "", "", fmt.Errorf("cannot parse template: %w", err)
    88  	}
    89  
    90  	return "New bug report", result.String(), nil
    91  }