-
Notifications
You must be signed in to change notification settings - Fork 204
Support multiple manual approvals #2802
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
46 commits
Select commit
Hold shift + click to select a range
4066174
Support multiple manual approvals
7d7c088
Fix unused variables
3a1981f
Format codes
fbeeb96
Fix test
3643ad2
Fix formats
3bd9b03
Fix formats
2c8ff73
Format unfmt-ed files
b6d5e99
Update log messages
ab87d54
Fix tests
e73e886
Update pkg/app/piped/executor/waitapproval/waitapproval.go
e511ec0
Update pkg/app/piped/executor/waitapproval/waitapproval.go
3c1d901
Update pkg/app/piped/executor/waitapproval/waitapproval.go
29e97bf
Update pkg/app/piped/executor/waitapproval/waitapproval.go
3f10ecc
Use num
ad2fd57
Rename key name
19c2c80
Move log part to into validateApproverNum function
b391a82
Fix test
9ee377d
fix test
ee6792c
fix
f0408d2
fix
a123648
Fix formats
c7d1cdc
Rename num
64c2361
Use fmt.Sprintf instead of addition
0074085
Use StageConfig instead of metadata
650f3bb
Fix test
8f9a544
Fix
c0e6d91
Fix typo
b2ab617
Refactor the logic
411f7fa
Format code
d4a7810
fix test
ddf3196
fix
8eeed0a
fix
64f0cea
fix
bcd2ea0
fix
d332a42
fix
c592269
fix
41d336b
fix
1101e89
format
19fbc7a
Fix
7d60a88
fix test
9449372
fix
82c2d8e
fix test
0c3dce1
fix
ed37174
Call validation for WaitApprovalStageOptions
741101e
Update pkg/config/deployment.go
2df64f6
Update pkg/app/piped/executor/waitapproval/waitapproval.go
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
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
217 changes: 217 additions & 0 deletions
217
pkg/app/piped/executor/waitapproval/waitapproval_test.go
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,217 @@ | ||
| // Copyright 2021 The PipeCD 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 waitapproval | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "google.golang.org/grpc" | ||
|
|
||
| "github.com/pipe-cd/pipe/pkg/app/api/service/pipedservice" | ||
| "github.com/pipe-cd/pipe/pkg/app/piped/executor" | ||
| "github.com/pipe-cd/pipe/pkg/app/piped/metadatastore" | ||
| "github.com/pipe-cd/pipe/pkg/model" | ||
| ) | ||
|
|
||
| type fakeLogPersister struct{} | ||
|
|
||
| func (l *fakeLogPersister) Write(_ []byte) (int, error) { return 0, nil } | ||
| func (l *fakeLogPersister) Info(_ string) {} | ||
| func (l *fakeLogPersister) Infof(_ string, _ ...interface{}) {} | ||
| func (l *fakeLogPersister) Success(_ string) {} | ||
| func (l *fakeLogPersister) Successf(_ string, _ ...interface{}) {} | ||
| func (l *fakeLogPersister) Error(_ string) {} | ||
| func (l *fakeLogPersister) Errorf(_ string, _ ...interface{}) {} | ||
|
|
||
| type metadata map[string]string | ||
|
|
||
| type fakeAPIClient struct { | ||
| shared metadata | ||
| stages map[string]metadata | ||
| } | ||
|
|
||
| func (c *fakeAPIClient) SaveDeploymentMetadata(_ context.Context, req *pipedservice.SaveDeploymentMetadataRequest, _ ...grpc.CallOption) (*pipedservice.SaveDeploymentMetadataResponse, error) { | ||
| md := make(map[string]string, len(c.shared)+len(req.Metadata)) | ||
| for k, v := range c.shared { | ||
| md[k] = v | ||
| } | ||
| for k, v := range req.Metadata { | ||
| md[k] = v | ||
| } | ||
| c.shared = md | ||
| return &pipedservice.SaveDeploymentMetadataResponse{}, nil | ||
| } | ||
|
|
||
| func (c *fakeAPIClient) SaveStageMetadata(_ context.Context, req *pipedservice.SaveStageMetadataRequest, _ ...grpc.CallOption) (*pipedservice.SaveStageMetadataResponse, error) { | ||
| ori := c.stages[req.StageId] | ||
| md := make(map[string]string, len(ori)+len(req.Metadata)) | ||
| for k, v := range ori { | ||
| md[k] = v | ||
| } | ||
| for k, v := range req.Metadata { | ||
| md[k] = v | ||
| } | ||
| c.stages[req.StageId] = md | ||
| return &pipedservice.SaveStageMetadataResponse{}, nil | ||
| } | ||
|
|
||
| type fakeNotifier struct{} | ||
|
|
||
| func (n *fakeNotifier) Notify(_ model.NotificationEvent) {} | ||
|
|
||
| func TestValidateApproverNum(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| ac := &fakeAPIClient{ | ||
| shared: make(map[string]string, 0), | ||
| stages: make(map[string]metadata, 0), | ||
| } | ||
| testcases := []struct { | ||
| name string | ||
| approver string | ||
| minApproverNum int | ||
| executor *Executor | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "return the person who just approved", | ||
| approver: "user-1", | ||
| minApproverNum: 0, | ||
| executor: &Executor{ | ||
| Input: executor.Input{ | ||
| Stage: &model.PipelineStage{ | ||
| Id: "stage-1", | ||
| }, | ||
| LogPersister: &fakeLogPersister{}, | ||
| MetadataStore: metadatastore.NewMetadataStore(ac, &model.Deployment{ | ||
| Stages: []*model.PipelineStage{ | ||
| { | ||
| Id: "stage-1", | ||
| Metadata: map[string]string{}, | ||
| }, | ||
| }, | ||
| }), | ||
| Notifier: &fakeNotifier{}, | ||
| }, | ||
| }, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "return an empty string because number of current approver is not enough", | ||
| approver: "user-1", | ||
| minApproverNum: 2, | ||
| executor: &Executor{ | ||
| Input: executor.Input{ | ||
| Stage: &model.PipelineStage{ | ||
| Id: "stage-1", | ||
| }, | ||
| LogPersister: &fakeLogPersister{}, | ||
| MetadataStore: metadatastore.NewMetadataStore(ac, &model.Deployment{ | ||
| Stages: []*model.PipelineStage{ | ||
| { | ||
| Id: "stage-1", | ||
| Metadata: map[string]string{}, | ||
| }, | ||
| }, | ||
| }), | ||
| }, | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "return an empty string because current approver is same as an approver in metadata", | ||
| approver: "user-1", | ||
| minApproverNum: 2, | ||
| executor: &Executor{ | ||
| Input: executor.Input{ | ||
| Stage: &model.PipelineStage{ | ||
| Id: "stage-1", | ||
| }, | ||
| LogPersister: &fakeLogPersister{}, | ||
| MetadataStore: metadatastore.NewMetadataStore(ac, &model.Deployment{ | ||
| Stages: []*model.PipelineStage{ | ||
| { | ||
| Id: "stage-1", | ||
| Metadata: map[string]string{ | ||
| approvedByKey: "user-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| Notifier: &fakeNotifier{}, | ||
| }, | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "return an empty string because number of current approver and approvers in metadata is not enough", | ||
| approver: "user-2", | ||
| minApproverNum: 3, | ||
| executor: &Executor{ | ||
| Input: executor.Input{ | ||
| Stage: &model.PipelineStage{ | ||
| Id: "stage-1", | ||
| }, | ||
| LogPersister: &fakeLogPersister{}, | ||
| MetadataStore: metadatastore.NewMetadataStore(ac, &model.Deployment{ | ||
| Stages: []*model.PipelineStage{ | ||
| { | ||
| Id: "stage-1", | ||
| Metadata: map[string]string{ | ||
| approvedByKey: "user-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| Notifier: &fakeNotifier{}, | ||
| }, | ||
| }, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "return all approvers", | ||
| approver: "user-2", | ||
| minApproverNum: 2, | ||
| executor: &Executor{ | ||
| Input: executor.Input{ | ||
| Stage: &model.PipelineStage{ | ||
| Id: "stage-1", | ||
| }, | ||
| LogPersister: &fakeLogPersister{}, | ||
| MetadataStore: metadatastore.NewMetadataStore(ac, &model.Deployment{ | ||
| Stages: []*model.PipelineStage{ | ||
| { | ||
| Id: "stage-1", | ||
| Metadata: map[string]string{ | ||
| approvedByKey: "user-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| Notifier: &fakeNotifier{}, | ||
| }, | ||
| }, | ||
| want: true, | ||
| }, | ||
| } | ||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| got := tc.executor.validateApproverNum(ctx, tc.approver, tc.minApproverNum) | ||
| assert.Equal(t, tc.want, got) | ||
| }) | ||
| } | ||
| } |
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
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
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.