Skip to content

Commit

Permalink
refactor: expose source type on phase
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Nov 18, 2024
1 parent 35c1d78 commit 4d9a5e0
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 20 deletions.
27 changes: 16 additions & 11 deletions cmd/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ func NewMockResource() *MockResource {

// MockSource implements a simple source for our mock resources
type MockSource struct {
typ string
}

func NewMockSource() *MockSource {
return &MockSource{}
func NewMockSource(typ string) *MockSource {
return &MockSource{typ: typ}
}

func (m *MockSource) Type() string {
return m.typ
}

func (m *MockSource) View(_ context.Context, _, _ core.Metadata, r *MockResource) error {
Expand All @@ -41,9 +46,9 @@ func run(ctx context.Context) error {
ccPipeline := glu.NewPipeline(glu.Name("checkout"), NewMockResource)

// OCI phase
ociSource := NewMockSource()
ociSource := NewMockSource("oci")
ociPhase, err := phases.New(
glu.Name("oci", glu.Label("type", "oci")),
glu.Name("oci"),
ccPipeline,
ociSource,
)
Expand All @@ -52,7 +57,7 @@ func run(ctx context.Context) error {
}

// Staging phase
stagingSource := NewMockSource()
stagingSource := NewMockSource("git")
stagingPhase, err := phases.New(
glu.Name("staging",
glu.Label("environment", "staging"),
Expand All @@ -67,7 +72,7 @@ func run(ctx context.Context) error {
}

// Production phases
prodEastSource := NewMockSource()
prodEastSource := NewMockSource("git")
phases.New(
glu.Name("production-east-1",
glu.Label("environment", "production"),
Expand All @@ -78,7 +83,7 @@ func run(ctx context.Context) error {
core.PromotesFrom(stagingPhase),
)

prodWestSource := NewMockSource()
prodWestSource := NewMockSource("git")
phases.New(
glu.Name("production-west-1",
glu.Label("environment", "production"),
Expand All @@ -97,9 +102,9 @@ func run(ctx context.Context) error {
fdPipeline := glu.NewPipeline(glu.Name("billing"), NewMockResource)

// OCI phase
fdOciSource := NewMockSource()
fdOciSource := NewMockSource("oci")
fdOciPhase, err := phases.New(
glu.Name("oci", glu.Label("type", "oci")),
glu.Name("oci"),
fdPipeline,
fdOciSource,
)
Expand All @@ -108,7 +113,7 @@ func run(ctx context.Context) error {
}

// Staging phase
fdStagingSource := NewMockSource()
fdStagingSource := NewMockSource("git")
fdStagingPhase, err := phases.New(
glu.Name("staging",
glu.Label("environment", "staging"),
Expand All @@ -123,7 +128,7 @@ func run(ctx context.Context) error {
}

// Production phase
fdProdSource := NewMockSource()
fdProdSource := NewMockSource("git")
phases.New(
glu.Name("production",
glu.Label("environment", "production"),
Expand Down
1 change: 1 addition & 0 deletions pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Resource interface {
// These types can be registered on pipelines and can depend upon on another for promotion.
type Phase interface {
Metadata() Metadata
SourceType() string
Get(context.Context) (any, error)
Promote(context.Context) error
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/phases/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// Source is an interface around storage for resources.
type Source[R core.Resource] interface {
Type() string
View(_ context.Context, pipeline, phase core.Metadata, _ R) error
}

Expand Down Expand Up @@ -58,6 +59,10 @@ func (i *Phase[R]) Metadata() core.Metadata {
return i.meta
}

func (i *Phase[R]) SourceType() string {
return i.source.Type()
}

func (i *Phase[R]) Get(ctx context.Context) (any, error) {
return i.GetResource(ctx)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/src/git/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ type Source[A Resource] struct {
proposalOptions ProposalOption
}

func (s Source[A]) Type() string {
return "git"
}

type ProposalOption struct {
Labels []string
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/src/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func New[R Resource](resolver Resolver) *Source[R] {
}
}

func (s *Source[R]) Type() string {
return "oci"
}

func (s *Source[R]) View(ctx context.Context, _, _ core.Metadata, r R) error {
desc, err := s.resolver.Resolve(ctx)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ type pipelineResponse struct {
}

type phaseResponse struct {
Name string `json:"name"`
DependsOn string `json:"depends_on,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Value interface{} `json:"value,omitempty"`
Name string `json:"name"`
DependsOn string `json:"depends_on,omitempty"`
SourceType string `json:"source_type,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Value interface{} `json:"value,omitempty"`
}

func (s *Server) createPhaseResponse(phase core.Phase, dependencies map[core.Phase]core.Phase) phaseResponse {
Expand All @@ -101,9 +102,10 @@ func (s *Server) createPhaseResponse(phase core.Phase, dependencies map[core.Pha
}

return phaseResponse{
Name: phase.Metadata().Name,
DependsOn: dependsOn,
Labels: labels,
Name: phase.Metadata().Name,
DependsOn: dependsOn,
Labels: labels,
SourceType: phase.SourceType(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { promotePhase } from '@/services/api';

const PhaseNode = ({ data }: NodeProps<PhaseNodeType>) => {
const getIcon = () => {
switch (data.labels?.type ?? '') {
switch (data.source_type ?? '') {
case 'oci':
return <Package className="h-4 w-4" />;
default:
Expand Down
3 changes: 2 additions & 1 deletion ui/src/components/pipeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ function getElements(pipeline: PipelineType): FlowPipeline {
pipeline: pipeline.name,
name: phase.name,
labels: phase.labels || {},
depends_on: phase.depends_on
depends_on: phase.depends_on,
source_type: phase.source_type
},
extent: 'parent'
};
Expand Down
1 change: 1 addition & 0 deletions ui/src/types/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type PhaseNodeData = {
name: string;
labels?: Record<string, string>;
depends_on?: string;
source_type?: string;
};

export type PhaseNode = Node<PhaseNodeData, 'phase'>;
Expand Down
1 change: 1 addition & 0 deletions ui/src/types/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Pipeline {
export interface Phase {
name: string;
depends_on?: string;
source_type?: string;
labels?: Record<string, string>;
value?: unknown;
}
Expand Down

0 comments on commit 4d9a5e0

Please sign in to comment.