forked from chainloop-dev/chainloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectversion.go
More file actions
152 lines (132 loc) · 4.61 KB
/
Copy pathprojectversion.go
File metadata and controls
152 lines (132 loc) · 4.61 KB
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
//
// Copyright 2024-2026 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/pkg/biz"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/project"
"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/projectversion"
"github.com/go-kratos/kratos/v2/log"
"github.com/google/uuid"
)
type ProjectVersionRepo struct {
data *Data
log *log.Helper
}
func NewProjectVersionRepo(data *Data, logger log.Logger) biz.ProjectVersionRepo {
return &ProjectVersionRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (r *ProjectVersionRepo) FindByProjectAndVersion(ctx context.Context, projectID uuid.UUID, version string) (*biz.ProjectVersion, error) {
pv, err := r.data.DB.ProjectVersion.Query().Where(projectversion.HasProjectWith(project.ID(projectID)), projectversion.VersionEQ(version)).Only(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if pv == nil {
return nil, biz.NewErrNotFound("Version")
}
return entProjectVersionToBiz(pv), nil
}
func (r *ProjectVersionRepo) Update(ctx context.Context, id uuid.UUID, updates *biz.ProjectVersionUpdateOpts) (*biz.ProjectVersion, error) {
if updates == nil {
updates = &biz.ProjectVersionUpdateOpts{}
}
// Only set released_at if it's not already set
existing, err := r.data.DB.ProjectVersion.Query().Where(projectversion.IDEQ(id), projectversion.DeletedAtIsNil()).Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
return nil, biz.NewErrNotFound("Version")
}
return nil, err
}
now := time.Now()
q := existing.Update().SetNillablePrerelease(updates.Prerelease).SetUpdatedAt(now)
// we are setting the value either false or true
if updates.Prerelease != nil {
// We are marking it as a release
if !*updates.Prerelease {
// if not set
if existing.ReleasedAt.IsZero() {
// Only set released_at if it's not already set
q.SetReleasedAt(now)
}
} else {
// We are resetting it to pre-release
q.ClearReleasedAt()
}
}
res, err := q.Save(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
} else if res == nil {
return nil, biz.NewErrNotFound("Version")
}
return entProjectVersionToBiz(res), nil
}
func (r *ProjectVersionRepo) Create(ctx context.Context, projectID uuid.UUID, version string, prerelease bool) (*biz.ProjectVersion, error) {
var res *ent.ProjectVersion
if err := WithTx(ctx, r.data.DB, func(tx *ent.Tx) error {
var err error
res, err = createProjectVersionWithTx(ctx, tx, projectID, version, prerelease)
return err
}); err != nil {
return nil, err
}
return entProjectVersionToBiz(res), nil
}
func createProjectVersionWithTx(ctx context.Context, tx *ent.Tx, projectID uuid.UUID, version string, prerelease bool) (*ent.ProjectVersion, error) {
if version == "" {
return nil, biz.NewErrValidationStr("version must not be empty")
}
// Update all existing versions of this project to not be the latest
if err := tx.ProjectVersion.Update().
Where(
projectversion.ProjectID(projectID),
projectversion.DeletedAtIsNil(),
projectversion.Latest(true),
).SetLatest(false).Exec(ctx); err != nil {
return nil, err
}
return tx.ProjectVersion.Create().
SetProjectID(projectID).
SetVersion(version).
SetPrerelease(prerelease).
SetLatest(true).
Save(ctx)
}
func findProjectVersionWithClient(ctx context.Context, client *ent.Client, projectID uuid.UUID, version string) (*ent.ProjectVersion, error) {
return client.ProjectVersion.Query().
Where(
projectversion.ProjectID(projectID),
projectversion.VersionEQ(version),
projectversion.DeletedAtIsNil(),
).Only(ctx)
}
func entProjectVersionToBiz(v *ent.ProjectVersion) *biz.ProjectVersion {
pv := &biz.ProjectVersion{
ID: v.ID,
Version: v.Version,
Prerelease: v.Prerelease,
TotalWorkflowRuns: v.WorkflowRunCount,
CreatedAt: toTimePtr(v.CreatedAt),
ReleasedAt: toTimePtr(v.ReleasedAt),
LastRunAt: toTimePtr(v.LastRunAt),
ProjectID: v.ProjectID,
}
return pv
}