Add cron feature (#934)

https://woodpecker-ci.org/docs/usage/cron

Co-authored-by: Anbraten <anton@ju60.de>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
This commit is contained in:
6543 2022-09-01 00:36:32 +02:00 committed by GitHub
parent 65587e3e30
commit 383f273392
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
63 changed files with 1604 additions and 123 deletions

21
cli/cron/cron.go Normal file
View file

@ -0,0 +1,21 @@
package cron
import (
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
)
// Command exports the cron command set.
var Command = &cli.Command{
Name: "cron",
Usage: "manage cron jobs",
Flags: common.GlobalFlags,
Subcommands: []*cli.Command{
cronCreateCmd,
cronDeleteCmd,
cronUpdateCmd,
cronInfoCmd,
cronListCmd,
},
}

72
cli/cron/cron_add.go Normal file
View file

@ -0,0 +1,72 @@
package cron
import (
"html/template"
"os"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)
var cronCreateCmd = &cli.Command{
Name: "add",
Usage: "adds a cron",
ArgsUsage: "[repo/name]",
Action: cronCreate,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "cron name",
Required: true,
},
&cli.StringFlag{
Name: "branch",
Usage: "cron branch",
},
&cli.StringFlag{
Name: "schedule",
Usage: "cron schedule",
Required: true,
},
common.FormatFlag(tmplCronList, true),
),
}
func cronCreate(c *cli.Context) error {
var (
jobName = c.String("name")
branch = c.String("branch")
schedule = c.String("schedule")
reponame = c.String("repository")
format = c.String("format") + "\n"
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
cron := &woodpecker.Cron{
Name: jobName,
Branch: branch,
Schedule: schedule,
}
cron, err = client.CronCreate(owner, name, cron)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}

55
cli/cron/cron_info.go Normal file
View file

@ -0,0 +1,55 @@
package cron
import (
"html/template"
"os"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var cronInfoCmd = &cli.Command{
Name: "info",
Usage: "display cron info",
ArgsUsage: "[repo/name]",
Action: cronInfo,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
common.FormatFlag(tmplCronList, true),
),
}
func cronInfo(c *cli.Context) error {
var (
jobID = c.Int64("id")
reponame = c.String("repository")
format = c.String("format") + "\n"
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
cron, err := client.CronGet(owner, name, jobID)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}

62
cli/cron/cron_list.go Normal file
View file

@ -0,0 +1,62 @@
package cron
import (
"html/template"
"os"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var cronListCmd = &cli.Command{
Name: "ls",
Usage: "list registries",
ArgsUsage: "[repo/name]",
Action: cronList,
Flags: append(common.GlobalFlags,
common.RepoFlag,
common.FormatFlag(tmplCronList, true),
),
}
func cronList(c *cli.Context) error {
var (
format = c.String("format") + "\n"
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
list, err := client.CronList(owner, name)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
for _, cron := range list {
if err := tmpl.Execute(os.Stdout, cron); err != nil {
return err
}
}
return nil
}
// template for build list information
var tmplCronList = "\x1b[33m{{ .Name }} \x1b[0m" + `
ID: {{ .ID }}
Branch: {{ .Branch }}
Schedule: {{ .Schedule }}
NextExec: {{ .NextExec }}
`

50
cli/cron/cron_rm.go Normal file
View file

@ -0,0 +1,50 @@
package cron
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
)
var cronDeleteCmd = &cli.Command{
Name: "rm",
Usage: "remove a cron",
ArgsUsage: "[repo/name]",
Action: cronDelete,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
),
}
func cronDelete(c *cli.Context) error {
var (
jobID = c.Int64("id")
reponame = c.String("repository")
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
err = client.CronDelete(owner, name, jobID)
if err != nil {
return err
}
fmt.Println("Success")
return nil
}

77
cli/cron/cron_update.go Normal file
View file

@ -0,0 +1,77 @@
package cron
import (
"html/template"
"os"
"github.com/urfave/cli/v2"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/internal"
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
)
var cronUpdateCmd = &cli.Command{
Name: "update",
Usage: "update a cron",
ArgsUsage: "[repo/name]",
Action: cronUpdate,
Flags: append(common.GlobalFlags,
common.RepoFlag,
&cli.StringFlag{
Name: "id",
Usage: "cron id",
Required: true,
},
&cli.StringFlag{
Name: "name",
Usage: "cron name",
},
&cli.StringFlag{
Name: "branch",
Usage: "cron branch",
},
&cli.StringFlag{
Name: "schedule",
Usage: "cron schedule",
},
common.FormatFlag(tmplCronList, true),
),
}
func cronUpdate(c *cli.Context) error {
var (
reponame = c.String("repository")
jobID = c.Int64("id")
jobName = c.String("name")
branch = c.String("branch")
schedule = c.String("schedule")
format = c.String("format") + "\n"
)
if reponame == "" {
reponame = c.Args().First()
}
owner, name, err := internal.ParseRepo(reponame)
if err != nil {
return err
}
client, err := internal.NewClient(c)
if err != nil {
return err
}
cron := &woodpecker.Cron{
ID: jobID,
Name: jobName,
Branch: branch,
Schedule: schedule,
}
cron, err = client.CronUpdate(owner, name, cron)
if err != nil {
return err
}
tmpl, err := template.New("_").Parse(format)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, cron)
}

View file

@ -25,10 +25,7 @@ var secretCreateCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",

View file

@ -25,10 +25,7 @@ var secretInfoCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",

View file

@ -26,10 +26,7 @@ var secretListCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
common.FormatFlag(tmplSecretList, true),
),
}

View file

@ -21,10 +21,7 @@ var secretDeleteCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",

View file

@ -25,10 +25,7 @@ var secretUpdateCmd = &cli.Command{
Name: "organization",
Usage: "organization name (e.g. octocat)",
},
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "name",
Usage: "secret name",

View file

