Skip to content

Commit

Permalink
Transport pkgversion check to resourcedocsgen
Browse files Browse the repository at this point in the history
  • Loading branch information
guineveresaenger committed May 31, 2024
1 parent 99a0b59 commit f76e20f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
115 changes: 115 additions & 0 deletions tools/resourcedocsgen/cmd/pkgversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/pulumi/registry/tools/resourcedocsgen/pkg"
"github.com/spf13/cobra"

"io/ioutil"
"net/http"
"strings"
)

func CheckVersion() *cobra.Command {

var repoSlug string
cmd := &cobra.Command{
Use: "pkgversion",
Short: "Check a Pulumi package version",
Long: `Print the most recent version of a Pulumi package. If the most recent version of a Pulumi package is the same as the version published in the Pulumi Registry, print nothing.
The most recent version of a Pulumi package is taken to be the most recent release published in GitHub.
The version in the registry is defined in the YAML file at:
https://raw.githubusercontent.com/pulumi/registry/master/themes/default/data/registry/packages/${PKG#pulumi/pulumi-}.yaml`,
RunE: func(cmd *cobra.Command, args []string) error {

if strings.Contains(repoSlug, "https") || strings.Contains(repoSlug, "github.com") {
return errors.New(fmt.Sprintf("Expected repoSlug to be in the format of `owner/repo`"+
" but got %q", repoSlug))
}

repoName := ""
githubSlugParts := strings.Split(repoSlug, "/")
if len(githubSlugParts) > 0 {
repoName = githubSlugParts[1]
}

version, err := getLatestVersion(repoSlug)
if err != nil {
return err
}

pkgName := strings.TrimPrefix(repoName, "pulumi-")
pkgMetadata := fmt.Sprintf("https://raw.githubusercontent.com/pulumi/registry/master/themes/default/data/registry/packages/%s.yaml", pkgName)
regVersion, err := getRegistryVersion(pkgMetadata)
if err != nil {
return err
}

// print version tag if there's a difference, and not if there isn't
// we assume that the published latest version from the provider repo is the desired one, so any difference
// between versions should indicate an update to the registry version
if version != regVersion {
fmt.Println(version)
}
return nil
},
}

cmd.Flags().StringVar(&repoSlug, "repoSlug", "", "The repository slug e.g. pulumi/pulumi-provider")
cmd.MarkFlagRequired("repoSlug")
return cmd
}

func getLatestVersion(repoSlug string) (string, error) {
path := fmt.Sprintf("/repos/%s/releases/latest", repoSlug)
resp, err := pkg.GetGitHubAPI(path)

if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("getting latest version from https://api.github.com%s", path))
}

defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.Wrap(err, fmt.Sprintf("Could not find a release at https://api.github.com%s", path))
}

var tag pkg.GitHubTag
err = json.NewDecoder(resp.Body).Decode(&tag)

if err != nil {
return "", errors.Wrap(err, "failure reading contents of latest tag")
}

return tag.Name, nil
}

func getRegistryVersion(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", errors.Wrap(err, fmt.Sprintf("getting latest version from %s", url))
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.Wrap(err, "file not found")
}

contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "failure reading contents of remote file")
}

var meta pkg.PackageMeta
err = yaml.Unmarshal(contents, &meta)
if err != nil {
return "", errors.Wrap(err, "error unmarshalling yaml file")
}

return meta.Version, nil
}
1 change: 1 addition & 0 deletions tools/resourcedocsgen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func RootCmd() *cobra.Command {

rootCmd.AddCommand(docs.ResourceDocsCmd())
rootCmd.AddCommand(PackageMetadataCmd())
rootCmd.AddCommand(CheckVersion())

return rootCmd
}

0 comments on commit f76e20f

Please sign in to comment.