Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kemzeb
Copy link
Contributor

@kemzeb kemzeb commented Sep 3, 2024

Resolves #31668.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 3, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 3, 2024
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Sep 3, 2024

// a limit of 50 is set because GitHub does the same
if len(message) > 50 {
message = fmt.Sprintf("%.47s...", message)
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

@kemzeb kemzeb Sep 12, 2024

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 {
Copy link
Contributor Author

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.

@lunny lunny added type/bug backport/v1.22 This PR should be backported to Gitea 1.22 labels Sep 6, 2024
@yp05327 yp05327 added this to the 1.23.0 milestone Sep 17, 2024
services/webhook/discord.go Outdated Show resolved Hide resolved
@kemzeb kemzeb force-pushed the feat-make-discord-msg-more-readable branch from 146603a to 0983b27 Compare September 17, 2024 23:12
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v1.22 This PR should be backported to Gitea 1.22 lgtm/need 1 This PR needs approval from one additional maintainer to be merged. modifies/go Pull requests that update Go code size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/bug
5 participants