8000 Always print URL scheme to stdout by heaths · Pull Request #9471 · cli/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Always print URL scheme to stdout #9471

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

Merged
merged 3 commits into from
Aug 21, 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
9 changes: 7 additions & 2 deletions internal/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ func FuzzyAgoAbbr(a, b time.Time) string {
return b.Format("Jan _2, 2006")
}

// DisplayURL returns a copy of the string urlStr removing everything except the hostname and path.
// DisplayURL returns a copy of the string urlStr removing everything except the scheme, hostname, and path.
// If the scheme is not specified, "https" is assumed.
// If there is an error parsing urlStr then urlStr is returned without modification.
func DisplayURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return urlStr
}
return u.Hostname() + u.Path
scheme := u.Scheme
if scheme == "" {
scheme = "https"
}
return scheme + "://" + u.Hostname() + u.Path
}

// RemoveDiacritics returns the input value without "diacritics", or accent marks
Expand Down
35 changes: 35 additions & 0 deletions internal/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,38 @@ func TestFormatSlice(t *testing.T) {
})
}
}

func TestDisplayURL(t *testing.T) {
tests := []struct {
name string
url string
want string
}{
{
name: "simple",
url: "https://github.com/cli/cli/issues/9470",
want: "https://github.com/cli/cli/issues/9470",
},
{
name: "without scheme",
url: "github.com/cli/cli/issues/9470",
want: "https://github.com/cli/cli/issues/9470",
},
{
name: "with query param and anchor",
url: "https://github.com/cli/cli/issues/9470?q=is:issue#issue-command",
want: "https://github.com/cli/cli/issues/9470",
},
{
name: "preserve http protocol use despite insecure",
url: "http://github.com/cli/cli/issues/9470",
want: "http://github.com/cli/cli/issues/9470",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, DisplayURL(tt.url))
})
}
}
2 changes: 1 addition & 1 deletion pkg/cmd/gist/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func Test_createRun(t *testing.T) {
WebMode: true,
Filenames: []string{fixtureFile},
},
wantOut: "Opening gist.github.com/aa5a315d61ae9438b18d in your browser.\n",
wantOut: "Opening https://gist.github.com/aa5a315d61ae9438b18d in your browser.\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created secret gist fixture.txt\n",
wantErr: false,
wantBrowse: "https://gist.github.com/aa5a315d61ae9438b18d",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/issue/comment/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func Test_commentRun(t *testing.T) {

OpenInBrowser: func(string) error { return nil },
},
stderr: "Opening github.com/OWNER/REPO/issues/123 in your browser.\n",
stderr: "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n",
},
{
name: "non-interactive web with edit last",
Expand All @@ -246,7 +246,7 @@ func Test_commentRun(t *testing.T) {

OpenInBrowser: func(string) error { return nil },
},
stderr: "Opening github.com/OWNER/REPO/issues/123 in your browser.\n",
stderr: "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n",
},
{
name: "non-interactive editor",
Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/issue/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func Test_createRun(t *testing.T) {
WebMode: true,
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n",
},
{
name: "title and body",
Expand All @@ -276,7 +276,7 @@ func Test_createRun(t *testing.T) {
Body: "hello cli",
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new?body=hello+cli&title=myissue",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n",
},
{
name: "assignee",
Expand All @@ -285,7 +285,7 @@ func Test_createRun(t *testing.T) {
Assignees: []string{"monalisa"},
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new?assignees=monalisa&body=",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n",
},
{
name: "@me",
Expand All @@ -302,7 +302,7 @@ func Test_createRun(t *testing.T) {
} }`))
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new?assignees=MonaLisa&body=",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n",
},
{
name: "project",
Expand Down Expand Up @@ -358,7 +358,7 @@ func Test_createRun(t *testing.T) {
} } } }`))
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new?body=&projects=OWNER%2FREPO%2F1",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new in your browser.\n",
},
{
name: "has templates",
Expand All @@ -378,7 +378,7 @@ func Test_createRun(t *testing.T) {
)
},
wantsBrowse: "https://github.com/OWNER/REPO/issues/new/choose",
wantsStderr: "Opening github.com/OWNER/REPO/issues/new/choose in your browser.\n",
wantsStderr: "Opening https://github.com/OWNER/REPO/issues/new/choose in your browser.\n",
},
{
name: "too long body",
Expand Down Expand Up @@ -763,7 +763,7 @@ func TestIssueCreate_continueInBrowser(t *testing.T) {

Creating issue in OWNER/REPO

Opening github.com/OWNER/REPO/issues/new in your browser.
Opening https://github.com/OWNER/REPO/issues/new in your browser.
`), output.Stderr())
assert.Equal(t, "https://github.com/OWNER/REPO/issues/new?body=body&title=hello", output.BrowsedURL)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/issue/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestIssueList_web(t *testing.T) {
}

assert.Equal(t, "", stdout.String())
assert.Equal(t, "Opening github.com/OWNER/REPO/issues in your browser.\n", stderr.String())
assert.Equal(t, "Opening https://github.com/OWNER/REPO/issues in your browser.\n", stderr.String())
browser.Verify(t, "https://github.com/OWNER/REPO/issues?q=assignee%3Apeter+author%3Ajohn+label%3Abug+label%3Adocs+mentions%3Afrank+milestone%3Av1.1+type%3Aissue")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/issue/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestIssueView_web(t *testing.T) {
}

assert.Equal(t, "", stdout.String())
assert.Equal(t, "Opening github.com/OWNER/REPO/issues/123 in your browser.\n", stderr.String())
assert.Equal(t, "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n", stderr.String())
browser.Verify(t, "https://github.com/OWNER/REPO/issues/123")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/label/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func TestListRun(t *testing.T) {
name: "web mode",
tty: true,
opts: &listOptions{WebMode: true},
wantStderr: "Opening github.com/OWNER/REPO/labels in your browser.\n",
wantStderr: "Opening https://github.com/OWNER/REPO/labels in your browser.\n",
},
{
name: "order by name ascending",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func TestChecksRun_web(t *testing.T) {
{
name: "tty",
isTTY: true,
wantStderr: "Opening github.com/OWNER/REPO/pull/123/checks in your browser.\n",
wantStderr: "Opening https://github.com/OWNER/REPO/pull/123/checks in your browser.\n",
wantStdout: "",
wantBrowse: "https://github.com/OWNER/REPO/pull/123/checks",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pr/comment/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func Test_commentRun(t *testing.T) {

OpenInBrowser: func(string) error { return nil },
},
stderr: "Opening github.com/OWNER/REPO/pull/123 in your browser.\n",
stderr: "Opening https://github.com/OWNER/REPO/pull/123 in your browser.\n",
},
{
name: "non-interactive web with edit last",
Expand All @@ -266,7 +266,7 @@ func Test_commentRun(t *testing.T) {

OpenInBrowser: func(string) error { return nil },
},
stderr: "Opening github.com/OWNER/REPO/pull/123 in your browser.\n",
stderr: "Opening https://github.com/OWNER/REPO/pull/123 in your browser.\n",
},
{
name: "non-interactive editor",
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pr/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ func Test_createRun(t *testing.T) {
}
}
},
expectedErrOut: "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n",
expectedErrOut: "Opening https://github.com/OWNER/REPO/compare/master...feature in your browser.\n",
expectedBrowse: "https://github.com/OWNER/REPO/compare/master...feature?body=&expand=1",
},
{
Expand Down Expand Up @@ -1113,7 +1113,7 @@ func Test_createRun(t *testing.T) {
}
}
},
expectedErrOut: "Opening github.com/OWNER/REPO/compare/master...feature in your browser.\n",
expectedErrOut: "Opening https://github.com/OWNER/REPO/compare/master...feature in your browser.\n",
expectedBrowse: "https://github.com/OWNER/REPO/compare/master...feature?body=&expand=1&projects=ORG%2F1",
},
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func Test_diffRun(t *testing.T) {
BrowserMode: true,
},
wantFields: []string{"url"},
wantStderr: "Opening github.com/OWNER/REPO/pull/123/files in your browser.\n",
wantStderr: "Opening https://github.com/OWNER/REPO/pull/123/files in your browser.\n",
wantBrowsedURL: "https://github.com/OWNER/REPO/pull/123/files",
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestPRList_web(t *testing.T) {
}

assert.Equal(t, "", output.String())
assert.Equal(t, "Opening github.com/OWNER/REPO/pulls in your browser.\n", output.Stderr())
assert.Equal(t, "Opening https://github.com/OWNER/REPO/pulls in your browser.\n", output.Stderr())
assert.Equal(t, test.expectedBrowserURL, output.BrowsedURL)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func TestPRView_web_currentBranch(t *testing.T) {
}

assert.Equal(t, "", output.String())
assert.Equal(t, "Opening github.com/OWNER/REPO/pull/10 in your browser.\n", output.Stderr())
assert.Equal(t, "Opening https://github.com/OWNER/REPO/pull/10 in your browser.\n", output.Stderr())
assert.Equal(t, "https://github.com/OWNER/REPO/pull/10", output.BrowsedURL)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/repo/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Test_RepoView_Web(t *testing.T) {
{
name: "tty",
stdoutTTY: true,
wantStderr: "Opening github.com/OWNER/REPO in your browser.\n",
wantStderr: "Opening https://github.com/OWNER/REPO in your browser.\n",
wantBrowse: "https://github.com/OWNER/REPO",
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/ruleset/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func Test_checkRun(t *testing.T) {
Branch: "my-branch",
WebMode: true,
},
wantStdout: "Opening github.com/my-org/repo-name/rules in your browser.\n",
wantStdout: "Opening https://github.com/my-org/repo-name/rules in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/my-org/repo-name/rules?ref=refs%2Fheads%2Fmy-branch",
},
Expand All @@ -173,7 +173,7 @@ func Test_checkRun(t *testing.T) {
Branch: "my-feature/my-branch",
WebMode: true,
},
wantStdout: "Opening github.com/my-org/repo-name/rules in your browser.\n",
wantStdout: "Opening https://github.com/my-org/repo-name/rules in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/my-org/repo-name/rules?ref=refs%2Fheads%2Fmy-feature%2Fmy-branch",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/ruleset/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func Test_listRun(t *testing.T) {
opts: ListOptions{
WebMode: true,
},
wantStdout: "Opening github.com/OWNER/REPO/rules in your browser.\n",
wantStdout: "Opening https://github.com/OWNER/REPO/rules in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/OWNER/REPO/rules",
},
Expand All @@ -266,7 +266,7 @@ func Test_listRun(t *testing.T) {
WebMode: true,
Organization: "my-org",
},
wantStdout: "Opening github.com/organizations/my-org/settings/rules in your browser.\n",
wantStdout: "Opening https://github.com/organizations/my-org/settings/rules in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/organizations/my-org/settings/rules",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/ruleset/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func Test_viewRun(t *testing.T) {
httpmock.FileResponse("./fixtures/rulesetViewRepo.json"),
)
},
wantStdout: "Opening github.com/my-owner/repo-name/rules/42 in your browser.\n",
wantStdout: "Opening https://github.com/my-owner/repo-name/rules/42 in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/my-owner/repo-name/rules/42",
},
Expand Down Expand Up @@ -352,7 +352,7 @@ func Test_viewRun(t *testing.T) {
httpmock.FileResponse("./fixtures/rulesetViewOrg.json"),
)
},
wantStdout: "Opening github.com/organizations/my-owner/settings/rules/74 in your browser.\n",
wantStdout: "Opening https://github.com/organizations/my-owner/settings/rules/74 in your browser.\n",
wantStderr: "",
wantBrowse: "https://github.com/organizations/my-owner/settings/rules/74",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/run/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ func TestViewRun(t *testing.T) {
httpmock.JSONResponse(shared.TestWorkflow))
},
browsedURL: "https://github.com/runs/3",
wantOut: "Opening github.com/runs/3 in your browser.\n",
wantOut: "Opening https://github.com/runs/3 in your browser.\n",
},
{
name: "web job",
Expand All @@ -1204,7 +1204,7 @@ func TestViewRun(t *testing.T) {
httpmock.JSONResponse(shared.TestWorkflow))
},
browsedURL: "https://github.com/jobs/10?check_suite_focus=true",
wantOut: "Opening github.com/jobs/10 in your browser.\n",
wantOut: "Opening https://github.com/jobs/10 in your browser.\n",
},
{
name: "hide job header, failure",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/search/code/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func TestCodeRun(t *testing.T) {
WebMode: true,
},
tty: true,
wantStderr: "Opening github.com/search in your browser.\n",
wantStderr: "Opening https://github.com/search in your browser.\n",
},
{
name: "opens browser for web mode notty",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/search/commits/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestCommitsRun(t *testing.T) {
WebMode: true,
},
tty: true,
wantStderr: "Opening github.com/search in your browser.\n",
wantStderr: "Opening https://github.com/search in your browser.\n",
},
{
name: "opens browser for web mode notty",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/search/repos/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestReposRun(t *testing.T) {
WebMode: true,
},
tty: true,
wantStderr: "Opening github.com/search in your browser.\n",
wantStderr: "Opening https://github.com/search in your browser.\n",
},
{
name: "opens browser for web mode notty",
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/search/shared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestSearchIssues(t *testing.T) {
WebMode: true,
},
tty: true,
wantStderr: "Opening github.com/search in your browser.\n",
wantStderr: "Opening https://github.com/search in your browser.\n",
},
{
name: "opens browser for web mode notty",
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/workflow/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestViewRun(t *testing.T) {
httpmock.JSONResponse(aWorkflow),
)
},
wantOut: "Opening github.com/OWNER/REPO/actions/workflows/flow.yml in your browser.\n",
wantOut: "Opening https://github.com/OWNER/REPO/actions/workflows/flow.yml in your browser.\n",
},
{
name: "web notty",
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestViewRun(t *testing.T) {
httpmock.StringResponse(`{ "data": { "repository": { "defaultBranchRef": { "name": "trunk" } } } }`),
)
},
wantOut: "Opening github.com/OWNER/REPO/blob/trunk/.github/workflows/flow.yml in your browser.\n",
wantOut: "Opening https://github.com/OWNER/REPO/blob/trunk/.github/workflows/flow.yml in your browser.\n",
},
{
name: "web with yaml and ref",
Expand All @@ -274,7 +274,7 @@ func TestViewRun(t *testing.T) {
httpmock.JSONResponse(aWorkflow),
)
},
wantOut: "Opening github.com/OWNER/REPO/blob/base/.github/workflows/flow.yml in your browser.\n",
wantOut: "Opening https://github.com/OWNER/REPO/blob/base/.github/workflows/flow.yml in your browser.\n",
},
{
name: "workflow with yaml",
Expand Down
Loading
0