Skip to content

Commit 98d9a71

Browse files
authored
Trim title before insert/update to database to match the size requirements of database (#32498)
Fix #32489
1 parent b4abb6d commit 98d9a71

File tree

8 files changed

+18
-0
lines changed

8 files changed

+18
-0
lines changed

models/actions/run.go

+3
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
261261
}
262262

263263
// InsertRun inserts a run
264+
// The title will be cut off at 255 characters if it's longer than 255 characters.
264265
func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWorkflow) error {
265266
ctx, committer, err := db.TxContext(ctx)
266267
if err != nil {
@@ -273,6 +274,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
273274
return err
274275
}
275276
run.Index = index
277+
run.Title, _ = util.SplitStringAtByteN(run.Title, 255)
276278

277279
if err := db.Insert(ctx, run); err != nil {
278280
return err
@@ -399,6 +401,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
399401
if len(cols) > 0 {
400402
sess.Cols(cols...)
401403
}
404+
run.Title, _ = util.SplitStringAtByteN(run.Title, 255)
402405
affected, err := sess.Update(run)
403406
if err != nil {
404407
return err

models/actions/runner.go

+2
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func GetRunnerByID(ctx context.Context, id int64) (*ActionRunner, error) {
252252
// UpdateRunner updates runner's information.
253253
func UpdateRunner(ctx context.Context, r *ActionRunner, cols ...string) error {
254254
e := db.GetEngine(ctx)
255+
r.Name, _ = util.SplitStringAtByteN(r.Name, 255)
255256
var err error
256257
if len(cols) == 0 {
257258
_, err = e.ID(r.ID).AllCols().Update(r)
@@ -278,6 +279,7 @@ func CreateRunner(ctx context.Context, t *ActionRunner) error {
278279
// Remove OwnerID to avoid confusion; it's not worth returning an error here.
279280
t.OwnerID = 0
280281
}
282+
t.Name, _ = util.SplitStringAtByteN(t.Name, 255)
281283
return db.Insert(ctx, t)
282284
}
283285

models/actions/schedule.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/timeutil"
15+
"code.gitea.io/gitea/modules/util"
1516
webhook_module "code.gitea.io/gitea/modules/webhook"
1617
)
1718

@@ -67,6 +68,7 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
6768

6869
// Loop through each schedule row
6970
for _, row := range rows {
71+
row.Title, _ = util.SplitStringAtByteN(row.Title, 255)
7072
// Create new schedule row
7173
if err = db.Insert(ctx, row); err != nil {
7274
return err

models/issues/issue_update.go

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/modules/references"
2222
api "code.gitea.io/gitea/modules/structs"
2323
"code.gitea.io/gitea/modules/timeutil"
24+
"code.gitea.io/gitea/modules/util"
2425

2526
"xorm.io/builder"
2627
)
@@ -138,6 +139,7 @@ func ChangeIssueTitle(ctx context.Context, issue *Issue, doer *user_model.User,
138139
}
139140
defer committer.Close()
140141

142+
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
141143
if err = UpdateIssueCols(ctx, issue, "name"); err != nil {
142144
return fmt.Errorf("updateIssueCols: %w", err)
143145
}
@@ -386,6 +388,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
386388
}
387389

388390
// NewIssue creates new issue with labels for repository.
391+
// The title will be cut off at 255 characters if it's longer than 255 characters.
389392
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
390393
ctx, committer, err := db.TxContext(ctx)
391394
if err != nil {
@@ -399,6 +402,7 @@ func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *Issue, la
399402
}
400403

401404
issue.Index = idx
405+
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
402406

403407
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
404408
Repo: repo,

models/issues/pull.go

+1
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Iss
572572
}
573573

574574
issue.Index = idx
575+
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
575576

576577
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
577578
Repo: repo,

models/project/project.go

+4
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy {
242242
}
243243

244244
// NewProject creates a new Project
245+
// The title will be cut off at 255 characters if it's longer than 255 characters.
245246
func NewProject(ctx context.Context, p *Project) error {
246247
if !IsTemplateTypeValid(p.TemplateType) {
247248
p.TemplateType = TemplateTypeNone
@@ -255,6 +256,8 @@ func NewProject(ctx context.Context, p *Project) error {
255256
return util.NewInvalidArgumentErrorf("project type is not valid")
256257
}
257258

259+
p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
260+
258261
return db.WithTx(ctx, func(ctx context.Context) error {
259262
if err := db.Insert(ctx, p); err != nil {
260263
return err
@@ -308,6 +311,7 @@ func UpdateProject(ctx context.Context, p *Project) error {
308311
p.CardType = CardTypeTextOnly
309312
}
310313

314+
p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
311315
_, err := db.GetEngine(ctx).ID(p.ID).Cols(
312316
"title",
313317
"description",

models/repo/release.go

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func IsReleaseExist(ctx context.Context, repoID int64, tagName string) (bool, er
156156

157157
// UpdateRelease updates all columns of a release
158158
func UpdateRelease(ctx context.Context, rel *Release) error {
159+
rel.Title, _ = util.SplitStringAtByteN(rel.Title, 255)
159160
_, err := db.GetEngine(ctx).ID(rel.ID).AllCols().Update(rel)
160161
return err
161162
}

services/release/release.go

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ func CreateRelease(gitRepo *git.Repository, rel *repo_model.Release, attachmentU
142142
return err
143143
}
144144

145+
rel.Title, _ = util.SplitStringAtByteN(rel.Title, 255)
145146
rel.LowerTagName = strings.ToLower(rel.TagName)
146147
if err = db.Insert(gitRepo.Ctx, rel); err != nil {
147148
return err

0 commit comments

Comments
 (0)