Skip to content

Commit 781310d

Browse files
authored
Trim title before insert/update to database to match the size requirements of database (#32498) (#32507)
1 parent f79f8e1 commit 781310d

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
@@ -386,6 +388,7 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
386388
if len(cols) > 0 {
387389
sess.Cols(cols...)
388390
}
391+
run.Title, _ = util.SplitStringAtByteN(run.Title, 255)
389392
affected, err := sess.Update(run)
390393
if err != nil {
391394
return err

models/actions/runner.go

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func GetRunnerByID(ctx context.Context, id int64) (*ActionRunner, error) {
242242
// UpdateRunner updates runner's information.
243243
func UpdateRunner(ctx context.Context, r *ActionRunner, cols ...string) error {
244244
e := db.GetEngine(ctx)
245+
r.Name, _ = util.SplitStringAtByteN(r.Name, 255)
245246
var err error
246247
if len(cols) == 0 {
247248
_, err = e.ID(r.ID).AllCols().Update(r)
@@ -263,6 +264,7 @@ func DeleteRunner(ctx context.Context, id int64) error {
263264

264265
// CreateRunner creates new runner.
265266
func CreateRunner(ctx context.Context, t *ActionRunner) error {
267+
t.Name, _ = util.SplitStringAtByteN(t.Name, 255)
266268
return db.Insert(ctx, t)
267269
}
268270

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
"github.com/robfig/cron/v3"
@@ -71,6 +72,7 @@ func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
7172

7273
// Loop through each schedule row
7374
for _, row := range rows {
75+
row.Title, _ = util.SplitStringAtByteN(row.Title, 255)
7476
// Create new schedule row
7577
if err = db.Insert(ctx, row); err != nil {
7678
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
}
@@ -381,6 +383,7 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
381383
}
382384

383385
// NewIssue creates new issue with labels for repository.
386+
// The title will be cut off at 255 characters if it's longer than 255 characters.
384387
func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) {
385388
ctx, committer, err := db.TxContext(ctx)
386389
if err != nil {
@@ -394,6 +397,7 @@ func NewIssue(ctx context.Context, repo *repo_model.Repository, issue *Issue, la
394397
}
395398

396399
issue.Index = idx
400+
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
397401

398402
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
399403
Repo: repo,

models/issues/pull.go

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

547547
issue.Index = idx
548+
issue.Title, _ = util.SplitStringAtByteN(issue.Title, 255)
548549

549550
if err = NewIssueWithIndex(ctx, issue.Poster, NewIssueOptions{
550551
Repo: repo,

models/project/project.go

+4
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy {
257257
}
258258

259259
// NewProject creates a new Project
260+
// The title will be cut off at 255 characters if it's longer than 255 characters.
260261
func NewProject(ctx context.Context, p *Project) error {
261262
if !IsBoardTypeValid(p.BoardType) {
262263
p.BoardType = BoardTypeNone
@@ -276,6 +277,8 @@ func NewProject(ctx context.Context, p *Project) error {
276277
}
277278
defer committer.Close()
278279

280+
p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
281+
279282
if err := db.Insert(ctx, p); err != nil {
280283
return err
281284
}
@@ -331,6 +334,7 @@ func UpdateProject(ctx context.Context, p *Project) error {
331334
p.CardType = CardTypeTextOnly
332335
}
333336

337+
p.Title, _ = util.SplitStringAtByteN(p.Title, 255)
334338
_, err := db.GetEngine(ctx).ID(p.ID).Cols(
335339
"title",
336340
"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)