Skip to content

Commit 6cc5906

Browse files
authored
feat: extensions readme generator (#191)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 292d157 commit 6cc5906

File tree

25 files changed

+650
-243
lines changed

25 files changed

+650
-243
lines changed

app/cli/cmd/available_integration_describe.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sort"
2323

2424
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
25+
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/sdk/v1"
2526
"github.com/jedib0t/go-pretty/v6/table"
2627
"github.com/jedib0t/go-pretty/v6/text"
2728
"github.com/spf13/cobra"
@@ -92,7 +93,7 @@ func availableIntegrationDescribeTableOutput(items []*action.AvailableIntegratio
9293
}
9394

9495
// render de-normalized schema format
95-
func renderSchemaTable(tableTitle string, properties action.SchemaPropertiesMap) error {
96+
func renderSchemaTable(tableTitle string, properties sdk.SchemaPropertiesMap) error {
9697
if len(properties) == 0 {
9798
return nil
9899
}

app/cli/cmd/registered_integration_add.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323

2424
"github.com/chainloop-dev/chainloop/app/cli/internal/action"
25+
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/sdk/v1"
2526
"github.com/santhosh-tekuri/jsonschema/v5"
2627
"github.com/spf13/cobra"
2728
)
@@ -98,7 +99,7 @@ func parseAndValidateOpts(opts []string, schema *action.JSONSchema) (map[string]
9899
// parseKeyValOpts performs two steps
99100
// 1 - Split the options into key/value pairs
100101
// 2 - Cast the values to the expected type defined in the schema
101-
func parseKeyValOpts(opts []string, propertiesMap action.SchemaPropertiesMap) (map[string]any, error) {
102+
func parseKeyValOpts(opts []string, propertiesMap sdk.SchemaPropertiesMap) (map[string]any, error) {
102103
// 1 - Split the options into key/value pairs
103104
var options = make(map[string]any)
104105
for _, opt := range opts {

app/cli/internal/action/available_integration_list.go

Lines changed: 9 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
package action
1717

1818
import (
19-
"bytes"
2019
"context"
2120
"errors"
2221
"fmt"
23-
"sort"
2422

2523
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
24+
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/sdk/v1"
2625
"github.com/santhosh-tekuri/jsonschema/v5"
2726
)
2827

@@ -45,22 +44,8 @@ type JSONSchema struct {
4544
Raw string `json:"schema"`
4645
// Parsed schema so it can be used for validation or other purposes
4746
// It's not shown in the json output
48-
Parsed *jsonschema.Schema `json:"-"`
49-
Properties SchemaPropertiesMap `json:"-"`
50-
}
51-
52-
type SchemaPropertiesMap map[string]*SchemaProperty
53-
type SchemaProperty struct {
54-
// Name of the property
55-
Name string
56-
// optional description
57-
Description string
58-
// Type of the property (string, boolean, number)
59-
Type string
60-
// If the property is required
61-
Required bool
62-
// Optional format (email, host)
63-
Format string
47+
Parsed *jsonschema.Schema `json:"-"`
48+
Properties sdk.SchemaPropertiesMap `json:"-"`
6449
}
6550

6651
func NewAvailableIntegrationList(cfg *ActionsOpts) *AvailableIntegrationList {
@@ -107,24 +92,24 @@ func pbAvailableIntegrationItemToAction(in *pb.IntegrationAvailableItem) (*Avail
10792

10893
// Parse the schemas so they can be used for validation or other purposes
10994
var err error
110-
i.Registration.Parsed, err = compileJSONSchema(foType.GetRegistrationSchema())
95+
i.Registration.Parsed, err = sdk.CompileJSONSchema(foType.GetRegistrationSchema())
11196
if err != nil {
11297
return nil, fmt.Errorf("failed to compile registration schema: %w", err)
11398
}
11499

115-
i.Attachment.Parsed, err = compileJSONSchema(foType.GetAttachmentSchema())
100+
i.Attachment.Parsed, err = sdk.CompileJSONSchema(foType.GetAttachmentSchema())
116101
if err != nil {
117102
return nil, fmt.Errorf("failed to compile registration schema: %w", err)
118103
}
119104

120105
// Calculate the properties map
121-
i.Registration.Properties = make(SchemaPropertiesMap)
122-
if err := calculatePropertiesMap(i.Registration.Parsed, &i.Registration.Properties); err != nil {
106+
i.Registration.Properties = make(sdk.SchemaPropertiesMap)
107+
if err := sdk.CalculatePropertiesMap(i.Registration.Parsed, &i.Registration.Properties); err != nil {
123108
return nil, fmt.Errorf("failed to calculate registration properties: %w", err)
124109
}
125110

126-
i.Attachment.Properties = make(SchemaPropertiesMap)
127-
if err := calculatePropertiesMap(i.Attachment.Parsed, &i.Attachment.Properties); err != nil {
111+
i.Attachment.Properties = make(sdk.SchemaPropertiesMap)
112+
if err := sdk.CalculatePropertiesMap(i.Attachment.Parsed, &i.Attachment.Properties); err != nil {
128113
return nil, fmt.Errorf("failed to calculate attachment properties: %w", err)
129114
}
130115

@@ -133,83 +118,3 @@ func pbAvailableIntegrationItemToAction(in *pb.IntegrationAvailableItem) (*Avail
133118

134119
return i, nil
135120
}
136-
137-
func compileJSONSchema(in []byte) (*jsonschema.Schema, error) {
138-
// Parse the schemas
139-
compiler := jsonschema.NewCompiler()
140-
// Enable format validation
141-
compiler.AssertFormat = true
142-
// Show description
143-
compiler.ExtractAnnotations = true
144-
145-
if err := compiler.AddResource("schema.json", bytes.NewReader(in)); err != nil {
146-
return nil, fmt.Errorf("failed to compile schema: %w", err)
147-
}
148-
149-
return compiler.Compile("schema.json")
150-
}
151-
152-
// calculate a map with all the properties of a schema
153-
func calculatePropertiesMap(s *jsonschema.Schema, m *SchemaPropertiesMap) error {
154-
if m == nil {
155-
return nil
156-
}
157-
158-
// Schema with reference
159-
if s.Ref != nil {
160-
return calculatePropertiesMap(s.Ref, m)
161-
}
162-
163-
// Appended schemas
164-
if s.AllOf != nil {
165-
for _, s := range s.AllOf {
166-
if err := calculatePropertiesMap(s, m); err != nil {
167-
return err
168-
}
169-
}
170-
}
171-
172-
if s.Properties != nil {
173-
requiredMap := make(map[string]bool)
174-
for _, r := range s.Required {
175-
requiredMap[r] = true
176-
}
177-
178-
for k, v := range s.Properties {
179-
if err := calculatePropertiesMap(v, m); err != nil {
180-
return err
181-
}
182-
183-
var required = requiredMap[k]
184-
(*m)[k] = &SchemaProperty{
185-
Name: k,
186-
Type: v.Types[0],
187-
Required: required,
188-
Description: v.Description,
189-
Format: v.Format,
190-
}
191-
}
192-
}
193-
194-
// We return the map sorted
195-
// This is not strictly necessary but it makes the output more readable
196-
// and it's easier to test
197-
198-
// Sort the keys
199-
keys := make([]string, 0, len(*m))
200-
for k := range *m {
201-
keys = append(keys, k)
202-
}
203-
204-
sort.Strings(keys)
205-
206-
// Create a new map with the sorted keys
207-
newMap := make(SchemaPropertiesMap)
208-
for _, k := range keys {
209-
newMap[k] = (*m)[k]
210-
}
211-
212-
*m = newMap
213-
214-
return nil
215-
}

app/cli/internal/action/available_integration_list_test.go

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### Dependency-Track fan-out Extension
2+
3+
This extension implements sending cycloneDX Software Bill of Materials (SBOM) to Dependency-Track.
4+
5+
See https://docs.chainloop.dev/guides/dependency-track/
6+
7+
8+
## Registration Input Schema
9+
10+
|Field|Type|Required|Description|
11+
|---|---|---|---|
12+
|allowAutoCreate|boolean|no|Support of creating projects on demand|
13+
|apiKey|string|yes|The API key to use for authentication|
14+
|instanceURI|string (uri)|yes|The URL of the Dependency-Track instance|
15+
16+
```json
17+
{
18+
"$schema": "https://json-schema.org/draft/2020-12/schema",
19+
"$id": "https://github.com/chainloop-dev/chainloop/app/controlplane/extensions/core/dependency-track/v1/registration-request",
20+
"properties": {
21+
"instanceURI": {
22+
"type": "string",
23+
"format": "uri",
24+
"description": "The URL of the Dependency-Track instance"
25+
},
26+
"apiKey": {
27+
"type": "string",
28+
"description": "The API key to use for authentication"
29+
},
30+
"allowAutoCreate": {
31+
"type": "boolean",
32+
"description": "Support of creating projects on demand"
33+
}
34+
},
35+
"additionalProperties": false,
36+
"type": "object",
37+
"required": [
38+
"instanceURI",
39+
"apiKey"
40+
]
41+
}
42+
```
43+
44+
## Attachment Input Schema
45+
46+
|Field|Type|Required|Description|
47+
|---|---|---|---|
48+
|projectID|string|no|The ID of the existing project to send the SBOMs to|
49+
|projectName|string|no|The name of the project to create and send the SBOMs to|
50+
51+
```json
52+
{
53+
"$schema": "https://json-schema.org/draft/2020-12/schema",
54+
"$id": "https://github.com/chainloop-dev/chainloop/app/controlplane/extensions/core/dependency-track/v1/attachment-request",
55+
"oneOf": [
56+
{
57+
"required": [
58+
"projectID"
59+
],
60+
"title": "projectID"
61+
},
62+
{
63+
"required": [
64+
"projectName"
65+
],
66+
"title": "projectName"
67+
}
68+
],
69+
"properties": {
70+
"projectID": {
71+
"type": "string",
72+
"minLength": 1,
73+
"description": "The ID of the existing project to send the SBOMs to"
74+
},
75+
"projectName": {
76+
"type": "string",
77+
"minLength": 1,
78+
"description": "The name of the project to create and send the SBOMs to"
79+
}
80+
},
81+
"additionalProperties": false,
82+
"type": "object"
83+
}
84+
```

app/controlplane/extensions/core/dependencytrack/v1/client/sbom.go renamed to app/controlplane/extensions/core/dependency-track/v1/client/sbom.go

File renamed without changes.

app/controlplane/extensions/core/dependencytrack/v1/client/sbom_test.go renamed to app/controlplane/extensions/core/dependency-track/v1/client/sbom_test.go

File renamed without changes.

app/controlplane/extensions/core/dependencytrack/v1/extension.go renamed to app/controlplane/extensions/core/dependency-track/v1/extension.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"fmt"
2323

2424
schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
25-
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/core/dependencytrack/v1/client"
25+
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/core/dependency-track/v1/client"
2626
"github.com/chainloop-dev/chainloop/app/controlplane/extensions/sdk/v1"
2727
"github.com/go-kratos/kratos/v2/log"
2828
)
@@ -62,7 +62,7 @@ const description = "Send CycloneDX SBOMs to your Dependency-Track instance"
6262
func New(l log.Logger) (sdk.FanOut, error) {
6363
base, err := sdk.NewFanOut(
6464
&sdk.NewParams{
65-
ID: "dependencytrack",
65+
ID: "dependency-track",
6666
Version: "0.2",
6767
Description: description,
6868
Logger: l,

app/controlplane/extensions/core/dependencytrack/v1/extension_test.go renamed to app/controlplane/extensions/core/dependency-track/v1/extension_test.go

File renamed without changes.

app/controlplane/extensions/core/dependencytrack/v1/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)