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
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
2017-05-12 13:02:24 +00:00
|
|
|
"math/rand"
|
2019-06-28 06:29:57 +00:00
|
|
|
"net/http"
|
2017-03-16 10:14:02 +00:00
|
|
|
"regexp"
|
|
|
|
"time"
|
2016-01-10 23:19:48 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-03-16 10:14:02 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-10-12 07:25:13 +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-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/token"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
var skipRe = regexp.MustCompile(`\[(?i:ci *skip|skip *ci)\]`)
|
|
|
|
|
2017-05-12 13:02:24 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
func GetQueueInfo(c *gin.Context) {
|
|
|
|
c.IndentedJSON(200,
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Info(c),
|
2017-03-16 10:14:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:29:57 +00:00
|
|
|
func PauseQueue(c *gin.Context) {
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Pause()
|
2019-06-28 06:29:57 +00:00
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResumeQueue(c *gin.Context) {
|
2021-09-22 18:48:01 +00:00
|
|
|
server.Config.Services.Queue.Resume()
|
2019-06-28 06:29:57 +00:00
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:42:06 +00:00
|
|
|
func BlockTilQueueHasRunningItem(c *gin.Context) {
|
|
|
|
for {
|
2021-09-22 18:48:01 +00:00
|
|
|
info := server.Config.Services.Queue.Info(c)
|
2019-06-28 06:42:06 +00:00
|
|
|
if info.Stats.Running == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Status(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// PostHook start a pipeline triggered by a forges post webhook
|
2015-04-08 22:43:59 +00:00
|
|
|
func PostHook(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2022-09-05 15:08:51 +00:00
|
|
|
remote := server.Config.Services.Remote
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
tmpRepo, tmpBuild, err := remote.Hook(c, c.Request)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2021-12-17 23:09:09 +00:00
|
|
|
msg := "failure to parse hook"
|
|
|
|
log.Debug().Err(err).Msg(msg)
|
2021-12-18 13:58:01 +00:00
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-15 19:33:29 +00:00
|
|
|
if tmpBuild == nil {
|
2021-12-18 13:58:01 +00:00
|
|
|
msg := "ignoring hook: hook parsing resulted in empty build"
|
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusOK, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-27 21:32:08 +00:00
|
|
|
if tmpRepo == nil {
|
2021-12-18 13:58:01 +00:00
|
|
|
msg := "failure to ascertain repo from hook"
|
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
// skip the tmpBuild if any case-insensitive combination of the words "skip" and "ci"
|
2015-12-11 00:44:14 +00:00
|
|
|
// wrapped in square brackets appear in the commit message
|
2022-06-15 19:33:29 +00:00
|
|
|
skipMatch := skipRe.FindString(tmpBuild.Message)
|
2015-12-11 01:12:27 +00:00
|
|
|
if len(skipMatch) > 0 {
|
2022-06-15 19:33:29 +00:00
|
|
|
msg := fmt.Sprintf("ignoring hook: %s found in %s", skipMatch, tmpBuild.Commit)
|
2021-12-17 23:09:09 +00:00
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusNoContent, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
repo, err := _store.GetRepoNameFallback(tmpRepo.RemoteID, tmpRepo.FullName)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2021-12-18 13:58:01 +00:00
|
|
|
msg := fmt.Sprintf("failure to get repo %s from store", tmpRepo.FullName)
|
2021-12-17 23:09:09 +00:00
|
|
|
log.Error().Err(err).Msg(msg)
|
2021-12-18 13:58:01 +00:00
|
|
|
c.String(http.StatusNotFound, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
if !repo.IsActive {
|
2021-12-18 13:58:01 +00:00
|
|
|
msg := fmt.Sprintf("ignoring hook: repo %s is inactive", tmpRepo.FullName)
|
|
|
|
log.Debug().Msg(msg)
|
|
|
|
c.String(http.StatusNoContent, msg)
|
2017-07-14 19:58:38 +00:00
|
|
|
return
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2022-09-05 15:08:51 +00:00
|
|
|
oldFullName := repo.FullName
|
|
|
|
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())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-11 08:36:07 +00:00
|
|
|
// get the token and verify the hook is authorized
|
2021-12-17 23:09:09 +00:00
|
|
|
parsed, err := token.ParseRequest(c.Request, func(_ *token.Token) (string, error) {
|
2015-09-09 21:05:52 +00:00
|
|
|
return repo.Hash, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-12-17 23:09:09 +00:00
|
|
|
msg := fmt.Sprintf("failure to parse token from hook for %s", repo.FullName)
|
|
|
|
log.Error().Err(err).Msg(msg)
|
2021-12-18 13:58:01 +00:00
|
|
|
c.String(http.StatusBadRequest, msg)
|
2015-09-09 21:05:52 +00:00
|
|
|
return
|
|
|
|
}
|
2022-09-05 15:08:51 +00: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-17 23:09:09 +00: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 08:36:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
if repo.UserID == 0 {
|
2021-12-17 23:09:09 +00:00
|
|
|
msg := fmt.Sprintf("ignoring hook. repo %s has no owner.", repo.FullName)
|
|
|
|
log.Warn().Msg(msg)
|
|
|
|
c.String(http.StatusNoContent, msg)
|
2015-04-11 22:46:30 +00:00
|
|
|
return
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
if tmpBuild.Event == model.EventPull && !repo.AllowPull {
|
2021-12-17 23:09:09 +00:00
|
|
|
msg := "ignoring hook: pull requests are disabled for this repo in woodpecker"
|
|
|
|
log.Debug().Str("repo", repo.FullName).Msg(msg)
|
|
|
|
c.String(http.StatusNoContent, msg)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 19:33:29 +00:00
|
|
|
build, err := pipeline.Create(c, _store, repo, tmpBuild)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2022-06-15 19:33:29 +00:00
|
|
|
handlePipelineErr(c, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, build)
|
2019-06-11 08:50:50 +00:00
|
|
|
}
|
2017-05-05 16:59:37 +00:00
|
|
|
}
|