mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-02 21:58:43 +00:00
Some small code refactorings (#1727)
Refactorings taken from pull requests #1722 and #1725
This commit is contained in:
parent
54d4ec04c0
commit
f3074ddaf9
6 changed files with 26 additions and 15 deletions
|
@ -43,9 +43,9 @@ func CreatePipeline(c *gin.Context) {
|
|||
_store := store.FromContext(c)
|
||||
repo := session.Repo(c)
|
||||
|
||||
var p model.PipelineOptions
|
||||
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&p)
|
||||
// parse create options
|
||||
var opts model.PipelineOptions
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&opts)
|
||||
if err != nil {
|
||||
_ = c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
|
@ -53,9 +53,9 @@ func CreatePipeline(c *gin.Context) {
|
|||
|
||||
user := session.User(c)
|
||||
|
||||
lastCommit, _ := server.Config.Services.Forge.BranchHead(c, user, repo, p.Branch)
|
||||
lastCommit, _ := server.Config.Services.Forge.BranchHead(c, user, repo, opts.Branch)
|
||||
|
||||
tmpBuild := createTmpPipeline(model.EventManual, lastCommit, repo, user, &p)
|
||||
tmpBuild := createTmpPipeline(model.EventManual, lastCommit, repo, user, &opts)
|
||||
|
||||
pl, err := pipeline.Create(c, _store, repo, tmpBuild)
|
||||
if err != nil {
|
||||
|
@ -368,9 +368,8 @@ func PostPipeline(c *gin.Context) {
|
|||
if event, ok := c.GetQuery("event"); ok {
|
||||
pl.Event = model.WebhookEvent(event)
|
||||
|
||||
if !model.ValidateWebhookEvent(pl.Event) {
|
||||
msg := fmt.Sprintf("pipeline event '%s' is invalid", event)
|
||||
c.String(http.StatusBadRequest, msg)
|
||||
if err := model.ValidateWebhookEvent(pl.Event); err != nil {
|
||||
_ = c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ type configFetcher struct {
|
|||
repo *model.Repo
|
||||
pipeline *model.Pipeline
|
||||
configExtension config.Extension
|
||||
configPath string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
|
@ -49,6 +50,7 @@ func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config
|
|||
repo: repo,
|
||||
pipeline: pipeline,
|
||||
configExtension: configExtension,
|
||||
configPath: repo.Config,
|
||||
timeout: timeout,
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +61,7 @@ func (cf *configFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, er
|
|||
|
||||
// try to fetch 3 times
|
||||
for i := 0; i < 3; i++ {
|
||||
files, err = cf.fetch(ctx, time.Second*cf.timeout, strings.TrimSpace(cf.repo.Config))
|
||||
files, err = cf.fetch(ctx, time.Second*cf.timeout, strings.TrimSpace(cf.configPath))
|
||||
if err != nil {
|
||||
log.Trace().Err(err).Msgf("%d. try failed", i+1)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,11 @@
|
|||
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type WebhookEvent string
|
||||
|
||||
const (
|
||||
|
@ -32,12 +37,14 @@ func (wel WebhookEventList) Len() int { return len(wel) }
|
|||
func (wel WebhookEventList) Swap(i, j int) { wel[i], wel[j] = wel[j], wel[i] }
|
||||
func (wel WebhookEventList) Less(i, j int) bool { return wel[i] < wel[j] }
|
||||
|
||||
func ValidateWebhookEvent(s WebhookEvent) bool {
|
||||
var ErrInvalidWebhookEvent = errors.New("invalid webhook event")
|
||||
|
||||
func ValidateWebhookEvent(s WebhookEvent) error {
|
||||
switch s {
|
||||
case EventPush, EventPull, EventTag, EventDeploy, EventCron, EventManual:
|
||||
return true
|
||||
return nil
|
||||
default:
|
||||
return false
|
||||
return fmt.Errorf("%w: %s", ErrInvalidWebhookEvent, s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -125,8 +125,8 @@ var validDockerImageString = regexp.MustCompile(
|
|||
// Validate validates the required fields and formats.
|
||||
func (s *Secret) Validate() error {
|
||||
for _, event := range s.Events {
|
||||
if !ValidateWebhookEvent(event) {
|
||||
return fmt.Errorf("%w: '%s'", ErrSecretEventInvalid, event)
|
||||
if err := ValidateWebhookEvent(event); err != nil {
|
||||
return errors.Join(err, ErrSecretEventInvalid)
|
||||
}
|
||||
}
|
||||
if len(s.Events) == 0 {
|
||||
|
|
|
@ -22,6 +22,7 @@ type RepoListOptions = {
|
|||
all?: boolean;
|
||||
};
|
||||
|
||||
// PipelineOptions is the data for creating a new pipeline
|
||||
type PipelineOptions = {
|
||||
branch: string;
|
||||
variables: Record<string, string>;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { WebhookEvents } from './webhook';
|
||||
|
||||
// A pipeline for a repository.
|
||||
export type Pipeline = {
|
||||
id: number;
|
||||
|
@ -8,7 +10,7 @@ export type Pipeline = {
|
|||
|
||||
parent: number;
|
||||
|
||||
event: 'push' | 'tag' | 'pull_request' | 'deployment' | 'cron' | 'manual';
|
||||
event: WebhookEvents;
|
||||
|
||||
// The current status of the pipeline.
|
||||
status: PipelineStatus;
|
||||
|
|
Loading…
Reference in a new issue