Skip to content

Commit 060ccd1

Browse files
author
Nate Smith
authored
Merge pull request cli#1781 from cristiand391/fix-gist-id-extraction
Fix gist ID extraction from url
2 parents 1e7a797 + a7a90b3 commit 060ccd1

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

pkg/cmd/gist/edit/edit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9-
"net/url"
109
"sort"
1110
"strings"
1211

@@ -69,11 +68,12 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
6968
func editRun(opts *EditOptions) error {
7069
gistID := opts.Selector
7170

72-
u, err := url.Parse(opts.Selector)
73-
if err == nil {
74-
if strings.HasPrefix(u.Path, "/") {
75-
gistID = u.Path[1:]
71+
if strings.Contains(gistID, "/") {
72+
id, err := shared.GistIDFromURL(gistID)
73+
if err != nil {
74+
return err
7675
}
76+
gistID = id
7777
}
7878

7979
client, err := opts.HttpClient()

pkg/cmd/gist/shared/shared.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package shared
33
import (
44
"fmt"
55
"net/http"
6+
"net/url"
7+
"strings"
68
"time"
79

810
"github.com/cli/cli/api"
@@ -37,3 +39,20 @@ func GetGist(client *http.Client, hostname, gistID string) (*Gist, error) {
3739

3840
return &gist, nil
3941
}
42+
43+
func GistIDFromURL(gistURL string) (string, error) {
44+
u, err := url.Parse(gistURL)
45+
if err == nil && strings.HasPrefix(u.Path, "/") {
46+
split := strings.Split(u.Path, "/")
47+
48+
if len(split) > 2 {
49+
return split[2], nil
50+
}
51+
52+
if len(split) == 2 && split[1] != "" {
53+
return split[1], nil
54+
}
55+
}
56+
57+
return "", fmt.Errorf("Invalid gist URL %s", u)
58+
}

pkg/cmd/gist/shared/shared_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package shared
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_GetGistIDFromURL(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
url string
13+
want string
14+
wantErr bool
15+
}{
16+
{
17+
name: "url",
18+
url: "https://gist.github.com/1234",
19+
want: "1234",
20+
},
21+
{
22+
name: "url with username",
23+
url: "https://gist.github.com/octocat/1234",
24+
want: "1234",
25+
},
26+
{
27+
name: "url, specific file",
28+
url: "https://gist.github.com/1234#file-test-md",
29+
want: "1234",
30+
},
31+
{
32+
name: "invalid url",
33+
url: "https://gist.github.com",
34+
wantErr: true,
35+
want: "Invalid gist URL https://gist.github.com",
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
id, err := GistIDFromURL(tt.url)
42+
if tt.wantErr {
43+
assert.Error(t, err)
44+
assert.EqualError(t, err, tt.want)
45+
return
46+
}
47+
assert.NoError(t, err)
48+
49+
assert.Equal(t, tt.want, id)
50+
})
51+
}
52+
}

pkg/cmd/gist/view/view.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package view
33
import (
44
"fmt"
55
"net/http"
6-
"net/url"
76
"sort"
87
"strings"
98

@@ -71,11 +70,12 @@ func viewRun(opts *ViewOptions) error {
7170
return utils.OpenInBrowser(gistURL)
7271
}
7372

74-
u, err := url.Parse(opts.Selector)
75-
if err == nil {
76-
if strings.HasPrefix(u.Path, "/") {
77-
gistID = u.Path[1:]
73+
if strings.Contains(gistID, "/") {
74+
id, err := shared.GistIDFromURL(gistID)
75+
if err != nil {
76+
return err
7877
}
78+
gistID = id
7979
}
8080

8181
client, err := opts.HttpClient()

pkg/cmd/gist/view/view_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ func Test_viewRun(t *testing.T) {
9090
wantErr bool
9191
}{
9292
{
93-
name: "no such gist",
93+
name: "no such gist",
94+
opts: &ViewOptions{
95+
Selector: "1234",
96+
},
9497
wantErr: true,
9598
},
9699
{
97100
name: "one file",
101+
opts: &ViewOptions{
102+
Selector: "1234",
103+
},
98104
gist: &shared.Gist{
99105
Files: map[string]*shared.GistFile{
100106
"cicada.txt": {
@@ -108,6 +114,7 @@ func Test_viewRun(t *testing.T) {
108114
{
109115
name: "filename selected",
110116
opts: &ViewOptions{
117+
Selector: "1234",
111118
Filename: "cicada.txt",
112119
},
113120
gist: &shared.Gist{
@@ -126,6 +133,9 @@ func Test_viewRun(t *testing.T) {
126133
},
127134
{
128135
name: "multiple files, no description",
136+
opts: &ViewOptions{
137+
Selector: "1234",
138+
},
129139
gist: &shared.Gist{
130140
Files: map[string]*shared.GistFile{
131141
"cicada.txt": {
@@ -142,6 +152,9 @@ func Test_viewRun(t *testing.T) {
142152
},
143153
{
144154
name: "multiple files, description",
155+
opts: &ViewOptions{
156+
Selector: "1234",
157+
},
145158
gist: &shared.Gist{
146159
Description: "some files",
147160
Files: map[string]*shared.GistFile{
@@ -160,7 +173,8 @@ func Test_viewRun(t *testing.T) {
160173
{
161174
name: "raw",
162175
opts: &ViewOptions{
163-
Raw: true,
176+
Selector: "1234",
177+
Raw: true,
164178
},
165179
gist: &shared.Gist{
166180
Description: "some files",
@@ -200,8 +214,6 @@ func Test_viewRun(t *testing.T) {
200214
io.SetStdoutTTY(true)
201215
tt.opts.IO = io
202216

203-
tt.opts.Selector = "1234"
204-
205217
t.Run(tt.name, func(t *testing.T) {
206218
err := viewRun(tt.opts)
207219
if tt.wantErr {

0 commit comments

Comments
 (0)