2022-08-31 22:36:32 +00: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 (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-09-02 11:31:10 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
cronScheduler "go.woodpecker-ci.org/woodpecker/v2/server/cron"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/router/middleware/session"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2022-08-31 22:36:32 +00:00
|
|
|
)
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetCron
|
|
|
|
//
|
|
|
|
// @Summary Get a cron job by id
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron/{cron} [get]
|
2023-06-03 19:38:36 +00: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 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param cron path string true "the cron job id"
|
2022-08-31 22:36:32 +00: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 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cron, err := store.FromContext(c).CronFind(repo, id)
|
|
|
|
if err != nil {
|
2023-09-02 11:31:10 +00:00
|
|
|
handleDbError(c, err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, cron)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// RunCron
|
|
|
|
//
|
|
|
|
// @Summary Start a cron job now
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron/{cron} [post]
|
2023-06-03 19:38:36 +00: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 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param cron path string true "the cron job id"
|
2022-10-25 23:23:28 +00: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 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
|
2022-10-25 23:23:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cron, err := _store.CronFind(repo, id)
|
|
|
|
if err != nil {
|
2023-09-02 11:31:10 +00:00
|
|
|
handleDbError(c, err)
|
2022-10-25 23:23:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
repo, newPipeline, err := cronScheduler.CreatePipeline(c, _store, server.Config.Services.Forge, cron)
|
2022-10-25 23:23:28 +00: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 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2022-10-25 23:23:28 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostCron
|
|
|
|
//
|
|
|
|
// @Summary Persist/creat a cron job
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron [post]
|
2023-06-03 19:38:36 +00: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 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param cronJob body Cron true "the new cron job"
|
2022-08-31 22:36:32 +00:00
|
|
|
func PostCron(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2023-03-19 12:52:58 +00:00
|
|
|
_store := store.FromContext(c)
|
2022-11-04 23:35:06 +00:00
|
|
|
forge := server.Config.Services.Forge
|
2022-08-31 22:36:32 +00: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,
|
|
|
|
Branch: in.Branch,
|
|
|
|
}
|
|
|
|
if err := cron.Validate(); err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusUnprocessableEntity, "Error inserting cron. validate failed: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nextExec, err := cronScheduler.CalcNewNext(in.Schedule, time.Now())
|
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cron.NextExec = nextExec.Unix()
|
|
|
|
|
|
|
|
if in.Branch != "" {
|
2022-11-04 23:35:06 +00:00
|
|
|
// check if branch exists on forge
|
|
|
|
_, err := forge.BranchHead(c, user, repo, in.Branch)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 12:52:58 +00:00
|
|
|
if err := _store.CronCreate(cron); err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Error inserting cron %q. %s", in.Name, err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, cron)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PatchCron
|
|
|
|
//
|
|
|
|
// @Summary Update a cron job
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron/{cron} [patch]
|
2023-06-03 19:38:36 +00: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 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param cron path string true "the cron job id"
|
|
|
|
// @Param cronJob body Cron true "the cron job data"
|
2022-08-31 22:36:32 +00:00
|
|
|
func PatchCron(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
user := session.User(c)
|
2023-03-19 12:52:58 +00:00
|
|
|
_store := store.FromContext(c)
|
2022-11-04 23:35:06 +00:00
|
|
|
forge := server.Config.Services.Forge
|
2022-08-31 22:36:32 +00:00
|
|
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("cron"), 10, 64)
|
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
in := new(model.Cron)
|
|
|
|
err = c.Bind(in)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, "Error parsing request. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-19 12:52:58 +00:00
|
|
|
cron, err := _store.CronFind(repo, id)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
2023-09-02 11:31:10 +00:00
|
|
|
handleDbError(c, err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if in.Branch != "" {
|
2022-11-04 23:35:06 +00:00
|
|
|
// check if branch exists on forge
|
|
|
|
_, err := forge.BranchHead(c, user, repo, in.Branch)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error inserting cron. branch not resolved: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cron.Branch = in.Branch
|
|
|
|
}
|
|
|
|
if in.Schedule != "" {
|
|
|
|
cron.Schedule = in.Schedule
|
|
|
|
nextExec, err := cronScheduler.CalcNewNext(in.Schedule, time.Now())
|
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error inserting cron. schedule could not parsed: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cron.NextExec = nextExec.Unix()
|
|
|
|
}
|
|
|
|
if in.Name != "" {
|
|
|
|
cron.Name = in.Name
|
|
|
|
}
|
|
|
|
cron.CreatorID = user.ID
|
|
|
|
|
|
|
|
if err := cron.Validate(); err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusUnprocessableEntity, "Error inserting cron. validate failed: %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
if err := _store.CronUpdate(repo, cron); err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "Error updating cron %q. %s", in.Name, err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, cron)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetCronList
|
|
|
|
//
|
|
|
|
// @Summary Get the cron job list
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron [get]
|
2023-06-03 19:38:36 +00: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 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00: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-08-31 22:36:32 +00:00
|
|
|
func GetCronList(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
2023-04-30 01:40:13 +00:00
|
|
|
list, err := store.FromContext(c).CronList(repo, session.Pagination(c))
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Error getting cron list. %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, list)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// DeleteCron
|
|
|
|
//
|
|
|
|
// @Summary Delete a cron job by id
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/cron/{cron} [delete]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce plain
|
2023-09-02 11:31:10 +00:00
|
|
|
// @Success 204
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Tags Repository cron jobs
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param cron path string true "the cron job id"
|
2022-08-31 22:36:32 +00: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 12:52:58 +00:00
|
|
|
c.String(http.StatusBadRequest, "Error parsing cron id. %s", err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := store.FromContext(c).CronDelete(repo, id); err != nil {
|
2023-09-02 11:31:10 +00:00
|
|
|
handleDbError(c, err)
|
2022-08-31 22:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|