Skip to content

Commit

Permalink
Allow the registry to pull from all HTTP sources
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Nov 13, 2024
1 parent 6ab2efc commit 4d72846
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 127 deletions.
2 changes: 1 addition & 1 deletion themes/default/layouts/partials/registry/package/icon.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ $url := printf "/logos/pkg/%s.svg" .name }}
{{ if ne .logo_url "" }}
{{ if and (isset .logo_url) (ne .logo_url "") }}
<!-- It is ok to load the logo_url directly without sanitizing it since we will validate packages before we add them to the registry. -->
{{ $url = .logo_url }}
{{ end }}
Expand Down
71 changes: 43 additions & 28 deletions tools/resourcedocsgen/cmd/docs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,13 @@ func genResourceDocsForPackageFromRegistryMetadata(
metadata pkg.PackageMeta, docsOutDir, packageTreeJSONOutDir string,
) error {
glog.Infoln("Generating docs for", metadata.Name)
if metadata.RepoURL == "" {
return errors.Errorf("metadata for package %q does not contain the repo_url", metadata.Name)
}

schemaFilePath := fmt.Sprintf(defaultSchemaFilePathFormat, metadata.Name)
if metadata.SchemaFilePath != "" {
schemaFilePath = metadata.SchemaFilePath
}

// Make sure the schema file path does not have a leading slash.
// We'll add in the URL format below. It's easier to read that way.
schemaFilePath = strings.TrimPrefix(schemaFilePath, "/")

repoSlug, err := getRepoSlug(metadata.RepoURL)
schemaFileURL, err := getSchemaFileURL(metadata)
if err != nil {
return errors.WithMessage(err, "could not get repo slug")
return fmt.Errorf("failed to get schema_file_url: %w", err)
}

glog.Infoln("Reading remote schema file from VCS")
// TODO: Support raw URLs for other VCS too.
schemaFileURL := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s", repoSlug, metadata.Version, schemaFilePath)

resp, err := http.Get(schemaFileURL) //nolint:gosec
if err != nil {
return errors.Wrapf(err, "reading schema file from VCS %s", schemaFileURL)
Expand All @@ -85,18 +71,19 @@ func genResourceDocsForPackageFromRegistryMetadata(
return errors.Wrapf(err, "reading response body from %s", schemaFileURL)
}

// The source schema can be in YAML format. If that's the case
// convert it to JSON first.
if strings.HasSuffix(schemaFilePath, ".yaml") {
schemaBytes, err = yaml.YAMLToJSON(schemaBytes)
if err != nil {
return errors.Wrap(err, "reading YAML schema")
}
}

var mainSpec pschema.PackageSpec
if err := json.Unmarshal(schemaBytes, &mainSpec); err != nil {
return errors.Wrap(err, "unmarshalling schema into a PackageSpec")

switch {
// The source schema can be in YAML format.
case strings.HasSuffix(schemaFileURL, ".yaml"):
if err := yaml.Unmarshal(schemaBytes, &mainSpec); err != nil {
return errors.Wrap(err, "unmarshalling YAML schema into PackageSpec")
}
// If we don't have another format, assume JSON.
default:
if err := json.Unmarshal(schemaBytes, &mainSpec); err != nil {
return errors.Wrap(err, "unmarshalling JSON schema into PackageSpec")
}
}

pulPkg, genctx, err := getPulumiPackageFromSchema(docsOutDir, mainSpec)
Expand All @@ -117,6 +104,34 @@ func genResourceDocsForPackageFromRegistryMetadata(
return nil
}

func getSchemaFileURL(metadata pkg.PackageMeta) (string, error) {
if metadata.SchemaFileURL != "" {
return metadata.SchemaFileURL, nil
}

// We don't have an explicit SchemaFileURL, so migrate from SchemaFilePath.

if metadata.RepoURL == "" {
return "", errors.Errorf("metadata for package %q does not contain the repo_url", metadata.Name)
}

schemaFilePath := fmt.Sprintf(defaultSchemaFilePathFormat, metadata.Name)
if p := metadata.SchemaFilePath; p != "" { //nolint:staticcheck
schemaFilePath = p
}

// Make sure the schema file path does not have a leading slash.
// We'll add in the URL format below. It's easier to read that way.
schemaFilePath = strings.TrimPrefix(schemaFilePath, "/")

repoSlug, err := getRepoSlug(metadata.RepoURL)
if err != nil {
return "", errors.WithMessage(err, "could not get repo slug")
}

return fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s", repoSlug, metadata.Version, schemaFilePath), nil
}

func getRegistryPackagesPath(repoPath string) string {
return filepath.Join(repoPath, "themes", "default", "data", "registry", "packages")
}
Expand Down
47 changes: 34 additions & 13 deletions tools/resourcedocsgen/cmd/docs/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ import (
"github.com/stretchr/testify/require"
)

//nolint:paralleltest // resourceDocsFromRegistryCmd relies on global state.
func TestGenerateDocsSinglePackage(t *testing.T) {
// Set up the correct directory structure:
registryDir := t.TempDir()
util.WriteFile(t,
filepath.Join(registryDir, "themes", "default", "data", "registry", "packages", "random.yaml"),
`category: Utility
t.Parallel()
const partialMetadata = `category: Utility
component: false
description: A Pulumi package to safely use randomness in Pulumi programs.
featured: false
Expand All @@ -38,10 +34,14 @@ native: false
package_status: ga
publisher: Pulumi
repo_url: https://github.com/pulumi/pulumi-random
schema_file_path: provider/cmd/pulumi-resource-random/schema.json
title: random
updated_on: 1729058626
version: v4.16.7`)
version: v4.16.7`
registryDir := t.TempDir()
util.WriteFile(t,
filepath.Join(registryDir, "themes", "default", "data", "registry", "packages", "random.yaml"),
partialMetadata+`
schema_file_path: provider/cmd/pulumi-resource-random/schema.json`)
basePackageTreeJSONOutDir := t.TempDir()
baseDocsOutDir := t.TempDir()

Expand All @@ -53,12 +53,33 @@ version: v4.16.7`)
"--registryDir", registryDir,
})
require.NoError(t, cmd.Execute())
t.Run("docs", func(t *testing.T) { util.AssertDirEqual(t, baseDocsOutDir) })
t.Run("tree", func(t *testing.T) { util.AssertDirEqual(t, basePackageTreeJSONOutDir) })
t.Run("docs", func(t *testing.T) { t.Parallel(); util.AssertDirEqual(t, baseDocsOutDir) })
t.Run("tree", func(t *testing.T) { t.Parallel(); util.AssertDirEqual(t, basePackageTreeJSONOutDir) })
t.Run("check-schema-file-url", func(t *testing.T) {
t.Parallel()
registryDirV2 := t.TempDir()
util.WriteFile(t,
filepath.Join(registryDirV2, "themes", "default", "data", "registry", "packages", "random.yaml"),
partialMetadata+`
+schema_file_url: https://raw.githubusercontent.com/pulumi/pulumi-random/v4.16.7/provider/cmd/pulumi-resource-random/schema.json`) //nolint:lll
basePackageTreeJSONOutDirV2 := t.TempDir()
baseDocsOutDirV2 := t.TempDir()

cmd := resourceDocsFromRegistryCmd()
cmd.SetArgs([]string{
"random", /* pkgName */
"--baseDocsOutDir", baseDocsOutDirV2,
"--basePackageTreeJSONOutDir", basePackageTreeJSONOutDirV2,
"--registryDir", registryDirV2,
})
require.NoError(t, cmd.Execute())
util.AssertDirsEqual(t, baseDocsOutDir, baseDocsOutDirV2)
util.AssertDirsEqual(t, basePackageTreeJSONOutDir, basePackageTreeJSONOutDirV2)
})
}

//nolint:paralleltest // resourceDocsFromRegistryCmd relies on global state.
func TestGenerateDocsAllPackage(t *testing.T) {
t.Parallel()
// Set up the correct directory structure:
registryDir := t.TempDir()
util.WriteFile(t,
Expand Down Expand Up @@ -106,6 +127,6 @@ version: v2.6.1
"--registryDir", registryDir,
})
require.NoError(t, cmd.Execute())
t.Run("docs", func(t *testing.T) { util.AssertDirEqual(t, baseDocsOutDir) })
t.Run("tree", func(t *testing.T) { util.AssertDirEqual(t, basePackageTreeJSONOutDir) })
t.Run("docs", func(t *testing.T) { t.Parallel(); util.AssertDirEqual(t, baseDocsOutDir) })
t.Run("tree", func(t *testing.T) { t.Parallel(); util.AssertDirEqual(t, basePackageTreeJSONOutDir) })
}
Loading

0 comments on commit 4d72846

Please sign in to comment.