Skip to content

Commit

Permalink
Implement /devstate/chart
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jun 30, 2023
1 parent 960caa5 commit 8af202b
Show file tree
Hide file tree
Showing 19 changed files with 2,075 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ require (
github.com/ActiveState/termtest/xpty v0.6.0 // indirect
github.com/ActiveState/vt10x v1.3.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Heiko-san/mermaidgen v0.0.0-20190623222458-ec72afae378b // indirect
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 // indirect
Expand Down Expand Up @@ -108,6 +109,7 @@ require (
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/feloy/devfile-lifecycle v0.0.0-20230515183001-11d088157da2 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/apiserver-impl/devstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,15 @@ func (s *DefaultApiService) DevstateMetadataPut(ctx context.Context, metadata op
}
return openapi.Response(http.StatusOK, newContent), nil
}

func (s *DefaultApiService) DevstateChartGet(context.Context) (openapi.ImplResponse, error) {
chart, err := s.devfileState.GetFlowChart()
if err != nil {
return openapi.Response(http.StatusInternalServerError, openapi.GeneralError{
Message: fmt.Sprintf("Error building the Devfile cycle chart: %s", err),
}), nil
}
return openapi.Response(http.StatusOK, openapi.DevstateChartGet200Response{
Chart: chart,
}), nil
}
15 changes: 15 additions & 0 deletions pkg/apiserver-impl/devstate/chart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package devstate

import (
"errors"

"github.com/feloy/devfile-lifecycle/pkg/graph"
)

func (o *DevfileState) GetFlowChart() (string, error) {
g, err := graph.Build(o.Devfile.Data)
if err != nil {
return "", errors.New("error building graph")
}
return g.ToFlowchart().String(), nil
}
42 changes: 42 additions & 0 deletions pkg/apiserver-impl/devstate/chart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package devstate

import (
"testing"
)

func TestDevfileState_GetFlowChart(t *testing.T) {
tests := []struct {
name string
state func() DevfileState
want string
wantErr bool
}{
{
name: "with initial devfile",
state: func() DevfileState {
return NewDevfileState()
},
want: `graph TB
containers["containers"]
start["start"]
sync-all-containers["Sync All Sources"]
start -->|"dev"| containers
containers -->|"container running"| sync-all-containers
`,
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := tt.state()
got, err := o.GetFlowChart()
if (err != nil) != tt.wantErr {
t.Errorf("DevfileState.GetFlowChart() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DevfileState.GetFlowChart() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 8af202b

Please sign in to comment.