woodpecker/server/api/pipeline.go

445 lines
10 KiB
Go
Raw Normal View History

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