You've already forked woodpecker
mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2026-06-03 16:35:37 +02:00
Trim white spaces for cron create/update (#6690)
This commit is contained in:
+38
-29
@@ -18,6 +18,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -126,11 +127,11 @@ func PostCron(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
cron := &model.Cron{
|
cron := &model.Cron{
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
Name: in.Name,
|
Name: strings.TrimSpace(in.Name),
|
||||||
CreatorID: user.ID,
|
CreatorID: user.ID,
|
||||||
Schedule: in.Schedule,
|
Schedule: strings.TrimSpace(in.Schedule),
|
||||||
Timezone: in.Timezone,
|
Timezone: strings.TrimSpace(in.Timezone),
|
||||||
Branch: in.Branch,
|
Branch: strings.TrimSpace(in.Branch),
|
||||||
Variables: in.Variables,
|
Variables: in.Variables,
|
||||||
Enabled: in.Enabled,
|
Enabled: in.Enabled,
|
||||||
}
|
}
|
||||||
@@ -142,7 +143,7 @@ func PostCron(c *gin.Context) {
|
|||||||
return
|
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 {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
||||||
return
|
return
|
||||||
@@ -151,7 +152,7 @@ func PostCron(c *gin.Context) {
|
|||||||
|
|
||||||
if in.Branch != "" {
|
if in.Branch != "" {
|
||||||
// check if branch exists on forge
|
// 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 {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
|
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
|
||||||
return
|
return
|
||||||
@@ -209,35 +210,43 @@ func PatchCron(c *gin.Context) {
|
|||||||
handleDBError(c, err)
|
handleDBError(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if in.Branch != nil && *in.Branch != "" {
|
if in.Branch != nil {
|
||||||
// check if branch exists on forge
|
if branch := strings.TrimSpace(*in.Branch); branch != "" {
|
||||||
_, err := _forge.BranchHead(c, user, repo, *in.Branch)
|
// check if branch exists on forge
|
||||||
if err != nil {
|
_, err := _forge.BranchHead(c, user, repo, branch)
|
||||||
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
|
if err != nil {
|
||||||
return
|
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 != "" {
|
if in.Timezone != nil {
|
||||||
cron.Timezone = *in.Timezone
|
if tz := strings.TrimSpace(*in.Timezone); tz != "" {
|
||||||
nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now())
|
nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, tz, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
cron.Timezone = tz
|
||||||
|
cron.NextExec = nextExec.Unix()
|
||||||
}
|
}
|
||||||
cron.NextExec = nextExec.Unix()
|
|
||||||
}
|
}
|
||||||
if in.Schedule != nil && *in.Schedule != "" {
|
if in.Schedule != nil {
|
||||||
cron.Schedule = *in.Schedule
|
if schedule := strings.TrimSpace(*in.Schedule); schedule != "" {
|
||||||
nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now())
|
nextExec, err := cron_scheduler.CalcNewNext(schedule, cron.Timezone, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
cron.Schedule = schedule
|
||||||
|
cron.NextExec = nextExec.Unix()
|
||||||
}
|
}
|
||||||
cron.NextExec = nextExec.Unix()
|
|
||||||
}
|
}
|
||||||
if in.Name != nil && *in.Name != "" {
|
if in.Name != nil {
|
||||||
cron.Name = *in.Name
|
if name := strings.TrimSpace(*in.Name); name != "" {
|
||||||
|
cron.Name = name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if in.Enabled != nil {
|
if in.Enabled != nil {
|
||||||
cron.Enabled = *in.Enabled
|
cron.Enabled = *in.Enabled
|
||||||
|
|||||||
Reference in New Issue
Block a user