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"
|
2017-03-05 11:05:16 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2016-09-28 01:30:28 +00:00
|
|
|
"io"
|
2016-03-31 06:24:47 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
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-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 20:29:09 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/queue"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-09-22 20:41:32 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
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 {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
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-10-28 09:12:58 +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 {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-28 09:12:58 +00:00
|
|
|
files, _ := store_.FileList(build)
|
|
|
|
procs, _ := store_.ProcList(build)
|
2017-04-02 14:13:26 +00:00
|
|
|
build.Procs = model.Tree(procs)
|
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-10-28 09:12:58 +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-10-28 09:12:58 +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-10-28 09:12:58 +00:00
|
|
|
procs, _ := store_.ProcList(build)
|
2017-04-02 14:13:26 +00:00
|
|
|
build.Procs = model.Tree(procs)
|
|
|
|
c.JSON(http.StatusOK, build)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBuildLogs(c *gin.Context) {
|
2021-10-28 09:12:58 +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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
proc, err := store_.ProcChild(build, ppid, name)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
rc, err := store_.LogFind(proc)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
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")
|
|
|
|
io.Copy(c.Writer, rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetProcLogs(c *gin.Context) {
|
2021-10-28 09:12:58 +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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
proc, err := store_.ProcFind(build, pid)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
rc, err := store_.LogFind(proc)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rc.Close()
|
|
|
|
|
2016-06-14 21:01:20 +00:00
|
|
|
c.Header("Content-Type", "application/json")
|
2017-04-02 14:13:26 +00:00
|
|
|
io.Copy(c.Writer, rc)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
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-10-28 09:12:58 +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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-09-24 14:29:26 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
procs, err := store_.ProcList(build)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2021-09-24 14:29:26 +00:00
|
|
|
_ = c.AbortWithError(404, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
2016-04-21 07:25:30 +00:00
|
|
|
|
2019-09-16 13:18:15 +00:00
|
|
|
if build.Status != model.StatusRunning && build.Status != model.StatusPending {
|
|
|
|
c.String(400, "Cannot cancel a non-running or non-pending build")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First cancel/evict procs in the queue in one go
|
2021-09-24 14:29:26 +00:00
|
|
|
var (
|
|
|
|
procToCancel []string
|
|
|
|
procToEvict []string
|
|
|
|
)
|
2019-06-19 06:36:13 +00:00
|
|
|
for _, proc := range procs {
|
|
|
|
if proc.PPID != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-16 13:18:15 +00:00
|
|
|
if proc.State == model.StatusRunning {
|
|
|
|
procToCancel = append(procToCancel, fmt.Sprint(proc.ID))
|
2019-06-19 06:36:13 +00:00
|
|
|
}
|
2019-09-16 13:18:15 +00:00
|
|
|
if proc.State == model.StatusPending {
|
|
|
|
procToEvict = append(procToEvict, fmt.Sprint(proc.ID))
|
2019-09-15 05:29:45 +00:00
|
|
|
}
|
2017-08-01 16:57:01 +00:00
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.EvictAtOnce(context.Background(), procToEvict)
|
|
|
|
server.Config.Services.Queue.ErrorAtOnce(context.Background(), procToEvict, queue.ErrCancel)
|
|
|
|
server.Config.Services.Queue.ErrorAtOnce(context.Background(), procToCancel, queue.ErrCancel)
|
2017-08-01 16:57:01 +00:00
|
|
|
|
2019-09-16 13:18:15 +00: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 16:57:01 +00:00
|
|
|
for _, proc := range procs {
|
2019-09-16 13:18:15 +00:00
|
|
|
if proc.State == model.StatusPending {
|
|
|
|
if proc.PPID != 0 {
|
2021-10-28 09:12:58 +00:00
|
|
|
if _, err = shared.UpdateProcToStatusSkipped(store_, *proc, 0); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
2019-09-16 13:18:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-28 09:12:58 +00:00
|
|
|
if _, err = shared.UpdateProcToStatusKilled(store_, *proc); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
2019-09-16 13:18:15 +00:00
|
|
|
}
|
2017-08-01 16:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
killedBuild, err := shared.UpdateToStatusKilled(store_, *build)
|
2019-09-16 13:18:15 +00:00
|
|
|
if err != nil {
|
2019-09-14 12:12:04 +00:00
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
2017-08-01 16:57:01 +00:00
|
|
|
|
2019-09-16 13:18:15 +00: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-10-28 09:12:58 +00:00
|
|
|
procs, err = store_.ProcList(killedBuild)
|
2019-09-16 13:18:15 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
killedBuild.Procs = model.Tree(procs)
|
|
|
|
publishToTopic(c, killedBuild, repo, model.Cancelled)
|
|
|
|
}
|
|
|
|
|
2017-08-01 16:57:01 +00:00
|
|
|
c.String(204, "")
|
|
|
|
}
|
|
|
|
|
2017-03-18 08:49:27 +00:00
|
|
|
func PostApproval(c *gin.Context) {
|
|
|
|
var (
|
2017-03-18 11:25:53 +00:00
|
|
|
remote_ = remote.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
store_ = store.FromContext(c)
|
2017-03-18 11:25:53 +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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2017-03-18 08:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if build.Status != model.StatusBlocked {
|
|
|
|
c.String(500, "cannot decline a build with status %s", build.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-18 11:25:53 +00:00
|
|
|
// fetch the build file from the database
|
2021-09-22 18:48:01 +00:00
|
|
|
configs, err := server.Config.Storage.Config.ConfigsForBuild(build.ID)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("failure to get build config for %s. %s", repo.FullName, err)
|
2017-03-18 11:25:53 +00:00
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netrc, err := remote_.Netrc(user, repo)
|
|
|
|
if err != nil {
|
2019-09-14 12:12:04 +00:00
|
|
|
c.String(500, "failed to generate netrc file. %s", err)
|
2017-03-18 11:25:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
if build, err = shared.UpdateToStatusPending(store_, *build, user.Login); err != nil {
|
2019-09-14 12:12:04 +00:00
|
|
|
c.String(500, "error updating build. %s", err)
|
2017-03-18 11:25:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, build)
|
|
|
|
|
|
|
|
// get the previous build so that we can send
|
|
|
|
// on status change notifications
|
2021-10-28 09:12:58 +00:00
|
|
|
last, _ := store_.GetBuildLastBefore(repo, build.Branch, build.ID)
|
2021-09-22 18:48:01 +00:00
|
|
|
secs, err := server.Config.Services.Secrets.SecretListBuild(repo, build)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err)
|
2017-03-18 11:25:53 +00:00
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo)
|
2017-04-06 16:04:25 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Error getting registry credentials for %s#%d. %s", repo.FullName, build.Number, err)
|
2017-04-06 16:04:25 +00:00
|
|
|
}
|
2017-06-26 19:27:53 +00:00
|
|
|
envs := map[string]string{}
|
2021-09-22 18:48:01 +00:00
|
|
|
if server.Config.Services.Environ != nil {
|
|
|
|
globals, _ := server.Config.Services.Environ.EnvironList(repo)
|
2017-06-26 19:27:53 +00:00
|
|
|
for _, global := range globals {
|
|
|
|
envs[global.Name] = global.Value
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 11:25:53 +00:00
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
var yamls []*remote.FileMeta
|
2019-06-07 08:40:16 +00:00
|
|
|
for _, y := range configs {
|
2021-11-13 19:18:06 +00:00
|
|
|
yamls = append(yamls, &remote.FileMeta{Data: y.Data, Name: y.Name})
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
b := shared.ProcBuilder{
|
2017-03-18 11:25:53 +00:00
|
|
|
Repo: repo,
|
|
|
|
Curr: build,
|
|
|
|
Last: last,
|
|
|
|
Netrc: netrc,
|
|
|
|
Secs: secs,
|
2017-04-06 16:04:25 +00:00
|
|
|
Regs: regs,
|
2021-09-22 18:48:01 +00:00
|
|
|
Link: server.Config.Server.Host,
|
2019-06-07 08:40:16 +00:00
|
|
|
Yamls: yamls,
|
2017-06-26 19:27:53 +00:00
|
|
|
Envs: envs,
|
2017-03-18 11:25:53 +00:00
|
|
|
}
|
2019-06-01 08:27:28 +00:00
|
|
|
buildItems, err := b.Build()
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
2021-10-28 09:12:58 +00:00
|
|
|
if _, err = shared.UpdateToStatusError(store_, *build, err); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("Error setting error status of build for %s#%d. %s", repo.FullName, build.Number, err)
|
2019-09-14 12:12:04 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
2017-03-18 08:49:27 +00:00
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
err = store_.ProcCreate(build.Procs)
|
2019-06-01 08:27:28 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error persisting procs %s/%d: %s", repo.FullName, build.Number, err)
|
2017-03-18 11:25:53 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
|
2019-06-17 08:48:40 +00:00
|
|
|
defer func() {
|
|
|
|
for _, item := range buildItems {
|
2021-09-22 18:48:01 +00:00
|
|
|
uri := fmt.Sprintf("%s/%s/%d", server.Config.Server.Host, repo.FullName, build.Number)
|
2019-06-17 08:48:40 +00:00
|
|
|
if len(buildItems) > 1 {
|
2021-09-28 10:56:59 +00:00
|
|
|
err = remote_.Status(c, user, repo, build, uri, item.Proc)
|
2019-06-17 08:48:40 +00:00
|
|
|
} else {
|
2021-09-28 10:56:59 +00:00
|
|
|
err = remote_.Status(c, user, repo, build, uri, nil)
|
2019-06-17 08:48:40 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error setting commit status for %s/%d: %v", repo.FullName, build.Number, err)
|
2019-06-17 08:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-09-16 13:18:15 +00:00
|
|
|
publishToTopic(c, build, repo, model.Enqueued)
|
2019-06-01 08:27:28 +00:00
|
|
|
queueBuild(build, repo, buildItems)
|
2017-03-18 08:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PostDecline(c *gin.Context) {
|
|
|
|
var (
|
2017-03-18 09:16:39 +00:00
|
|
|
remote_ = remote.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
store_ = store.FromContext(c)
|
|
|
|
|
|
|
|
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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2017-03-18 08:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if build.Status != model.StatusBlocked {
|
|
|
|
c.String(500, "cannot decline a build with status %s", build.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
if _, err = shared.UpdateToStatusDeclined(store_, *build, user.Login); err != nil {
|
2017-03-18 08:49:27 +00:00
|
|
|
c.String(500, "error updating build. %s", err)
|
|
|
|
return
|
|
|
|
}
|
2017-03-18 09:16:39 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
uri := fmt.Sprintf("%s/%s/%d", server.Config.Server.Host, repo.FullName, build.Number)
|
2021-09-28 10:56:59 +00:00
|
|
|
err = remote_.Status(c, user, repo, build, uri, nil)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error setting commit status for %s/%d: %v", repo.FullName, build.Number, err)
|
2017-03-18 09:16:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 08:49:27 +00:00
|
|
|
c.JSON(200, build)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-01 10:52:02 +00:00
|
|
|
// PostBuild restarts a build
|
2017-03-16 10:14:02 +00:00
|
|
|
func PostBuild(c *gin.Context) {
|
2017-03-14 15:56:22 +00:00
|
|
|
remote_ := remote.FromContext(c)
|
2021-10-28 09:12:58 +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 {
|
|
|
|
c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +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)
|
2017-03-14 15:56:22 +00:00
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +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)
|
2017-03-14 15:56:22 +00:00
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:33:12 +00:00
|
|
|
switch build.Status {
|
2018-04-05 12:53:28 +00:00
|
|
|
case model.StatusDeclined,
|
|
|
|
model.StatusBlocked:
|
2017-09-14 22:33:12 +00:00
|
|
|
c.String(500, "cannot restart a build with status %s", build.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 15:56:22 +00:00
|
|
|
// if the remote has a refresh token, the current access token
|
|
|
|
// may be stale. Therefore, we should refresh prior to dispatching
|
|
|
|
// the job.
|
|
|
|
if refresher, ok := remote_.(remote.Refresher); ok {
|
2021-09-28 10:56:59 +00:00
|
|
|
ok, _ := refresher.Refresh(c, user)
|
2017-03-14 15:56:22 +00:00
|
|
|
if ok {
|
2021-10-28 09:12:58 +00:00
|
|
|
store_.UpdateUser(user)
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 19:40:43 +00:00
|
|
|
// fetch the pipeline config from database
|
2021-09-22 18:48:01 +00:00
|
|
|
configs, err := server.Config.Storage.Config.ConfigsForBuild(build.ID)
|
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 config for %s. %s", repo.FullName, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
netrc, err := remote_.Netrc(user, repo)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("failure to generate netrc for %s. %s", repo.FullName, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-11 00:45:42 +00: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 00:43:33 +00:00
|
|
|
|
2017-09-11 00:45:42 +00:00
|
|
|
event := c.DefaultQuery("event", build.Event)
|
|
|
|
if event == model.EventPush ||
|
|
|
|
event == model.EventPull ||
|
|
|
|
event == model.EventTag ||
|
|
|
|
event == model.EventDeploy {
|
|
|
|
build.Event = event
|
|
|
|
}
|
2017-04-04 10:50:15 +00:00
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
err = store_.CreateBuild(build)
|
2017-09-11 00:45:42 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, err.Error())
|
|
|
|
return
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
err = persistBuildConfigs(configs, build.ID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("failure to persist build config for %s. %s", repo.FullName, err)
|
2019-06-13 15:38:19 +00:00
|
|
|
c.AbortWithError(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 15:56:22 +00:00
|
|
|
// Read query string parameters into buildParams, exclude reserved params
|
|
|
|
var buildParams = map[string]string{}
|
|
|
|
for key, val := range c.Request.URL.Query() {
|
|
|
|
switch key {
|
|
|
|
case "fork", "event", "deploy_to":
|
|
|
|
default:
|
|
|
|
// We only accept string literals, because build parameters will be
|
|
|
|
// injected as environment variables
|
|
|
|
buildParams[key] = val[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the previous build so that we can send
|
|
|
|
// on status change notifications
|
2021-10-28 09:12:58 +00:00
|
|
|
last, _ := store_.GetBuildLastBefore(repo, build.Branch, build.ID)
|
2021-09-22 18:48:01 +00:00
|
|
|
secs, err := server.Config.Services.Secrets.SecretListBuild(repo, build)
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Error getting secrets for %s#%d. %s", repo.FullName, build.Number, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
regs, err := server.Config.Services.Registries.RegistryList(repo)
|
2017-04-06 16:04:25 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("Error getting registry credentials for %s#%d. %s", repo.FullName, build.Number, err)
|
2017-04-06 16:04:25 +00:00
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
if server.Config.Services.Environ != nil {
|
|
|
|
globals, _ := server.Config.Services.Environ.EnvironList(repo)
|
2017-06-26 19:27:53 +00:00
|
|
|
for _, global := range globals {
|
|
|
|
buildParams[global.Name] = global.Value
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 15:56:22 +00:00
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
var yamls []*remote.FileMeta
|
2019-06-07 08:40:16 +00:00
|
|
|
for _, y := range configs {
|
2021-11-13 19:18:06 +00:00
|
|
|
yamls = append(yamls, &remote.FileMeta{Data: y.Data, Name: y.Name})
|
2019-06-07 08:40:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
b := shared.ProcBuilder{
|
2017-03-14 15:56:22 +00:00
|
|
|
Repo: repo,
|
|
|
|
Curr: build,
|
|
|
|
Last: last,
|
|
|
|
Netrc: netrc,
|
|
|
|
Secs: secs,
|
2017-04-06 16:04:25 +00:00
|
|
|
Regs: regs,
|
2021-09-22 18:48:01 +00:00
|
|
|
Link: server.Config.Server.Host,
|
2019-06-07 08:40:16 +00:00
|
|
|
Yamls: yamls,
|
2017-05-23 18:08:06 +00:00
|
|
|
Envs: buildParams,
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
2019-06-01 08:27:28 +00:00
|
|
|
buildItems, err := b.Build()
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
|
|
|
build.Status = model.StatusError
|
|
|
|
build.Started = time.Now().Unix()
|
|
|
|
build.Finished = build.Started
|
|
|
|
build.Error = err.Error()
|
2017-04-04 10:50:15 +00:00
|
|
|
c.JSON(500, build)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
build = shared.SetBuildStepsOnBuild(b.Curr, buildItems)
|
2017-03-14 15:56:22 +00:00
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
err = store_.ProcCreate(build.Procs)
|
2017-04-04 10:50:15 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("cannot restart %s#%d: %s", repo.FullName, build.Number, err)
|
2017-04-04 10:50:15 +00:00
|
|
|
build.Status = model.StatusError
|
|
|
|
build.Started = time.Now().Unix()
|
|
|
|
build.Finished = build.Started
|
|
|
|
build.Error = err.Error()
|
|
|
|
c.JSON(500, build)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(202, build)
|
|
|
|
|
2019-09-16 13:18:15 +00:00
|
|
|
publishToTopic(c, build, repo, model.Enqueued)
|
2019-06-01 08:27:28 +00:00
|
|
|
queueBuild(build, repo, buildItems)
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
2018-01-18 17:26:28 +00:00
|
|
|
|
|
|
|
func DeleteBuildLogs(c *gin.Context) {
|
2021-10-28 09:12:58 +00:00
|
|
|
store_ := store.FromContext(c)
|
|
|
|
|
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-10-28 09:12:58 +00:00
|
|
|
build, err := store_.GetBuildNumber(repo, num)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:12:58 +00:00
|
|
|
procs, err := store_.ProcList(build)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
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-10-28 09:12:58 +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, "")
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:38:19 +00:00
|
|
|
func persistBuildConfigs(configs []*model.Config, buildID int64) error {
|
|
|
|
for _, conf := range configs {
|
|
|
|
buildConfig := &model.BuildConfig{
|
|
|
|
ConfigID: conf.ID,
|
|
|
|
BuildID: buildID,
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
err := server.Config.Storage.Config.BuildConfigCreate(buildConfig)
|
2019-06-13 15:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 17:26:28 +00:00
|
|
|
var deleteStr = `[
|
|
|
|
{
|
|
|
|
"proc": %q,
|
|
|
|
"pos": 0,
|
2018-01-18 18:08:11 +00:00
|
|
|
"out": "logs purged by %s on %s\n"
|
2018-01-18 17:26:28 +00:00
|
|
|
}
|
|
|
|
]`
|