Merge pull request #9471 from heaths/issue9470

Always print URL scheme to stdout
This commit is contained in:
Andy Feller 2024-08-21 14:46:52 -04:00 committed by GitHub
commit 779a1203b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 79 additions and 39 deletions

View file

@ -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

View file

@ -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))
})
}
}

View file

@ -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",

View file

@ -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",
@ -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",

View file

@ -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",
@ -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",
@ -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",
@ -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",
@ -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",
@ -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",
@ -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)
}

View file

@ -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")
}

View file

@ -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")
}

View file

@ -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",

View file

@ -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",
},

View file

@ -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",
@ -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",

View file

@ -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",
},
{
@ -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",
},
{

View file

@ -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",
},
}

View file

@ -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)
})
}

View file

@ -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)
}

View file

@ -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",
},
{

View file

@ -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",
},
@ -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",
},

View file

@ -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",
},
@ -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",
},

View file

@ -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",
},
@ -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",
},

View file

@ -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",
@ -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",

View file

@ -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",

View file

@ -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",

View file

@ -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",

View file

@ -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",

View file

@ -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",
@ -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",
@ -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",