-
Notifications
You must be signed in to change notification settings - Fork 27
/
integrationattachment.go
121 lines (101 loc) · 3.5 KB
/
integrationattachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright 2023 The Chainloop 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 data
import (
"context"
"time"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/biz"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/integrationattachment"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/workflow"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
)
type IntegrationAttachmentRepo struct {
data *Data
log *log.Helper
}
func NewIntegrationAttachmentRepo(data *Data, logger log.Logger) biz.IntegrationAttachmentRepo {
return &IntegrationAttachmentRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (r *IntegrationAttachmentRepo) Create(ctx context.Context, integrationID, workflowID uuid.UUID, config []byte) (*biz.IntegrationAttachment, error) {
ia, err := r.data.db.IntegrationAttachment.Create().
SetWorkflowID(workflowID).
SetIntegrationID(integrationID).
SetConfiguration(config).
Save(ctx)
if err != nil {
return nil, err
}
res := entIntegrationAttachmentToBiz(ia)
if res != nil {
res.IntegrationID = integrationID
res.WorkflowID = workflowID
}
return res, nil
}
func (r *IntegrationAttachmentRepo) List(ctx context.Context, orgID, workflowID uuid.UUID) ([]*biz.IntegrationAttachment, error) {
wfQuery := orgScopedQuery(r.data.db, orgID).QueryWorkflows()
if workflowID != uuid.Nil {
wfQuery = wfQuery.Where(workflow.ID(workflowID))
}
res, err := wfQuery.QueryIntegrationAttachments().WithIntegration().WithWorkflow().
Where(integrationattachment.DeletedAtIsNil()).
Order(ent.Desc(integrationattachment.FieldCreatedAt)).
All(ctx)
if err != nil {
return nil, err
}
result := make([]*biz.IntegrationAttachment, 0, len(res))
for _, r := range res {
result = append(result, entIntegrationAttachmentToBiz(r))
}
return result, nil
}
func (r *IntegrationAttachmentRepo) FindByIDInOrg(ctx context.Context, orgID, id uuid.UUID) (*biz.IntegrationAttachment, error) {
integration, err := orgScopedQuery(r.data.db, orgID).
QueryIntegrations().
QueryAttachments().
Where(integrationattachment.DeletedAtIsNil(), integrationattachment.ID(id)).
WithIntegration().WithWorkflow().
Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if integration == nil {
return nil, nil
}
return entIntegrationAttachmentToBiz(integration), nil
}
func (r *IntegrationAttachmentRepo) SoftDelete(ctx context.Context, id uuid.UUID) error {
return r.data.db.IntegrationAttachment.UpdateOneID(id).SetDeletedAt(time.Now()).Exec(ctx)
}
func entIntegrationAttachmentToBiz(i *ent.IntegrationAttachment) *biz.IntegrationAttachment {
if i == nil {
return nil
}
r := &biz.IntegrationAttachment{ID: i.ID,
CreatedAt: toTimePtr(i.CreatedAt), Config: i.Configuration,
}
if i.Edges.Workflow != nil {
r.WorkflowID = i.Edges.Workflow.ID
}
if i.Edges.Integration != nil {
r.IntegrationID = i.Edges.Integration.ID
}
return r
}