Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Idempotent module tags #1033

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/internal/github/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import (
"strings"
)

func parseTagsFromStdout(lines []string) ([]string, error) {
tags := make([]string, 0, len(lines))
type Tag struct {
Name string
Commit string
}

func parseTagsFromStdout(lines []string) ([]Tag, error) {
tags := make([]Tag, 0, len(lines))

for _, line := range lines {
if !strings.Contains(line, "refs/tags/") {
Expand All @@ -20,6 +25,7 @@ func parseTagsFromStdout(lines []string) ([]string, error) {
if len(fields) != 2 {
return nil, fmt.Errorf("invalid format for tag '%s', expected two fields", line)
}
commit := fields[0]

ref := fields[1]
if !strings.HasPrefix(ref, "refs/tags/") {
Expand All @@ -31,14 +37,15 @@ func parseTagsFromStdout(lines []string) ([]string, error) {
return nil, fmt.Errorf("invalid format for tag '%s', no version provided", line)
}

tags = append(tags, tag)
tags = append(tags, Tag{Name: tag, Commit: commit})
}

return tags, nil
}

// GetTags lists the tags of the remote repository and returns the refs/tags/ found
func (c Client) GetTags(repositoryUrl string) ([]string, error) {
// Order is not guaranteed
func (c Client) GetTags(repositoryUrl string) ([]Tag, error) {
done := c.cliThrottle()
defer done()

Expand Down
15 changes: 8 additions & 7 deletions src/internal/module/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"slices"

"github.com/opentofu/registry-stable/internal"
"github.com/opentofu/registry-stable/internal/github"

"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -42,13 +43,13 @@ func (m Module) BuildMetadata() (*Metadata, error) {
for _, t := range tags {
found := false
for _, v := range meta.Versions {
if v.Version == t {
if v.Version == t.Name {
found = true
break
}
}
if !found {
meta.Versions = append(meta.Versions, Version{Version: t})
meta.Versions = append(meta.Versions, Version{Version: t.Name, Ref: t.Commit})
}
}

Expand All @@ -60,22 +61,22 @@ func (m Module) BuildMetadata() (*Metadata, error) {
return &meta, nil
}

func (m Module) getSemverTags() ([]string, error) {
func (m Module) getSemverTags() ([]github.Tag, error) {
tags, err := m.Github.GetTags(m.RepositoryURL())
if err != nil {
return nil, err
}

var semverTags = make([]string, 0)
var semverTags = make([]github.Tag, 0)
for _, tag := range tags {
tagWithPrefix := fmt.Sprintf("v%s", internal.TrimTagPrefix(tag))
tagWithPrefix := fmt.Sprintf("v%s", internal.TrimTagPrefix(tag.Name))
if semver.IsValid(tagWithPrefix) {
semverTags = append(semverTags, tag)
}
}

semverSortFunc := func(a, b string) int {
return -semver.Compare(fmt.Sprintf(a), fmt.Sprintf(b))
semverSortFunc := func(a, b github.Tag) int {
return -semver.Compare(a.Name, b.Name)
}
slices.SortFunc(semverTags, semverSortFunc)

Expand Down
3 changes: 2 additions & 1 deletion src/internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// Version represents a single version of a module.
type Version struct {
Version string `json:"version"` // The version number of the provider. Correlates to a tag in the module repository
Ref string `json:"ref"` // Identifier that pins the version to a particular point in history
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be retrofitted into libregistry too.

}

// Metadata represents all the metadata for a module. This includes the list of
Expand Down Expand Up @@ -48,7 +49,7 @@ func (m Module) RSSURL() string {
// the file should just contain a link to GitHub to download the tarball, ie:
// git::https://github.com/terraform-aws-modules/terraform-aws-iam?ref=v5.30.0
func (m Module) VersionDownloadURL(version Version) string {
return fmt.Sprintf("git::%s?ref=%s", m.RepositoryURL(), version.Version)
return fmt.Sprintf("git::%s?ref=%s", m.RepositoryURL(), version.Ref)
}

// MetadataPath returns the path to the metadata file for the module.
Expand Down
4 changes: 2 additions & 2 deletions src/internal/provider/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func (p Provider) getSemverTags() ([]string, error) {

var semverTags = make([]string, 0)
for _, tag := range tags {
tagWithPrefix := fmt.Sprintf("v%s", internal.TrimTagPrefix(tag))
tagWithPrefix := fmt.Sprintf("v%s", internal.TrimTagPrefix(tag.Name))
if semver.IsValid(tagWithPrefix) {
semverTags = append(semverTags, tag)
semverTags = append(semverTags, tag.Name)
}
}

Expand Down