Skip to content

Commit 899e81d

Browse files
authored
feat(api): get workflowRuns by digest (#344)
--------- Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 2ff72da commit 899e81d

File tree

19 files changed

+491
-134
lines changed

19 files changed

+491
-134
lines changed

app/cli/internal/action/workflow_run_describe.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ func NewWorkflowRunDescribe(cfg *ActionsOpts) *WorkflowRunDescribe {
8585

8686
func (action *WorkflowRunDescribe) Run(runID string, verify bool, publicKey string) (*WorkflowRunItemFull, error) {
8787
client := pb.NewWorkflowRunServiceClient(action.cfg.CPConnection)
88-
resp, err := client.View(context.Background(), &pb.WorkflowRunServiceViewRequest{Id: runID})
88+
resp, err := client.View(context.Background(), &pb.WorkflowRunServiceViewRequest{
89+
Ref: &pb.WorkflowRunServiceViewRequest_Id{Id: runID},
90+
})
8991
if err != nil {
9092
return nil, err
9193
}

app/controlplane/api/controlplane/v1/workflow_run.pb.go

Lines changed: 133 additions & 90 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/controlplane/v1/workflow_run.pb.validate.go

Lines changed: 58 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/api/controlplane/v1/workflow_run.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ message WorkflowRunServiceListResponse {
107107
}
108108

109109
message WorkflowRunServiceViewRequest {
110-
string id = 1 [(validate.rules).string.uuid = true];
110+
// It can search by either ID or digest
111+
oneof ref {
112+
option (validate.required) = true;
113+
114+
string id = 1 [(validate.rules).string.uuid = true];
115+
string digest = 2 [(validate.rules).string = {min_len: 1}];
116+
}
111117
}
112118

113119
message WorkflowRunServiceViewResponse {

app/controlplane/api/gen/frontend/controlplane/v1/workflow_run.ts

Lines changed: 21 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/biz/mocks/WorkflowRunRepo.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/biz/workflow.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type WorkflowRepo interface {
4949

5050
type CreateOpts struct {
5151
Name, OrgID, Project, Team, ContractID string
52+
// Public means that the associated workflow runs, attestations and materials
53+
// are reachable by other users, regardless of their organization
54+
Public bool
5255
}
5356

5457
type WorkflowUseCase struct {

app/controlplane/internal/biz/workflowcontract.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package biz
1818
import (
1919
"context"
2020
"errors"
21+
"fmt"
2122
"time"
2223

2324
schemav1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
@@ -152,7 +153,14 @@ func (uc *WorkflowContractUseCase) FindVersionByID(ctx context.Context, versionI
152153
return nil, err
153154
}
154155

155-
return uc.repo.FindVersionByID(ctx, versionUUID)
156+
r, err := uc.repo.FindVersionByID(ctx, versionUUID)
157+
if err != nil {
158+
return nil, fmt.Errorf("finding contract version: %w", err)
159+
} else if r == nil {
160+
return nil, NewErrNotFound("contract version")
161+
}
162+
163+
return r, nil
156164
}
157165

158166
func (uc *WorkflowContractUseCase) Update(ctx context.Context, orgID, contractID, name string, schema *schemav1.CraftingSchema) (*WorkflowContractWithVersion, error) {

app/controlplane/internal/biz/workflowrun.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const (
6464
type WorkflowRunRepo interface {
6565
Create(ctx context.Context, workflowID, robotaccountID, contractVersion uuid.UUID, runURL, runnerType string, casBackends []uuid.UUID) (*WorkflowRun, error)
6666
FindByID(ctx context.Context, ID uuid.UUID) (*WorkflowRun, error)
67+
FindByAttestationDigest(ctx context.Context, digest string) (*WorkflowRun, error)
6768
FindByIDInOrg(ctx context.Context, orgID, ID uuid.UUID) (*WorkflowRun, error)
6869
MarkAsFinished(ctx context.Context, ID uuid.UUID, status WorkflowRunStatus, reason string) error
6970
SaveAttestation(ctx context.Context, ID uuid.UUID, att *dsse.Envelope, digest string) error
@@ -247,7 +248,8 @@ func (uc *WorkflowRunUseCase) List(ctx context.Context, orgID, workflowID string
247248
return uc.wfRunRepo.List(ctx, orgUUID, workflowUUID, p)
248249
}
249250

250-
func (uc *WorkflowRunUseCase) View(ctx context.Context, orgID, runID string) (*WorkflowRun, error) {
251+
// Returns the workflow run with the provided ID if it belongs to the org or its public
252+
func (uc *WorkflowRunUseCase) GetByIDInOrgOrPublic(ctx context.Context, orgID, runID string) (*WorkflowRun, error) {
251253
orgUUID, err := uuid.Parse(orgID)
252254
if err != nil {
253255
return nil, NewErrInvalidUUID(err)
@@ -258,7 +260,41 @@ func (uc *WorkflowRunUseCase) View(ctx context.Context, orgID, runID string) (*W
258260
return nil, NewErrInvalidUUID(err)
259261
}
260262

261-
return uc.wfRunRepo.FindByIDInOrg(ctx, orgUUID, runUUID)
263+
wfrun, err := uc.wfRunRepo.FindByID(ctx, runUUID)
264+
if err != nil {
265+
return nil, fmt.Errorf("finding workflow run: %w", err)
266+
}
267+
268+
// If the workflow is public or belongs to the org we can return it
269+
return workflowRunInOrgOrPublic(wfrun, orgUUID)
270+
}
271+
272+
func (uc *WorkflowRunUseCase) GetByDigestInOrgOrPublic(ctx context.Context, orgID, digest string) (*WorkflowRun, error) {
273+
orgUUID, err := uuid.Parse(orgID)
274+
if err != nil {
275+
return nil, NewErrInvalidUUID(err)
276+
}
277+
278+
if _, err := v1.NewHash(digest); err != nil {
279+
return nil, NewErrValidation(fmt.Errorf("invalid digest format: %w", err))
280+
}
281+
282+
wfrun, err := uc.wfRunRepo.FindByAttestationDigest(ctx, digest)
283+
if err != nil {
284+
return nil, fmt.Errorf("finding workflow run: %w", err)
285+
}
286+
287+
// If the workflow is public or belongs to the org we can return it
288+
return workflowRunInOrgOrPublic(wfrun, orgUUID)
289+
}
290+
291+
// filter the workflow runs that belong to the org or are public
292+
func workflowRunInOrgOrPublic(wfRun *WorkflowRun, orgID uuid.UUID) (*WorkflowRun, error) {
293+
if wfRun == nil || (wfRun.Workflow.OrgID != orgID && !wfRun.Workflow.Public) {
294+
return nil, NewErrNotFound("workflow run")
295+
}
296+
297+
return wfRun, nil
262298
}
263299

264300
// Implements https://pkg.go.dev/entgo.io/ent/schema/field#EnumValues

0 commit comments

Comments
 (0)