-
Notifications
You must be signed in to change notification settings - Fork 30
feat(middleware): add support for pipelines #627
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
57e5162
Merge branch 'master' of github.com:go-vela/server
jbrockopp b6b840c
Merge branch 'master' of github.com:go-vela/server
jbrockopp 858c818
Merge branch 'master' of github.com:go-vela/server
jbrockopp ceddb11
Merge branch 'master' of github.com:go-vela/server
jbrockopp 331b2a2
Merge branch 'master' of github.com:go-vela/server
jbrockopp c4ec5fb
Merge branches 'master' and 'master' of github.com:go-vela/server
jbrockopp 30c1151
Merge branch 'master' of github.com:go-vela/server
jbrockopp 9af8c4a
feat(middleware): add support for pipelines
jbrockopp 6d71246
test: use subtests for table tests
jbrockopp 62cf15d
Merge branch 'master' into feature/middleware/pipeline
jbrockopp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.