2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2021-06-28 19:28:18 +02:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// 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.
|
2021-06-28 19:28:18 +02:00
|
|
|
//
|
|
|
|
// This file has been modified by Informatyka Boguslawski sp. z o.o. sp.k.
|
2018-02-20 00:24:10 +02:00
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
package api
|
2016-03-31 08:24:47 +02:00
|
|
|
|
|
|
|
import (
|
2018-01-18 19:26:28 +02:00
|
|
|
"bytes"
|
2022-09-27 11:05:00 +02:00
|
|
|
"encoding/json"
|
2022-10-26 01:23:28 +02:00
|
|
|
"errors"
|
2017-03-05 13:05:16 +02:00
|
|
|
"fmt"
|
2016-09-28 03:30:28 +02:00
|
|
|
"io"
|
2016-03-31 08:24:47 +02:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-09-27 11:05:00 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2022-10-26 01:23:28 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2022-09-27 11:05:00 +02:00
|
|
|
|
2019-06-01 10:17:02 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2022-06-15 21:33:29 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
2021-09-22 22:41:32 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2016-03-31 08:24:47 +02:00
|
|
|
)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func CreatePipeline(c *gin.Context) {
|
2022-09-27 11:05:00 +02:00
|
|
|
_store := store.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
var p model.PipelineOptions
|
2022-09-27 11:05:00 +02:00
|
|
|
|
|
|
|
err := json.NewDecoder(c.Request.Body).Decode(&p)
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := session.User(c)
|
|
|
|
|
|
|
|
lastCommit, _ := server.Config.Services.Remote.BranchHead(c, user, repo, p.Branch)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
tmpBuild := createTmpPipeline(model.EventManual, lastCommit, repo, user, &p)
|
2022-09-27 11:05:00 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := pipeline.Create(c, _store, repo, tmpBuild)
|
2022-09-27 11:05:00 +02:00
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2022-09-27 11:05:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func createTmpPipeline(event model.WebhookEvent, commitSHA string, repo *model.Repo, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
|
|
|
|
return &model.Pipeline{
|
2022-09-27 11:05:00 +02:00
|
|
|
Event: event,
|
|
|
|
Commit: commitSHA,
|
|
|
|
Branch: opts.Branch,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
|
|
|
|
Avatar: user.Avatar,
|
2022-10-18 03:24:12 +02:00
|
|
|
Message: "MANUAL PIPELINE @ " + opts.Branch,
|
2022-09-27 11:05:00 +02:00
|
|
|
|
|
|
|
Ref: opts.Branch,
|
|
|
|
AdditionalVariables: opts.Variables,
|
|
|
|
|
|
|
|
Author: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
|
|
|
|
// TODO: Generate proper link to commit
|
|
|
|
Link: repo.Link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipelines(c *gin.Context) {
|
2016-03-31 08:24:47 +02:00
|
|
|
repo := session.Repo(c)
|
2018-02-02 21:04:21 +02:00
|
|
|
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2018-02-02 21:04:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelines, err := store.FromContext(c).GetPipelineList(repo, page)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2022-10-26 01:23:28 +02:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-03-31 08:24:47 +02:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(http.StatusOK, pipelines)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipeline(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 08:24:47 +02:00
|
|
|
if c.Param("number") == "latest" {
|
2022-10-18 03:24:12 +02:00
|
|
|
GetPipelineLast(c)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := session.Repo(c)
|
2021-11-13 21:18:06 +02:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
files, _ := _store.FileList(pl)
|
2022-10-28 17:38:53 +02:00
|
|
|
steps, _ := _store.StepList(pl)
|
|
|
|
if pl.Steps, err = model.Tree(steps); err != nil {
|
2021-12-09 00:36:23 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
pl.Files = files
|
2016-03-31 08:24:47 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipelineLast(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 08:24:47 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
branch := c.DefaultQuery("branch", repo.Branch)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineLast(repo, branch)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
steps, err := _store.StepList(pl)
|
2021-12-09 00:36:23 +02:00
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
if pl.Steps, err = model.Tree(steps); err != nil {
|
2021-12-09 00:36:23 +02:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipelineLogs(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 08:24:47 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// parse the pipeline number and step sequence number from
|
2021-09-22 20:48:01 +02:00
|
|
|
// the request parameter.
|
2021-11-13 21:18:06 +02:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-08-25 02:03:11 +02:00
|
|
|
ppid, _ := strconv.Atoi(c.Params.ByName("pid"))
|
2022-10-28 17:38:53 +02:00
|
|
|
name := c.Params.ByName("step")
|
2016-03-31 08:24:47 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
step, err := _store.StepChild(pl, ppid, name)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
rc, err := _store.LogFind(step)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-02 16:13:26 +02:00
|
|
|
defer rc.Close()
|
2016-05-11 09:36:01 +02:00
|
|
|
|
2017-08-25 02:03:11 +02:00
|
|
|
c.Header("Content-Type", "application/json")
|
2021-11-23 16:36:52 +02:00
|
|
|
if _, err := io.Copy(c.Writer, rc); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not copy log to http response")
|
|
|
|
}
|
2017-08-25 02:03:11 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
func GetStepLogs(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2017-08-25 02:03:11 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// parse the pipeline number and step sequence number from
|
2021-09-22 20:48:01 +02:00
|
|
|
// the request parameter.
|
2021-11-13 21:18:06 +02:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-08-25 02:03:11 +02:00
|
|
|
pid, _ := strconv.Atoi(c.Params.ByName("pid"))
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-08-25 02:03:11 +02:00
|
|
|
if err != nil {
|
2021-12-09 00:40:00 +02:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2017-08-25 02:03:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
step, err := _store.StepFind(pl, pid)
|
2017-08-25 02:03:11 +02:00
|
|
|
if err != nil {
|
2021-12-09 00:40:00 +02:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2017-08-25 02:03:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
rc, err := _store.LogFind(step)
|
2017-08-25 02:03:11 +02:00
|
|
|
if err != nil {
|
2021-12-09 00:40:00 +02:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2017-08-25 02:03:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rc.Close()
|
|
|
|
|
2016-06-14 23:01:20 +02:00
|
|
|
c.Header("Content-Type", "application/json")
|
2021-11-23 16:36:52 +02:00
|
|
|
if _, err := io.Copy(c.Writer, rc); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not copy log to http response")
|
|
|
|
}
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipelineConfig(c *gin.Context) {
|
2022-01-09 04:59:45 +02:00
|
|
|
_store := store.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2022-01-09 04:59:45 +02:00
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
configs, err := _store.ConfigsForPipeline(pl.ID)
|
2022-01-09 04:59:45 +02:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, configs)
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
// DeletePipeline cancels a pipeline
|
|
|
|
func DeletePipeline(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 08:24:47 +02:00
|
|
|
repo := session.Repo(c)
|
2021-11-13 21:18:06 +02:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2016-03-31 08:24:47 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
2021-12-09 00:40:00 +02:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2016-03-31 08:24:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
if err := pipeline.Cancel(c, _store, repo, pl); err != nil {
|
2022-06-15 21:33:29 +02:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.Status(http.StatusNoContent)
|
2019-09-16 15:18:15 +02:00
|
|
|
}
|
2017-08-01 18:57:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-15 21:33:29 +02:00
|
|
|
// PostApproval start pipelines in gated repos
|
2017-03-18 10:49:27 +02:00
|
|
|
func PostApproval(c *gin.Context) {
|
|
|
|
var (
|
2021-12-18 01:09:09 +02:00
|
|
|
_store = store.FromContext(c)
|
|
|
|
repo = session.Repo(c)
|
|
|
|
user = session.User(c)
|
|
|
|
num, _ = strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-03-18 10:49:27 +02:00
|
|
|
)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-03-18 10:49:27 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-18 10:49:27 +02:00
|
|
|
return
|
|
|
|
}
|
2017-03-18 13:25:53 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
newpipeline, err := pipeline.Approve(c, _store, pl, user, repo)
|
2017-03-18 13:25:53 +02:00
|
|
|
if err != nil {
|
2022-06-15 21:33:29 +02:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(200, newpipeline)
|
2021-11-23 16:36:52 +02:00
|
|
|
}
|
2017-03-18 10:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-06-15 21:33:29 +02:00
|
|
|
// PostDecline decline pipelines in gated repos
|
2017-03-18 10:49:27 +02:00
|
|
|
func PostDecline(c *gin.Context) {
|
|
|
|
var (
|
2021-12-28 18:02:49 +02:00
|
|
|
_store = store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
repo = session.Repo(c)
|
|
|
|
user = session.User(c)
|
2021-11-13 21:18:06 +02:00
|
|
|
num, _ = strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-03-18 10:49:27 +02:00
|
|
|
)
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-03-18 10:49:27 +02:00
|
|
|
if err != nil {
|
2022-06-15 21:33:29 +02:00
|
|
|
c.String(http.StatusNotFound, "%v", err)
|
2017-03-18 10:49:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err = pipeline.Decline(c, _store, pl, user, repo)
|
2022-06-15 21:33:29 +02:00
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(200, pl)
|
2022-01-09 04:47:31 +02:00
|
|
|
}
|
2017-03-18 10:49:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func GetPipelineQueue(c *gin.Context) {
|
|
|
|
out, err := store.FromContext(c).GetPipelineQueue()
|
2016-05-11 07:19:45 +02:00
|
|
|
if err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
c.String(500, "Error getting pipeline queue. %s", err)
|
2016-05-11 07:19:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(200, out)
|
|
|
|
}
|
2016-09-28 03:30:28 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
// PostPipeline restarts a pipeline optional with altered event, deploy or environment
|
|
|
|
func PostPipeline(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2017-03-14 17:56:22 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2017-03-14 17:56:22 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2017-03-14 17:56:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
user, err := _store.GetUser(repo.UserID)
|
2017-03-14 17:56:22 +02:00
|
|
|
if err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("failure to find repo owner %s. %s", repo.FullName, err)
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(500, err)
|
2017-03-14 17:56:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-03-14 17:56:22 +02:00
|
|
|
if err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
log.Error().Msgf("failure to get pipeline %d. %s", num, err)
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-14 17:56:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 21:33:29 +02:00
|
|
|
// refresh the token to make sure, pipeline.ReStart can still obtain the pipeline config if nessessary again
|
|
|
|
refreshUserToken(c, user)
|
2017-03-14 17:56:22 +02:00
|
|
|
|
2022-06-15 21:33:29 +02:00
|
|
|
// make Deploy overridable
|
2022-10-18 03:24:12 +02:00
|
|
|
pl.Deploy = c.DefaultQuery("deploy_to", pl.Deploy)
|
2017-09-08 02:43:33 +02:00
|
|
|
|
2022-06-15 21:33:29 +02:00
|
|
|
// make Event overridable
|
2021-12-11 03:37:40 +02:00
|
|
|
if event, ok := c.GetQuery("event"); ok {
|
2022-10-18 03:24:12 +02:00
|
|
|
pl.Event = model.WebhookEvent(event)
|
2021-12-28 18:02:49 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
if !model.ValidateWebhookEvent(pl.Event) {
|
|
|
|
msg := fmt.Sprintf("pipeline event '%s' is invalid", event)
|
2021-12-28 18:02:49 +02:00
|
|
|
c.String(http.StatusBadRequest, msg)
|
|
|
|
return
|
2021-12-11 03:37:40 +02:00
|
|
|
}
|
2017-09-11 02:45:42 +02:00
|
|
|
}
|
2017-04-04 12:50:15 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
// Read query string parameters into pipelineParams, exclude reserved params
|
2022-01-05 22:50:23 +02:00
|
|
|
envs := map[string]string{}
|
2017-03-14 17:56:22 +02:00
|
|
|
for key, val := range c.Request.URL.Query() {
|
|
|
|
switch key {
|
2021-12-28 18:02:49 +02:00
|
|
|
// Skip some options of the endpoint
|
2017-03-14 17:56:22 +02:00
|
|
|
case "fork", "event", "deploy_to":
|
2021-12-28 18:02:49 +02:00
|
|
|
continue
|
2017-03-14 17:56:22 +02:00
|
|
|
default:
|
2022-10-18 03:24:12 +02:00
|
|
|
// We only accept string literals, because pipeline parameters will be
|
2017-03-14 17:56:22 +02:00
|
|
|
// injected as environment variables
|
2021-12-28 18:02:49 +02:00
|
|
|
// TODO: sanitize the value
|
|
|
|
envs[key] = val[0]
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
newpipeline, err := pipeline.Restart(c, _store, pl, user, repo, envs)
|
2017-04-06 18:04:25 +02:00
|
|
|
if err != nil {
|
2022-06-15 21:33:29 +02:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2022-10-18 03:24:12 +02:00
|
|
|
c.JSON(200, newpipeline)
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 19:26:28 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
func DeletePipelineLogs(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
|
2018-01-18 19:26:28 +02:00
|
|
|
repo := session.Repo(c)
|
2018-01-18 19:30:41 +02:00
|
|
|
user := session.User(c)
|
2021-11-13 21:18:06 +02:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2018-01-18 19:26:28 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2018-01-18 19:26:28 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2018-01-18 19:26:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
steps, err := _store.StepList(pl)
|
2018-01-18 19:26:28 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2018-01-18 19:26:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
switch pl.Status {
|
2018-01-18 19:26:28 +02:00
|
|
|
case model.StatusRunning, model.StatusPending:
|
2022-10-18 03:24:12 +02:00
|
|
|
c.String(400, "Cannot delete logs for a pending or running pipeline")
|
2018-01-18 19:26:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
for _, step := range steps {
|
2018-01-18 20:20:42 +02:00
|
|
|
t := time.Now().UTC()
|
2022-10-28 17:38:53 +02:00
|
|
|
buf := bytes.NewBufferString(fmt.Sprintf(deleteStr, step.Name, user.Login, t.Format(time.UnixDate)))
|
|
|
|
lerr := _store.LogSave(step, buf)
|
2018-01-18 19:26:28 +02:00
|
|
|
if lerr != nil {
|
|
|
|
err = lerr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, "There was a problem deleting your logs. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.String(204, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteStr = `[
|
|
|
|
{
|
2022-10-28 17:38:53 +02:00
|
|
|
"step": %q,
|
2022-06-17 12:03:34 +02:00
|
|
|
"pos": 0,
|
|
|
|
"out": "logs purged by %s on %s\n"
|
2018-01-18 19:26:28 +02:00
|
|
|
}
|
|
|
|
]`
|