Skip to content

Commit

Permalink
Merge pull request #1 from armosec/url-composer
Browse files Browse the repository at this point in the history
compose file urls for supported providers
  • Loading branch information
amirmalka authored Jul 20, 2022
2 parents 02f02f0 + fa8503a commit 34b96b6
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
22 changes: 22 additions & 0 deletions apis/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package apis

import "errors"

type ProviderType string

const (
ProviderGitHub ProviderType = "github"
ProviderAzure ProviderType = "azure"
)

func (pt ProviderType) IsSupported() error {
switch pt {
case ProviderGitHub, ProviderAzure:
return nil
}
return errors.New("unsupported provider")
}

func (pt ProviderType) String() string {
return string(pt)
}
62 changes: 62 additions & 0 deletions apis/urlcomposer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package apis

import (
"fmt"
)

type UrlComposer interface {
FileUrlByCommit(commit string) string
FileUrlByBranch(branch string) string
FileUrlByTag(tag string) string
}

type GitHubUrlComposer struct {
remoteUrl, path string
}

type AzureUrlComposer struct {
remoteUrl, path string
}

func NewUrlComposer(provider ProviderType, remoteUrl, path string) (UrlComposer, error) {
switch provider {
case ProviderAzure:
return &AzureUrlComposer{remoteUrl: remoteUrl, path: path}, nil
case ProviderGitHub:
return &GitHubUrlComposer{remoteUrl: remoteUrl, path: path}, nil
default:
return nil, fmt.Errorf("unknown provider")
}
}

func (r *GitHubUrlComposer) fileUrlBase(commitOrBranchOrTag string) string {
return fmt.Sprintf("%s/blob/%s/%s", r.remoteUrl, commitOrBranchOrTag, r.path)
}

func (r *GitHubUrlComposer) FileUrlByCommit(commit string) string {
return r.fileUrlBase(commit)
}

func (r *GitHubUrlComposer) FileUrlByBranch(branch string) string {
return r.fileUrlBase(branch)
}

func (r *GitHubUrlComposer) FileUrlByTag(tag string) string {
return r.fileUrlBase(tag)
}

func (r *AzureUrlComposer) fileUrlBase(version string) string {
return fmt.Sprintf("%s?path=%s&version=%s", r.remoteUrl, r.path, version)
}

func (r *AzureUrlComposer) FileUrlByCommit(commit string) string {
return r.fileUrlBase(fmt.Sprintf("GC%s", commit))
}

func (r *AzureUrlComposer) FileUrlByBranch(branch string) string {
return r.fileUrlBase(fmt.Sprintf("GB%s", branch))
}

func (r *AzureUrlComposer) FileUrlByTag(tag string) string {
return r.fileUrlBase(fmt.Sprintf("GT%s", tag))
}
44 changes: 44 additions & 0 deletions apis/urlcomposer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package apis

import (
"testing"

"github.com/stretchr/testify/assert"
)

var (
urlAzure = "https://dev.azure.com/dwertent/ks-testing-public/_git/ks-testing-public"
urlGitHub = "https://github.com/armosec/go-git-url"
)

func TestNewUrlComposer(t *testing.T) {
{
var provider = ProviderType(ProviderAzure.String())
g, err := NewUrlComposer(provider, urlAzure, "rules-tests/k8s-audit-logs-enabled-native/test-failed/input/apiserverpod.json")
assert.NoError(t, err)
assert.Equal(t,
"https://dev.azure.com/dwertent/ks-testing-public/_git/ks-testing-public?path=rules-tests/k8s-audit-logs-enabled-native/test-failed/input/apiserverpod.json&version=GBmaster",
g.FileUrlByBranch("master"))
assert.Equal(t,
"https://dev.azure.com/dwertent/ks-testing-public/_git/ks-testing-public?path=rules-tests/k8s-audit-logs-enabled-native/test-failed/input/apiserverpod.json&version=GC9300d038ca1be76711aacb2f7ba59b67f4bbddf1",
g.FileUrlByCommit("9300d038ca1be76711aacb2f7ba59b67f4bbddf1"))
assert.Equal(t,
"https://dev.azure.com/dwertent/ks-testing-public/_git/ks-testing-public?path=rules-tests/k8s-audit-logs-enabled-native/test-failed/input/apiserverpod.json&version=GTv1.0.179",
g.FileUrlByTag("v1.0.179"))
}
{
var provider = ProviderType(ProviderGitHub.String())
g, err := NewUrlComposer(provider, urlGitHub, "files/file0.json")
assert.NoError(t, err)
assert.Equal(t,
"https://github.com/armosec/go-git-url/blob/master/files/file0.json",
g.FileUrlByBranch("master"))
assert.Equal(t,
"https://github.com/armosec/go-git-url/blob/c7380d9b941a671253a81c694f644868270a1d81/files/file0.json",
g.FileUrlByCommit("c7380d9b941a671253a81c694f644868270a1d81"))
assert.Equal(t,
"https://github.com/armosec/go-git-url/blob/v0.0.13/files/file0.json",
g.FileUrlByTag("v0.0.13"))
}

}
3 changes: 2 additions & 1 deletion azureparser/v1/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/armosec/go-git-url/apis"
giturl "github.com/whilp/git-urls"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func (az *AzureURL) GetURL() *url.URL {

func IsHostAzure(host string) bool { return strings.HasSuffix(host, HOST) }

func (az *AzureURL) GetProvider() string { return "azure" }
func (az *AzureURL) GetProvider() string { return apis.ProviderAzure.String() }
func (az *AzureURL) GetHostName() string { return az.host }
func (az *AzureURL) GetProjectName() string { return az.project }
func (az *AzureURL) GetBranchName() string { return az.branch }
Expand Down
3 changes: 2 additions & 1 deletion githubparser/v1/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/armosec/go-git-url/apis"
"github.com/armosec/go-git-url/apis/githubapi"
giturl "github.com/whilp/git-urls"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func IsHostGitHub(host string) bool {
return host == githubapi.DEFAULT_HOST || host == githubapi.RAW_HOST
}

func (gh *GitHubURL) GetProvider() string { return "github" }
func (gh *GitHubURL) GetProvider() string { return apis.ProviderGitHub.String() }
func (gh *GitHubURL) GetHostName() string { return gh.host }
func (gh *GitHubURL) GetBranchName() string { return gh.branch }
func (gh *GitHubURL) GetOwnerName() string { return gh.owner }
Expand Down

0 comments on commit 34b96b6

Please sign in to comment.