From fa8503ae771ce610afeea251addab2602cc3e1f5 Mon Sep 17 00:00:00 2001 From: Amir Malka Date: Wed, 20 Jul 2022 14:05:58 +0300 Subject: [PATCH] compose file urls for supported providers --- apis/provider.go | 22 ++++++++++++++ apis/urlcomposer.go | 62 +++++++++++++++++++++++++++++++++++++++ apis/urlcomposer_test.go | 44 +++++++++++++++++++++++++++ azureparser/v1/parser.go | 3 +- githubparser/v1/parser.go | 3 +- 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 apis/provider.go create mode 100644 apis/urlcomposer.go create mode 100644 apis/urlcomposer_test.go diff --git a/apis/provider.go b/apis/provider.go new file mode 100644 index 0000000..209d9f5 --- /dev/null +++ b/apis/provider.go @@ -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) +} diff --git a/apis/urlcomposer.go b/apis/urlcomposer.go new file mode 100644 index 0000000..f9218ed --- /dev/null +++ b/apis/urlcomposer.go @@ -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)) +} diff --git a/apis/urlcomposer_test.go b/apis/urlcomposer_test.go new file mode 100644 index 0000000..c77017e --- /dev/null +++ b/apis/urlcomposer_test.go @@ -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")) + } + +} diff --git a/azureparser/v1/parser.go b/azureparser/v1/parser.go index 9575a1f..3860ea5 100644 --- a/azureparser/v1/parser.go +++ b/azureparser/v1/parser.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "github.com/armosec/go-git-url/apis" giturl "github.com/whilp/git-urls" ) @@ -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 } diff --git a/githubparser/v1/parser.go b/githubparser/v1/parser.go index 45d3e26..fa3501b 100644 --- a/githubparser/v1/parser.go +++ b/githubparser/v1/parser.go @@ -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" ) @@ -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 }