fix: truncated title

This commit is contained in:
Jason Song 2022-12-12 10:54:28 +08:00
parent 22203eeb38
commit dbef504584
No known key found for this signature in database
GPG Key ID: 8402EEEE4511A8B5

View File

@ -6,6 +6,7 @@ package actions
import (
"context"
"fmt"
"strings"
actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
@ -22,6 +23,7 @@ import (
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
actions_service "code.gitea.io/gitea/services/actions"
"github.com/nektos/act/pkg/jobparser"
@ -134,7 +136,7 @@ func notify(ctx context.Context, input *notifyInput) error {
for id, content := range workflows {
run := actions_model.ActionRun{
Title: strings.SplitN(commit.CommitMessage, "\n", 2)[0],
Title: truncateContent(strings.SplitN(commit.CommitMessage, "\n", 2)[0], 255),
RepoID: input.Repo.ID,
OwnerID: input.Repo.OwnerID,
WorkflowID: id,
@ -146,9 +148,6 @@ func notify(ctx context.Context, input *notifyInput) error {
EventPayload: string(p),
Status: actions_model.StatusWaiting,
}
if len(run.Title) > 255 {
run.Title = run.Title[:255] // FIXME: we should use a better method to cut title
}
jobs, err := jobparser.Parse(content)
if err != nil {
log.Error("jobparser.Parse: %v", err)
@ -215,3 +214,15 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo
}).
Notify(ctx)
}
func truncateContent(content string, n int) string {
truncatedContent, truncatedRight := util.SplitStringAtByteN(content, n)
if truncatedRight != "" {
// in case the content is in a Latin family language, we remove the last broken word.
lastSpaceIdx := strings.LastIndex(truncatedContent, " ")
if lastSpaceIdx != -1 && (len(truncatedContent)-lastSpaceIdx < 15) {
truncatedContent = truncatedContent[:lastSpaceIdx] + "…"
}
}
return truncatedContent
}