Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameters for manual pipelines #4861

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions server/api/repo_parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package api

import (
"errors"
"net/http"
"strconv"

"github.com/gin-gonic/gin"

"go.woodpecker-ci.org/woodpecker/v3/server"
"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/router/middleware/session"
"go.woodpecker-ci.org/woodpecker/v3/server/store/types"
)

// GetParameter returns a repository parameter by ID.
func GetParameter(c *gin.Context) {
repo := session.Repo(c)
paramID, err := strconv.ParseInt(c.Param("parameter"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Invalid parameter ID")
return
}

parameterService := server.Config.Services.Manager.ParameterServiceFromRepo(repo)
parameter, err := parameterService.ParameterFindByID(repo, paramID)
if err != nil {
handleDBError(c, err)
return
}
c.JSON(http.StatusOK, parameter)
}

// PostParameter persists a parameter.
func PostParameter(c *gin.Context) {
repo := session.Repo(c)

in := new(model.Parameter)
if err := c.Bind(in); err != nil {
c.String(http.StatusBadRequest, "Error parsing parameter. %s", err)
return
}
in.RepoID = repo.ID

if err := in.Validate(); err != nil {
c.String(http.StatusBadRequest, "Error validating parameter. %s", err)
return
}

parameterService := server.Config.Services.Manager.ParameterServiceFromRepo(repo)

// Check if parameter with same name and branch already exists
existing, err := parameterService.ParameterFindByNameAndBranch(repo, in.Name, in.Branch)
if err != nil && !errors.Is(err, types.RecordNotExist) {
handleDBError(c, err)
return
}
if existing != nil && existing.ID != 0 {
c.String(http.StatusConflict, "Parameter with name '%s' already exists for branch '%s': existing: %d, new: %d", in.Name, in.Branch, existing.ID, in.ID)
return
}

err = parameterService.ParameterCreate(repo, in)
if err != nil {
handleDBError(c, err)
return
}
parameter, err := parameterService.ParameterFind(repo, in.Name)
c.JSON(http.StatusOK, parameter)
}

// PatchParameter updates an existing parameter by ID
func PatchParameter(c *gin.Context) {
repo := session.Repo(c)
paramID, err := strconv.ParseInt(c.Param("parameter"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Invalid parameter ID")
return
}

in := new(model.Parameter)
if err := c.Bind(in); err != nil {
c.String(http.StatusBadRequest, "Error parsing parameter. %s", err)
return
}
in.RepoID = repo.ID
in.ID = paramID

if err := in.Validate(); err != nil {
c.String(http.StatusBadRequest, "Error validating parameter. %s", err)
return
}

parameterService := server.Config.Services.Manager.ParameterServiceFromRepo(repo)

// Get existing parameter to check if name/branch changed
existing, err := parameterService.ParameterFindByID(repo, paramID)
if err != nil {
handleDBError(c, err)
return
}

// If name or branch changed, check for conflicts
if existing.Name != in.Name || existing.Branch != in.Branch {
conflict, err := parameterService.ParameterFindByNameAndBranch(repo, in.Name, in.Branch)
if err != nil && !errors.Is(err, types.RecordNotExist) {
handleDBError(c, err)
return
}
if conflict != nil && conflict.ID != 0 && conflict.ID != paramID {
c.String(http.StatusConflict, "Parameter with name '%s' already exists for branch '%s'", in.Name, in.Branch)
return
}
}

err = parameterService.ParameterUpdate(repo, in)
if err != nil {
handleDBError(c, err)
return
}
parameter, err := parameterService.ParameterFindByID(repo, paramID)
c.JSON(http.StatusOK, parameter)
}

// GetParameterList returns all repository parameters.
func GetParameterList(c *gin.Context) {
repo := session.Repo(c)
parameterService := server.Config.Services.Manager.ParameterServiceFromRepo(repo)
list, err := parameterService.ParameterList(repo)
if err != nil {
handleDBError(c, err)
return
}
c.JSON(http.StatusOK, list)
}

// DeleteParameter deletes a parameter by ID.
func DeleteParameter(c *gin.Context) {
repo := session.Repo(c)
paramID, err := strconv.ParseInt(c.Param("parameter"), 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "Invalid parameter ID")
return
}

parameterService := server.Config.Services.Manager.ParameterServiceFromRepo(repo)
if err := parameterService.ParameterDeleteByID(repo, paramID); err != nil {
handleDBError(c, err)
return
}
c.Status(http.StatusNoContent)
}
63 changes: 63 additions & 0 deletions server/model/parameter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package model

import (
"errors"
"fmt"
)

var (
ErrParameterNameInvalid = errors.New("invalid parameter name")
ErrParameterTypeInvalid = errors.New("invalid parameter type")
)

type ParameterType string

const (
ParameterTypeBoolean ParameterType = "boolean"
ParameterTypeSingleChoice ParameterType = "single_choice"
ParameterTypeMultipleChoice ParameterType = "multiple_choice"
ParameterTypeString ParameterType = "string"
ParameterTypeText ParameterType = "text"
ParameterTypePassword ParameterType = "password"
)

// Parameter represents a configurable parameter for a repository.
type Parameter struct {
ID int64 `json:"id" xorm:"pk autoincr 'parameter_id'"`
RepoID int64 `json:"repo_id" xorm:"UNIQUE(s) 'parameter_repo_id'"`
Name string `json:"name" xorm:"UNIQUE(s) 'parameter_name'"`
Branch string `json:"branch" xorm:"UNIQUE(s) 'parameter_branch'"`
Type ParameterType `json:"type" xorm:"'parameter_type'"`
Description string `json:"description" xorm:"TEXT 'parameter_description'"`
DefaultValue string `json:"default_value" xorm:"TEXT 'parameter_default_value'"`
TrimString bool `json:"trim_string" xorm:"'parameter_trim_string'"`
}

// TableName return database table name for xorm.
func (Parameter) TableName() string {
return "parameters"
}

// Validate validates the required fields and formats.
func (p *Parameter) Validate() error {
switch {
case len(p.Name) == 0:
return fmt.Errorf("%w: empty name", ErrParameterNameInvalid)
case len(p.Branch) == 0:
return fmt.Errorf("%w: empty branch", ErrParameterNameInvalid)
case !validParameterType(p.Type):
return fmt.Errorf("%w: %s", ErrParameterTypeInvalid, p.Type)
default:
return nil
}
}

func validParameterType(t ParameterType) bool {
switch t {
case ParameterTypeBoolean, ParameterTypeSingleChoice, ParameterTypeMultipleChoice,
ParameterTypeString, ParameterTypeText, ParameterTypePassword:
return true
default:
return false
}
}
7 changes: 7 additions & 0 deletions server/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func apiRoutes(e *gin.RouterGroup) {
// requires push permissions
repo.DELETE("/logs/:number", session.MustPush, api.DeletePipelineLogs)

// requires push permissions
repo.GET("/parameters", api.GetParameterList)
repo.POST("/parameters", session.MustPush, api.PostParameter)
repo.GET("/parameters/:parameter", api.GetParameter)
repo.PATCH("/parameters/:parameter", session.MustPush, api.PatchParameter)
repo.DELETE("/parameters/:parameter", session.MustPush, api.DeleteParameter)

// requires push permissions
repo.GET("/secrets", session.MustPush, api.GetSecretList)
repo.POST("/secrets", session.MustPush, api.PostSecret)
Expand Down
12 changes: 12 additions & 0 deletions server/services/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/services/config"
"go.woodpecker-ci.org/woodpecker/v3/server/services/environment"
"go.woodpecker-ci.org/woodpecker/v3/server/services/parameter"
"go.woodpecker-ci.org/woodpecker/v3/server/services/registry"
"go.woodpecker-ci.org/woodpecker/v3/server/services/secret"
"go.woodpecker-ci.org/woodpecker/v3/server/store"
Expand All @@ -38,6 +39,7 @@ type SetupForge func(forge *model.Forge) (forge.Forge, error)

type Manager interface {
SignaturePublicKey() crypto.PublicKey
ParameterServiceFromRepo(repo *model.Repo) parameter.Service
SecretServiceFromRepo(repo *model.Repo) secret.Service
SecretService() secret.Service
RegistryServiceFromRepo(repo *model.Repo) registry.Service
Expand All @@ -53,6 +55,7 @@ type manager struct {
signaturePrivateKey crypto.PrivateKey
signaturePublicKey crypto.PublicKey
store store.Store
parameter parameter.Service
secret secret.Service
registry registry.Service
config config.Service
Expand Down Expand Up @@ -81,6 +84,7 @@ func NewManager(c *cli.Command, store store.Store, setupForge SetupForge) (Manag
signaturePrivateKey: signaturePrivateKey,
signaturePublicKey: signaturePublicKey,
store: store,
parameter: setupParameterService(store),
secret: setupSecretService(store),
registry: setupRegistryService(store, c.String("docker-config")),
config: configService,
Expand All @@ -94,10 +98,18 @@ func (m *manager) SignaturePublicKey() crypto.PublicKey {
return m.signaturePublicKey
}

func (m *manager) ParameterServiceFromRepo(_ *model.Repo) parameter.Service {
return m.ParameterService()
}

func (m *manager) SecretServiceFromRepo(_ *model.Repo) secret.Service {
return m.SecretService()
}

func (m *manager) ParameterService() parameter.Service {
return m.parameter
}

func (m *manager) SecretService() secret.Service {
return m.secret
}
Expand Down
22 changes: 22 additions & 0 deletions server/services/mocks/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions server/services/parameter/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Woodpecker Authors
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

package parameter

import (
"go.woodpecker-ci.org/woodpecker/v3/server/model"
"go.woodpecker-ci.org/woodpecker/v3/server/store"
)

type db struct {
store store.Store
}

// NewDB returns a new parameter service.
func NewDB(store store.Store) Service {
return &db{store: store}
}

func (d *db) ParameterFind(repo *model.Repo, name string) (*model.Parameter, error) {
return d.store.ParameterFind(repo, name)
}

func (d *db) ParameterFindByID(repo *model.Repo, id int64) (*model.Parameter, error) {
return d.store.ParameterFindByID(repo, id)
}

func (d *db) ParameterFindByNameAndBranch(repo *model.Repo, name string, branch string) (*model.Parameter, error) {
return d.store.ParameterFindByNameAndBranch(repo, name, branch)
}

func (d *db) ParameterList(repo *model.Repo) ([]*model.Parameter, error) {
return d.store.ParameterList(repo)
}

func (d *db) ParameterCreate(repo *model.Repo, parameter *model.Parameter) error {
return d.store.ParameterCreate(repo, parameter)
}

func (d *db) ParameterUpdate(repo *model.Repo, parameter *model.Parameter) error {
return d.store.ParameterUpdate(repo, parameter)
}

func (d *db) ParameterDelete(repo *model.Repo, name string) error {
return d.store.ParameterDelete(repo, name)
}

func (d *db) ParameterDeleteByID(repo *model.Repo, id int64) error {
return d.store.ParameterDeleteByID(repo, id)
}
Loading