Skip to content

Commit b4927ee

Browse files
authored
fix: use base-url for finding link (#196)
1 parent e259b09 commit b4927ee

File tree

8 files changed

+79
-11
lines changed

8 files changed

+79
-11
lines changed

analyze/analyze.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/boostsecurityio/poutine/opa"
1515
"github.com/boostsecurityio/poutine/providers/pkgsupply"
16+
"github.com/boostsecurityio/poutine/providers/scm/domain"
1617
"github.com/boostsecurityio/poutine/scanner"
1718
"github.com/rs/zerolog/log"
1819
"github.com/schollz/progressbar/v3"
@@ -274,7 +275,16 @@ func (a *Analyzer) generatePackageInsights(ctx context.Context, tempDir string,
274275
return nil, fmt.Errorf("failed to get commit SHA: %w", err)
275276
}
276277

277-
purl, _ := models.NewPurl(fmt.Sprintf("pkg:%s/%s", repo.GetProviderName(), repo.GetRepoIdentifier()))
278+
var (
279+
purl models.Purl
280+
domain = a.ScmClient.GetProviderBaseURL()
281+
)
282+
if domain != scm_domain.DefaultGitHubDomain && domain != scm_domain.DefaultGitLabDomain {
283+
purl, _ = models.NewPurl(fmt.Sprintf("pkg:%s/%s?repository_url=%s", repo.GetProviderName(), repo.GetRepoIdentifier(), domain))
284+
} else {
285+
purl, _ = models.NewPurl(fmt.Sprintf("pkg:%s/%s", repo.GetProviderName(), repo.GetRepoIdentifier()))
286+
}
287+
278288
switch ref {
279289
case "HEAD", "":
280290
ref, err = a.GitClient.GetRepoHeadBranchName(ctx, tempDir)

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/boostsecurityio/poutine/opa"
1212
"github.com/boostsecurityio/poutine/providers/gitops"
1313
"github.com/boostsecurityio/poutine/providers/scm"
14+
"github.com/boostsecurityio/poutine/providers/scm/domain"
1415
"github.com/rs/zerolog"
1516
"github.com/rs/zerolog/log"
1617
"github.com/spf13/viper"
@@ -26,7 +27,7 @@ import (
2627
var Format string
2728
var Verbose bool
2829
var ScmProvider string
29-
var ScmBaseURL scm.ScmBaseDomain
30+
var ScmBaseURL scm_domain.ScmBaseDomain
3031
var (
3132
Version string
3233
Commit string

models/purl.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package models
22

33
import (
44
"fmt"
5-
"github.com/package-url/packageurl-go"
65
"strings"
6+
7+
"github.com/package-url/packageurl-go"
78
)
89

910
type Purl struct {
@@ -45,11 +46,22 @@ func (p *Purl) FullName() string {
4546

4647
func (p *Purl) Link() string {
4748
repo := p.FullName()
49+
qualifiers := p.Qualifiers.Map()
50+
repoUrl := qualifiers["repository_url"]
51+
4852
if p.Type == "githubactions" || p.Type == "github" {
49-
return fmt.Sprintf("https://github.com/%s", repo)
53+
if repoUrl != "" {
54+
return fmt.Sprintf("https://%s/%s", repoUrl, repo)
55+
} else {
56+
return fmt.Sprintf("https://github.com/%s", repo)
57+
}
5058
}
5159
if p.Type == "gitlab" {
52-
return fmt.Sprintf("https://gitlab.com/%s", repo)
60+
if repoUrl != "" {
61+
return fmt.Sprintf("https://%s/%s", repoUrl, repo)
62+
} else {
63+
return fmt.Sprintf("https://gitlab.com/%s", repo)
64+
}
5365
}
5466
return ""
5567
}

models/purl_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,44 @@ func TestPurlFromGithubActions(t *testing.T) {
8787
}
8888
}
8989
}
90+
91+
func TestPurlLink(t *testing.T) {
92+
cases := []struct {
93+
name string
94+
purl string
95+
expected string
96+
}{
97+
// GitHub
98+
{
99+
name: "github.com default",
100+
purl: "pkg:githubactions/actions/checkout@v4",
101+
expected: "https://github.com/actions/checkout",
102+
},
103+
{
104+
name: "github custom base ",
105+
purl: "pkg:githubactions/actions/checkout@v4?repository_url=github.example.com",
106+
expected: "https://github.example.com/actions/checkout",
107+
},
108+
// GitLab
109+
{
110+
name: "gitlab.com default",
111+
purl: "pkg:gitlab/include/remote?download_url=https%3A%2F%2Fexample.com%2F.gitlab-ci.yml",
112+
expected: "https://gitlab.com/include/remote",
113+
},
114+
{
115+
name: "gitlab custom base",
116+
purl: "pkg:gitlab/include/remote?download_url=https%3A%2F%2Fexample.com%2F.gitlab-ci.yml&repository_url=gitlab.example.com",
117+
expected: "https://gitlab.example.com/include/remote",
118+
},
119+
}
120+
121+
for _, c := range cases {
122+
t.Run(c.name, func(t *testing.T) {
123+
p, err := NewPurl(c.purl)
124+
assert.Nil(t, err)
125+
126+
link := p.Link()
127+
assert.Equal(t, c.expected, link)
128+
})
129+
}
130+
}

providers/github/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/boostsecurityio/poutine/analyze"
13+
"github.com/boostsecurityio/poutine/providers/scm/domain"
1314
"github.com/rs/zerolog/log"
1415

1516
"github.com/gofri/go-github-ratelimit/github_ratelimit"
@@ -19,10 +20,9 @@ import (
1920
)
2021

2122
const GitHub string = "github"
22-
const defaultDomain string = "github.com"
2323

2424
func NewGithubSCMClient(ctx context.Context, baseURL string, token string) (*ScmClient, error) {
25-
domain := defaultDomain
25+
domain := scm_domain.DefaultGitHubDomain
2626
if baseURL != "" {
2727
domain = baseURL
2828
}
@@ -150,7 +150,7 @@ func NewClient(ctx context.Context, token string, domain string) (*Client, error
150150
graphQLClient *githubv4.Client
151151
)
152152

153-
if domain == defaultDomain {
153+
if domain == scm_domain.DefaultGitHubDomain {
154154
graphQLClient = githubv4.NewClient(httpClient)
155155
} else {
156156
baseURL := fmt.Sprintf("https://%s/", domain)

providers/gitlab/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
"strings"
99

1010
"github.com/boostsecurityio/poutine/analyze"
11+
"github.com/boostsecurityio/poutine/providers/scm/domain"
1112
"github.com/xanzy/go-gitlab"
1213
)
1314

1415
const GitLab string = "gitlab"
1516

1617
func NewGitlabSCMClient(ctx context.Context, baseURL string, token string) (*ScmClient, error) {
17-
domain := "gitlab.com"
18+
domain := scm_domain.DefaultGitLabDomain
1819
if baseURL != "" {
1920
domain = baseURL
2021
}

providers/scm/scm_domain.go renamed to providers/scm/domain/scm_domain.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package scm
1+
package scm_domain
22

33
import "strings"
44

55
// ScmBaseDomain represent the base domain for a SCM provider.
66
type ScmBaseDomain string
77

8+
const DefaultGitHubDomain string = "github.com"
9+
const DefaultGitLabDomain string = "gitlab.com"
10+
811
var schemePrefixes = []string{"https://", "http://"}
912

1013
func (d *ScmBaseDomain) Set(value string) error {

providers/scm/scm_domain_test.go renamed to providers/scm/domain/scm_domain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package scm
1+
package scm_domain
22

33
import "testing"
44

0 commit comments

Comments
 (0)