Skip to content

Commit

Permalink
Fixed issue resulting from GitHub tag ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Attwood committed Dec 2, 2022
1 parent 53d5d64 commit 56fa173
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
60 changes: 39 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ func (packageWrapper PackageWrapper) annotate(annotation, value string, remove,
repositoryChartsDir,
packageWrapper.ParsedVendor,
chartName,
version.Version,
)
helmChart, err := loader.Load(versionPath)
if err != nil {
Expand Down Expand Up @@ -415,6 +414,10 @@ func commitChanges(updatedList PackageList) error {

logrus.Info("Committing changes")

opts := git.AddOptions{
All: true,
}

for _, packageWrapper := range updatedList {
assetsPath := path.Join(
repositoryAssetsDir,
Expand All @@ -423,16 +426,20 @@ func commitChanges(updatedList PackageList) error {
chartsPath := path.Join(
repositoryChartsDir,
packageWrapper.ParsedVendor,
packageWrapper.SourceMetadata.Versions[0].Name)
packageWrapper.Name)

packagesPath := path.Join(
repositoryPackagesDir,
packageWrapper.ParsedVendor,
packageWrapper.SourceMetadata.Versions[0].Name)
packageWrapper.Name)

opts.Path = assetsPath
wt.AddWithOptions(&opts)
opts.Path = chartsPath
wt.AddWithOptions(&opts)
opts.Path = packagesPath
wt.AddWithOptions(&opts)

wt.Add(assetsPath)
wt.Add(chartsPath)
wt.Add(packagesPath)
}

wt.Add(indexFile)
Expand Down Expand Up @@ -716,6 +723,10 @@ func filterVersions(upstreamVersions repo.ChartVersions, fetch string, tracked [
logrus.Debug("No newer untracked versions found")
}
}
if len(upstreamVersions) == 0 {
err := fmt.Errorf("No versions available in upstream or all versions are marked pre-release")
return repo.ChartVersions{}, err
}
filteredVersions := make(repo.ChartVersions, 0)
allStoredVersions, err := getStoredVersions(upstreamVersions[0].Name)
if len(tracked) > 0 {
Expand Down Expand Up @@ -851,8 +862,12 @@ func conformPackage(packageWrapper PackageWrapper) error {

} else {
packageWrapper.Annotations[annotationCertified] = "partner"
packageWrapper.Annotations[annotationReleaseName] = packageWrapper.Name
packageWrapper.Annotations[annotationDisplayName] = packageWrapper.DisplayName
if packageWrapper.UpstreamYaml.ReleaseName != "" {
packageWrapper.Annotations[annotationReleaseName] = packageWrapper.UpstreamYaml.ReleaseName
} else {
packageWrapper.Annotations[annotationReleaseName] = packageWrapper.Name
}

conform.OverlayChartMetadata(helmChart, packageWrapper.UpstreamYaml.ChartYaml)

Expand Down Expand Up @@ -901,8 +916,11 @@ func conformPackage(packageWrapper PackageWrapper) error {
getRepoRoot(),
repositoryChartsDir,
packageWrapper.ParsedVendor,
helmChart.Metadata.Name,
helmChart.Metadata.Version)
helmChart.Metadata.Name)

if _, err := os.Stat(chartsPath); !os.IsNotExist(err) {
os.RemoveAll(chartsPath)
}

err = saveChart(helmChart, assetsPath, chartsPath)
if err != nil {
Expand Down Expand Up @@ -1471,43 +1489,43 @@ func validateRepo(c *cli.Context) {
logrus.Error(err)
}

if len(directoryComparison.Modified) > 0 {
if len(directoryComparison.Added) > 0 {
outString := ""
for dirPath := range validatePaths {
if len(validatePaths[dirPath].Modified) > 0 {
if len(validatePaths[dirPath].Added) > 0 {
outString += fmt.Sprintf("\n - %s", dirPath)
stringJoiner := fmt.Sprintf("\n - %s", dirPath)
fileList := strings.Join(validatePaths[dirPath].Modified[:], stringJoiner)
fileList := strings.Join(validatePaths[dirPath].Added[:], stringJoiner)
outString += fileList
}
}
logrus.Fatalf("Files Modified:%s", outString)
logrus.Infof("Files Added:%s", outString)
}

if len(directoryComparison.Added) > 0 {
if len(directoryComparison.Removed) > 0 {
outString := ""
for dirPath := range validatePaths {
if len(validatePaths[dirPath].Added) > 0 {
if len(validatePaths[dirPath].Removed) > 0 {
outString += fmt.Sprintf("\n - %s", dirPath)
stringJoiner := fmt.Sprintf("\n - %s", dirPath)
fileList := strings.Join(validatePaths[dirPath].Added[:], stringJoiner)
fileList := strings.Join(validatePaths[dirPath].Removed[:], stringJoiner)
outString += fileList
}
}
logrus.Infof("Files Added:%s", outString)
logrus.Warnf("Files Removed:%s", outString)
}

if len(directoryComparison.Removed) > 0 {
if len(directoryComparison.Modified) > 0 {
outString := ""
for dirPath := range validatePaths {
if len(validatePaths[dirPath].Removed) > 0 {
if len(validatePaths[dirPath].Modified) > 0 {
outString += fmt.Sprintf("\n - %s", dirPath)
stringJoiner := fmt.Sprintf("\n - %s", dirPath)
fileList := strings.Join(validatePaths[dirPath].Removed[:], stringJoiner)
fileList := strings.Join(validatePaths[dirPath].Modified[:], stringJoiner)
outString += fileList
}
}
logrus.Warnf("Files Removed:%s", outString)
logrus.Fatalf("Files Modified:%s", outString)
}

logrus.Infof("Successfully validated\n Upstream: %s\n Branch: %s\n",
Expand Down
2 changes: 1 addition & 1 deletion pkg/conform/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func ExportChartDirectory(chart *chart.Chart, targetPath string) error {

tgz, err := chartutil.Save(chart, tempDir)
if err != nil {
err = fmt.Errorf("unable to save chart archive to %s", tempDir)
err = fmt.Errorf("Unable to save chart archive to %s", tempDir)
return err
}

Expand Down
37 changes: 29 additions & 8 deletions pkg/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,28 @@ func fetchGitHubRelease(repoUrl string) (string, error) {
return "", err
}
ctx := context.Background()
opt := &github.ListOptions{}
opt := &github.ListOptions{Page: 1, PerPage: 50}
latestRelease, _, err := client.Repositories.GetLatestRelease(ctx, gitHubUser, gitHubRepo)
if err != nil {
return "", err
}
tags, _, _ := client.Repositories.ListTags(ctx, gitHubUser, gitHubRepo, opt)
for _, tag := range tags {
if tag.GetName() == *latestRelease.TagName {
releaseCommit = *tag.GetCommit().SHA
for releaseCommit == "" {
tags, _, _ := client.Repositories.ListTags(ctx, gitHubUser, gitHubRepo, opt)
if len(tags) == 0 {
break
}
opt.Page += 1
for _, tag := range tags {
if tag.GetName() == *latestRelease.TagName {
releaseCommit = *tag.GetCommit().SHA
break
}
}
}

if releaseCommit == "" {
err = fmt.Errorf("Commit not found for GitHub release")
return "", err
}

logrus.Debugf("Fetching GitHub Release: %s (%s)\n", *latestRelease.Name, releaseCommit)
Expand Down Expand Up @@ -290,16 +301,26 @@ func fetchUpstreamGit(upstreamYaml parse.UpstreamYaml) (ChartSourceMetadata, err
}

func FetchUpstream(upstreamYaml parse.UpstreamYaml) (ChartSourceMetadata, error) {
var err error
chartSourceMetadata := ChartSourceMetadata{}
if upstreamYaml.AHRepoName != "" && upstreamYaml.AHPackageName != "" {
return fetchUpstreamArtifacthub(upstreamYaml)
chartSourceMetadata, err = fetchUpstreamArtifacthub(upstreamYaml)
} else if upstreamYaml.HelmRepoUrl != "" && upstreamYaml.HelmChart != "" {
return fetchUpstreamHelmrepo(upstreamYaml)
chartSourceMetadata, err = fetchUpstreamHelmrepo(upstreamYaml)
} else if upstreamYaml.GitRepoUrl != "" {
return fetchUpstreamGit(upstreamYaml)
chartSourceMetadata, err = fetchUpstreamGit(upstreamYaml)
} else {
err := errors.New("no valid repo options found")
return ChartSourceMetadata{}, err
}

if upstreamYaml.ChartYaml.Name != "" {
for _, version := range chartSourceMetadata.Versions {
version.Name = upstreamYaml.ChartYaml.Name
}
}

return chartSourceMetadata, err
}

func LoadChartFromUrl(url string) (*chart.Chart, error) {
Expand Down

0 comments on commit 56fa173

Please sign in to comment.