-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add function to create a more speaking id for commit status #705
base: main
Are you sure you want to change the base?
Changes from all commits
fa99576
4556ea1
ac2cd57
9f67364
d5b9a1a
d007470
9394e1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"crypto/sha1" | ||
"encoding/base64" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"unicode" | ||
"unicode/utf8" | ||
|
@@ -59,10 +60,37 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has the potential to result in a very long string which is undesirable. We need to think on a way to constrain this to a certain length. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, it might be an option to change eventMetadata keys accepted for building an extended commit status ID? @makkes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any suggestion how to proceed @makkes @stefanprodan ? |
||
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 { | ||
metadataKeys := make([]string, 0, len(event.Metadata)) | ||
metadataLabels := make([]string, 0) | ||
prefixedMetadataLabelsSuffix := "" | ||
|
||
// order Metadata keys | ||
for labelKey := range event.Metadata { | ||
metadataKeys = append(metadataKeys, labelKey) | ||
} | ||
sort.Strings(metadataKeys) | ||
// iteratore over ordered Metadata keys | ||
for _, key := range metadataKeys { | ||
if strings.Contains(key, "app.kubernetes.io/") { | ||
metadataLabels = append(metadataLabels, event.Metadata[key]) | ||
} | ||
} | ||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should explain here what the commit status ID actually is and how it can be leveraged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it might be enough to link to GitHub and GitLab related API endpoints - as this documentation stays up2date? @makkes
GitHub: https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28
GitLab: https://docs.gitlab.com/ee/api/commits.html#set-the-pipeline-status-of-a-commit