mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 11:51:02 +00:00
Restructure webhook parsing (#2221)
Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
parent
0563d7a30f
commit
c2448b1745
1 changed files with 46 additions and 17 deletions
|
@ -108,6 +108,10 @@ func PostHook(c *gin.Context) {
|
||||||
_store := store.FromContext(c)
|
_store := store.FromContext(c)
|
||||||
forge := server.Config.Services.Forge
|
forge := server.Config.Services.Forge
|
||||||
|
|
||||||
|
//
|
||||||
|
// 1. Parse webhook
|
||||||
|
//
|
||||||
|
|
||||||
tmpRepo, tmpPipeline, err := forge.Hook(c, c.Request)
|
tmpRepo, tmpPipeline, err := forge.Hook(c, c.Request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, &types.ErrIgnoreEvent{}) {
|
if errors.Is(err, &types.ErrIgnoreEvent{}) {
|
||||||
|
@ -136,6 +140,11 @@ func PostHook(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Skip if commit message contains skip-ci
|
||||||
|
// TODO: move into global pipeline conditions logic
|
||||||
|
//
|
||||||
|
|
||||||
// skip the tmpPipeline if any case-insensitive combination of the words "skip" and "ci"
|
// skip the tmpPipeline if any case-insensitive combination of the words "skip" and "ci"
|
||||||
// wrapped in square brackets appear in the commit message
|
// wrapped in square brackets appear in the commit message
|
||||||
skipMatch := skipRe.FindString(tmpPipeline.Message)
|
skipMatch := skipRe.FindString(tmpPipeline.Message)
|
||||||
|
@ -146,6 +155,10 @@ func PostHook(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 2. Get related repo from store and take repo renaming into account
|
||||||
|
//
|
||||||
|
|
||||||
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := fmt.Sprintf("failure to get repo %s from store", tmpRepo.FullName)
|
msg := fmt.Sprintf("failure to get repo %s from store", tmpRepo.FullName)
|
||||||
|
@ -159,24 +172,19 @@ func PostHook(c *gin.Context) {
|
||||||
c.String(http.StatusNoContent, msg)
|
c.String(http.StatusNoContent, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
oldFullName := repo.FullName
|
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)
|
if repo.UserID == 0 {
|
||||||
err = _store.UpdateRepo(repo)
|
msg := fmt.Sprintf("ignoring hook. repo %s has no owner.", repo.FullName)
|
||||||
if err != nil {
|
log.Warn().Msg(msg)
|
||||||
c.String(http.StatusInternalServerError, err.Error())
|
c.String(http.StatusNoContent, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 3. Check if the webhook is a valid and authorized one
|
||||||
|
//
|
||||||
|
|
||||||
// get the token and verify the hook is authorized
|
// get the token and verify the hook is authorized
|
||||||
parsed, err := token.ParseRequest(c.Request, func(_ *token.Token) (string, error) {
|
parsed, err := token.ParseRequest(c.Request, func(_ *token.Token) (string, error) {
|
||||||
return repo.Hash, nil
|
return repo.Hash, nil
|
||||||
|
@ -205,13 +213,30 @@ func PostHook(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if repo.UserID == 0 {
|
//
|
||||||
msg := fmt.Sprintf("ignoring hook. repo %s has no owner.", repo.FullName)
|
// 4. Update repo
|
||||||
log.Warn().Msg(msg)
|
//
|
||||||
c.String(http.StatusNoContent, msg)
|
|
||||||
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 5. Check if pull requests are allowed for this repo
|
||||||
|
//
|
||||||
|
|
||||||
if tmpPipeline.Event == model.EventPull && !repo.AllowPull {
|
if tmpPipeline.Event == model.EventPull && !repo.AllowPull {
|
||||||
msg := "ignoring hook: pull requests are disabled for this repo in woodpecker"
|
msg := "ignoring hook: pull requests are disabled for this repo in woodpecker"
|
||||||
log.Debug().Str("repo", repo.FullName).Msg(msg)
|
log.Debug().Str("repo", repo.FullName).Msg(msg)
|
||||||
|
@ -219,6 +244,10 @@ func PostHook(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// 6. Finally create a pipeline
|
||||||
|
//
|
||||||
|
|
||||||
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
|
pl, err := pipeline.Create(c, _store, repo, tmpPipeline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handlePipelineErr(c, err)
|
handlePipelineErr(c, err)
|
||||||
|
|
Loading…
Reference in a new issue