Skip to content

Commit

Permalink
Merge branch 'main' into enhance/validate-deploy-on-create
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper authored Jan 8, 2025
2 parents 609cbdb + f34ffb9 commit 99942e4
Show file tree
Hide file tree
Showing 29 changed files with 438 additions and 163 deletions.
69 changes: 69 additions & 0 deletions api/build/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: Apache-2.0

package build

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/router/middleware/build"
)

// swagger:operation GET /status/{org}/{repo}/{build} builds GetBuildStatus
//
// Get a build status
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the organization
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repository
// required: true
// type: string
// - in: path
// name: build
// description: Build number
// required: true
// type: integer
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the build
// schema:
// "$ref": "#/definitions/Build"
// '400':
// description: Invalid request payload or path
// schema:
// "$ref": "#/definitions/Build"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Build"
// '404':
// description: Not found
// schema:
// "$ref": "#/definitions/Build"

// GetBuildStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access.
func GetBuildStatus(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
b := build.Retrieve(c)

l.Debug("reading status for build")

// sanitize fields for the unauthenticated response
b.StatusSanitize()

c.JSON(http.StatusOK, b)
}
2 changes: 1 addition & 1 deletion api/pipeline/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GetTemplates(c *gin.Context) {
compiler := compiler.FromContext(c).Duplicate().WithCommit(p.GetCommit()).WithMetadata(m).WithRepo(r).WithUser(u)

// parse the pipeline configuration
pipeline, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
pipeline, _, _, err := compiler.Parse(p.GetData(), p.GetType(), new(yaml.Template))
if err != nil {
util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to parse pipeline %s: %w", entry, err))

Expand Down
64 changes: 64 additions & 0 deletions api/repo/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: Apache-2.0

package repo

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/router/middleware/repo"
)

// swagger:operation GET /status/{org}/{repo} repos GetRepoStatus
//
// Get a repository status
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the organization
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repository
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the repo
// schema:
// "$ref": "#/definitions/Repo"
// '400':
// description: Invalid request payload or path
// schema:
// "$ref": "#/definitions/Repo"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Repo"
// '404':
// description: Not found
// schema:
// "$ref": "#/definitions/Repo"

// GetRepoStatus represents the API handler to return "status", a lite representation of the resource with limited fields for unauthenticated access.
func GetRepoStatus(c *gin.Context) {
// capture middleware values
l := c.MustGet("logger").(*logrus.Entry)
r := repo.Retrieve(c)

l.Debug("reading status for repo")

// sanitize fields for the unauthenticated response
r.StatusSanitize()

c.JSON(http.StatusOK, r)
}
11 changes: 11 additions & 0 deletions api/types/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,14 @@ func (b *Build) String() string {
b.GetTitle(),
)
}

// StatusSanitize removes sensitive information before producing a "status".
func (b *Build) StatusSanitize() {
// sanitize repo
if b.Repo != nil {
b.Repo.StatusSanitize()
}

b.Email = nil
b.DeployPayload = nil
}
57 changes: 43 additions & 14 deletions api/types/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ import (
//
// swagger:model Pipeline
type Pipeline struct {
ID *int64 `json:"id,omitempty"`
Repo *Repo `json:"repo,omitempty"`
Commit *string `json:"commit,omitempty"`
Flavor *string `json:"flavor,omitempty"`
Platform *string `json:"platform,omitempty"`
Ref *string `json:"ref,omitempty"`
Type *string `json:"type,omitempty"`
Version *string `json:"version,omitempty"`
ExternalSecrets *bool `json:"external_secrets,omitempty"`
InternalSecrets *bool `json:"internal_secrets,omitempty"`
Services *bool `json:"services,omitempty"`
Stages *bool `json:"stages,omitempty"`
Steps *bool `json:"steps,omitempty"`
Templates *bool `json:"templates,omitempty"`
ID *int64 `json:"id,omitempty"`
Repo *Repo `json:"repo,omitempty"`
Commit *string `json:"commit,omitempty"`
Flavor *string `json:"flavor,omitempty"`
Platform *string `json:"platform,omitempty"`
Ref *string `json:"ref,omitempty"`
Type *string `json:"type,omitempty"`
Version *string `json:"version,omitempty"`
ExternalSecrets *bool `json:"external_secrets,omitempty"`
InternalSecrets *bool `json:"internal_secrets,omitempty"`
Services *bool `json:"services,omitempty"`
Stages *bool `json:"stages,omitempty"`
Steps *bool `json:"steps,omitempty"`
Templates *bool `json:"templates,omitempty"`
Warnings *[]string `json:"warnings,omitempty"`
// swagger:strfmt base64
Data *[]byte `json:"data,omitempty"`
}
Expand Down Expand Up @@ -210,6 +211,19 @@ func (p *Pipeline) GetTemplates() bool {
return *p.Templates
}

// GetWarnings returns the Warnings field.
//
// When the provided Pipeline type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (p *Pipeline) GetWarnings() []string {
// return zero value if Pipeline type or Warnings field is nil
if p == nil || p.Warnings == nil {
return []string{}
}

return *p.Warnings
}

// GetData returns the Data field.
//
// When the provided Pipeline type is nil, or the field within
Expand Down Expand Up @@ -405,6 +419,19 @@ func (p *Pipeline) SetTemplates(v bool) {
p.Templates = &v
}

// SetWarnings sets the Warnings field.
//
// When the provided Pipeline type is nil, it
// will set nothing and immediately return.
func (p *Pipeline) SetWarnings(v []string) {
// return if Pipeline type is nil
if p == nil {
return
}

p.Warnings = &v
}

