-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RHTAPSRE-469: Webhook configuration based on repo url
Allow to configure the webhook target url based on the repository url of the component by providing a config file. Configuring the webhook target url using environment variable has precedence. In case that no config file or environment variable for configuring the webhook target are provided, the PAC route will be used (same as before this change). Signed-off-by: gbenhaim <[email protected]>
- Loading branch information
Showing
6 changed files
with
279 additions
and
13 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package webhook | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
) | ||
|
||
var log logr.Logger = ctrl.Log.WithName("webhook") | ||
|
||
type WebhookURLLoader interface { | ||
Load(repositoryUrl string) string | ||
} | ||
|
||
type ConfigWebhookURLLoader struct { | ||
// Prefix to target url mapping | ||
mapping map[string]string | ||
} | ||
|
||
func NewConfigWebhookURLLoader(mapping map[string]string) ConfigWebhookURLLoader { | ||
return ConfigWebhookURLLoader{mapping: mapping} | ||
} | ||
|
||
/* | ||
Load implements WebhookURLLoader. | ||
Find the longest prefix match of `repositoryUrl“ and the keys of `mapping`, | ||
and return the value of that key. | ||
*/ | ||
func (c ConfigWebhookURLLoader) Load(repositoryUrl string) string { | ||
longestPrefixLen := 0 | ||
matchedTarget := "" | ||
for prefix, target := range c.mapping { | ||
if strings.HasPrefix(repositoryUrl, prefix) && len(prefix) > longestPrefixLen { | ||
longestPrefixLen = len(prefix) | ||
matchedTarget = target | ||
} | ||
} | ||
|
||
// Provide a default using the empty string | ||
if matchedTarget == "" { | ||
if val, ok := c.mapping[""]; ok { | ||
matchedTarget = val | ||
} | ||
} | ||
|
||
return matchedTarget | ||
} | ||
|
||
var _ WebhookURLLoader = ConfigWebhookURLLoader{} | ||
|
||
type FileReader func(name string) ([]byte, error) | ||
|
||
// Load the prefix to target url from a file | ||
func LoadMappingFromFile(path string, fileReader FileReader) (map[string]string, error) { | ||
if path == "" { | ||
log.Info("Webhook config was not provided") | ||
return map[string]string{}, nil | ||
} | ||
|
||
content, err := fileReader(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var mapping map[string]string | ||
err = json.Unmarshal(content, &mapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
log.Info("Using webhook config", "config", mapping) | ||
|
||
return mapping, nil | ||
} |
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,162 @@ | ||
package webhook | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestConfigWebhookURLLoader_Load(t *testing.T) { | ||
type fields struct { | ||
mapping map[string]string | ||
} | ||
type args struct { | ||
repositoryUrl string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "No match should return an empty string", | ||
fields: fields{ | ||
mapping: map[string]string{}, | ||
}, | ||
args: args{ | ||
repositoryUrl: "https://github.com/org/repo", | ||
}, | ||
want: "", | ||
}, | ||
{ | ||
name: "Multiple prefix' with an exact match", | ||
fields: fields{ | ||
mapping: map[string]string{ | ||
"https://github.com/org/repo": "chosenTarget", | ||
"https://github.com/org/": "otherTarget1", | ||
"https://github.com/": "otherTarget2", | ||
"https://gitlab.com/": "otherTarget3", | ||
}, | ||
}, | ||
args: args{ | ||
repositoryUrl: "https://github.com/org/repo", | ||
}, | ||
want: "chosenTarget", | ||
}, | ||
{ | ||
name: "No exact match, the longest prefix is chosen", | ||
fields: fields{ | ||
mapping: map[string]string{ | ||
"https://github.com/org/": "chosenTarget", | ||
"https://github.com/": "otherTarget1", | ||
"https://gitlab.com/": "otherTarget2", | ||
}, | ||
}, | ||
args: args{ | ||
repositoryUrl: "https://github.com/org/repo", | ||
}, | ||
want: "chosenTarget", | ||
}, | ||
{ | ||
name: "Match on an empty string", | ||
fields: fields{ | ||
mapping: map[string]string{ | ||
"": "chosenTarget", | ||
"https://gitlab.com/": "otherTarget2", | ||
}, | ||
}, | ||
args: args{ | ||
repositoryUrl: "https://github.com/org/repo", | ||
}, | ||
want: "chosenTarget", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := NewConfigWebhookURLLoader(tt.fields.mapping) | ||
if got := c.Load(tt.args.repositoryUrl); got != tt.want { | ||
t.Errorf("ConfigWebhookURLLoader.Load() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLoadMappingFromFile(t *testing.T) { | ||
type args struct { | ||
path string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
fileReader FileReader | ||
want map[string]string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Load empty file", | ||
args: args{path: "file"}, | ||
fileReader: func(name string) ([]byte, error) { | ||
return []byte("{}"), nil | ||
}, | ||
want: map[string]string{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Load non empty file", | ||
args: args{path: "file"}, | ||
fileReader: func(name string) ([]byte, error) { | ||
return []byte(` | ||
{ | ||
"a": "1", | ||
"b": "2" | ||
} | ||
`), nil | ||
}, | ||
want: map[string]string{ | ||
"a": "1", | ||
"b": "2", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "The given path is an empty string", | ||
args: args{path: ""}, | ||
fileReader: func(name string) ([]byte, error) { | ||
return nil, nil | ||
}, | ||
want: map[string]string{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Load file with broken json", | ||
args: args{path: "file"}, | ||
fileReader: func(name string) ([]byte, error) { | ||
return []byte("abc"), nil | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Load file random error", | ||
args: args{path: "file"}, | ||
fileReader: func(name string) ([]byte, error) { | ||
return nil, errors.New("Random Error") | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := LoadMappingFromFile(tt.args.path, tt.fileReader) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("LoadMappingFromFile() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("LoadMappingFromFile() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |