Skip to content
Merged
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
39 changes: 39 additions & 0 deletions router/middleware/pipeline/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package pipeline

import (
"context"

"github.com/go-vela/types/library"
)

const key = "pipeline"

// Setter defines a context that enables setting values.
type Setter interface {
Set(string, interface{})
}

// FromContext returns the Pipeline associated with this context.
func FromContext(c context.Context) *library.Pipeline {
value := c.Value(key)
if value == nil {
return nil
}

b, ok := value.(*library.Pipeline)
if !ok {
return nil
}

return b
}

// ToContext adds the Pipeline to this context if it supports
// the Setter interface.
func ToContext(c Setter, b *library.Pipeline) {
c.Set(key, b)
}
103 changes: 103 additions & 0 deletions router/middleware/pipeline/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package pipeline

import (
"reflect"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/types/library"
)

func TestPipeline_FromContext(t *testing.T) {
// setup types
_pipeline := new(library.Pipeline)

gin.SetMode(gin.TestMode)
_context, _ := gin.CreateTestContext(nil)
_context.Set(key, _pipeline)

_emptyContext, _ := gin.CreateTestContext(nil)

_nilContext, _ := gin.CreateTestContext(nil)
_nilContext.Set(key, nil)

_typeContext, _ := gin.CreateTestContext(nil)
_typeContext.Set(key, 1)

// setup tests
tests := []struct {
name string
context *gin.Context
want *library.Pipeline
}{
{
name: "context",
context: _context,
want: _pipeline,
},
{
name: "context with no value",
context: _emptyContext,
want: nil,
},
{
name: "context with nil value",
context: _nilContext,
want: nil,
},
{
name: "context with wrong value type",
context: _typeContext,
want: nil,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := FromContext(test.context)

if !reflect.DeepEqual(got, test.want) {
t.Errorf("FromContext for %s is %v, want %v", test.name, got, test.want)
}
})
}
}

func TestPipeline_ToContext(t *testing.T) {
// setup types
_pipeline := new(library.Pipeline)

gin.SetMode(gin.TestMode)
_context, _ := gin.CreateTestContext(nil)

// setup tests
tests := []struct {
name string
context *gin.Context
want *library.Pipeline
}{
{
name: "context",
context: _context,
want: _pipeline,
},
}

// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ToContext(test.context, test.want)

got := test.context.Value(key)

if !reflect.DeepEqual(got, test.want) {
t.Errorf("ToContext for %s is %v, want %v", test.name, got, test.want)
}
})
}
}
12 changes: 12 additions & 0 deletions router/middleware/pipeline/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

// Package pipeline provides the ability for inserting
// Vela pipeline resources into or extracting Vela pipeline
// resources from the middleware chain for the API.
//
// Usage:
//
// import "github.com/go-vela/server/router/middleware/pipeline"
package pipeline
73 changes: 73 additions & 0 deletions router/middleware/pipeline/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package pipeline

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// Retrieve gets the pipeline in the given context.
func Retrieve(c *gin.Context) *library.Pipeline {
return FromContext(c)
}

// Establish sets the pipeline in the given context.
func Establish() gin.HandlerFunc {
return func(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)

if r == nil {
retErr := fmt.Errorf("repo %s/%s not found", c.Param("org"), c.Param("repo"))

util.HandleError(c, http.StatusNotFound, retErr)

return
}

p := c.Param("pipeline")
if len(p) == 0 {
retErr := fmt.Errorf("no pipeline parameter provided")

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"org": o,
"pipeline": p,
"repo": r.GetName(),
"user": u.GetName(),
}).Debugf("reading pipeline %s/%s", r.GetFullName(), p)

pipeline, err := database.FromContext(c).GetPipelineForRepo(p, r)
if err != nil {
retErr := fmt.Errorf("unable to read pipeline %s/%s: %w", r.GetFullName(), p, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

ToContext(c, pipeline)

c.Next()
}
}
Loading