From ede1fcd45b7388b3bfcbd3fba4f340210f2dc298 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 2 Jun 2026 10:31:53 +0200 Subject: [PATCH] Trim white spaces for cron create/update (#6690) --- server/api/cron.go | 67 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/server/api/cron.go b/server/api/cron.go index ccb4f69696..49e4a59498 100644 --- a/server/api/cron.go +++ b/server/api/cron.go @@ -18,6 +18,7 @@ import ( "errors" "net/http" "strconv" + "strings" "time" "github.com/gin-gonic/gin" @@ -126,11 +127,11 @@ func PostCron(c *gin.Context) { } cron := &model.Cron{ RepoID: repo.ID, - Name: in.Name, + Name: strings.TrimSpace(in.Name), CreatorID: user.ID, - Schedule: in.Schedule, - Timezone: in.Timezone, - Branch: in.Branch, + Schedule: strings.TrimSpace(in.Schedule), + Timezone: strings.TrimSpace(in.Timezone), + Branch: strings.TrimSpace(in.Branch), Variables: in.Variables, Enabled: in.Enabled, } @@ -142,7 +143,7 @@ func PostCron(c *gin.Context) { return } - nextExec, err := cron_scheduler.CalcNewNext(in.Schedule, in.Timezone, time.Now()) + nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now()) if err != nil { c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err) return @@ -151,7 +152,7 @@ func PostCron(c *gin.Context) { if in.Branch != "" { // check if branch exists on forge - _, err := _forge.BranchHead(c, user, repo, in.Branch) + _, err := _forge.BranchHead(c, user, repo, cron.Branch) if err != nil { c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err) return @@ -209,35 +210,43 @@ func PatchCron(c *gin.Context) { handleDBError(c, err) return } - if in.Branch != nil && *in.Branch != "" { - // check if branch exists on forge - _, err := _forge.BranchHead(c, user, repo, *in.Branch) - if err != nil { - c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err) - return + if in.Branch != nil { + if branch := strings.TrimSpace(*in.Branch); branch != "" { + // check if branch exists on forge + _, err := _forge.BranchHead(c, user, repo, branch) + if err != nil { + c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err) + return + } + cron.Branch = branch } - cron.Branch = *in.Branch } - if in.Timezone != nil && *in.Timezone != "" { - cron.Timezone = *in.Timezone - nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now()) - if err != nil { - c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err) - return + if in.Timezone != nil { + if tz := strings.TrimSpace(*in.Timezone); tz != "" { + nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, tz, time.Now()) + if err != nil { + c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err) + return + } + cron.Timezone = tz + cron.NextExec = nextExec.Unix() } - cron.NextExec = nextExec.Unix() } - if in.Schedule != nil && *in.Schedule != "" { - cron.Schedule = *in.Schedule - nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now()) - if err != nil { - c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err) - return + if in.Schedule != nil { + if schedule := strings.TrimSpace(*in.Schedule); schedule != "" { + nextExec, err := cron_scheduler.CalcNewNext(schedule, cron.Timezone, time.Now()) + if err != nil { + c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err) + return + } + cron.Schedule = schedule + cron.NextExec = nextExec.Unix() } - cron.NextExec = nextExec.Unix() } - if in.Name != nil && *in.Name != "" { - cron.Name = *in.Name + if in.Name != nil { + if name := strings.TrimSpace(*in.Name); name != "" { + cron.Name = name + } } if in.Enabled != nil { cron.Enabled = *in.Enabled