-
Notifications
You must be signed in to change notification settings - Fork 25
/
workflowrun.go
203 lines (170 loc) · 6.05 KB
/
workflowrun.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// 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"
"errors"
"fmt"
"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/organization"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/workflow"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/data/ent/workflowrun"
"github.com/chainloop-dev/chainloop/app/controlplane/internal/pagination"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
"entgo.io/ent/dialect/sql"
)
type WorkflowRunRepo struct {
data *Data
log *log.Helper
}
func NewWorkflowRunRepo(data *Data, logger log.Logger) biz.WorkflowRunRepo {
return &WorkflowRunRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (r *WorkflowRunRepo) Create(ctx context.Context, workflowID, robotaccountID, schemaVersionID uuid.UUID, runURL, runnerType string) (*biz.WorkflowRun, error) {
p, err := r.data.db.WorkflowRun.Create().
SetRobotaccountID(robotaccountID).
SetWorkflowID(workflowID).
SetContractVersionID(schemaVersionID).
SetRunURL(runURL).
SetRunnerType(runnerType).
Save(ctx)
if err != nil {
return nil, err
}
return r.FindByID(ctx, p.ID)
}
func (r *WorkflowRunRepo) FindByID(ctx context.Context, id uuid.UUID) (*biz.WorkflowRun, error) {
run, err := r.data.db.WorkflowRun.Query().
Where(workflowrun.ID(id)).WithWorkflow().WithContractVersion().Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if run == nil {
return nil, nil
}
return entWrToBizWr(run), nil
}
func (r *WorkflowRunRepo) FindByIDInOrg(ctx context.Context, orgID, id uuid.UUID) (*biz.WorkflowRun, error) {
run, err := orgScopedQuery(r.data.db, orgID).
QueryWorkflows().
QueryWorkflowruns().Where(workflowrun.ID(id)).
WithWorkflow().WithContractVersion().
Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if run == nil {
return nil, nil
}
return entWrToBizWr(run), nil
}
func (r *WorkflowRunRepo) SaveAttestationRef(ctx context.Context, id uuid.UUID, ref *biz.AttestationRef) error {
run, err := r.data.db.WorkflowRun.UpdateOneID(id).SetAttestationRef(ref).Save(ctx)
if err != nil && !ent.IsNotFound(err) {
return err
} else if run == nil {
return biz.NewErrNotFound(fmt.Sprintf("workflow run with id %s not found", id))
}
return nil
}
func (r *WorkflowRunRepo) MarkAsFinished(ctx context.Context, id uuid.UUID, status biz.WorkflowRunStatus, reason string) error {
run, err := r.data.db.WorkflowRun.Query().Where(workflowrun.ID(id)).WithWorkflow().First(ctx)
if err != nil {
return err
}
_, err = run.Update().SetFinishedAt(time.Now()).SetState(status).SetReason(reason).Save(ctx)
if err != nil {
return err
}
return nil
}
// List the runs in an organization, optionally filtered out by workflow
func (r *WorkflowRunRepo) List(ctx context.Context, orgID, workflowID uuid.UUID, p *pagination.Options) (result []*biz.WorkflowRun, cursor string, err error) {
if p == nil {
return nil, "", errors.New("pagination options is required")
}
orgQuery := r.data.db.Organization.Query().Where(organization.ID(orgID))
// Skip the runs that have a workflow marked as deleted
wfQuery := orgQuery.QueryWorkflows().Where(workflow.DeletedAtIsNil())
if workflowID != uuid.Nil {
wfQuery = wfQuery.Where(workflow.ID(workflowID))
}
wfRunsQuery := wfQuery.QueryWorkflowruns().
Order(ent.Desc(workflowrun.FieldCreatedAt)).
Limit(p.Limit + 1).WithWorkflow()
if p.Cursor != nil {
wfRunsQuery = wfRunsQuery.Where(
func(s *sql.Selector) {
s.Where(sql.CompositeLT([]string{s.C(workflowrun.FieldCreatedAt), s.C(workflowrun.FieldID)}, p.Cursor.Timestamp, p.Cursor.ID))
})
}
workflowRuns, err := wfRunsQuery.All(ctx)
if err != nil {
return
}
for i, wr := range workflowRuns {
// Check if there are additional items for another page
// if so, set the cursor to the last item in the window
if i == p.Limit {
prevwr := workflowRuns[i-1]
cursor = pagination.EncodeCursor(prevwr.CreatedAt, prevwr.ID)
continue
}
result = append(result, entWrToBizWr(wr))
}
return result, cursor, nil
}
func (r *WorkflowRunRepo) ListNotFinishedOlderThan(ctx context.Context, olderThan time.Time) ([]*biz.WorkflowRun, error) {
// TODO: Look into adding upper bound on the createdAt column to prevent full table scans
// For now this is fine especially because we have a composite index
workflowRuns, err := r.data.db.WorkflowRun.Query().
Where(workflowrun.CreatedAtLTE(olderThan)).
Where(workflowrun.StateEQ(biz.WorkflowRunInitialized)).
All(ctx)
if err != nil {
return nil, err
}
result := make([]*biz.WorkflowRun, 0, len(workflowRuns))
for _, wr := range workflowRuns {
result = append(result, entWrToBizWr(wr))
}
return result, nil
}
func (r *WorkflowRunRepo) Expire(ctx context.Context, id uuid.UUID) error {
return r.data.db.WorkflowRun.UpdateOneID(id).SetState(biz.WorkflowRunExpired).Exec(ctx)
}
func entWrToBizWr(wr *ent.WorkflowRun) *biz.WorkflowRun {
r := &biz.WorkflowRun{
ID: wr.ID,
CreatedAt: toTimePtr(wr.CreatedAt),
FinishedAt: toTimePtr(wr.FinishedAt),
State: string(wr.State),
Reason: wr.Reason,
RunURL: wr.RunURL,
RunnerType: wr.RunnerType,
AttestationRef: wr.AttestationRef,
}
if cv := wr.Edges.ContractVersion; cv != nil {
r.ContractVersionID = cv.ID
}
if wf := wr.Edges.Workflow; wf != nil {
r.Workflow = entWFToBizWF(wf, nil)
}
return r
}