Skip to content

Commit

Permalink
open PRs in web browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawid Ciepiela committed Sep 2, 2024
1 parent 40a694d commit 048e93e
Show file tree
Hide file tree
Showing 20 changed files with 947 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.10.1-0.20240413172830-d0be07ea6b9c // indirect
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f // indirect
github.com/cli/browser v1.3.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
Expand All @@ -44,6 +45,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/charmbracelet/lipgloss v0.10.1-0.20240413172830-d0be07ea6b9c h1:0FwZb
github.com/charmbracelet/lipgloss v0.10.1-0.20240413172830-d0be07ea6b9c/go.mod h1:EPP2QJ0ectp3zo6gx9f8oJGq8keirqPJ3XpYEI8wrrs=
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f h1:1BXkZqDueTOBECyDoFGRi0xMYgjJ6vvoPIkWyKOwzTc=
github.com/charmbracelet/x/exp/term v0.0.0-20240425164147-ba2a9512b05f/go.mod h1:yQqGHmheaQfkqiJWjklPHVAq1dKbk8uGbcoS/lcKCJ0=
github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo=
github.com/cli/browser v1.3.0/go.mod h1:HH8s+fOAxjhQoBUAsKuPCbqUuxZDhQ2/aD+SzsEfBTk=
github.com/cli/go-gh/v2 v2.9.0 h1:D3lTjEneMYl54M+WjZ+kRPrR5CEJ5BHS05isBPOV3LI=
github.com/cli/go-gh/v2 v2.9.0/go.mod h1:MeRoKzXff3ygHu7zP+NVTT+imcHW6p3tpuxHAzRM2xE=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
Expand Down Expand Up @@ -92,6 +94,8 @@ github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQF
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
Expand Down
8 changes: 7 additions & 1 deletion pkg/commands/command_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
color "github.com/fatih/color"
configfile "github.com/sarumaj/gh-gr/v2/pkg/configfile"
restclient "github.com/sarumaj/gh-gr/v2/pkg/restclient"
"github.com/sarumaj/gh-gr/v2/pkg/restclient/resources"
resources "github.com/sarumaj/gh-gr/v2/pkg/restclient/resources"
util "github.com/sarumaj/gh-gr/v2/pkg/util"
logrus "github.com/sirupsen/logrus"
cobra "github.com/spf13/cobra"
Expand All @@ -29,6 +29,7 @@ var prFlags struct {
filters []string
labels []string
titles []string
web bool
}

// prCmd represents the pr command
Expand Down Expand Up @@ -67,11 +68,16 @@ var prCmd = func() *cobra.Command {
if len(list) == 0 {
util.PrintlnAndExit(c.CheckColors(color.RedString, "No pull requests matching following constraints found"))
}

if prFlags.web {
list.Browse()
}
},
}

flags := prCmd.Flags()
flags.StringVar(&prFlags.state, "state", "open", "Filter pull requests by state (\"open\", \"closed\", \"all\")")
flags.BoolVar(&prFlags.web, "web", false, "Open pull requests in a web browser")

flags = prCmd.PersistentFlags()
flags.StringVar(&prFlags.base, "base", "", "Filter pull requests by base branch")
Expand Down
27 changes: 27 additions & 0 deletions pkg/configfile/configuration_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package configfile

import (
"net/url"
"os"
"path/filepath"
"strings"

terminal "github.com/AlecAivazis/survey/v2/terminal"
auth "github.com/cli/go-gh/v2/pkg/auth"
browser "github.com/cli/go-gh/v2/pkg/browser"
util "github.com/sarumaj/gh-gr/v2/pkg/util"
supererrors "github.com/sarumaj/go-super/errors"
)

// Retrieve all configured hosts from GitHub CLI.
Expand Down Expand Up @@ -72,3 +76,26 @@ func newBinaryProgressbar() *util.Progressbar {
util.ShowCount(),
)
}

// Open links in browser.
func OpenLins(links []string) {
c := util.Console()
client := browser.New("", c.Stdout(), c.Stderr())
for {
choice := supererrors.ExceptFn(supererrors.W(
prompt.Select(
"Select a link to open:",
links[0],
links,
),
), terminal.InterruptErr)

if supererrors.LastErrorWas(terminal.InterruptErr) || choice >= len(links) {
os.Exit(0)
}

supererrors.Except(client.Browse(links[choice]))
links = append(links[:choice], links[choice+1:]...)[: len(links)-1 : len(links)-1]
}

}
12 changes: 11 additions & 1 deletion pkg/configfile/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ func (prl *PullRequestList) Append(pr PullRequest) {
}
}

// Browse allows to open the URLs of the PullRequests in the default browser.
func (prl PullRequestList) Browse() {
links := make([]string, len(prl))
for i, pr := range prl {
links[i] = pr.URL
}

OpenLins(links)
}

// Has returns true if the PullRequestList contains the given PullRequest.
func (prl PullRequestList) Has(pr PullRequest) bool {
for _, own := range prl {
Expand Down Expand Up @@ -73,7 +83,7 @@ func PullRequestFromResponse(response resources.PullRequest) PullRequest {
Repository: response.Repository,
State: response.State,
Title: response.Title,
URL: response.URL,
URL: response.HTMLURL,
}

if len(pr.Title) > 40 {
Expand Down
23 changes: 23 additions & 0 deletions vendor/github.com/cli/browser/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions vendor/github.com/cli/browser/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions vendor/github.com/cli/browser/browser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/cli/browser/browser_darwin.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/cli/browser/browser_freebsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/cli/browser/browser_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/cli/browser/browser_netbsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/cli/browser/browser_openbsd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/cli/browser/browser_unsupported.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/cli/browser/browser_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 048e93e

Please sign in to comment.