-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Truncate commit message during Discord webhook push events #31970
base: main
Are you sure you want to change the base?
Conversation
|
||
// a limit of 50 is set because GitHub does the same | ||
if len(message) > 50 { | ||
message = fmt.Sprintf("%.47s...", message) |
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.
From reading the Go docs, it appears that this will treat message
as bytes when truncating. Is there a better way of truncating the size of a Go string without having problems with unicode characters? I will continue to find a solution to this when I get the time.
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.
After further doc reading, I learned that precision is measured in runes when using the %s
specifier (if I used %x
it would be measured in bytes). So I think my current usage is fine.
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.
Are you able to test with unicode to see how discord would behave if we sent them truncated characters?
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.
I modified my test to contain unicode characters. I also realized len(message)
returns the # of bytes in message
rather than characters. Therefore, I opted to use utf8.RuneCountInString()
.
However, it appears that git commit messages, although typically utf-8 encoded, could accept other encodings. git encourages use of utf-8 for commit messages, but it doesn't enforce it. So I am not sure if the way I count the # of characters in message
and how I truncate message
is correct. Using utf8.RuneCountInString()
could come with a performance cost (i.e. if the commit summary is very large).
return pushTestPayloadWithCommitMessage("This is a commit summary that is longer than 50 characters\n\nThis is the message body.") | ||
} | ||
|
||
func pushTestPayloadWithCommitMessage(message string) *api.PushPayload { |
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.
Wanted to avoid test code duplication here by adding these 2 functions. I'm open for alternative ways to writing my test case.
This is in-line with what our other webhook implementations do.
146603a
to
0983b27
Compare
Resolves #31668.