2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2021-06-28 17:28:18 +00:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
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.
|
2021-06-28 17:28:18 +00:00
|
|
|
//
|
|
|
|
// This file has been modified by Informatyka Boguslawski sp. z o.o. sp.k.
|
2018-02-19 22:24:10 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2016-03-31 06:24:47 +00:00
|
|
|
|
|
|
|
import (
|
2018-01-18 17:26:28 +00:00
|
|
|
"bytes"
|
2022-09-27 09:05:00 +00:00
|
|
|
"encoding/json"
|
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"
|
|
|
|
|
2022-09-27 09:05:00 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
|
|
|
|
2019-06-01 08:17:02 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2022-06-15 19:33:29 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
2021-09-22 20:41:32 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2016-03-31 06:24:47 +00:00
|
|
|
)
|
|
|
|
|
2022-09-27 09:05:00 +00:00
|
|
|
func CreateBuild(c *gin.Context) {
|
|
|
|
_store := store.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
|
|
|
var p model.BuildOptions
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
tmpBuild := createTmpBuild(model.EventManual, lastCommit, repo, user, &p)
|
|
|
|
|
|
|
|
build, err := pipeline.Create(c, _store, repo, tmpBuild)
|
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, build)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTmpBuild(event model.WebhookEvent, commitSHA string, repo *model.Repo, user *model.User, opts *model.BuildOptions) *model.Build {
|
|
|
|
return &model.Build{
|
|
|
|
Event: event,
|
|
|
|
Commit: commitSHA,
|
|
|
|
Branch: opts.Branch,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
|
|
|
|
Avatar: user.Avatar,
|
|
|
|
Message: "MANUAL BUILD @ " + opts.Branch,
|
|
|
|
|
|
|
|
Ref: opts.Branch,
|
|
|
|
AdditionalVariables: opts.Variables,
|
|
|
|
|
|
|
|
Author: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
|
|
|
|
// TODO: Generate proper link to commit
|
|
|
|
Link: repo.Link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 06:24:47 +00:00
|
|
|
func GetBuilds(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
2018-02-02 19:04:21 +00:00
|
|
|
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2018-02-02 19:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
builds, err := store.FromContext(c).GetBuildList(repo, page)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-06-14 21:01:20 +00:00
|
|
|
c.JSON(http.StatusOK, builds)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuild(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 06:24:47 +00:00
|
|
|
if c.Param("number") == "latest" {
|
|
|
|
GetBuildLast(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := session.Repo(c)
|
2021-11-13 19:18:06 +00:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
files, _ := _store.FileList(build)
|
|
|
|
procs, _ := _store.ProcList(build)
|
2021-12-08 22:36:23 +00:00
|
|
|
if build.Procs, err = model.Tree(procs); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 21:58:44 +00:00
|
|
|
build.Files = files
|
2016-03-31 06:24:47 +00:00
|
|
|
|
2017-04-02 14:13:26 +00:00
|
|
|
c.JSON(http.StatusOK, build)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildLast(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 06:24:47 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
branch := c.DefaultQuery("branch", repo.Branch)
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildLast(repo, branch)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
procs, err := _store.ProcList(build)
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if build.Procs, err = model.Tree(procs); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-04-02 14:13:26 +00:00
|
|
|
c.JSON(http.StatusOK, build)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildLogs(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 06:24:47 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
|
|
|
// parse the build number and job sequence number from
|
2021-09-22 18:48:01 +00:00
|
|
|
// the request parameter.
|
2021-11-13 19:18:06 +00:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-08-25 00:03:11 +00:00
|
|
|
ppid, _ := strconv.Atoi(c.Params.ByName("pid"))
|
2017-04-03 09:34:37 +00:00
|
|
|
name := c.Params.ByName("proc")
|
2016-03-31 06:24:47 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
proc, err := _store.ProcChild(build, ppid, name)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
rc, err := _store.LogFind(proc)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = 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")
|
2021-11-23 14:36:52 +00: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 00:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetProcLogs(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2017-08-25 00:03:11 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
|
|
|
// parse the build number and job sequence number from
|
2021-09-22 18:48:01 +00:00
|
|
|
// the request parameter.
|
2021-11-13 19:18:06 +00:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-08-25 00:03:11 +00:00
|
|
|
pid, _ := strconv.Atoi(c.Params.ByName("pid"))
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
2021-12-08 22:40:00 +00:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2017-08-25 00:03:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
proc, err := _store.ProcFind(build, pid)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
2021-12-08 22:40:00 +00:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2017-08-25 00:03:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
rc, err := _store.LogFind(proc)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
2021-12-08 22:40:00 +00:00
|
|
|
_ = 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")
|
2021-11-23 14:36:52 +00: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 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 02:59:45 +00:00
|
|
|
func GetBuildConfig(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
|
|
|
|
}
|
|
|
|
|
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configs, err := _store.ConfigsForBuild(build.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, configs)
|
|
|
|
}
|
|
|
|
|
2019-06-19 06:36:13 +00:00
|
|
|
// DeleteBuild cancels a build
|
2016-03-31 06:24:47 +00:00
|
|
|
func DeleteBuild(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2016-03-31 06:24:47 +00:00
|
|
|
repo := session.Repo(c)
|
2021-11-13 19:18:06 +00:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2016-03-31 06:24:47 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-12-08 22:40:00 +00:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
if err := pipeline.Cancel(c, _store, repo, build); 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
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// PostApproval start pipelines in gated repos
|
2017-03-18 08:49:27 +00:00
|
|
|
func PostApproval(c *gin.Context) {
|
|
|
|
var (
|
2021-12-17 23:09:09 +00: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 08:49:27 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2017-03-18 08:49:27 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-18 08:49:27 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-18 11:25:53 +00:00
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
newBuild, err := pipeline.Approve(c, _store, build, user, repo)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
2022-06-15 19:33:29 +00:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, newBuild)
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// PostDecline decline pipelines in gated repos
|
2017-03-18 08:49:27 +00:00
|
|
|
func PostDecline(c *gin.Context) {
|
|
|
|
var (
|
2021-12-28 16:02:49 +00:00
|
|
|
_store = store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
repo = session.Repo(c)
|
|
|
|
user = session.User(c)
|
2021-11-13 19:18:06 +00:00
|
|
|
num, _ = strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2017-03-18 08:49:27 +00:00
|
|
|
)
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2017-03-18 08:49:27 +00:00
|
|
|
if err != nil {
|
2022-06-15 19:33:29 +00:00
|
|
|
c.String(http.StatusNotFound, "%v", err)
|
2017-03-18 08:49:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
build, err = pipeline.Decline(c, _store, build, user, repo)
|
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, build)
|
2022-01-09 02:47:31 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 05:19:45 +00:00
|
|
|
func GetBuildQueue(c *gin.Context) {
|
2021-10-28 09:12:58 +00:00
|
|
|
out, err := store.FromContext(c).GetBuildQueue()
|
2016-05-11 05:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error getting build queue. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(200, out)
|
|
|
|
}
|
2016-09-28 01:30:28 +00:00
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// PostBuild restarts a build optional with altered event, deploy or environment
|
2017-03-16 10:14:02 +00:00
|
|
|
func PostBuild(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2017-03-14 15:56:22 +00:00
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
user, err := _store.GetUser(repo.UserID)
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("failure to find repo owner %s. %s", repo.FullName, err)
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(500, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("failure to get build %d. %s", num, err)
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// refresh the token to make sure, pipeline.ReStart can still obtain the pipeline config if nessessary again
|
|
|
|
refreshUserToken(c, user)
|
2017-03-14 15:56:22 +00:00
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// make Deploy overridable
|
2017-09-11 00:45:42 +00:00
|
|
|
build.Deploy = c.DefaultQuery("deploy_to", build.Deploy)
|
2017-09-08 00:43:33 +00:00
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// make Event overridable
|
2021-12-11 01:37:40 +00:00
|
|
|
if event, ok := c.GetQuery("event"); ok {
|
2021-12-28 16:02:49 +00:00
|
|
|
build.Event = model.WebhookEvent(event)
|
|
|
|
|
|
|
|
if !model.ValidateWebhookEvent(build.Event) {
|
|
|
|
msg := fmt.Sprintf("build event '%s' is invalid", event)
|
|
|
|
c.String(http.StatusBadRequest, msg)
|
|
|
|
return
|
2021-12-11 01:37:40 +00:00
|
|
|
}
|
2017-09-11 00:45:42 +00:00
|
|
|
}
|
2017-04-04 10:50:15 +00:00
|
|
|
|
2017-03-14 15:56:22 +00:00
|
|
|
// Read query string parameters into buildParams, 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 {
|
2021-12-28 16:02:49 +00:00
|
|
|
// Skip some options of the endpoint
|
2017-03-14 15:56:22 +00:00
|
|
|
case "fork", "event", "deploy_to":
|
2021-12-28 16:02:49 +00:00
|
|
|
continue
|
2017-03-14 15:56:22 +00:00
|
|
|
default:
|
|
|
|
// We only accept string literals, because build parameters will be
|
|
|
|
// injected as environment variables
|
2021-12-28 16:02:49 +00:00
|
|
|
// TODO: sanitize the value
|
|
|
|
envs[key] = val[0]
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
newBuild, err := pipeline.Restart(c, _store, build, user, repo, envs)
|
2017-04-06 16:04:25 +00:00
|
|
|
if err != nil {
|
2022-06-15 19:33:29 +00:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, newBuild)
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 17:26:28 +00:00
|
|
|
|
|
|
|
func DeleteBuildLogs(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
|
2018-01-18 17:26:28 +00:00
|
|
|
repo := session.Repo(c)
|
2018-01-18 17:30:41 +00:00
|
|
|
user := session.User(c)
|
2021-11-13 19:18:06 +00:00
|
|
|
num, _ := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2018-01-18 17:26:28 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
procs, err := _store.ProcList(build)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch build.Status {
|
|
|
|
case model.StatusRunning, model.StatusPending:
|
|
|
|
c.String(400, "Cannot delete logs for a pending or running build")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, proc := range procs {
|
2018-01-18 18:20:42 +00:00
|
|
|
t := time.Now().UTC()
|
|
|
|
buf := bytes.NewBufferString(fmt.Sprintf(deleteStr, proc.Name, user.Login, t.Format(time.UnixDate)))
|
2021-12-01 13:22:06 +00:00
|
|
|
lerr := _store.LogSave(proc, 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 = `[
|
|
|
|
{
|
2022-06-17 10:03:34 +00:00
|
|
|
"proc": %q,
|
|
|
|
"pos": 0,
|
|
|
|
"out": "logs purged by %s on %s\n"
|
2018-01-18 17:26:28 +00:00
|
|
|
}
|
|
|
|
]`
|