Skip to content

Commit

Permalink
feat: add function to create a more speaking id for commit status
Browse files Browse the repository at this point in the history
Signed-off-by: Brandt, Sebastian (ITO-PC) <[email protected]>
  • Loading branch information
sebbrandt87 committed Jan 8, 2024
1 parent dcabd7c commit ecbcebf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 20 additions & 1 deletion internal/notifier/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,29 @@ func formatNameAndDescription(event eventv1.Event) (string, string) {
// involved object kind and name.
func generateCommitStatusID(providerUID string, event eventv1.Event) string {
uidParts := strings.Split(providerUID, "-")
id := fmt.Sprintf("%s/%s/%s", event.InvolvedObject.Kind, event.InvolvedObject.Name, uidParts[0])
metadataLabelsSuffix := createMetadataLabelsSuffix(event)
id := fmt.Sprintf("%s/%s/%s%s", event.InvolvedObject.Kind, event.InvolvedObject.Name, uidParts[0], metadataLabelsSuffix)
return strings.ToLower(id)
}

// createMetadataLabelsSuffix returns a concated string depending on app.kubernetes.io/,
// prefixed common labels https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
func createMetadataLabelsSuffix(event eventv1.Event) string {
metadataLabels := make([]string, 0)
prefixedMetadataLabelsSuffix := ""
for labelKey, labelValue := range event.Metadata {
if strings.Contains(labelKey, "app.kubernetes.io/") {
metadataLabels = append(metadataLabels, labelValue)
}
}
metadataLabelsSuffix := strings.Join(metadataLabels, "/")
if len(metadataLabelsSuffix) > 0 {
prefixedMetadataLabelsSuffix = fmt.Sprintf("/%s", metadataLabelsSuffix)
}

return strings.ToLower(prefixedMetadataLabelsSuffix)
}

func splitCamelcase(src string) (entries []string) {
// don't split invalid utf8
if !utf8.ValidString(src) {
Expand Down
32 changes: 32 additions & 0 deletions internal/notifier/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,38 @@ func TestUtil_GenerateCommitStatusID(t *testing.T) {
},
want: "kustomization/gitops-system/0c9c2e41",
},
{
name: "test with non related metadata event case",
providerUID: "0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a",
event: eventv1.Event{
Metadata: map[string]string{
"foobar/batz": "test",
},
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Name: "gitops-system",
},
Reason: "ApplySucceeded",
},
want: "kustomization/gitops-system/0c9c2e41",
},
{
name: "test with related metadata event case",
providerUID: "0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a",
event: eventv1.Event{
Metadata: map[string]string{
"app.kubernetes.io/cluster": "testcluster",
"app.kubernetes.io/env": "test",
"app.kubernetes.io/region": "europe-west4",
},
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Name: "gitops-system",
},
Reason: "ApplySucceeded",
},
want: "kustomization/gitops-system/0c9c2e41/testcluster/test/europe-west4",
},
}

for _, tt := range statusIDTests {
Expand Down

0 comments on commit ecbcebf

Please sign in to comment.