// SetData sets the Data field.
//
// When the provided Pipeline type is nil, it
Expand Down Expand Up @@ -436,6 +463,7 @@ func (p *Pipeline) String() string {
Templates: %t,
Type: %s,
Version: %s,
Warnings: %v,
}`,
p.GetCommit(),
p.GetData(),
Expand All @@ -452,5 +480,6 @@ func (p *Pipeline) String() string {
p.GetTemplates(),
p.GetType(),
p.GetVersion(),
p.GetWarnings(),
)
}
12 changes: 12 additions & 0 deletions api/types/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func TestAPI_Pipeline_Getters(t *testing.T) {
t.Errorf("GetTemplates is %v, want %v", test.pipeline.GetTemplates(), test.want.GetTemplates())
}

if !reflect.DeepEqual(test.pipeline.GetWarnings(), test.want.GetWarnings()) {
t.Errorf("GetWarnings is %v, want %v", test.pipeline.GetWarnings(), test.want.GetWarnings())
}

if !reflect.DeepEqual(test.pipeline.GetData(), test.want.GetData()) {
t.Errorf("GetData is %v, want %v", test.pipeline.GetData(), test.want.GetData())
}
Expand Down Expand Up @@ -123,6 +127,7 @@ func TestAPI_Pipeline_Setters(t *testing.T) {
test.pipeline.SetStages(test.want.GetStages())
test.pipeline.SetSteps(test.want.GetSteps())
test.pipeline.SetTemplates(test.want.GetTemplates())
test.pipeline.SetWarnings(test.want.GetWarnings())
test.pipeline.SetData(test.want.GetData())

if test.pipeline.GetID() != test.want.GetID() {
Expand Down Expand Up @@ -181,6 +186,10 @@ func TestAPI_Pipeline_Setters(t *testing.T) {
t.Errorf("SetTemplates is %v, want %v", test.pipeline.GetTemplates(), test.want.GetTemplates())
}

if !reflect.DeepEqual(test.pipeline.GetWarnings(), test.want.GetWarnings()) {
t.Errorf("SetWarnings is %v, want %v", test.pipeline.GetWarnings(), test.want.GetWarnings())
}

if !reflect.DeepEqual(test.pipeline.GetData(), test.want.GetData()) {
t.Errorf("SetData is %v, want %v", test.pipeline.GetData(), test.want.GetData())
}
Expand All @@ -207,6 +216,7 @@ func TestAPI_Pipeline_String(t *testing.T) {
Templates: %t,
Type: %s,
Version: %s,
Warnings: %v,
}`,
p.GetCommit(),
p.GetData(),
Expand All @@ -223,6 +233,7 @@ func TestAPI_Pipeline_String(t *testing.T) {
p.GetTemplates(),
p.GetType(),
p.GetVersion(),
p.GetWarnings(),
)

// run test
Expand Down Expand Up @@ -253,6 +264,7 @@ func testPipeline() *Pipeline {
p.SetSteps(true)
p.SetTemplates(false)
p.SetData(testPipelineData())
p.SetWarnings([]string{"42:this is a warning"})

return p
}
Expand Down
6 changes: 6 additions & 0 deletions api/types/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,9 @@ func (r *Repo) String() string {
r.GetInstallID(),
)
}

// StatusSanitize removes sensitive information before producing a "status".
func (r *Repo) StatusSanitize() {
// remove allowed events
r.AllowEvents = nil
}
2 changes: 1 addition & 1 deletion compiler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Engine interface {

// Parse defines a function that converts
// an object to a yaml configuration.
Parse(interface{}, string, *yaml.Template) (*yaml.Build, []byte, error)
Parse(interface{}, string, *yaml.Template) (*yaml.Build, []byte, []string, error)

// ParseRaw defines a function that converts
// an object to a string.
Expand Down
8 changes: 5 additions & 3 deletions compiler/native/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ModifyResponse struct {

// Compile produces an executable pipeline from a yaml configuration.
func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *api.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
p, data, warnings, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand All @@ -61,6 +61,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *
_pipeline := p.ToPipelineAPI()
_pipeline.SetData(data)
_pipeline.SetType(c.repo.GetPipelineType())
_pipeline.SetWarnings(warnings)

// create map of templates for easy lookup
templates := mapFromTemplates(p.Templates)
Expand Down Expand Up @@ -117,7 +118,7 @@ func (c *client) Compile(ctx context.Context, v interface{}) (*pipeline.Build, *

// CompileLite produces a partial of an executable pipeline from a yaml configuration.
func (c *client) CompileLite(ctx context.Context, v interface{}, ruleData *pipeline.RuleData, substitute bool) (*yaml.Build, *api.Pipeline, error) {
p, data, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
p, data, warnings, err := c.Parse(v, c.repo.GetPipelineType(), new(yaml.Template))
if err != nil {
return nil, nil, err
}
Expand All @@ -126,6 +127,7 @@ func (c *client) CompileLite(ctx context.Context, v interface{}, ruleData *pipel
_pipeline := p.ToPipelineAPI()
_pipeline.SetData(data)
_pipeline.SetType(c.repo.GetPipelineType())
_pipeline.SetWarnings(warnings)

if p.Metadata.RenderInline {
newPipeline, err := c.compileInline(ctx, p, c.GetTemplateDepth())
Expand Down Expand Up @@ -267,7 +269,7 @@ func (c *client) compileInline(ctx context.Context, p *yaml.Build, depth int) (*
// inject template name into variables
template.Variables["VELA_TEMPLATE_NAME"] = template.Name

parsed, _, err := c.Parse(bytes, format, template)
parsed, _, _, err := c.Parse(bytes, format, template)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 99942e4

Please sign in to comment.