Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement gno bug #1325

Merged
merged 21 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/BUG-REPORT.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
name: Bug Report Template
about: Create a bug report
# NOTE: keep in sync with gnovm/cmd/gno/bug.go
---

## [Subject of the issue]
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/go-gno-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ Legend:

| go command | gno command | comment |
|-------------------|------------------|-----------------------------------------------------------------------|
| go bug | | see https://github.com/gnolang/gno/issues/733 |
| go bug | gno bug | same behavior |
| go build | gno build | same intention, limited compatibility |
| go clean | gno clean | same intention, limited compatibility |
| go doc | gno doc | limited compatibility; see https://github.com/gnolang/gno/issues/522 |
Expand Down
165 changes: 165 additions & 0 deletions gnovm/cmd/gno/bug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package main

import (
"context"
"flag"
"net/url"
"os/exec"
"runtime"
"runtime/debug"
"strings"
"text/template"
"time"

"github.com/gnolang/gno/tm2/pkg/commands"
)

// NOTE: keep in sync with .github/ISSUE_TEMPLATE/BUG-REPORT.md
const bugTmpl = `## [Subject of the issue]
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

### Description

Describe your issue in as much detail as possible here

### Your environment

* go version {{.GoVersion}} {{.Os}}/{{.Arch}}
* gno commit that causes this issue: {{.Commit}}

### Steps to reproduce

* Tell us how to reproduce this issue
* Where the issue is, if you know
* Which commands triggered the issue, if any

### Expected behaviour

Tell us what should happen

### Actual behaviour

Tell us what happens instead

### Logs

Please paste any logs here that demonstrate the issue, if they exist

### Proposed solution

If you have an idea of how to fix this issue, please write it down here, so we can begin discussing it

`

type bugCfg struct {
skipBrowser bool
}

func newBugCmd(io commands.IO) *commands.Command {
cfg := &bugCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "bug",
ShortUsage: "bug",
ShortHelp: "Start a bug report",
},
cfg,
func(_ context.Context, args []string) error {
return execBug(cfg, args, io)
},
)
}

func (c *bugCfg) RegisterFlags(fs *flag.FlagSet) {
fs.BoolVar(
&c.skipBrowser,
"skip-browser",
false,
"do not open the browser",
)
}

func execBug(cfg *bugCfg, args []string, io commands.IO) error {
if len(args) != 0 {
return flag.ErrHelp
}

bugReportEnv := struct {
Os, Arch, GoVersion, Commit string
}{
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
getCommitHash(),
}

var buf strings.Builder
tmpl, err := template.New("bug.tmpl").Parse(bugTmpl)
if err != nil {
return err
}
tmpl.Execute(&buf, bugReportEnv)

body := buf.String()
url := "https://github.com/gnolang/gno/issues/new?body=" + url.QueryEscape(body)
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

if !cfg.skipBrowser && openBrowser(url) {
return nil
}

io.Println("Please file a new issue at github.com/gnolang/gno/issues/new using this template:")
io.Println()
io.Println(body)

return nil
}

// openBrowser opens a default web browser with the specified URL.
func openBrowser(url string) bool {
var cmdArgs []string
switch runtime.GOOS {
case "windows":
cmdArgs = []string{"cmd", "/c", "start", url}
case "darwin":
cmdArgs = []string{"/usr/bin/open", url}
default: // "linux"
cmdArgs = []string{"xdg-open", url}
}

cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
return true
}

return false
}

// getCommitHash returns the commit hash from build info, or an
// empty string if not found.
func getCommitHash() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}

// appearsSuccessful reports whether the command appears to have run successfully.
// If the command runs longer than the timeout, it's deemed successful.
// If the command runs within the timeout, it's deemed successful if it exited cleanly.
// Note: Taken from Go's `internal/browser“
func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
errc := make(chan error, 1)
go func() {
errc <- cmd.Wait()
}()

select {
case <-time.After(timeout):
return true
case err := <-errc:
return err == nil
}
}
21 changes: 21 additions & 0 deletions gnovm/cmd/gno/bug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main
harry-hov marked this conversation as resolved.
Show resolved Hide resolved

import "testing"

func TestBugApp(t *testing.T) {
tc := []testMainCase{
{
args: []string{"bug -h"},
errShouldBe: "flag: help requested",
},
{
args: []string{"bug unknown"},
errShouldBe: "flag: help requested",
},
{
args: []string{"bug", "-skip-browser"},
stdoutShouldContain: "go version go1.",
},
}
harry-hov marked this conversation as resolved.
Show resolved Hide resolved
testMainCaseRun(t, tc)
}
4 changes: 2 additions & 2 deletions gnovm/cmd/gno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
newReplCmd(),
newDocCmd(io),
newEnvCmd(io),
newBugCmd(io),
// fmt -- gofmt
// graph
// vendor -- download deps from the chain in vendor/
Expand All @@ -41,8 +42,7 @@ func newGnocliCmd(io commands.IO) *commands.Command {
// publish/release
// generate
// "vm" -- starts an in-memory chain that can be interacted with?
// bug -- start a bug report
// version -- show gno, golang versions
// version -- show cmd/gno, golang versions
)

return cmd
Expand Down
Loading