Merge pull request #8609 from ywk253100/190808_replicate_meta

Only replicate "public" metadata when creating project in Harbor
This commit is contained in:
Wenkai Yin(尹文开) 2019-08-12 17:31:02 +08:00 committed by GitHub
commit 67a60df033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -156,7 +156,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
paths := strings.Split(resource.Metadata.Repository.Name, "/")
projectName := paths[0]
// handle the public properties
metadata := resource.Metadata.Repository.Metadata
metadata := abstractPublicMetadata(resource.Metadata.Repository.Metadata)
pro, exist := projects[projectName]
if exist {
metadata = mergeMetadata(pro.Metadata, metadata)
@ -187,6 +187,19 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
return nil
}
func abstractPublicMetadata(metadata map[string]interface{}) map[string]interface{} {
if metadata == nil {
return nil
}
public, exist := metadata["public"]
if !exist {
return nil
}
return map[string]interface{}{
"public": public,
}
}
// currently, mergeMetadata only handles the public metadata
func mergeMetadata(metadata1, metadata2 map[string]interface{}) map[string]interface{} {
public := parsePublic(metadata1) && parsePublic(metadata2)

View File

@ -210,3 +210,26 @@ func TestMergeMetadata(t *testing.T) {
assert.Equal(t, strconv.FormatBool(c.public), m["public"].(string))
}
}
func TestAbstractPublicMetadata(t *testing.T) {
// nil input metadata
meta := abstractPublicMetadata(nil)
assert.Nil(t, meta)
// contains no public metadata
metadata := map[string]interface{}{
"other": "test",
}
meta = abstractPublicMetadata(metadata)
assert.Nil(t, meta)
// contains public metadata
metadata = map[string]interface{}{
"other": "test",
"public": "true",
}
meta = abstractPublicMetadata(metadata)
require.NotNil(t, meta)
require.Equal(t, 1, len(meta))
require.Equal(t, "true", meta["public"].(string))
}