2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2021-06-28 17:28:18 +00:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2022-10-18 01:24:12 +00:00
|
|
|
// 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.
|
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 (
|
2022-09-27 09:05:00 +00:00
|
|
|
"encoding/json"
|
2022-10-25 23:23:28 +00:00
|
|
|
"errors"
|
2017-03-05 11:05:16 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
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"
|
2023-04-30 01:40:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store/types"
|
2016-03-31 06:24:47 +00:00
|
|
|
)
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// CreatePipeline
|
|
|
|
//
|
|
|
|
// @Summary Run/trigger a pipelines
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Pipeline
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Tags Pipelines
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param options body PipelineOptions true "the options for the pipeline to run"
|
2022-10-18 01:24:12 +00:00
|
|
|
func CreatePipeline(c *gin.Context) {
|
2022-09-27 09:05:00 +00:00
|
|
|
_store := store.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
2023-04-30 15:02:47 +00:00
|
|
|
// parse create options
|
|
|
|
var opts model.PipelineOptions
|
|
|
|
err := json.NewDecoder(c.Request.Body).Decode(&opts)
|
2022-09-27 09:05:00 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := session.User(c)
|
|
|
|
|
2023-04-30 15:02:47 +00:00
|
|
|
lastCommit, _ := server.Config.Services.Forge.BranchHead(c, user, repo, opts.Branch)
|
2022-09-27 09:05:00 +00:00
|
|
|
|
2023-04-30 15:02:47 +00:00
|
|
|
tmpBuild := createTmpPipeline(model.EventManual, lastCommit, repo, user, &opts)
|
2022-09-27 09:05:00 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := pipeline.Create(c, _store, repo, tmpBuild)
|
2022-09-27 09:05:00 +00:00
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2022-10-18 01:24:12 +00:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2022-09-27 09:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func createTmpPipeline(event model.WebhookEvent, commitSHA string, repo *model.Repo, user *model.User, opts *model.PipelineOptions) *model.Pipeline {
|
|
|
|
return &model.Pipeline{
|
2022-09-27 09:05:00 +00:00
|
|
|
Event: event,
|
|
|
|
Commit: commitSHA,
|
|
|
|
Branch: opts.Branch,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
|
|
|
|
Avatar: user.Avatar,
|
2022-10-18 01:24:12 +00:00
|
|
|
Message: "MANUAL PIPELINE @ " + opts.Branch,
|
2022-09-27 09:05:00 +00:00
|
|
|
|
|
|
|
Ref: opts.Branch,
|
|
|
|
AdditionalVariables: opts.Variables,
|
|
|
|
|
|
|
|
Author: user.Login,
|
|
|
|
Email: user.Email,
|
|
|
|
|
|
|
|
// TODO: Generate proper link to commit
|
|
|
|
Link: repo.Link,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetPipelines
|
|
|
|
//
|
|
|
|
// @Summary Get pipelines, current running and past ones
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Pipeline
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param page query int false "for response pagination, page offset number" default(1)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param perPage query int false "for response pagination, max items per page" default(50)
|
2022-10-18 01:24:12 +00:00
|
|
|
func GetPipelines(c *gin.Context) {
|
2016-03-31 06:24:47 +00:00
|
|
|
repo := session.Repo(c)
|
2018-02-02 19:04:21 +00:00
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
pipelines, err := store.FromContext(c).GetPipelineList(repo, session.Pagination(c))
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2022-10-25 23:23:28 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2023-03-17 01:10:51 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
c.JSON(http.StatusOK, pipelines)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetPipeline
|
|
|
|
//
|
|
|
|
// @Summary Pipeline information by number
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number} [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Pipeline
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
|
|
|
// @Param number path int true "the number of the pipeline, OR 'latest'"
|
2022-10-18 01:24:12 +00:00
|
|
|
func GetPipeline(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" {
|
2022-10-18 01:24:12 +00:00
|
|
|
GetPipelineLast(c)
|
2016-03-31 06:24:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-03-17 01:10:51 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-27 16:01:18 +00:00
|
|
|
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
|
2021-12-08 22:36:23 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-31 06:24:47 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func GetPipelineLast(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)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineLast(repo, branch)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-27 16:01:18 +00:00
|
|
|
if pl.Workflows, err = _store.WorkflowGetTree(pl); err != nil {
|
2021-12-08 22:36:23 +00:00
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
// GetStepLogs
|
2023-06-03 19:38:36 +00:00
|
|
|
//
|
2023-06-06 07:52:08 +00:00
|
|
|
// @Summary Log information
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/logs/{number}/{stepID} [get]
|
2023-06-06 07:52:08 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} LogEntry
|
|
|
|
// @Tags Pipeline logs
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-06 07:52:08 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
|
|
|
// @Param stepID path int true "the step id"
|
|
|
|
func GetStepLogs(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)
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
// parse the pipeline number and step sequence number from
|
2021-09-22 18:48:01 +00:00
|
|
|
// the request parameter.
|
2023-06-06 07:52:08 +00:00
|
|
|
num, err := strconv.ParseInt(c.Params.ByName("number"), 10, 64)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-06-06 07:52:08 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
stepID, err := strconv.ParseInt(c.Params.ByName("stepId"), 10, 64)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-06-06 07:52:08 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
step, err := _store.StepLoad(stepID)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2017-08-25 00:03:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
if step.PipelineID != pl.ID {
|
|
|
|
// make sure we can not read arbitrary logs by id
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("step with id %d is not part of repo %s", stepID, repo.FullName))
|
2017-08-25 00:03:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
logs, err := _store.LogFind(step)
|
2017-08-25 00:03:11 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2017-08-25 00:03:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:52:08 +00:00
|
|
|
c.JSON(http.StatusOK, logs)
|
2016-03-31 06:24:47 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetPipelineConfig
|
|
|
|
//
|
|
|
|
// @Summary Pipeline configuration
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number}/config [get]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Config
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Tags Pipelines
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
2022-10-18 01:24:12 +00:00
|
|
|
func GetPipelineConfig(c *gin.Context) {
|
2022-01-09 02:59:45 +00:00
|
|
|
_store := store.FromContext(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
num, err := strconv.ParseInt(c.Param("number"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2022-01-09 02:59:45 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2022-01-09 02:59:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
configs, err := _store.ConfigsForPipeline(pl.ID)
|
2022-01-09 02:59:45 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, configs)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// CancelPipeline
|
|
|
|
//
|
|
|
|
// @Summary Cancels a pipeline
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number}/cancel [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce plain
|
|
|
|
// @Success 200
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
2022-10-30 13:39:01 +00:00
|
|
|
func CancelPipeline(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)
|
2023-05-14 12:18:43 +00:00
|
|
|
user := session.User(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
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2016-03-31 06:24:47 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2016-03-31 06:24:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-14 12:18:43 +00:00
|
|
|
if err := pipeline.Cancel(c, _store, repo, user, pl); err != nil {
|
2022-06-15 19:33:29 +00:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.Status(http.StatusNoContent)
|
2019-09-16 13:18:15 +00:00
|
|
|
}
|
2017-08-01 16:57:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostApproval
|
|
|
|
//
|
|
|
|
// @Summary Start pipelines in gated repos
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number}/approve [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Pipeline
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
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
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-03-18 08:49:27 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2017-03-18 08:49:27 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-18 11:25:53 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
newpipeline, err := pipeline.Approve(c, _store, pl, 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 {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, newpipeline)
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostDecline
|
|
|
|
//
|
|
|
|
// @Summary Decline pipelines in gated repos
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number}/decline [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Pipeline
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
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
|
|
|
)
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(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-10-18 01:24:12 +00:00
|
|
|
pl, err = pipeline.Decline(c, _store, pl, user, repo)
|
2022-06-15 19:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2022-01-09 02:47:31 +00:00
|
|
|
}
|
2017-03-18 08:49:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetPipelineQueue
|
|
|
|
//
|
|
|
|
// @Summary List pipeline queues
|
|
|
|
// @Router /pipelines [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} Feed
|
|
|
|
// @Tags Pipeline queues
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2022-10-18 01:24:12 +00:00
|
|
|
func GetPipelineQueue(c *gin.Context) {
|
|
|
|
out, err := store.FromContext(c).GetPipelineQueue()
|
2016-05-11 05:19:45 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Error getting pipeline queue. %s", err)
|
2016-05-11 05:19:45 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, out)
|
2016-05-11 05:19:45 +00:00
|
|
|
}
|
2016-09-28 01:30:28 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostPipeline
|
|
|
|
//
|
|
|
|
// @Summary Restart a pipeline
|
|
|
|
// @Description Restarts a pipeline optional with altered event, deploy or environment
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/pipelines/{number} [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} Pipeline
|
|
|
|
// @Tags Pipelines
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param owner path string true "the repository owner's name"
|
|
|
|
// @Param name path string true "the repository name"
|
|
|
|
// @Param number path int true "the number of the pipeline"
|
|
|
|
// @Param event query string false "override the event type"
|
|
|
|
// @Param deploy_to query string false "override the target deploy value"
|
2022-10-18 01:24:12 +00:00
|
|
|
func PostPipeline(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 {
|
2023-03-19 12:52:58 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2017-03-14 15:56:22 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
if errors.Is(err, types.RecordNotExist) {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
2017-03-14 15:56:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:12:17 +00:00
|
|
|
// refresh the token to make sure, pipeline.ReStart can still obtain the pipeline config if necessary again
|
2022-06-15 19:33:29 +00:00
|
|
|
refreshUserToken(c, user)
|
2017-03-14 15:56:22 +00:00
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// make Deploy overridable
|
2022-10-18 01:24:12 +00:00
|
|
|
pl.Deploy = c.DefaultQuery("deploy_to", pl.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 {
|
2022-10-18 01:24:12 +00:00
|
|
|
pl.Event = model.WebhookEvent(event)
|
2021-12-28 16:02:49 +00:00
|
|
|
|
2023-04-30 15:02:47 +00:00
|
|
|
if err := model.ValidateWebhookEvent(pl.Event); err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2021-12-28 16:02:49 +00:00
|
|
|
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
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// 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 {
|
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:
|
2022-10-18 01:24:12 +00:00
|
|
|
// We only accept string literals, because pipeline parameters will be
|
2017-03-14 15:56:22 +00:00
|
|
|
// 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-10-18 01:24:12 +00:00
|
|
|
newpipeline, err := pipeline.Restart(c, _store, pl, 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 {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, newpipeline)
|
2017-03-14 15:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-18 17:26:28 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// DeletePipelineLogs
|
|
|
|
//
|
|
|
|
// @Summary Deletes log
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Router /repos/{repo_id}/logs/{number} [post]
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Produce plain
|
|
|
|
// @Success 200
|
|
|
|
// @Tags Pipeline logs
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-12 23:07:52 +00:00
|
|
|
// @Param repo_id path int true "the repository id"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param number path int true "the number of the pipeline"
|
2022-10-18 01:24:12 +00:00
|
|
|
func DeletePipelineLogs(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)
|
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
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pl, err := _store.GetPipelineNumber(repo, num)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
handleDbGetError(c, err)
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
steps, err := _store.StepList(pl)
|
2018-01-18 17:26:28 +00:00
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
_ = c.AbortWithError(http.StatusNotFound, err)
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
switch pl.Status {
|
2018-01-18 17:26:28 +00:00
|
|
|
case model.StatusRunning, model.StatusPending:
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusUnprocessableEntity, "Cannot delete logs for a pending or running pipeline")
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
for _, step := range steps {
|
2023-06-06 07:52:08 +00:00
|
|
|
if lErr := _store.LogDelete(step); err != nil {
|
|
|
|
err = errors.Join(err, lErr)
|
2018-01-18 17:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, "There was a problem deleting your logs. %s", err)
|
2018-01-18 17:26:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusNoContent, "")
|
2018-01-18 17:26:28 +00:00
|
|
|
}
|