2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2021-06-28 19:28:18 +02:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
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"
|
2021-12-18 01:09:09 +02:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"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"
|
|
|
|
|
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-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/queue"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-09-22 22:41:32 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2016-03-31 08:24:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetBuilds(c *gin.Context) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:12:58 +02:00
|
|
|
builds, err := store.FromContext(c).GetBuildList(repo, page)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-06-14 23:01:20 +02:00
|
|
|
c.JSON(http.StatusOK, builds)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuild(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" {
|
|
|
|
GetBuildLast(c)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
2021-12-01 15:22:06 +02:00
|
|
|
files, _ := _store.FileList(build)
|
|
|
|
procs, _ := _store.ProcList(build)
|
2021-12-09 00:36:23 +02:00
|
|
|
if build.Procs, err = model.Tree(procs); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 23:58:44 +02:00
|
|
|
build.Files = files
|
2016-03-31 08:24:47 +02:00
|
|
|
|
2017-04-02 16:13:26 +02:00
|
|
|
c.JSON(http.StatusOK, build)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildLast(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)
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildLast(repo, branch)
|
2016-03-31 08:24:47 +02:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-09 00:36:23 +02: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 16:13:26 +02:00
|
|
|
c.JSON(http.StatusOK, build)
|
2016-03-31 08:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildLogs(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)
|
|
|
|
|
|
|
|
// parse the build number and job 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"))
|
2017-04-03 11:34:37 +02:00
|
|
|
name := c.Params.ByName("proc")
|
2016-03-31 08:24:47 +02:00
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
proc, err := _store.ProcChild(build, 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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
rc, err := _store.LogFind(proc)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func GetProcLogs(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)
|
|
|
|
|
|
|
|
// parse the build number and job 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"))
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
proc, err := _store.ProcFind(build, 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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
rc, err := _store.LogFind(proc)
|
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-01-09 04:59:45 +02: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 08:36:13 +02:00
|
|
|
// DeleteBuild cancels a build
|
2016-03-31 08:24:47 +02:00
|
|
|
func DeleteBuild(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
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
procs, err := _store.ProcList(build)
|
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
|
|
|
|
}
|
2016-04-21 09:25:30 +02:00
|
|
|
|
2019-09-16 15:18:15 +02:00
|
|
|
if build.Status != model.StatusRunning && build.Status != model.StatusPending {
|
2021-12-09 00:40:00 +02:00
|
|
|
c.String(http.StatusBadRequest, "Cannot cancel a non-running or non-pending build")
|
2019-09-16 15:18:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First cancel/evict procs in the queue in one go
|
2021-09-24 16:29:26 +02:00
|
|
|
var (
|
|
|
|
procToCancel []string
|
|
|
|
procToEvict []string
|
|
|
|
)
|
2019-06-19 08:36:13 +02:00
|
|
|
for _, proc := range procs {
|
|
|
|
if proc.PPID != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-16 15:18:15 +02:00
|
|
|
if proc.State == model.StatusRunning {
|
|
|
|
procToCancel = append(procToCancel, fmt.Sprint(proc.ID))
|
2019-06-19 08:36:13 +02:00
|
|
|
}
|
2019-09-16 15:18:15 +02:00
|
|
|
if proc.State == model.StatusPending {
|
|
|
|
procToEvict = append(procToEvict, fmt.Sprint(proc.ID))
|
2019-09-15 07:29:45 +02:00
|
|
|
}
|
2017-08-01 18:57:01 +02:00
|
|
|
}
|
2021-11-23 16:36:52 +02:00
|
|
|
|
2021-12-09 00:40:00 +02:00
|
|
|
if len(procToEvict) != 0 {
|
|
|
|
if err := server.Config.Services.Queue.EvictAtOnce(c, procToEvict); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("queue: evict_at_once: %v", procToEvict)
|
|
|
|
}
|
|
|
|
if err := server.Config.Services.Queue.ErrorAtOnce(c, procToEvict, queue.ErrCancel); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("queue: evict_at_once: %v", procToEvict)
|
|
|
|
}
|
2021-11-23 16:36:52 +02:00
|
|
|
}
|
2021-12-09 00:40:00 +02:00
|
|
|
if len(procToCancel) != 0 {
|
|
|
|
if err := server.Config.Services.Queue.ErrorAtOnce(c, procToCancel, queue.ErrCancel); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("queue: evict_at_once: %v", procToCancel)
|
|
|
|
}
|
2021-11-23 16:36:52 +02:00
|
|
|
}
|
2017-08-01 18:57:01 +02:00
|
|
|
|
2019-09-16 15:18:15 +02:00
|
|
|
// Then update the DB status for pending builds
|
|
|
|
// Running ones will be set when the agents stop on the cancel signal
|
2017-08-01 18:57:01 +02:00
|
|
|
for _, proc := range procs {
|
2019-09-16 15:18:15 +02:00
|
|
|
if proc.State == model.StatusPending {
|
|
|
|
if proc.PPID != 0 {
|
2021-12-01 15:22:06 +02:00
|
|
|
if _, err = shared.UpdateProcToStatusSkipped(_store, *proc, 0); err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
2019-09-16 15:18:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-12-01 15:22:06 +02:00
|
|
|
if _, err = shared.UpdateProcToStatusKilled(_store, *proc); err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
2019-09-16 15:18:15 +02:00
|
|
|
}
|
2017-08-01 18:57:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
killedBuild, err := shared.UpdateToStatusKilled(_store, *build)
|
2019-09-16 15:18:15 +02:00
|
|
|
if err != nil {
|
2021-12-09 00:40:00 +02:00
|
|
|
log.Error().Err(err).Msgf("UpdateToStatusKilled: %v", build)
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2019-09-14 14:12:04 +02:00
|
|
|
return
|
|
|
|
}
|
2017-08-01 18:57:01 +02:00
|
|
|
|
2019-09-16 15:18:15 +02:00
|
|
|
// For pending builds, we stream the UI the latest state.
|
|
|
|
// For running builds, the UI will be updated when the agents acknowledge the cancel
|
|
|
|
if build.Status == model.StatusPending {
|
2021-12-01 15:22:06 +02:00
|
|
|
procs, err = _store.ProcList(killedBuild)
|
2019-09-16 15:18:15 +02:00
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2019-09-16 15:18:15 +02:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 00:36:23 +02:00
|
|
|
if killedBuild.Procs, err = model.Tree(procs); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-09 04:47:31 +02:00
|
|
|
if err := publishToTopic(c, killedBuild, repo); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
log.Error().Err(err).Msg("publishToTopic")
|
|
|
|
}
|
2019-09-16 15:18:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:57:01 +02:00
|
|
|
c.String(204, "")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
if build.Status != model.StatusBlocked {
|
2021-12-18 01:09:09 +02:00
|
|
|
c.String(http.StatusBadRequest, "cannot decline a build with status %s", build.Status)
|
2017-03-18 10:49:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-18 13:25:53 +02:00
|
|
|
// fetch the build file from the database
|
2022-01-05 18:54:44 +02:00
|
|
|
configs, err := _store.ConfigsForBuild(build.ID)
|
2017-03-18 13:25:53 +02:00
|
|
|
if err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("failure to get build config for %s. %s", repo.FullName, err)
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-18 13:25:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-18 01:09:09 +02:00
|
|
|
if build, err = shared.UpdateToStatusPending(_store, *build, user.Login); err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, "error updating build. %s", err)
|
2017-03-18 13:25:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-18 01:09:09 +02:00
|
|
|
var yamls []*remote.FileMeta
|
|
|
|
for _, y := range configs {
|
|
|
|
yamls = append(yamls, &remote.FileMeta{Data: y.Data, Name: y.Name})
|
2017-03-18 13:25:53 +02:00
|
|
|
}
|
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
build, buildItems, err := createBuildItems(c, _store, build, user, repo, yamls, nil)
|
2021-12-18 01:09:09 +02:00
|
|
|
if err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
2021-12-18 01:09:09 +02:00
|
|
|
log.Error().Err(err).Msg(msg)
|
2021-12-28 18:02:49 +02:00
|
|
|
c.String(http.StatusInternalServerError, msg)
|
|
|
|
return
|
2017-06-26 21:27:53 +02:00
|
|
|
}
|
2017-03-18 13:25:53 +02:00
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
build, err = startBuild(c, _store, build, user, repo, buildItems)
|
2017-03-18 13:25:53 +02:00
|
|
|
if err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to start build for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
|
|
|
return
|
2021-11-23 16:36:52 +02:00
|
|
|
}
|
2021-12-18 01:09:09 +02:00
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
c.JSON(200, build)
|
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
|
|
|
)
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
if build.Status != model.StatusBlocked {
|
|
|
|
c.String(500, "cannot decline a build with status %s", build.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
if _, err = shared.UpdateToStatusDeclined(_store, *build, user.Login); err != nil {
|
2017-03-18 10:49:27 +02:00
|
|
|
c.String(500, "error updating build. %s", err)
|
|
|
|
return
|
|
|
|
}
|
2017-03-18 11:16:39 +02:00
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
if build.Procs, err = _store.ProcList(build); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not get proc list from store")
|
|
|
|
}
|
|
|
|
if build.Procs, err = model.Tree(build.Procs); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not build tree from proc list")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateBuildStatus(c, build, repo, user); err != nil {
|
|
|
|
log.Error().Err(err).Msg("updateBuildStatus")
|
2017-03-18 11:16:39 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 04:47:31 +02:00
|
|
|
if err := publishToTopic(c, build, repo); err != nil {
|
|
|
|
log.Error().Err(err).Msg("publishToTopic")
|
|
|
|
}
|
|
|
|
|
2017-03-18 10:49:27 +02:00
|
|
|
c.JSON(200, build)
|
|
|
|
}
|
|
|
|
|
2016-05-11 07:19:45 +02:00
|
|
|
func GetBuildQueue(c *gin.Context) {
|
2021-10-28 11:12:58 +02:00
|
|
|
out, err := store.FromContext(c).GetBuildQueue()
|
2016-05-11 07:19:45 +02:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error getting build queue. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(200, out)
|
|
|
|
}
|
2016-09-28 03:30:28 +02:00
|
|
|
|
2019-06-01 12:52:02 +02:00
|
|
|
// PostBuild restarts a build
|
2017-03-16 12:14:02 +02:00
|
|
|
func PostBuild(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_remote := server.Config.Services.Remote
|
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(repo, num)
|
2017-03-14 17:56:22 +02:00
|
|
|
if err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("failure to get build %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
|
|
|
|
}
|
|
|
|
|
2017-09-15 00:33:12 +02:00
|
|
|
switch build.Status {
|
2018-04-05 14:53:28 +02:00
|
|
|
case model.StatusDeclined,
|
|
|
|
model.StatusBlocked:
|
2017-09-15 00:33:12 +02:00
|
|
|
c.String(500, "cannot restart a build with status %s", build.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:56:22 +02:00
|
|
|
// if the remote has a refresh token, the current access token
|
|
|
|
// may be stale. Therefore, we should refresh prior to dispatching
|
|
|
|
// the job.
|
2021-12-01 15:22:06 +02:00
|
|
|
if refresher, ok := _remote.(remote.Refresher); ok {
|
2021-11-23 16:36:52 +02:00
|
|
|
ok, err := refresher.Refresh(c, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("refresh oauth token of user '%s' failed", user.Login)
|
|
|
|
} else if ok {
|
2021-12-01 15:22:06 +02:00
|
|
|
if err := _store.UpdateUser(user); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
log.Error().Err(err).Msg("fail to save user to store after refresh oauth token")
|
|
|
|
}
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 11:56:23 +02:00
|
|
|
var pipelineFiles []*remote.FileMeta
|
|
|
|
|
|
|
|
// fetch the old pipeline config from database
|
2022-01-05 18:54:44 +02:00
|
|
|
configs, err := _store.ConfigsForBuild(build.ID)
|
2017-03-14 17:56:22 +02:00
|
|
|
if err != nil {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Error().Msgf("failure to get build config for %s. %s", repo.FullName, err)
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(404, err)
|
2017-03-14 17:56:22 +02:00
|
|
|
return
|
|
|
|
}
|
2022-02-28 11:56:23 +02:00
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
for _, y := range configs {
|
2022-02-28 11:56:23 +02:00
|
|
|
pipelineFiles = append(pipelineFiles, &remote.FileMeta{Data: y.Data, Name: y.Name})
|
|
|
|
}
|
|
|
|
|
|
|
|
// If config extension is active we should refetch the config in case something changed
|
|
|
|
if server.Config.Services.ConfigService.IsConfigured() {
|
|
|
|
currentFileMeta := make([]*remote.FileMeta, len(configs))
|
|
|
|
for i, cfg := range configs {
|
|
|
|
currentFileMeta[i] = &remote.FileMeta{Name: cfg.Name, Data: cfg.Data}
|
|
|
|
}
|
|
|
|
|
|
|
|
newConfig, useOld, err := server.Config.Services.ConfigService.FetchExternalConfig(c, repo, build, currentFileMeta)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("On fetching external build config: %s", err)
|
|
|
|
c.String(http.StatusBadRequest, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !useOld {
|
|
|
|
pipelineFiles = newConfig
|
|
|
|
}
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
|
|
|
|
2017-09-11 02:45:42 +02:00
|
|
|
build.ID = 0
|
|
|
|
build.Number = 0
|
|
|
|
build.Parent = num
|
|
|
|
build.Status = model.StatusPending
|
|
|
|
build.Started = 0
|
|
|
|
build.Finished = 0
|
|
|
|
build.Enqueued = time.Now().UTC().Unix()
|
|
|
|
build.Error = ""
|
|
|
|
build.Deploy = c.DefaultQuery("deploy_to", build.Deploy)
|
2017-09-08 02:43:33 +02:00
|
|
|
|
2021-12-11 03:37:40 +02:00
|
|
|
if event, ok := c.GetQuery("event"); ok {
|
2021-12-28 18:02:49 +02: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 03:37:40 +02:00
|
|
|
}
|
2017-09-11 02:45:42 +02:00
|
|
|
}
|
2017-04-04 12:50:15 +02:00
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
err = _store.CreateBuild(build)
|
2017-09-11 02:45:42 +02:00
|
|
|
if err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to save build for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2017-09-11 02:45:42 +02:00
|
|
|
return
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:54:44 +02:00
|
|
|
if err := persistBuildConfigs(_store, configs, build.ID); err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to persist build config for %s.", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2019-06-13 17:38:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:56:22 +02:00
|
|
|
// Read query string parameters into buildParams, 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:
|
|
|
|
// We only accept string literals, because build parameters will be
|
|
|
|
// 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-02-28 11:56:23 +02:00
|
|
|
build, buildItems, err := createBuildItems(c, _store, build, user, repo, pipelineFiles, envs)
|
2017-04-06 18:04:25 +02:00
|
|
|
if err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to createBuildItems for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2017-03-14 17:56:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
build, err = startBuild(c, _store, build, user, repo, buildItems)
|
2017-04-04 12:50:15 +02:00
|
|
|
if err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
msg := fmt.Sprintf("failure to start build for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
2017-04-04 12:50:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
c.JSON(200, build)
|
2017-03-14 17:56:22 +02:00
|
|
|
}
|
2018-01-18 19:26:28 +02:00
|
|
|
|
|
|
|
func DeleteBuildLogs(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
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
build, err := _store.GetBuildNumber(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
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
procs, err := _store.ProcList(build)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:20:42 +02:00
|
|
|
t := time.Now().UTC()
|
|
|
|
buf := bytes.NewBufferString(fmt.Sprintf(deleteStr, proc.Name, user.Login, t.Format(time.UnixDate)))
|
2021-12-01 15:22:06 +02:00
|
|
|
lerr := _store.LogSave(proc, 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, "")
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:02:49 +02:00
|
|
|
func createBuildItems(ctx context.Context, store store.Store, build *model.Build, user *model.User, repo *model.Repo, yamls []*remote.FileMeta, envs map[string]string) (*model.Build, []*shared.BuildItem, error) {
|
|
|
|
netrc, err := server.Config.Services.Remote.Netrc(user, repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Failed to generate netrc file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the previous build so that we can send status change notifications
|
|
|
|
last, err := store.GetBuildLastBefore(repo, build.Branch, build.ID)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
log.Error().Err(err).Str("repo", repo.FullName).Msgf("Error getting last build before build number '%d'", build.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
secs, err := server.Config.Services.Secrets.SecretListBuild(repo, build)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error getting secrets for %s#%d", repo.FullName, build.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error getting registry credentials for %s#%d", repo.FullName, build.Number)
|
|
|
|
}
|
|
|
|
|
|
|
|
if envs == nil {
|
|
|
|
envs = map[string]string{}
|
|
|
|
}
|
|
|
|
if server.Config.Services.Environ != nil {
|
|
|
|
globals, _ := server.Config.Services.Environ.EnvironList(repo)
|
|
|
|
for _, global := range globals {
|
|
|
|
envs[global.Name] = global.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b := shared.ProcBuilder{
|
|
|
|
Repo: repo,
|
|
|
|
Curr: build,
|
|
|
|
Last: last,
|
|
|
|
Netrc: netrc,
|
|
|
|
Secs: secs,
|
|
|
|
Regs: regs,
|
|
|
|
Envs: envs,
|
|
|
|
Link: server.Config.Server.Host,
|
|
|
|
Yamls: yamls,
|
|
|
|
}
|
|
|
|
buildItems, err := b.Build()
|
|
|
|
if err != nil {
|
|
|
|
if _, err := shared.UpdateToStatusError(store, *build, err); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error setting error status of build for %s#%d", repo.FullName, build.Number)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
|
|
|
|
|
|
|
return build, buildItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startBuild(ctx context.Context, store store.Store, build *model.Build, user *model.User, repo *model.Repo, buildItems []*shared.BuildItem) (*model.Build, error) {
|
|
|
|
if err := store.ProcCreate(build.Procs); err != nil {
|
|
|
|
log.Error().Err(err).Str("repo", repo.FullName).Msgf("error persisting procs for %s#%d", repo.FullName, build.Number)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-09 04:47:31 +02:00
|
|
|
if err := publishToTopic(ctx, build, repo); err != nil {
|
2021-12-28 18:02:49 +02:00
|
|
|
log.Error().Err(err).Msg("publishToTopic")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := queueBuild(build, repo, buildItems); err != nil {
|
|
|
|
log.Error().Err(err).Msg("queueBuild")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updateBuildStatus(ctx, build, repo, user); err != nil {
|
|
|
|
log.Error().Err(err).Msg("updateBuildStatus")
|
|
|
|
}
|
|
|
|
|
|
|
|
return build, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBuildStatus(ctx context.Context, build *model.Build, repo *model.Repo, user *model.User) error {
|
|
|
|
for _, proc := range build.Procs {
|
|
|
|
// skip child procs
|
|
|
|
if !proc.IsParent() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := server.Config.Services.Remote.Status(ctx, user, repo, build, proc)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("error setting commit status for %s/%d", repo.FullName, build.Number)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-05 18:54:44 +02:00
|
|
|
func persistBuildConfigs(store store.Store, configs []*model.Config, buildID int64) error {
|
2019-06-13 17:38:19 +02:00
|
|
|
for _, conf := range configs {
|
|
|
|
buildConfig := &model.BuildConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
BuildID: buildID,
|
|
|
|
}
|
2022-01-05 18:54:44 +02:00
|
|
|
err := store.BuildConfigCreate(buildConfig)
|
2019-06-13 17:38:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 19:26:28 +02:00
|
|
|
var deleteStr = `[
|
|
|
|
{
|
|
|
|
"proc": %q,
|
|
|
|
"pos": 0,
|
2018-01-18 20:08:11 +02:00
|
|
|
"out": "logs purged by %s on %s\n"
|
2018-01-18 19:26:28 +02:00
|
|
|
}
|
|
|
|
]`
|