|
| 1 | +// |
| 2 | +// Copyright 2025 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package action |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 23 | +) |
| 24 | + |
| 25 | +type WorkflowContractApply struct { |
| 26 | + cfg *ActionsOpts |
| 27 | +} |
| 28 | + |
| 29 | +func NewWorkflowContractApply(cfg *ActionsOpts) *WorkflowContractApply { |
| 30 | + return &WorkflowContractApply{cfg} |
| 31 | +} |
| 32 | + |
| 33 | +func (action *WorkflowContractApply) Run(ctx context.Context, contractName string, contractPath string, description *string, projectName string) (*WorkflowContractItem, error) { |
| 34 | + client := pb.NewWorkflowContractServiceClient(action.cfg.CPConnection) |
| 35 | + |
| 36 | + // Try to describe the specific contract first to determine if we should create or update |
| 37 | + describeReq := &pb.WorkflowContractServiceDescribeRequest{ |
| 38 | + Name: contractName, |
| 39 | + } |
| 40 | + |
| 41 | + var rawContract []byte |
| 42 | + if contractPath != "" { |
| 43 | + raw, err := LoadFileOrURL(contractPath) |
| 44 | + if err != nil { |
| 45 | + action.cfg.Logger.Debug().Err(err).Msg("loading the contract") |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + rawContract = raw |
| 49 | + } |
| 50 | + |
| 51 | + _, err := client.Describe(ctx, describeReq) |
| 52 | + if err == nil { |
| 53 | + // Contract exists, perform update |
| 54 | + updateReq := &pb.WorkflowContractServiceUpdateRequest{ |
| 55 | + Name: contractName, |
| 56 | + Description: description, |
| 57 | + RawContract: rawContract, |
| 58 | + } |
| 59 | + |
| 60 | + res, err := client.Update(ctx, updateReq) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed to update existing contract '%s': %w", contractName, err) |
| 63 | + } |
| 64 | + |
| 65 | + return pbWorkflowContractItemToAction(res.Result.Contract), nil |
| 66 | + } |
| 67 | + |
| 68 | + // Contract doesn't exist, perform create |
| 69 | + createReq := &pb.WorkflowContractServiceCreateRequest{ |
| 70 | + Name: contractName, |
| 71 | + Description: description, |
| 72 | + RawContract: rawContract, |
| 73 | + } |
| 74 | + |
| 75 | + if projectName != "" { |
| 76 | + createReq.ProjectReference = &pb.IdentityReference{ |
| 77 | + Name: &projectName, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + res, err := client.Create(ctx, createReq) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("failed to create new contract '%s': %w", contractName, err) |
| 84 | + } |
| 85 | + |
| 86 | + return pbWorkflowContractItemToAction(res.Result), nil |
| 87 | +} |
0 commit comments