@ -23,6 +23,7 @@ import (
"github.com/woodpecker-ci/woodpecker/cli/build"
"github.com/woodpecker-ci/woodpecker/cli/common"
"github.com/woodpecker-ci/woodpecker/cli/cron"
"github.com/woodpecker-ci/woodpecker/cli/deploy"
"github.com/woodpecker-ci/woodpecker/cli/exec"
"github.com/woodpecker-ci/woodpecker/cli/info"
@ -56,6 +57,7 @@ func newApp() *cli.App {
user.Command,
lint.Command,
loglevel.Command,
cron.Command,
}
zlog.Logger = zlog.Output(

View file

@ -39,6 +39,7 @@ import (
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/cron"
woodpeckerGrpcServer "github.com/woodpecker-ci/woodpecker/server/grpc"
"github.com/woodpecker-ci/woodpecker/server/logging"
"github.com/woodpecker-ci/woodpecker/server/model"
@ -185,6 +186,10 @@ func run(c *cli.Context) error {
setupMetrics(&g, _store)
g.Go(func() error {
return cron.Start(c.Context, _store)
})
// start the server with tls enabled
if c.String("server-cert") != "" {
g.Go(func() error {

View file

@ -341,10 +341,10 @@ when:
:::info
**By default steps are filtered by following event types:**
`push`, `pull_request, `tag`, `deployment`.
`push`, `pull_request`, `tag`, `deployment`.
:::
Available events: `push`, `pull_request`, `tag`, `deployment`
Available events: `push`, `pull_request`, `tag`, `deployment`, `cron`
Execute a step if the build event is a `tag`:
@ -368,6 +368,20 @@ when:
- event: [push, tag, deployment]
```
#### `cron`
This filter **only** applies to cron events and filters based on the name of a cron job.
Make sure to have a `event: cron` condition in the `when`-filters as well.
```yaml
when:
- event: cron
cron: sync_* # name of your cron job
```
[Read more about cron](/docs/usage/cron)
#### `tag`
This filter only applies to tag events.

View file

@ -0,0 +1,39 @@
# Cron
To configure cron jobs you need at least push access to the repository.
:::warning
By default pipelines triggered by cron jobs wont execute any steps in pipelines, as they are not part of the default event filter and you explicitly need to set a `event: cron` filter.
Read more at: [pipeline-syntax#event](/docs/usage/pipeline-syntax#event)
:::
## Add a new cron job
1. To create a new cron job adjust your pipeline config(s) and add the event filter to all steps you would like to run by the cron job:
```diff
pipeline:
sync_locales:
image: weblate_sync
settings:
url: example.com
token:
from_secret: weblate_token
+ when:
+ event: cron
+ cron: "name of the cron job" # if you only want to execute this step by a specific cron job
```
1. Create a new cron job in the repository settings:
![cron settings](./cron-settings.png)
The supported schedule syntax can be found at <https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-CRON_Expression_Format>. If you need general understanding of the cron syntax <https://crontab.guru/> is a good place to start and experiment.
Examples: `@every 5m`, `@daily`, `0 30 * * * *` ...
:::info
Woodpeckers cron syntax starts with seconds instead of minutes as used by most linux cron schedulers.
Example: "At minute 30 every hour" would be `0 30 * * * *` instead of `30 * * * *`
:::

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

1
go.mod
View file

@ -29,6 +29,7 @@ require (
github.com/mrjones/oauth v0.0.0-20190623134757-126b35219450
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.0
github.com/robfig/cron v1.2.0
github.com/rs/zerolog v1.27.0
github.com/stretchr/testify v1.8.0
github.com/tevino/abool v1.2.0

2
go.sum
View file

@ -554,6 +554,8 @@ github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0ua
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=

View file

@ -14,6 +14,7 @@ const (
EventPull = "pull_request"
EventTag = "tag"
EventDeploy = "deployment"
EventCron = "cron"
)
type (
@ -51,6 +52,7 @@ type (
Trusted bool `json:"trusted,omitempty"`
Commit Commit `json:"commit,omitempty"`
Parent int64 `json:"parent,omitempty"`
Cron string `json:"cron,omitempty"`
}
// Commit defines runtime metadata for a commit.

View file

@ -26,6 +26,7 @@ type (
Environment List
Event List
Branch List
Cron List
Status List
Matrix Map
Local types.BoolTrue
@ -153,6 +154,10 @@ func (c *Constraint) Match(metadata frontend.Metadata) bool {
match = match && c.Branch.Match(metadata.Curr.Commit.Branch)
}
if metadata.Curr.Event == frontend.EventCron {
match = match && c.Cron.Match(metadata.Curr.Cron)
}
return match
}

View file

@ -459,7 +459,25 @@ func TestConstraints(t *testing.T) {
want: false,
},
{
desc: "no constraints, and event get filtered by default default event filter",
desc: "filter cron by default constraint",
conf: "{}",
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventCron}},
want: false,
},
{
desc: "filter cron by matching name",
conf: "{ event: cron, cron: job1 }",
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventCron, Cron: "job1"}},
want: true,
},
{
desc: "filter cron by name",
conf: "{ event: cron, cron: job2 }",
with: frontend.Metadata{Curr: frontend.Build{Event: frontend.EventCron, Cron: "job1"}},
want: false,
},
{
desc: "no constraints, event gets filtered by default event filter",
conf: "",
with: frontend.Metadata{
Curr: frontend.Build{Event: "non-default"},
@ -467,6 +485,7 @@ func TestConstraints(t *testing.T) {
want: false,
},
}
for _, test := range testdata {
t.Run(test.desc, func(t *testing.T) {
c := parseConstraints(t, test.conf)

View file

@ -58,7 +58,9 @@ volumes:
tmpfs:
- /var/lib/test
when:
branch: master
- branch: master
- event: cron
cron: job1
settings:
foo: bar
baz: false
@ -116,6 +118,14 @@ func TestUnmarshalContainer(t *testing.T) {
Include: []string{"master"},
},
},
{
Event: constraint.List{
Include: []string{"cron"},
},
Cron: constraint.List{
Include: []string{"job1"},
},
},
},
},
Settings: map[string]interface{}{

View file

@ -121,3 +121,22 @@ pipeline:
repo: test/test
- event: push
branch: main
when-cron:
image: alpine
commands:
- echo "test"
when:
cron: "update locales"
event: cron
when-cron-list:
image: alpine
commands: echo "test"
when:
event: cron
cron:
include:
- test
- hello
exclude: hi

View file

@ -157,29 +157,11 @@
"properties": {
"repo": {
"description": "Execute a step only on a specific repository. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#repo",
"oneOf": [
{
"type": "array",
"items": {
"type": "string"
},
"minLength": 1
},
{ "type": "string" }
]
"$ref": "#/definitions/constraint_list"
},
"branch": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#branch",
"oneOf": [
{
"type": "array",
"items": {
"type": "string"
},
"minLength": 1
},
{ "type": "string" }
]
"$ref": "#/definitions/constraint_list"
},
"event": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#event",
@ -198,29 +180,31 @@
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#tag",
"type": "string"
},
"cron": {
"description": "filter cron by title. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#cron",
"$ref": "#/definitions/constraint_list"
},
"status": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#status",
"type": "array",
"items": {
"enum": ["success", "failure"]
}
},
"platform": {
"description": "Execute a step only on a specific platform. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#platform",
"oneOf": [
{
"type": "array",
"items": {
"type": "string"
},
"minLength": 1
"minLength": 1,
"items": { "type": "string", "enum": ["success", "failure"] }
},
{ "type": "string" }
{
"type": "string",
"enum": ["success", "failure"]
}
]
},
"platform": {
"description": "Execute a step only on a specific platform. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#platform",
"$ref": "#/definitions/constraint_list"
},
"environment": {
"description": "Execute a step only for a specific environment. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#environment",
"type": "string"
"$ref": "#/definitions/constraint_list"
},
"matrix": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/matrix-pipelines",
@ -231,7 +215,7 @@
},
"instance": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#instance",
"type": "string"
"$ref": "#/definitions/constraint_list"
},
"path": {
"description": "Execute a step only on commit with certain files added/removed/modified. Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#path",
@ -270,7 +254,49 @@
},
"event_enum": {
"default": ["push", "pull_request", "tag", "deployment"],
"enum": ["push", "pull_request", "tag", "deployment"]
"enum": ["push", "pull_request", "tag", "deployment", "cron"]
},
"constraint_list": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"minLength": 1,
"items": { "type": "string" }
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"include": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"minLength": 1,
"items": { "type": "string" }
}
]
},
"exclude": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"minLength": 1,
"items": { "type": "string" }
}
]
}
}
}
]
},
"step_image": {
"description": "Read more: https://woodpecker-ci.org/docs/usage/pipeline-syntax#image",

179
server/api/cron.go Normal file
View file

@ -0,0 +1,179 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/woodpecker-ci/woodpecker/server"
cronScheduler "github.com/woodpecker-ci/woodpecker/server/cron"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
"github.com/woodpecker-ci/woodpecker/server/store"
)
// GetCron gets a cron job by id from the database and writes
// to the response in json format.
func GetCron(c *gin.Context) {
repo := session.Repo(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
c.String(400, "Error parsing cron id. %s", err)
return
}
cron, err := store.FromContext(c).CronFind(repo, id)
if err != nil {
c.String(404, "Error getting cron %q. %s", id, err)
return
}
c.JSON(200, cron)
}
// PostCron persists the cron job to the database.
func PostCron(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
store := store.FromContext(c)
remote := server.Config.Services.Remote
in := new(model.Cron)
if err := c.Bind(in); err != nil {
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
return
}
cron := &model.Cron{
RepoID: repo.ID,
Name: in.Name,
CreatorID: user.ID,
Schedule: in.Schedule,
Branch: in.Branch,
}
if err := cron.Validate(); err != nil {
c.String(400, "Error inserting cron. validate failed: %s", err)
return
}
nextExec, err := cronScheduler.CalcNewNext(in.Schedule, time.Now())
if err != nil {
c.String(400, "Error inserting cron. schedule could not parsed: %s", err)
return
}
cron.NextExec = nextExec.Unix()
if in.Branch != "" {
// check if branch exists on remote
_, err := remote.BranchHead(c, user, repo, in.Branch)
if err != nil {
c.String(400, "Error inserting cron. branch not resolved: %s", err)
return
}
}
if err := store.CronCreate(cron); err != nil {
c.String(500, "Error inserting cron %q. %s", in.Name, err)
return
}
c.JSON(200, cron)
}
// PatchCron updates the cron job in the database.
func PatchCron(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
store := store.FromContext(c)
remote := server.Config.Services.Remote
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
c.String(400, "Error parsing cron id. %s", err)
return
}
in := new(model.Cron)
err = c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
return
}
cron, err := store.CronFind(repo, id)
if err != nil {
c.String(404, "Error getting cron %d. %s", id, err)
return
}
if in.Branch != "" {
// check if branch exists on remote
_, err := remote.BranchHead(c, user, repo, in.Branch)
if err != nil {
c.String(400, "Error inserting cron. branch not resolved: %s", err)
return
}
cron.Branch = in.Branch
}
if in.Schedule != "" {
cron.Schedule = in.Schedule
nextExec, err := cronScheduler.CalcNewNext(in.Schedule, time.Now())
if err != nil {
c.String(400, "Error inserting cron. schedule could not parsed: %s", err)
return
}
cron.NextExec = nextExec.Unix()
}
if in.Name != "" {
cron.Name = in.Name
}
cron.CreatorID = user.ID
if err := cron.Validate(); err != nil {
c.String(400, "Error inserting cron. validate failed: %s", err)
return
}
if err := store.CronUpdate(repo, cron); err != nil {
c.String(500, "Error updating cron %q. %s", in.Name, err)
return
}
c.JSON(200, cron)
}
// GetCronList gets the cron job list from the database and writes
// to the response in json format.
func GetCronList(c *gin.Context) {
repo := session.Repo(c)
list, err := store.FromContext(c).CronList(repo)
if err != nil {
c.String(500, "Error getting cron list. %s", err)
return
}
c.JSON(200, list)
}
// DeleteCron deletes the named cron job from the database.
func DeleteCron(c *gin.Context) {
repo := session.Repo(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
c.String(400, "Error parsing cron id. %s", err)
return
}
if err := store.FromContext(c).CronDelete(repo, id); err != nil {
c.String(500, "Error deleting cron %d. %s", id, err)
return
}
c.String(204, "")
}

141
server/cron/cron.go Normal file
View file

@ -0,0 +1,141 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cron
import (
"context"
"fmt"
"time"
"github.com/robfig/cron"
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/pipeline"
"github.com/woodpecker-ci/woodpecker/server/store"
)
const (
// checkTime specifies the interval woodpecker checks for new crons to exec
checkTime = 10 * time.Second
// checkItems specifies the batch size of crons to retrieve per check from database
checkItems = 10
)
// Start starts the cron scheduler loop
func Start(ctx context.Context, store store.Store) error {
for {
select {
case <-ctx.Done():
return nil
case <-time.After(checkTime):
go func() {
now := time.Now()
log.Trace().Msg("Cron: fetch next crons")
crons, err := store.CronListNextExecute(now.Unix(), checkItems)
if err != nil {
log.Error().Err(err).Int64("now", now.Unix()).Msg("obtain cron cron list")
return
}
for _, cron := range crons {
if err := runCron(cron, store, now); err != nil {
log.Error().Err(err).Int64("cronID", cron.ID).Msg("run cron failed")
}
}
}()
}
}
}
// CalcNewNext parses a cron string and calculates the next exec time based on it
func CalcNewNext(schedule string, now time.Time) (time.Time, error) {
// remove local timezone
now = now.UTC()
// TODO: allow the users / the admin to set a specific timezone
c, err := cron.Parse(schedule)
if err != nil {
return time.Time{}, fmt.Errorf("cron parse schedule: %v", err)
}
return c.Next(now), nil
}
func runCron(cron *model.Cron, store store.Store, now time.Time) error {
log.Trace().Msgf("Cron: run id[%d]", cron.ID)
ctx := context.Background()
newNext, err := CalcNewNext(cron.Schedule, now)
if err != nil {
return err
}
// try to get lock on cron
gotLock, err := store.CronGetLock(cron, newNext.Unix())
if err != nil {
return err
}
if !gotLock {
// another go routine caught it
return nil
}
repo, newBuild, err := createBuild(ctx, cron, store)
if err != nil {
return err
}
_, err = pipeline.Create(ctx, store, repo, newBuild)
return err
}
func createBuild(ctx context.Context, cron *model.Cron, store store.Store) (*model.Repo, *model.Build, error) {
remote := server.Config.Services.Remote
repo, err := store.GetRepo(cron.RepoID)
if err != nil {
return nil, nil, err
}
if cron.Branch == "" {
// fallback to the repos default branch
cron.Branch = repo.Branch
}
creator, err := store.GetUser(cron.CreatorID)
if err != nil {
return nil, nil, err
}
commit, err := remote.BranchHead(ctx, creator, repo, cron.Branch)
if err != nil {
return nil, nil, err
}
return repo, &model.Build{
Event: model.EventCron,
Commit: commit,
Ref: "refs/heads/" + cron.Branch,
Branch: cron.Branch,
Message: cron.Name,
Timestamp: cron.NextExec,
Sender: cron.Name,
Link: repo.Link,
}, nil
}

46
server/cron/cron_test.go Normal file
View file

@ -0,0 +1,46 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cron
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/remote/mocks"
)
func TestCreateBuild(t *testing.T) {
rOld := server.Config.Services.Remote
defer func() {
server.Config.Services.Remote = rOld
}()
server.Config.Services.Remote = mocks.NewRemote(t)
// TODO: mockStore
// createBuild(context.TODO(), &model.Cron{}, mockStore)
}
func TestCalcNewNext(t *testing.T) {
now := time.Unix(1661962369, 0)
_, err := CalcNewNext("", now)
assert.Error(t, err)
schedule, err := CalcNewNext("@every 5m", now)
assert.NoError(t, err)
assert.EqualValues(t, 1661962669, schedule.Unix())
}

View file

@ -40,7 +40,7 @@ type Build struct {
Title string `json:"title" xorm:"build_title"`
Message string `json:"message" xorm:"build_message"`
Timestamp int64 `json:"timestamp" xorm:"build_timestamp"`
Sender string `json:"sender" xorm:"build_sender"`
Sender string `json:"sender" xorm:"build_sender"` // uses reported user for webhooks and name of cron for cron pipelines
Avatar string `json:"author_avatar" xorm:"build_avatar"`
Email string `json:"author_email" xorm:"build_email"`
Link string `json:"link_url" xorm:"build_link"`

View file

@ -21,11 +21,12 @@ const (
EventPull WebhookEvent = "pull_request"
EventTag WebhookEvent = "tag"
EventDeploy WebhookEvent = "deployment"
EventCron WebhookEvent = "cron"
)
func ValidateWebhookEvent(s WebhookEvent) bool {
switch s {
case EventPush, EventPull, EventTag, EventDeploy:
case EventPush, EventPull, EventTag, EventDeploy, EventCron:
return true
default:
return false

56
server/model/cron.go Normal file
View file

@ -0,0 +1,56 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package model
import (
"fmt"
"github.com/robfig/cron"
)
// swagger:model cron
type Cron struct {
ID int64 `json:"id" xorm:"pk autoincr"`
Name string `json:"name" xorm:"UNIQUE(s) INDEX"`
RepoID int64 `json:"repo_id" xorm:"repo_id UNIQUE(s) INDEX"`
CreatorID int64 `json:"creator_id" xorm:"creator_id INDEX"`
NextExec int64 `json:"next_exec"`
Schedule string `json:"schedule" xorm:"NOT NULL"` // @weekly, 3min, ...
Created int64 `json:"created_at" xorm:"created NOT NULL DEFAULT 0"`
Branch string `json:"branch"`
}
// TableName returns the database table name for xorm
func (Cron) TableName() string {
return "crons"
}
// Validate a cron
func (c *Cron) Validate() error {
if c.Name == "" {
return fmt.Errorf("name is required")
}
if c.Schedule == "" {
return fmt.Errorf("schedule is required")
}
_, err := cron.Parse(c.Schedule)
if err != nil {
return fmt.Errorf("can't parse schedule: %v", err)
}
return nil
}

View file

@ -295,6 +295,12 @@ func (c *config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]
return branches, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (c *config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
// TODO(1138): missing implementation
return "", fmt.Errorf("missing implementation")
}
// Hook parses the incoming Bitbucket hook and returns the Repository and
// Build details. If the hook is unsupported nil values are returned.
func (c *config) Hook(ctx context.Context, req *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -236,6 +236,12 @@ func (c *Config) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]
return branches, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (c *Config) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
// TODO(1138): missing implementation
return "", fmt.Errorf("missing implementation")
}
func (c *Config) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
client := internal.NewClientWithToken(ctx, c.URL, c.Consumer, u.Token)
return client.DeleteHook(r.Owner, r.Name, link)

View file

@ -291,6 +291,12 @@ func (c *Coding) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]
return []string{r.Branch}, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (c *Coding) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
// TODO(1138): missing implementation
return "", fmt.Errorf("missing implementation")
}
// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
func (c *Coding) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -451,6 +451,25 @@ func (c *Gitea) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]s
return branches, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (c *Gitea) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
token := ""
if u != nil {
token = u.Token
}
client, err := c.newClientToken(ctx, token)
if err != nil {
return "", err
}
b, _, err := client.GetRepoBranch(r.Owner, r.Name, branch)
if err != nil {
return "", err
}
return b.Commit.ID, nil
}
// Hook parses the incoming Gitea hook and returns the Repository and Build
// details. If the hook is unsupported nil values are returned.
func (c *Gitea) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -493,6 +493,19 @@ func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]
return branches, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
token := ""
if u != nil {
token = u.Token
}
b, _, err := c.newClientToken(ctx, token).Repositories.GetBranch(ctx, r.Owner, r.Name, branch, true)
if err != nil {
return "", err
}
return b.GetCommit().GetSHA(), nil
}
// Hook parses the post-commit hook from the Request body
// and returns the required data in a standard format.
func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -545,6 +545,30 @@ func (g *Gitlab) Branches(ctx context.Context, user *model.User, repo *model.Rep
return branches, nil
}
// BranchHead returns the sha of the head (lastest commit) of the specified branch
func (g *Gitlab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
token := ""
if u != nil {
token = u.Token
}
client, err := newClient(g.URL, token, g.SkipVerify)
if err != nil {
return "", err
}
_repo, err := g.getProject(ctx, client, r.Owner, r.Name)
if err != nil {
return "", err
}
b, _, err := client.Branches.GetBranch(_repo.ID, branch, gitlab.WithContext(ctx))
if err != nil {
return "", err
}
return b.Commit.ID, nil
}
// Hook parses the post-commit hook from the Request body
// and returns the required data in a standard format.
func (g *Gitlab) Hook(ctx context.Context, req *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -284,6 +284,19 @@ func (c *client) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]
return branches, nil
}
// BranchHead returns sha of commit on top of the specified branch
func (c *client) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
token := ""
if u != nil {
token = u.Token
}
b, err := c.newClientToken(token).GetRepoBranch(r.Owner, r.Name, branch)
if err != nil {
return "", err
}
return b.Commit.ID, nil
}
// Hook parses the incoming Gogs hook and returns the Repository and Build
// details. If the hook is unsupported nil values are returned.
func (c *client) Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error) {

View file

@ -1,4 +1,4 @@
// Code generated by mockery v2.13.1. DO NOT EDIT.
// Code generated by mockery v2.14.0. DO NOT EDIT.
package mocks
@ -53,6 +53,27 @@ func (_m *Remote) Auth(ctx context.Context, token string, secret string) (string
return r0, r1
}
// BranchHead provides a mock function with given fields: ctx, u, r, branch
func (_m *Remote) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
ret := _m.Called(ctx, u, r, branch)
var r0 string
if rf, ok := ret.Get(0).(func(context.Context, *model.User, *model.Repo, string) string); ok {
r0 = rf(ctx, u, r, branch)
} else {
r0 = ret.Get(0).(string)
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *model.User, *model.Repo, string) error); ok {
r1 = rf(ctx, u, r, branch)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Branches provides a mock function with given fields: ctx, u, r
func (_m *Remote) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error) {
ret := _m.Called(ctx, u, r)

View file

@ -77,6 +77,9 @@ type Remote interface {
// Branches returns the names of all branches for the named repository.
Branches(ctx context.Context, u *model.User, r *model.Repo) ([]string, error)
// BranchHead returns the sha of the head (lastest commit) of the specified branch
BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error)
// Hook parses the post-commit hook from the Request body and returns the
// required data in a standard format.
Hook(ctx context.Context, r *http.Request) (*model.Repo, *model.Build, error)

View file

@ -108,6 +108,13 @@ func apiRoutes(e *gin.Engine) {
repo.PATCH("/registry/:registry", session.MustPush, api.PatchRegistry)
repo.DELETE("/registry/:registry", session.MustPush, api.DeleteRegistry)
// requires push permissions
repo.GET("/cron", session.MustPush, api.GetCronList)
repo.POST("/cron", session.MustPush, api.PostCron)
repo.GET("/cron/:cron", session.MustPush, api.GetCron)
repo.PATCH("/cron/:cron", session.MustPush, api.PatchCron)
repo.DELETE("/cron/:cron", session.MustPush, api.DeleteCron)
// requires admin permissions
repo.PATCH("", session.MustRepoAdmin(), api.PatchRepo)
repo.DELETE("", session.MustRepoAdmin(), api.DeleteRepo)

View file

@ -338,6 +338,11 @@ func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.
}
func metadataBuildFromModelBuild(build *model.Build, includeParent bool) frontend.Build {
cron := ""
if build.Event == model.EventCron {
cron = build.Sender
}
parent := int64(0)
if includeParent {
parent = build.Parent
@ -366,6 +371,7 @@ func metadataBuildFromModelBuild(build *model.Build, includeParent bool) fronten
},
ChangedFiles: build.ChangedFiles,
},
Cron: cron,
}
}

View file

@ -0,0 +1,71 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package datastore
import (
"github.com/woodpecker-ci/woodpecker/server/model"
"xorm.io/builder"
)
func (s storage) CronCreate(cron *model.Cron) error {
if err := cron.Validate(); err != nil {
return err
}
_, err := s.engine.Insert(cron)
return err
}
func (s storage) CronFind(repo *model.Repo, id int64) (*model.Cron, error) {
cron := &model.Cron{
RepoID: repo.ID,
ID: id,
}
return cron, wrapGet(s.engine.Get(cron))
}
func (s storage) CronList(repo *model.Repo) ([]*model.Cron, error) {
crons := make([]*model.Cron, 0, perPage)
return crons, s.engine.Where("repo_id = ?", repo.ID).Find(&crons)
}
func (s storage) CronUpdate(repo *model.Repo, cron *model.Cron) error {
_, err := s.engine.ID(cron.ID).AllCols().Update(cron)
return err
}
func (s storage) CronDelete(repo *model.Repo, id int64) error {
_, err := s.engine.ID(id).Where("repo_id = ?", repo.ID).Delete(new(model.Cron))
return err
}
// CronListNextExecute returns limited number of jobs with NextExec being less or equal to the provided unix timestamp
func (s storage) CronListNextExecute(nextExec, limit int64) ([]*model.Cron, error) {
crons := make([]*model.Cron, 0, limit)
return crons, s.engine.Where(builder.Lte{"next_exec": nextExec}).Limit(int(limit)).Find(&crons)
}
// CronGetLock try to get a lock by updating NextExec
func (s storage) CronGetLock(cron *model.Cron, newNextExec int64) (bool, error) {
cols, err := s.engine.ID(cron.ID).Where(builder.Eq{"next_exec": cron.NextExec}).
Cols("next_exec").Update(&model.Cron{NextExec: newNextExec})
gotLock := cols != 0
if err == nil && gotLock {
cron.NextExec = newNextExec
}
return gotLock, err
}

View file

@ -0,0 +1,92 @@
// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package datastore
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/woodpecker-ci/woodpecker/server/model"
)
func TestCronCreate(t *testing.T) {
store, closer := newTestStore(t, new(model.Cron))
defer closer()
repo := &model.Repo{ID: 1, Name: "repo"}
cron1 := &model.Cron{RepoID: repo.ID, CreatorID: 1, Name: "sync", NextExec: 10000, Schedule: "@every 1h"}
assert.NoError(t, store.CronCreate(cron1))
assert.NotEqualValues(t, 0, cron1.ID)
// can not insert cron job with same repoID and title
assert.Error(t, store.CronCreate(cron1))
oldID := cron1.ID
assert.NoError(t, store.CronDelete(repo, oldID))
cron1.ID = 0
assert.NoError(t, store.CronCreate(cron1))
assert.NotEqual(t, oldID, cron1.ID)
}
func TestCronListNextExecute(t *testing.T) {
store, closer := newTestStore(t, new(model.Cron))
defer closer()
jobs, err := store.CronListNextExecute(0, 10)
assert.NoError(t, err)
assert.Len(t, jobs, 0)
now := time.Now().Unix()
assert.NoError(t, store.CronCreate(&model.Cron{Schedule: "@every 1h", Name: "some", RepoID: 1, NextExec: now}))
assert.NoError(t, store.CronCreate(&model.Cron{Schedule: "@every 1h", Name: "aaaa", RepoID: 1, NextExec: now}))
assert.NoError(t, store.CronCreate(&model.Cron{Schedule: "@every 1h", Name: "bbbb", RepoID: 1, NextExec: now}))
assert.NoError(t, store.CronCreate(&model.Cron{Schedule: "@every 1h", Name: "none", RepoID: 1, NextExec: now + 1000}))
assert.NoError(t, store.CronCreate(&model.Cron{Schedule: "@every 1h", Name: "test", RepoID: 1, NextExec: now + 2000}))
jobs, err = store.CronListNextExecute(now, 10)
assert.NoError(t, err)
assert.Len(t, jobs, 3)
jobs, err = store.CronListNextExecute(now+1500, 10)
assert.NoError(t, err)
assert.Len(t, jobs, 4)
}
func TestCronGetLock(t *testing.T) {
store, closer := newTestStore(t, new(model.Cron))
defer closer()
nonExistingJob := &model.Cron{ID: 1000, Name: "locales", NextExec: 10000}
gotLock, err := store.CronGetLock(nonExistingJob, time.Now().Unix()+100)
assert.NoError(t, err)
assert.False(t, gotLock)
cron1 := &model.Cron{RepoID: 1, Name: "some-title", NextExec: 10000, Schedule: "@every 1h"}
assert.NoError(t, store.CronCreate(cron1))
oldJob := *cron1
gotLock, err = store.CronGetLock(cron1, cron1.NextExec+1000)
assert.NoError(t, err)
assert.True(t, gotLock)
assert.NotEqualValues(t, oldJob.NextExec, cron1.NextExec)
gotLock, err = store.CronGetLock(&oldJob, oldJob.NextExec+1000)
assert.NoError(t, err)
assert.False(t, gotLock)
assert.EqualValues(t, oldJob.NextExec, oldJob.NextExec)
}

View file

@ -52,6 +52,7 @@ var allBeans = []interface{}{
new(model.Task),
new(model.User),
new(model.ServerConfig),
new(model.Cron),
}
type migrations struct {

View file

@ -153,6 +153,15 @@ type Store interface {
ServerConfigGet(string) (string, error)
ServerConfigSet(string, string) error
// Cron
CronCreate(*model.Cron) error
CronFind(*model.Repo, int64) (*model.Cron, error)
CronList(*model.Repo) ([]*model.Cron, error)
CronUpdate(*model.Repo, *model.Cron) error
CronDelete(*model.Repo, int64) error
CronListNextExecute(int64, int64) ([]*model.Cron, error)
CronGetLock(*model.Cron, int64) (bool, error)
// Store operations
Ping() error
Close() error

43
web/components.d.ts vendored
View file

@ -9,6 +9,7 @@ declare module '@vue/runtime-core' {
export interface GlobalComponents {
ActionsTab: typeof import('./src/components/repo/settings/ActionsTab.vue')['default']
ActiveBuilds: typeof import('./src/components/layout/header/ActiveBuilds.vue')['default']
AdminSecretsTab: typeof import('./src/components/admin/settings/AdminSecretsTab.vue')['default']
BadgeTab: typeof import('./src/components/repo/settings/BadgeTab.vue')['default']
BuildFeedItem: typeof import('./src/components/build-feed/BuildFeedItem.vue')['default']
BuildFeedSidebar: typeof import('./src/components/build-feed/BuildFeedSidebar.vue')['default']
@ -22,24 +23,66 @@ declare module '@vue/runtime-core' {
Button: typeof import('./src/components/atomic/Button.vue')['default']
Checkbox: typeof import('./src/components/form/Checkbox.vue')['default']
CheckboxesField: typeof import('./src/components/form/CheckboxesField.vue')['default']
CronTab: typeof import('./src/components/repo/settings/CronTab.vue')['default']
DocsLink: typeof import('./src/components/atomic/DocsLink.vue')['default']
FluidContainer: typeof import('./src/components/layout/FluidContainer.vue')['default']
GeneralTab: typeof import('./src/components/repo/settings/GeneralTab.vue')['default']
IBxBxPowerOff: typeof import('~icons/bx/bx-power-off')['default']
ICarbonCloseOutline: typeof import('~icons/carbon/close-outline')['default']
IClarityDeployLine: typeof import('~icons/clarity/deploy-line')['default']
IClaritySettingsSolid: typeof import('~icons/clarity/settings-solid')['default']
Icon: typeof import('./src/components/atomic/Icon.vue')['default']
IconButton: typeof import('./src/components/atomic/IconButton.vue')['default']
IEntypoDotsTwoVertical: typeof import('~icons/entypo/dots-two-vertical')['default']
IGgTrash: typeof import('~icons/gg/trash')['default']
IIcBaselineDarkMode: typeof import('~icons/ic/baseline-dark-mode')['default']
IIcBaselineDownload: typeof import('~icons/ic/baseline-download')['default']
IIcBaselineEdit: typeof import('~icons/ic/baseline-edit')['default']
IIcBaselineHealing: typeof import('~icons/ic/baseline-healing')['default']
IIconoirArrowLeft: typeof import('~icons/iconoir/arrow-left')['default']
IIconParkOutlineAlarmClock: typeof import('~icons/icon-park-outline/alarm-clock')['default']
IIcRoundLightMode: typeof import('~icons/ic/round-light-mode')['default']
IIcSharpTimelapse: typeof import('~icons/ic/sharp-timelapse')['default']
IIcTwotoneAdd: typeof import('~icons/ic/twotone-add')['default']
IMdiBitbucket: typeof import('~icons/mdi/bitbucket')['default']
IMdiChevronRight: typeof import('~icons/mdi/chevron-right')['default']
IMdiClockTimeEightOutline: typeof import('~icons/mdi/clock-time-eight-outline')['default']
IMdiFormatListBulleted: typeof import('~icons/mdi/format-list-bulleted')['default']
IMdiGithub: typeof import('~icons/mdi/github')['default']
IMdiLoading: typeof import('~icons/mdi/loading')['default']
IMdiSourceBranch: typeof import('~icons/mdi/source-branch')['default']
IMdisourceCommit: typeof import('~icons/mdi/source-commit')['default']
IMdiSourcePull: typeof import('~icons/mdi/source-pull')['default']
IMdiSync: typeof import('~icons/mdi/sync')['default']
IMdiTagOutline: typeof import('~icons/mdi/tag-outline')['default']
InputField: typeof import('./src/components/form/InputField.vue')['default']
IOcticonSkip24: typeof import('~icons/octicon/skip24')['default']
IPhCheckCircle: typeof import('~icons/ph/check-circle')['default']
IPhGitlabLogoSimpleFill: typeof import('~icons/ph/gitlab-logo-simple-fill')['default']
IPhHand: typeof import('~icons/ph/hand')['default']
IPhHourglass: typeof import('~icons/ph/hourglass')['default']
IPhProhibit: typeof import('~icons/ph/prohibit')['default']
IPhWarning: typeof import('~icons/ph/warning')['default']
IPhXCircle: typeof import('~icons/ph/x-circle')['default']
ISimpleIconsGitea: typeof import('~icons/simple-icons/gitea')['default']
ITeenyiconsGitSolid: typeof import('~icons/teenyicons/git-solid')['default']
IVaadinQuestionCircleO: typeof import('~icons/vaadin/question-circle-o')['default']
ListItem: typeof import('./src/components/atomic/ListItem.vue')['default']
Navbar: typeof import('./src/components/layout/header/Navbar.vue')['default']
NumberField: typeof import('./src/components/form/NumberField.vue')['default']
OrgSecretsTab: typeof import('./src/components/org/settings/OrgSecretsTab.vue')['default']
Panel: typeof import('./src/components/layout/Panel.vue')['default']
RadioField: typeof import('./src/components/form/RadioField.vue')['default']
RegistriesTab: typeof import('./src/components/repo/settings/RegistriesTab.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView']
SecretEdit: typeof import('./src/components/secrets/SecretEdit.vue')['default']
SecretList: typeof import('./src/components/secrets/SecretList.vue')['default']
SecretsTab: typeof import('./src/components/repo/settings/SecretsTab.vue')['default']
SelectField: typeof import('./src/components/form/SelectField.vue')['default']
Tab: typeof import('./src/components/tabs/Tab.vue')['default']
Tabs: typeof import('./src/components/tabs/Tabs.vue')['default']
TextField: typeof import('./src/components/form/TextField.vue')['default']
Warning: typeof import('./src/components/atomic/Warning.vue')['default']
}
}

View file

@ -122,6 +122,31 @@
"placeholder": "Registry Address (e.g. docker.io)"
}
},
"crons": {
"crons": "Crons",
"desc": "Cron jobs can be used to trigger pipelines on a regular basis.",
"show": "Show crons",
"add": "Add cron",
"none": "There are no crons yet.",
"save": "Save cron",
"created": "Cron created",
"saved": "Cron saved",
"deleted": "Cron deleted",
"next_exec": "Next execution",
"not_executed_yet": "Not executed yet",
"branch": {
"title": "Branch",
"placeholder": "Branch (uses default branch if empty)"
},
"name": {
"name": "Name",
"placeholder": "Name of the cron job"
},
"schedule": {
"title": "Schedule (based on UTC)",
"placeholder": "Schedule"
}
},
"badge": {
"badge": "Badge",
"url_branch": "URL for specific branch",
@ -179,7 +204,8 @@
"push": "Push",
"tag": "Tag",
"pr": "Pull Request",
"deploy": "Deploy"
"deploy": "Deploy",
"cron": "Cron"
}
}
},

View file

@ -1,32 +1,22 @@
<template>
<a :href="`${docsBaseUrl}${url}`" target="_blank" class="text-blue-500 hover:text-blue-600 cursor-pointer mt-1"
<a :href="`${docsUrl}`" target="_blank" class="text-blue-500 hover:text-blue-600 cursor-pointer mt-1"
><Icon name="question" class="!w-4 !h-4"
/></a>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
<script lang="ts" setup>
import { computed, defineProps, toRef } from 'vue';
import Icon from '~/components/atomic/Icon.vue';
export default defineComponent({
name: 'DocsLink',
components: {
Icon,
},
props: {
url: {
type: String,
required: true,
},
},
setup() {
const docsBaseUrl = window.WOODPECKER_DOCS;
return { docsBaseUrl };
const props = defineProps({
url: {
type: String,
required: true,
},
});
const docsBaseUrl = window.WOODPECKER_DOCS;
const url = toRef(props, 'url');
const docsUrl = computed(() => (url.value.startsWith('http') ? url.value : `${docsBaseUrl}${url.value}`));
</script>

View file

@ -37,6 +37,7 @@
<i-carbon-close-outline v-else-if="name === 'close'" class="h-6 w-6" />
<i-ic-baseline-edit v-else-if="name === 'edit'" class="h-6 w-6" />
<i-ic-baseline-download v-else-if="name === 'download'" class="h-6 w-6" />
<i-icon-park-outline-alarm-clock v-else-if="name === 'stopwatch'" class="h-6 w-6" />
<div v-else-if="name === 'blank'" class="h-6 w-6" />
</template>
@ -82,6 +83,7 @@ export type IconNames =
| 'turn-off'
| 'close'
| 'edit'
| 'stopwatch'
| 'download';
export default defineComponent({

View file

@ -19,7 +19,8 @@
<div class="flex py-2 px-4 flex-grow min-w-0 <md:flex-wrap">
<div class="<md:hidden flex items-center flex-shrink-0">
<img class="w-8" :src="build.author_avatar" />
<Icon v-if="build.event === 'cron'" name="stopwatch" class="text-color" />
<img v-else class="w-8" :src="build.author_avatar" />
</div>
<div class="w-full md:w-auto md:mx-4 flex items-center min-w-0">
@ -33,6 +34,7 @@
<Icon v-if="build.event === 'pull_request'" name="pull_request" />
<Icon v-else-if="build.event === 'deployment'" name="deployment" />
<Icon v-else-if="build.event === 'tag'" name="tag" />
<Icon v-else-if="build.event === 'cron'" name="push" />
<Icon v-else name="push" />
<span class="truncate">{{ prettyRef }}</span>
</div>

View file

@ -4,7 +4,10 @@
class="flex md:ml-2 p-4 space-x-1 justify-between flex-shrink-0 border-b-1 md:rounded-md bg-gray-300 dark:border-b-dark-gray-600 dark:bg-dark-gray-700"
>
<div class="flex space-x-1 items-center flex-shrink-0">
<div class="flex items-center"><img class="w-6" :src="build.author_avatar" /></div>
<div class="flex items-center">
<Icon v-if="build.event === 'cron'" name="stopwatch" />
<img v-else class="w-6" :src="build.author_avatar" />
</div>
<span>{{ build.author }}</span>
</div>
<div class="flex space-x-1 items-center min-w-0">

View file

@ -0,0 +1,155 @@
<template>
<Panel>
<div class="flex flex-row border-b mb-4 pb-4 items-center dark:border-gray-600">
<div class="ml-2">
<h1 class="text-xl text-color">{{ $t('repo.settings.crons.crons') }}</h1>
<p class="text-sm text-color-alt">
{{ $t('repo.settings.crons.desc') }}
<DocsLink url="docs/usage/crons" />
</p>
</div>
<Button
v-if="selectedCron"
class="ml-auto"
start-icon="back"
:text="$t('repo.settings.crons.show')"
@click="selectedCron = undefined"
/>
<Button
v-else
class="ml-auto"
start-icon="plus"
:text="$t('repo.settings.crons.add')"
@click="selectedCron = {}"
/>
</div>
<div v-if="!selectedCron" class="space-y-4 text-color">
<ListItem v-for="cron in crons" :key="cron.id" class="items-center">
<span>{{ cron.name }}</span>
<span v-if="cron.next_exec && cron.next_exec > 0" class="ml-auto">
{{ $t('repo.settings.crons.next_exec') }}: {{ date.toLocaleString(new Date(cron.next_exec * 1000)) }}</span
>
<span v-else class="ml-auto">{{ $t('repo.settings.crons.not_executed_yet') }}</span>
<IconButton icon="edit" class="ml-auto w-8 h-8" @click="selectedCron = cron" />
<IconButton
icon="trash"
class="w-8 h-8 hover:text-red-400 hover:dark:text-red-500"
:is-loading="isDeleting"
@click="deleteCron(cron)"
/>
</ListItem>
<div v-if="crons?.length === 0" class="ml-2">{{ $t('repo.settings.crons.none') }}</div>
</div>
<div v-else class="space-y-4">
<form @submit.prevent="createCron">
<InputField :label="$t('repo.settings.crons.name.name')">
<TextField v-model="selectedCron.name" :placeholder="$t('repo.settings.crons.name.placeholder')" required />
</InputField>
<InputField :label="$t('repo.settings.crons.branch.title')">
<TextField v-model="selectedCron.branch" :placeholder="$t('repo.settings.crons.branch.placeholder')" />
</InputField>
<InputField
:label="$t('repo.settings.crons.schedule.title')"
docs-url="https://pkg.go.dev/github.com/robfig/cron?utm_source=godoc#hdr-CRON_Expression_Format"
>
<TextField
v-model="selectedCron.schedule"
:placeholder="$t('repo.settings.crons.schedule.placeholder')"
required
/>
</InputField>
<div v-if="isEditingCron" class="ml-auto mb-4">
<span v-if="selectedCron.next_exec && selectedCron.next_exec > 0" class="text-color">
{{ $t('repo.settings.crons.next_exec') }}:
{{ date.toLocaleString(new Date(selectedCron.next_exec * 1000)) }}
</span>
<span v-else class="text-color">{{ $t('repo.settings.crons.not_executed_yet') }}</span>
</div>
<Button
type="submit"
:is-loading="isSaving"
:text="isEditingCron ? $t('repo.settings.crons.save') : $t('repo.settings.crons.add')"
/>
</form>
</div>
</Panel>
</template>
<script lang="ts" setup>
import { computed, inject, onMounted, Ref, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import Button from '~/components/atomic/Button.vue';
import DocsLink from '~/components/atomic/DocsLink.vue';
import IconButton from '~/components/atomic/IconButton.vue';
import ListItem from '~/components/atomic/ListItem.vue';
import InputField from '~/components/form/InputField.vue';
import TextField from '~/components/form/TextField.vue';
import Panel from '~/components/layout/Panel.vue';
import useApiClient from '~/compositions/useApiClient';
import { useAsyncAction } from '~/compositions/useAsyncAction';
import { useDate } from '~/compositions/useDate';
import useNotifications from '~/compositions/useNotifications';
import { Cron, Repo } from '~/lib/api/types';
const apiClient = useApiClient();
const notifications = useNotifications();
const i18n = useI18n();
const repo = inject<Ref<Repo>>('repo');
const crons = ref<Cron[]>();
const selectedCron = ref<Partial<Cron>>();
const isEditingCron = computed(() => !!selectedCron.value?.id);
const date = useDate();
async function loadCrons() {
if (!repo?.value) {
throw new Error("Unexpected: Can't load repo");
}
crons.value = await apiClient.getCronList(repo.value.owner, repo.value.name);
}
const { doSubmit: createCron, isLoading: isSaving } = useAsyncAction(async () => {
if (!repo?.value) {
throw new Error("Unexpected: Can't load repo");
}
if (!selectedCron.value) {
throw new Error("Unexpected: Can't get cron");
}
if (isEditingCron.value) {
await apiClient.updateCron(repo.value.owner, repo.value.name, selectedCron.value);
} else {
await apiClient.createCron(repo.value.owner, repo.value.name, selectedCron.value);
}
notifications.notify({
title: i18n.t(isEditingCron.value ? 'repo.settings.crons.saved' : i18n.t('repo.settings.crons.created')),
type: 'success',
});
selectedCron.value = undefined;
await loadCrons();
});
const { doSubmit: deleteCron, isLoading: isDeleting } = useAsyncAction(async (_cron: Cron) => {
if (!repo?.value) {
throw new Error("Unexpected: Can't load repo");
}
await apiClient.deleteCron(repo.value.owner, repo.value.name, _cron.id);
notifications.notify({ title: i18n.t('repo.settings.crons.deleted'), type: 'success' });
await loadCrons();
});
onMounted(async () => {
await loadCrons();
});
</script>

View file

@ -111,6 +111,7 @@ export default defineComponent({
description: i18n.t('repo.settings.secrets.events.pr_warning'),
},
{ value: WebhookEvents.Deploy, text: i18n.t('repo.build.event.deploy') },
{ value: WebhookEvents.Cron, text: i18n.t('repo.build.event.cron') },
];
function save() {

View file

@ -87,6 +87,10 @@ export default (build: Ref<Build | undefined>) => {
return build.value.branch;
}
if (build.value?.event === 'cron') {
return build.value.ref.replaceAll('refs/heads/', '');
}
if (build.value?.event === 'tag') {
return build.value.ref.replaceAll('refs/tags/', '');
}

View file

@ -12,6 +12,7 @@ import {
RepoSettings,
Secret,
} from './types';
import { Cron } from './types/cron';
type RepoListOptions = {
all?: boolean;
@ -136,6 +137,22 @@ export default class WoodpeckerClient extends ApiClient {
return this._delete(`/api/repos/${owner}/${repo}/registry/${registryAddress}`);
}
getCronList(owner: string, repo: string): Promise<Cron[]> {
return this._get(`/api/repos/${owner}/${repo}/cron`) as Promise<Cron[]>;
}
createCron(owner: string, repo: string, cron: Partial<Cron>): Promise<unknown> {
return this._post(`/api/repos/${owner}/${repo}/cron`, cron);
}
updateCron(owner: string, repo: string, cron: Partial<Cron>): Promise<unknown> {
return this._patch(`/api/repos/${owner}/${repo}/cron/${cron.id}`, cron);
}
deleteCron(owner: string, repo: string, cronId: number): Promise<unknown> {
return this._delete(`/api/repos/${owner}/${repo}/cron/${cronId}`);
}
getOrgPermissions(owner: string): Promise<OrgPermissions> {
return this._get(`/api/orgs/${owner}/permissions`) as Promise<OrgPermissions>;
}

View file

@ -8,7 +8,7 @@ export type Build = {
parent: number;
event: 'push' | 'tag' | 'pull_request' | 'deployment';
event: 'push' | 'tag' | 'pull_request' | 'deployment' | 'cron';
// The current status of the build.
status: BuildStatus;

View file

@ -0,0 +1,7 @@
export type Cron = {
id: number;
name: string;
branch: string;
schedule: string;
next_exec: number;
};

View file

@ -1,5 +1,6 @@
export * from './build';
export * from './buildConfig';
export * from './cron';
export * from './org';
export * from './registry';
export * from './repo';

View file

@ -3,4 +3,5 @@ export enum WebhookEvents {
Tag = 'tag',
PullRequest = 'pull_request',
Deploy = 'deployment',
Cron = 'cron',
}

View file

@ -15,6 +15,9 @@
<Tab id="registries" :title="$t('repo.settings.registries.registries')">
<RegistriesTab />
</Tab>
<Tab id="crons" :title="$t('repo.settings.crons.crons')">
<CronTab />
</Tab>
<Tab id="badge" :title="$t('repo.settings.badge.badge')">
<BadgeTab />
</Tab>
@ -25,8 +28,8 @@
</FluidContainer>
</template>
<script lang="ts">
import { defineComponent, inject, onMounted, Ref } from 'vue';
<script lang="ts" setup>
import { inject, onMounted, Ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
@ -34,6 +37,7 @@ import IconButton from '~/components/atomic/IconButton.vue';
import FluidContainer from '~/components/layout/FluidContainer.vue';
import ActionsTab from '~/components/repo/settings/ActionsTab.vue';
import BadgeTab from '~/components/repo/settings/BadgeTab.vue';
import CronTab from '~/components/repo/settings/CronTab.vue';
import GeneralTab from '~/components/repo/settings/GeneralTab.vue';
import RegistriesTab from '~/components/repo/settings/RegistriesTab.vue';
import SecretsTab from '~/components/repo/settings/SecretsTab.vue';
@ -43,41 +47,21 @@ import useNotifications from '~/compositions/useNotifications';
import { useRouteBackOrDefault } from '~/compositions/useRouteBackOrDefault';
import { RepoPermissions } from '~/lib/api/types';
export default defineComponent({
name: 'RepoSettings',
const notifications = useNotifications();
const router = useRouter();
const i18n = useI18n();
components: {
FluidContainer,
IconButton,
Tabs,
Tab,
GeneralTab,
SecretsTab,
RegistriesTab,
ActionsTab,
BadgeTab,
},
const repoPermissions = inject<Ref<RepoPermissions>>('repo-permissions');
if (!repoPermissions) {
throw new Error('Unexpected: "repoPermissions" should be provided at this place');
}
setup() {
const notifications = useNotifications();
const router = useRouter();
const i18n = useI18n();
const repoPermissions = inject<Ref<RepoPermissions>>('repo-permissions');
if (!repoPermissions) {
throw new Error('Unexpected: "repoPermissions" should be provided at this place');
}
onMounted(async () => {
if (!repoPermissions.value.admin) {
notifications.notify({ type: 'error', title: i18n.t('repo.settings.not_allowed') });
await router.replace({ name: 'home' });
}
});
return {
goBack: useRouteBackOrDefault({ name: 'repo' }),
};
},
onMounted(async () => {
if (!repoPermissions.value.admin) {
notifications.notify({ type: 'error', title: i18n.t('repo.settings.not_allowed') });
await router.replace({ name: 'home' });
}
});
const goBack = useRouteBackOrDefault({ name: 'repo' });
</script>

View file

@ -29,6 +29,8 @@ const (
pathRepoSecret = "%s/api/repos/%s/%s/secrets/%s"
pathRepoRegistries = "%s/api/repos/%s/%s/registry"
pathRepoRegistry = "%s/api/repos/%s/%s/registry/%s"
pathRepoCrons = "%s/api/repos/%s/%s/cron"
pathRepoCron = "%s/api/repos/%s/%s/cron/%d"
pathOrgSecrets = "%s/api/orgs/%s/secrets"
pathOrgSecret = "%s/api/orgs/%s/secrets/%s"
pathGlobalSecrets = "%s/api/secrets"
@ -463,6 +465,36 @@ func (c *client) SetLogLevel(in *LogLevel) (*LogLevel, error) {
return out, err
}
func (c *client) CronList(owner, repo string) ([]*Cron, error) {
out := make([]*Cron, 0, 5)
uri := fmt.Sprintf(pathRepoCrons, c.addr, owner, repo)
return out, c.get(uri, &out)
}
func (c *client) CronCreate(owner, repo string, in *Cron) (*Cron, error) {
out := new(Cron)
uri := fmt.Sprintf(pathRepoCrons, c.addr, owner, repo)
return out, c.post(uri, in, out)
}
func (c *client) CronUpdate(owner, repo string, in *Cron) (*Cron, error) {
out := new(Cron)
uri := fmt.Sprintf(pathRepoCron, c.addr, owner, repo, in.ID)
err := c.patch(uri, in, out)
return out, err
}
func (c *client) CronDelete(owner, repo string, cronID int64) error {
uri := fmt.Sprintf(pathRepoCron, c.addr, owner, repo, cronID)
return c.delete(uri)
}
func (c *client) CronGet(owner, repo string, cronID int64) (*Cron, error) {
out := new(Cron)
uri := fmt.Sprintf(pathRepoCron, c.addr, owner, repo, cronID)
return out, c.get(uri, out)
}
//
// http request helper functions
//

View file

@ -166,4 +166,19 @@ type Client interface {
// SetLogLevel sets the server's logging level
SetLogLevel(logLevel *LogLevel) (*LogLevel, error)
// CronList list all cron jobs of a repo
CronList(owner, repo string) ([]*Cron, error)
// CronGet get a specific cron job of a repo by id
CronGet(owner, repo string, cronID int64) (*Cron, error)
// CronDelete delete a specific cron job of a repo by id
CronDelete(owner, repo string, cronID int64) error
// CronCreate create a new cron job in a repo
CronCreate(owner, repo string, cron *Cron) (*Cron, error)
// CronUpdate update an existing cron job of a repo
CronUpdate(owner, repo string, cron *Cron) (*Cron, error)
}

View file

@ -163,4 +163,16 @@ type (
Proc string `json:"proc"`
Output string `json:"out"`
}
// Cron is the JSON data of a cron job
Cron struct {
ID int64 `json:"id"`
Name string `json:"name"`
RepoID int64 `json:"repo_id"`
CreatorID int64 `json:"creator_id"`
NextExec int64 `json:"next_exec"`
Schedule string `json:"schedule"`
Created int64 `json:"created_at"`
Branch string `json:"branch"`
}
)