2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2021-06-28 19:28:18 +02:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
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
|
2015-04-09 00:43:59 +02:00
|
|
|
|
|
|
|
import (
|
2023-07-14 02:03:54 +02:00
|
|
|
"errors"
|
2015-09-30 03:21:17 +02:00
|
|
|
"fmt"
|
2019-06-28 08:29:57 +02:00
|
|
|
"net/http"
|
2016-01-11 01:19:48 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-03-16 12:14:02 +02:00
|
|
|
|
2023-12-08 09:15:08 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/token"
|
2015-04-09 00:43:59 +02:00
|
|
|
)
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// GetQueueInfo
|
|
|
|
//
|
2023-12-24 16:50:01 +02:00
|
|
|
// @Summary Get pipeline queue information
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Description TODO: link the InfoT response object - this is blocked, until the `swaggo/swag` tool dependency is v1.18.12 or newer
|
2023-12-24 16:50:01 +02:00
|
|
|
// @Router /queue/info [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} map[string]string
|
|
|
|
// @Tags Pipeline queues
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2017-03-16 12:14:02 +02:00
|
|
|
func GetQueueInfo(c *gin.Context) {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.IndentedJSON(http.StatusOK,
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Services.Queue.Info(c),
|
2017-03-16 12:14:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// PauseQueue
|
|
|
|
//
|
|
|
|
// @Summary Pause a pipeline queue
|
|
|
|
// @Router /queue/pause [post]
|
|
|
|
// @Produce plain
|
2023-10-28 13:37:54 +02:00
|
|
|
// @Success 204
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Tags Pipeline queues
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2019-06-28 08:29:57 +02:00
|
|
|
func PauseQueue(c *gin.Context) {
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Services.Queue.Pause()
|
2023-10-28 13:37:54 +02:00
|
|
|
c.Status(http.StatusNoContent)
|
2019-06-28 08:29:57 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// ResumeQueue
|
|
|
|
//
|
|
|
|
// @Summary Resume a pipeline queue
|
|
|
|
// @Router /queue/resume [post]
|
|
|
|
// @Produce plain
|
2023-10-28 13:37:54 +02:00
|
|
|
// @Success 204
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Tags Pipeline queues
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2019-06-28 08:29:57 +02:00
|
|
|
func ResumeQueue(c *gin.Context) {
|
2021-09-22 20:48:01 +02:00
|
|
|
server.Config.Services.Queue.Resume()
|
2023-10-28 13:37:54 +02:00
|
|
|
c.Status(http.StatusNoContent)
|
2019-06-28 08:29:57 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// BlockTilQueueHasRunningItem
|
|
|
|
//
|
|
|
|
// @Summary Block til pipeline queue has a running item
|
|
|
|
// @Router /queue/norunningpipelines [get]
|
|
|
|
// @Produce plain
|
2023-10-28 13:37:54 +02:00
|
|
|
// @Success 204
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Tags Pipeline queues
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2019-06-28 08:42:06 +02:00
|
|
|
func BlockTilQueueHasRunningItem(c *gin.Context) {
|
|
|
|
for {
|
2021-09-22 20:48:01 +02:00
|
|
|
info := server.Config.Services.Queue.Info(c)
|
2019-06-28 08:42:06 +02:00
|
|
|
if info.Stats.Running == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-10-28 13:37:54 +02:00
|
|
|
c.Status(http.StatusNoContent)
|
2019-06-28 08:42:06 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// PostHook
|
|
|
|
//
|
|
|
|
// @Summary Incoming webhook from forge
|
|
|
|
// @Router /hook [post]
|
|
|
|
// @Produce plain
|
|
|
|
// @Success 200
|
|
|
|
// @Tags System
|
|
|
|
// @Param hook body object true "the webhook payload; forge is automatically detected"
|
2015-04-09 00:43:59 +02:00
|
|
|
func PostHook(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2022-11-05 01:35:06 +02:00
|
|
|
forge := server.Config.Services.Forge
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 1. Parse webhook
|
|
|
|
//
|
|
|
|
|
2023-08-16 02:42:37 +02:00
|
|
|
tmpRepo, tmpPipeline, err := forge.Hook(c, c.Request)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2023-07-14 02:03:54 +02:00
|
|
|
if errors.Is(err, &types.ErrIgnoreEvent{}) {
|
|
|
|
msg := fmt.Sprintf("forge driver: %s", err)
|
|
|
|
log.Debug().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusOK, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-18 01:09:09 +02:00
|
|
|
msg := "failure to parse hook"
|
|
|
|
log.Debug().Err(err).Msg(msg)
|
2021-12-18 15:58:01 +02:00
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2023-07-14 02:03:54 +02:00
|
|
|
|
2023-08-16 02:42:37 +02:00
|
|
|
if tmpPipeline == nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
msg := "ignoring hook: hook parsing resulted in empty pipeline"
|
2021-12-18 15:58:01 +02:00
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusOK, msg)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2021-09-27 23:32:08 +02:00
|
|
|
if tmpRepo == nil {
|
2021-12-18 15:58:01 +02:00
|
|
|
msg := "failure to ascertain repo from hook"
|
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 2. Get related repo from store and take repo renaming into account
|
|
|
|
//
|
|
|
|
|
2022-11-15 16:01:23 +02:00
|
|
|
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2023-09-02 13:31:10 +02:00
|
|
|
log.Error().Err(err).Msgf("failure to get repo %s from store", tmpRepo.FullName)
|
|
|
|
handleDbError(c, err)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2017-07-14 21:58:38 +02:00
|
|
|
if !repo.IsActive {
|
2023-09-02 13:31:10 +02:00
|
|
|
log.Debug().Msgf("ignoring hook: repo %s is inactive", tmpRepo.FullName)
|
|
|
|
c.Status(http.StatusNoContent)
|
2017-07-14 21:58:38 +02:00
|
|
|
return
|
|
|
|
}
|
2022-09-05 17:08:51 +02:00
|
|
|
oldFullName := repo.FullName
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
if repo.UserID == 0 {
|
2023-09-02 13:31:10 +02:00
|
|
|
log.Warn().Msgf("ignoring hook. repo %s has no owner.", repo.FullName)
|
|
|
|
c.Status(http.StatusNoContent)
|
2022-09-05 17:08:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 3. Check if the webhook is a valid and authorized one
|
|
|
|
//
|
|
|
|
|
2015-08-11 10:36:07 +02:00
|
|
|
// get the token and verify the hook is authorized
|
2021-12-18 01:09:09 +02:00
|
|
|
parsed, err := token.ParseRequest(c.Request, func(_ *token.Token) (string, error) {
|
2015-09-09 23:05:52 +02:00
|
|
|
return repo.Hash, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-12-18 01:09:09 +02:00
|
|
|
msg := fmt.Sprintf("failure to parse token from hook for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
2021-12-18 15:58:01 +02:00
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-09-09 23:05:52 +02:00
|
|
|
return
|
|
|
|
}
|
2022-09-05 17:08:51 +02:00
|
|
|
verifiedKey := parsed.Text == oldFullName
|
|
|
|
if !verifiedKey {
|
|
|
|
verifiedKey, err = _store.HasRedirectionForRepo(repo.ID, repo.FullName)
|
|
|
|
if err != nil {
|
|
|
|
msg := "failure to verify token from hook. Could not check for redirections of the repo"
|
|
|
|
log.Error().Err(err).Msg(msg)
|
|
|
|
c.String(http.StatusInternalServerError, msg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verifiedKey {
|
2021-12-18 01:09:09 +02:00
|
|
|
msg := fmt.Sprintf("failure to verify token from hook. Expected %s, got %s", repo.FullName, parsed.Text)
|
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusForbidden, msg)
|
2015-08-11 10:36:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 4. Update repo
|
|
|
|
//
|
|
|
|
|
|
|
|
if oldFullName != tmpRepo.FullName {
|
|
|
|
// create a redirection
|
|
|
|
err = _store.CreateRedirection(&model.Redirection{RepoID: repo.ID, FullName: repo.FullName})
|
|
|
|
if err != nil {
|
|
|
|
_ = c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Update(tmpRepo)
|
|
|
|
err = _store.UpdateRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2015-04-12 00:46:30 +02:00
|
|
|
return
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 5. Check if pull requests are allowed for this repo
|
|
|
|
//
|
|
|
|
|
2023-08-16 02:42:37 +02:00
|
|
|
if tmpPipeline.Event == model.EventPull && !repo.AllowPull {
|
2023-09-02 13:31:10 +02:00
|
|
|
log.Debug().Str("repo", repo.FullName).Msg("ignoring hook: pull requests are disabled for this repo in woodpecker")
|
|
|
|
c.Status(http.StatusNoContent)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:15:44 +02:00
|
|
|
//
|
|
|
|
// 6. Finally create a pipeline
|
|
|
|
//
|
|
|
|
|
2023-08-16 02:42:37 +02:00
|
|
|
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2022-06-15 21:33:29 +02:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.JSON(http.StatusOK, pl)
|
2019-06-11 10:50:50 +02:00
|
|
|
}
|
2017-05-05 18:59:37 +02:00
|
|
|
}
|