1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2026-06-03 16:35:37 +02:00
Files
woodpecker/server/api/cron.go
T

314 lines
9.4 KiB
Go
Raw Normal View History

2022-09-01 00:36:32 +02:00
// 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 (
2026-03-18 20:55:30 +01:00
"errors"
2022-09-01 00:36:32 +02:00
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
2024-04-16 08:04:55 +02:00
"github.com/rs/zerolog/log"
2024-12-22 11:44:34 +02:00
"go.woodpecker-ci.org/woodpecker/v3/server"
2026-03-28 15:45:23 +01:00
cron_scheduler "go.woodpecker-ci.org/woodpecker/v3/server/cron"
2024-12-22 11:44:34 +02:00
"go.woodpecker-ci.org/woodpecker/v3/server/model"
"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"
2026-03-18 20:55:30 +01:00
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
2022-09-01 00:36:32 +02:00
)
2023-06-03 21:38:36 +02:00
// GetCron
//
// @Summary Get a cron job
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron/{cron} [get]
2023-06-03 21:38:36 +02:00
// @Produce json
// @Success 200 {object} Cron
// @Tags Repository cron jobs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
2023-06-12 16:07:52 -07:00
// @Param repo_id path int true "the repository id"
2023-06-03 21:38:36 +02:00
// @Param cron path string true "the cron job id"
2022-09-01 00:36:32 +02:00
func GetCron(c *gin.Context) {
repo := session.Repo(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
2022-09-01 00:36:32 +02:00
return
}
cron, err := store.FromContext(c).CronFind(repo, id)
if err != nil {
2024-01-10 22:56:42 +01:00
handleDBError(c, err)
2022-09-01 00:36:32 +02:00
return
}
2023-03-19 13:52:58 +01:00
c.JSON(http.StatusOK, cron)
2022-09-01 00:36:32 +02:00
}
2023-06-03 21:38:36 +02:00
// RunCron
//
// @Summary Start a cron job now
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron/{cron} [post]
2023-06-03 21:38:36 +02:00
// @Produce json
// @Success 200 {object} Pipeline
// @Tags Repository cron jobs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
2023-06-12 16:07:52 -07:00
// @Param repo_id path int true "the repository id"
2023-06-03 21:38:36 +02:00
// @Param cron path string true "the cron job id"
2022-10-26 01:23:28 +02:00
func RunCron(c *gin.Context) {
repo := session.Repo(c)
_store := store.FromContext(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
2022-10-26 01:23:28 +02:00
return
}
cron, err := _store.CronFind(repo, id)
if err != nil {
2024-01-10 22:56:42 +01:00
handleDBError(c, err)
2022-10-26 01:23:28 +02:00
return
}
2026-03-28 15:45:23 +01:00
repo, newPipeline, err := cron_scheduler.CreatePipeline(c, _store, cron)
2022-10-26 01:23:28 +02:00
if err != nil {
c.String(http.StatusInternalServerError, "Error creating pipeline for cron %q. %s", id, err)
return
}
pl, err := pipeline.Create(c, _store, repo, newPipeline)
if err != nil {
handlePipelineErr(c, err)
return
}
2023-03-19 13:52:58 +01:00
c.JSON(http.StatusOK, pl)
2022-10-26 01:23:28 +02:00
}
2023-06-03 21:38:36 +02:00
// PostCron
//
// @Summary Create a cron job
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron [post]
2023-06-03 21:38:36 +02:00
// @Produce json
// @Success 200 {object} Cron
// @Tags Repository cron jobs
2023-12-24 15:50:01 +01:00
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
2023-06-12 16:07:52 -07:00
// @Param repo_id path int true "the repository id"
2023-06-03 21:38:36 +02:00
// @Param cronJob body Cron true "the new cron job"
2022-09-01 00:36:32 +02:00
func PostCron(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
2023-03-19 13:52:58 +01:00
_store := store.FromContext(c)
2024-04-16 08:04:55 +02:00
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
if err != nil {
log.Error().Err(err).Msg("Cannot get forge from repo")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
2022-09-01 00:36:32 +02:00
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,
2026-05-17 20:26:37 +02:00
Timezone: in.Timezone,
2022-09-01 00:36:32 +02:00
Branch: in.Branch,
2025-12-28 21:15:29 +01:00
Variables: in.Variables,
2025-12-24 10:02:03 +01:00
Enabled: in.Enabled,
2022-09-01 00:36:32 +02:00
}
2026-05-17 20:26:37 +02:00
if cron.Timezone == "" {
cron.Timezone = "UTC"
}
2022-09-01 00:36:32 +02:00
if err := cron.Validate(); err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusUnprocessableEntity, "Error inserting cron. validate failed: %s", err)
2022-09-01 00:36:32 +02:00
return
}
2026-05-17 20:26:37 +02:00
nextExec, err := cron_scheduler.CalcNewNext(in.Schedule, in.Timezone, time.Now())
2022-09-01 00:36:32 +02:00
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
2022-09-01 00:36:32 +02:00
return
}
cron.NextExec = nextExec.Unix()
if in.Branch != "" {
2022-11-05 00:35:06 +01:00
// check if branch exists on forge
2024-04-16 08:04:55 +02:00
_, err := _forge.BranchHead(c, user, repo, in.Branch)
2022-09-01 00:36:32 +02:00
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
2022-09-01 00:36:32 +02:00
return
}
}
2023-03-19 13:52:58 +01:00
if err := _store.CronCreate(cron); err != nil {
if errors.Is(err, types.ErrInsertDuplicateDetected) {
2026-03-18 20:55:30 +01:00
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)
}
2022-09-01 00:36:32 +02:00
return
}
2023-03-19 13:52:58 +01:00
c.JSON(http.StatusOK, cron)
2022-09-01 00:36:32 +02:00
}
2023-06-03 21:38:36 +02:00
// PatchCron
//
// @Summary Update a cron job
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron/{cron} [patch]
2023-06-03 21:38:36 +02:00
// @Produce json
// @Success 200 {object} Cron
// @Tags Repository cron jobs
2025-12-24 10:02:03 +01:00
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param repo_id path int true "the repository id"
// @Param cron path string true "the cron job id"
// @Param cronJob body CronPatch true "the cron job data"
2022-09-01 00:36:32 +02:00
func PatchCron(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
2023-03-19 13:52:58 +01:00
_store := store.FromContext(c)
2024-04-16 08:04:55 +02:00
_forge, err := server.Config.Services.Manager.ForgeFromRepo(repo)
if err != nil {
log.Error().Err(err).Msg("Cannot get forge from repo")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
2022-09-01 00:36:32 +02:00
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
2022-09-01 00:36:32 +02:00
return
}
2025-12-24 10:02:03 +01:00
in := new(model.CronPatch)
2022-09-01 00:36:32 +02:00
err = c.Bind(in)
if err != nil {
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
return
}
2023-03-19 13:52:58 +01:00
cron, err := _store.CronFind(repo, id)
2022-09-01 00:36:32 +02:00
if err != nil {
2024-01-10 22:56:42 +01:00
handleDBError(c, err)
2022-09-01 00:36:32 +02:00
return
}
2025-12-24 10:02:03 +01:00
if in.Branch != nil && *in.Branch != "" {
2022-11-05 00:35:06 +01:00
// check if branch exists on forge
2025-12-24 10:02:03 +01:00
_, err := _forge.BranchHead(c, user, repo, *in.Branch)
2022-09-01 00:36:32 +02:00
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
2022-09-01 00:36:32 +02:00
return
}
2025-12-24 10:02:03 +01:00
cron.Branch = *in.Branch
2022-09-01 00:36:32 +02:00
}
2026-05-17 20:26:37 +02:00
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
}
cron.NextExec = nextExec.Unix()
}
2025-12-24 10:02:03 +01:00
if in.Schedule != nil && *in.Schedule != "" {
cron.Schedule = *in.Schedule
2026-05-17 20:26:37 +02:00
nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now())
2022-09-01 00:36:32 +02:00
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
2022-09-01 00:36:32 +02:00
return
}
cron.NextExec = nextExec.Unix()
}
2025-12-24 10:02:03 +01:00
if in.Name != nil && *in.Name != "" {
cron.Name = *in.Name
}
if in.Enabled != nil {
cron.Enabled = *in.Enabled
// if we re-enable a cron we have to calc NextExec because it was not while disabled
if cron.Enabled {
2026-05-17 20:26:37 +02:00
nextExec, err := cron_scheduler.CalcNewNext(cron.Schedule, cron.Timezone, time.Now())
2025-12-24 10:02:03 +01:00
if err != nil {
c.String(http.StatusInternalServerError, "Cron schedule could not parsed: %s", err)
return
}
cron.NextExec = nextExec.Unix()
}
2022-09-01 00:36:32 +02:00
}
2025-12-28 21:15:29 +01:00
if in.Variables != nil {
cron.Variables = in.Variables
}
2022-09-01 00:36:32 +02:00
cron.CreatorID = user.ID
if err := cron.Validate(); err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusUnprocessableEntity, "Error inserting cron. validate failed: %s", err)
2022-09-01 00:36:32 +02:00
return
}
2023-03-19 13:52:58 +01:00
if err := _store.CronUpdate(repo, cron); err != nil {
c.String(http.StatusInternalServerError, "Error updating cron %q. %s", in.Name, err)
2022-09-01 00:36:32 +02:00
return
}
2023-03-19 13:52:58 +01:00
c.JSON(http.StatusOK, cron)
2022-09-01 00:36:32 +02:00
}
2023-06-03 21:38:36 +02:00
// GetCronList
//
// @Summary List cron jobs
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron [get]
2023-06-03 21:38:36 +02:00
// @Produce json
// @Success 200 {array} Cron
// @Tags Repository cron jobs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
2023-06-12 16:07:52 -07:00
// @Param repo_id path int true "the repository id"
2023-06-03 21:38:36 +02:00
// @Param page query int false "for response pagination, page offset number" default(1)
// @Param perPage query int false "for response pagination, max items per page" default(50)
2022-09-01 00:36:32 +02:00
func GetCronList(c *gin.Context) {
repo := session.Repo(c)
list, err := store.FromContext(c).CronList(repo, session.Pagination(c))
2022-09-01 00:36:32 +02:00
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusInternalServerError, "Error getting cron list. %s", err)
2022-09-01 00:36:32 +02:00
return
}
2023-03-19 13:52:58 +01:00
c.JSON(http.StatusOK, list)
2022-09-01 00:36:32 +02:00
}
2023-06-03 21:38:36 +02:00
// DeleteCron
//
// @Summary Delete a cron job
2023-06-12 16:07:52 -07:00
// @Router /repos/{repo_id}/cron/{cron} [delete]
2023-06-03 21:38:36 +02:00
// @Produce plain
// @Success 204
2023-06-03 21:38:36 +02:00
// @Tags Repository cron jobs
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
2023-06-12 16:07:52 -07:00
// @Param repo_id path int true "the repository id"
2023-06-03 21:38:36 +02:00
// @Param cron path string true "the cron job id"
2022-09-01 00:36:32 +02:00
func DeleteCron(c *gin.Context) {
repo := session.Repo(c)
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
if err != nil {
2023-03-19 13:52:58 +01:00
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
2022-09-01 00:36:32 +02:00
return
}
if err := store.FromContext(c).CronDelete(repo, id); err != nil {
2024-01-10 22:56:42 +01:00
handleDBError(c, err)
2022-09-01 00:36:32 +02:00
return
}
c.Status(http.StatusNoContent)
2022-09-01 00:36:32 +02:00
}