-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from armosec/url-composer
compose file urls for supported providers
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters