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)
|
_store := store.FromContext(c)
|
||||||
repo := session.Repo(c)
|
repo := session.Repo(c)
|
||||||
|
|
||||||
var p model.PipelineOptions
|
// parse create options
|
||||||
|
var opts model.PipelineOptions
|
||||||
err := json.NewDecoder(c.Request.Body).Decode(&p)
|
err := json.NewDecoder(c.Request.Body).Decode(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = c.AbortWithError(http.StatusBadRequest, err)
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
|
@ -53,9 +53,9 @@ func CreatePipeline(c *gin.Context) {
|
||||||
|
|
||||||
user := session.User(c)
|
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)
|
pl, err := pipeline.Create(c, _store, repo, tmpBuild)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -368,9 +368,8 @@ func PostPipeline(c *gin.Context) {
|
||||||
if event, ok := c.GetQuery("event"); ok {
|
if event, ok := c.GetQuery("event"); ok {
|
||||||
pl.Event = model.WebhookEvent(event)
|
pl.Event = model.WebhookEvent(event)
|
||||||
|
|
||||||
if !model.ValidateWebhookEvent(pl.Event) {
|
if err := model.ValidateWebhookEvent(pl.Event); err != nil {
|
||||||
msg := fmt.Sprintf("pipeline event '%s' is invalid", event)
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
||||||
c.String(http.StatusBadRequest, msg)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ type configFetcher struct {
|
||||||
repo *model.Repo
|
repo *model.Repo
|
||||||
pipeline *model.Pipeline
|
pipeline *model.Pipeline
|
||||||
configExtension config.Extension
|
configExtension config.Extension
|
||||||
|
configPath string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,6 +50,7 @@ func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config
|
||||||
repo: repo,
|
repo: repo,
|
||||||
pipeline: pipeline,
|
pipeline: pipeline,
|
||||||
configExtension: configExtension,
|
configExtension: configExtension,
|
||||||
|
configPath: repo.Config,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +61,7 @@ func (cf *configFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, er
|
||||||
|
|
||||||
// try to fetch 3 times
|
// try to fetch 3 times
|
||||||
for i := 0; i < 3; i++ {
|
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 {
|
if err != nil {
|
||||||
log.Trace().Err(err).Msgf("%d. try failed", i+1)
|
log.Trace().Err(err).Msgf("%d. try failed", i+1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,11 @@
|
||||||
|
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type WebhookEvent string
|
type WebhookEvent string
|
||||||
|
|
||||||
const (
|
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) 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 (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 {
|
switch s {
|
||||||
case EventPush, EventPull, EventTag, EventDeploy, EventCron, EventManual:
|
case EventPush, EventPull, EventTag, EventDeploy, EventCron, EventManual:
|
||||||
return true
|
return nil
|
||||||
default:
|
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.
|
// Validate validates the required fields and formats.
|
||||||
func (s *Secret) Validate() error {
|
func (s *Secret) Validate() error {
|
||||||
for _, event := range s.Events {
|
for _, event := range s.Events {
|
||||||
if !ValidateWebhookEvent(event) {
|
if err := ValidateWebhookEvent(event); err != nil {
|
||||||
return fmt.Errorf("%w: '%s'", ErrSecretEventInvalid, event)
|
return errors.Join(err, ErrSecretEventInvalid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(s.Events) == 0 {
|
if len(s.Events) == 0 {
|
||||||
|
|
|
@ -22,6 +22,7 @@ type RepoListOptions = {
|
||||||
all?: boolean;
|
all?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// PipelineOptions is the data for creating a new pipeline
|
||||||
type PipelineOptions = {
|
type PipelineOptions = {
|
||||||
branch: string;
|
branch: string;
|
||||||
variables: Record<string, string>;
|
variables: Record<string, string>;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { WebhookEvents } from './webhook';
|
||||||
|
|
||||||
// A pipeline for a repository.
|
// A pipeline for a repository.
|
||||||
export type Pipeline = {
|
export type Pipeline = {
|
||||||
id: number;
|
id: number;
|
||||||
|
@ -8,7 +10,7 @@ export type Pipeline = {
|
||||||
|
|
||||||
parent: number;
|
parent: number;
|
||||||
|
|
||||||
event: 'push' | 'tag' | 'pull_request' | 'deployment' | 'cron' | 'manual';
|
event: WebhookEvents;
|
||||||
|
|
||||||
// The current status of the pipeline.
|
// The current status of the pipeline.
|
||||||
status: PipelineStatus;
|
status: PipelineStatus;
|
||||||
|
|
Loading…
Reference in a new issue