From 56fa173f51dbaac884b0f8336e99a1f0e8017282 Mon Sep 17 00:00:00 2001 From: Samuel Attwood Date: Fri, 2 Dec 2022 00:40:56 -0500 Subject: [PATCH] Fixed issue resulting from GitHub tag ordering --- main.go | 60 +++++++++++++++++++++++++++--------------- pkg/conform/export.go | 2 +- pkg/fetcher/fetcher.go | 37 ++++++++++++++++++++------ 3 files changed, 69 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 4a96d43..246f2ee 100644 --- a/main.go +++ b/main.go @@ -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 { @@ -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, @@ -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) @@ -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 { @@ -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) @@ -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 { @@ -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", diff --git a/pkg/conform/export.go b/pkg/conform/export.go index f1bb09d..436d113 100644 --- a/pkg/conform/export.go +++ b/pkg/conform/export.go @@ -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 } diff --git a/pkg/fetcher/fetcher.go b/pkg/fetcher/fetcher.go index e95f151..c5c661e 100644 --- a/pkg/fetcher/fetcher.go +++ b/pkg/fetcher/fetcher.go @@ -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) @@ -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) {