Skip to content

Commit f7cb4d7

Browse files
Merge pull request #100 from naveensrinivasan/fix/http-path
fix - URL with trailing slash
2 parents 6b80b78 + 4362368 commit f7cb4d7

File tree

4 files changed

+105
-15
lines changed

4 files changed

+105
-15
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require (
1515
go.uber.org/zap v1.16.0
1616
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
1717
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
18-
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c // indirect
1918
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd // indirect
2019
google.golang.org/appengine v1.6.7 // indirect
2120
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,6 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R
362362
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
363363
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
364364
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
365-
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 h1:42cLlJJdEh+ySyeUUbEQ5bsTiq8voBeTuweGVkY6Puw=
366-
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
367365
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
368366
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
369367
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
@@ -374,8 +372,6 @@ golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7O
374372
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
375373
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
376374
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
377-
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 h1:ld7aEMNHoBnnDAX15v1T6z31v8HwR2A9FYOuAhWqkwc=
378-
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
379375
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58 h1:Mj83v+wSRNEar42a/MQgxk9X42TdEmrOl9i+y8WbxLo=
380376
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
381377
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -419,8 +415,6 @@ golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7w
419415
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
420416
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
421417
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
422-
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c h1:+B+zPA6081G5cEb2triOIJpcvSW4AYzmIyWAqMn2JAc=
423-
golang.org/x/sys v0.0.0-20201109165425-215b40eba54c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
424418
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
425419
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
426420
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -476,8 +470,6 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
476470
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
477471
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
478472
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
479-
golang.org/x/tools v0.0.0-20201105220310-78b158585360 h1:/9CzsU8hOpnSUCtem1vfWNgsVeCTgkMdx+VE5YIYxnU=
480-
golang.org/x/tools v0.0.0-20201105220310-78b158585360/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
481473
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd h1:kJP9fbfkpUoA4y03Nxor8be+YbShcXP16fc7G4nlgpw=
482474
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
483475
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/scorecard.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"log"
2121
"net/http"
22-
"regexp"
22+
"net/url"
2323
"strings"
2424
"sync"
2525

@@ -48,13 +48,21 @@ func (r *RepoURL) Type() string {
4848
}
4949

5050
func (r *RepoURL) Set(s string) error {
51-
rgx, _ := regexp.Compile("^https?://")
52-
s = rgx.ReplaceAllString(s, "")
53-
split := strings.SplitN(s, "/", 3)
54-
if len(split) != 3 {
51+
u, e := url.Parse(s)
52+
if e != nil {
53+
return e
54+
}
55+
56+
split := strings.SplitN(strings.Trim(u.Path, "/"), "/", 2)
57+
if len(split) != 2 {
58+
log.Fatalf("invalid repo flag: [%s], pass the full repository URL", s)
59+
}
60+
61+
if len(strings.TrimSpace(split[0])) == 0 || len(strings.TrimSpace(split[1])) == 0 {
5562
log.Fatalf("invalid repo flag: [%s], pass the full repository URL", s)
5663
}
57-
r.Host, r.Owner, r.Repo = split[0], split[1], split[2]
64+
65+
r.Host, r.Owner, r.Repo = u.Host, split[0], split[1]
5866

5967
switch r.Host {
6068
case "github.com":

pkg/scorecard_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2020 Security Scorecard Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pkg
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestRepoURL_Set(t *testing.T) {
22+
type fields struct {
23+
Host string
24+
Owner string
25+
Repo string
26+
}
27+
type args struct {
28+
s string
29+
}
30+
tests := []struct {
31+
name string
32+
fields fields
33+
args args
34+
wantErr bool
35+
}{
36+
{
37+
name: "Valid http address",
38+
fields: fields{
39+
Host: "github.com",
40+
Owner: "foo",
41+
Repo: "kubeflow",
42+
},
43+
args: args{s: "https://github.com/foo/kubeflow"},
44+
wantErr: false,
45+
},
46+
{
47+
name: "Valid http address with trailing slash",
48+
fields: fields{
49+
Host: "github.com",
50+
Owner: "foo",
51+
Repo: "kubeflow",
52+
},
53+
args: args{s: "https://github.com/foo/kubeflow/"},
54+
wantErr: false,
55+
},
56+
{
57+
name: "Non github repository",
58+
fields: fields{
59+
Host: "gitlab.com",
60+
Owner: "foo",
61+
Repo: "kubeflow",
62+
},
63+
args: args{s: "https://gitlab.com/foo/kubeflow"},
64+
wantErr: true,
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
r := &RepoURL{
70+
Host: tt.fields.Host,
71+
Owner: tt.fields.Owner,
72+
Repo: tt.fields.Repo,
73+
}
74+
t.Log("Test")
75+
if err := r.Set(tt.args.s); (err != nil) != tt.wantErr {
76+
t.Errorf("RepoURL.Set() error = %v, wantErr %v", err, tt.wantErr)
77+
}
78+
if !tt.wantErr {
79+
if tt.fields.Host != r.Host {
80+
t.Errorf("Repo Host expected to be %s but got %s", tt.fields.Host, r.Host)
81+
}
82+
if tt.fields.Owner != r.Owner {
83+
t.Errorf("Repo owner expected to be %s but got %s", tt.fields.Owner, r.Owner)
84+
}
85+
if tt.fields.Repo != r.Repo {
86+
t.Errorf("Repo expected to be %s but got %s", tt.fields.Repo, r.Repo)
87+
}
88+
}
89+
})
90+
}
91+
}

0 commit comments

Comments
 (0)