Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/repo/cron/cron_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ var cronCreateCmd = &cli.Command{
Usage: "cron schedule",
Required: true,
},
&cli.BoolFlag{
Name: "enabled",
Usage: "whether cron is enabled",
Value: true,
},
common.FormatFlag(tmplCronList, true),
},
}
Expand All @@ -58,6 +63,7 @@ func cronCreate(ctx context.Context, c *cli.Command) error {
schedule = c.String("schedule")
repoIDOrFullName = c.String("repository")
format = c.String("format") + "\n"
enabled = c.Bool("enabled")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
Expand All @@ -77,6 +83,7 @@ func cronCreate(ctx context.Context, c *cli.Command) error {
Name: cronName,
Branch: branch,
Schedule: schedule,
Enabled: enabled,
}
cron, err = client.CronCreate(repoID, cron)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions cli/repo/cron/cron_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ var cronUpdateCmd = &cli.Command{
Name: "schedule",
Usage: "cron schedule",
},
&cli.BoolFlag{
Name: "enabled",
Usage: "whether cron is enabled",
Value: true,
},
common.FormatFlag(tmplCronList, true),
},
}
Expand All @@ -62,6 +67,7 @@ func cronUpdate(ctx context.Context, c *cli.Command) error {
branch = c.String("branch")
schedule = c.String("schedule")
format = c.String("format") + "\n"
enabled = c.Bool("enabled")
)
if repoIDOrFullName == "" {
repoIDOrFullName = c.Args().First()
Expand All @@ -79,6 +85,7 @@ func cronUpdate(ctx context.Context, c *cli.Command) error {
Name: jobName,
Branch: branch,
Schedule: schedule,
Enabled: enabled,
}
cron, err = client.CronUpdate(repoID, cron)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion server/api/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package api

import (
"errors"
"net/http"
"strconv"
"time"
Expand All @@ -28,6 +29,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v3/server/pipeline"
"go.woodpecker-ci.org/woodpecker/v3/server/router/middleware/session"
"go.woodpecker-ci.org/woodpecker/v3/server/store"
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
)

// GetCron
Expand Down Expand Up @@ -153,7 +155,11 @@ func PostCron(c *gin.Context) {
}

if err := _store.CronCreate(cron); err != nil {
c.String(http.StatusInternalServerError, "Error inserting cron %q. %s", in.Name, err)
if errors.Is(err, types.ErrUniqueExists) {
c.String(http.StatusConflict, "cron with this exists for this repo already")
} else {
c.String(http.StatusInternalServerError, "Error inserting cron %q. %s", in.Name, err)
}
return
}
c.JSON(http.StatusOK, cron)
Expand Down
6 changes: 6 additions & 0 deletions server/store/datastore/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
package datastore

import (
"strings"

"xorm.io/builder"

"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
)

func (s storage) CronCreate(cron *model.Cron) error {
if err := cron.Validate(); err != nil {
return err
}
_, err := s.engine.Insert(cron)
if err != nil && (strings.HasPrefix(err.Error(), "UNIQUE constraint failed") || strings.HasPrefix(err.Error(), "pq: duplicate key value violates unique constraint") || strings.Contains(err.Error(), "Duplicate entry")) {
return types.ErrUniqueExists
}
return err
}

Expand Down
3 changes: 2 additions & 1 deletion server/store/datastore/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/assert"

"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
)

func TestCronCreate(t *testing.T) {
Expand All @@ -33,7 +34,7 @@ func TestCronCreate(t *testing.T) {
assert.NotEqualValues(t, 0, cron1.ID)

// cannot insert cron job with same repoID and title
assert.Error(t, store.CronCreate(cron1))
assert.ErrorIs(t, types.ErrUniqueExists, store.CronCreate(cron1))

oldID := cron1.ID
assert.NoError(t, store.CronDelete(repo, oldID))
Expand Down
66 changes: 66 additions & 0 deletions server/store/mocks/mock_Store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion server/store/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package types

import "database/sql"
import (
"database/sql"
"errors"
)

var RecordNotExist = sql.ErrNoRows

var ErrUniqueExists = errors.New("unique constraint failed")
1 change: 1 addition & 0 deletions woodpecker-go/woodpecker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type (
Schedule string `json:"schedule"`
Created int64 `json:"created"`
Branch string `json:"branch"`
Enabled bool `json:"enabled"`
}

// PipelineOptions is the JSON data for creating a new pipeline.
Expand Down