|
| 1 | +//go:build mage_helm |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/aquasecurity/go-version/pkg/semver" |
| 11 | + |
| 12 | + "github.com/magefile/mage/sh" |
| 13 | + "golang.org/x/xerrors" |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +const chartFile = "./helm/trivy/Chart.yaml" |
| 18 | + |
| 19 | +func main() { |
| 20 | + trivyVersion, err := version() |
| 21 | + if err != nil { |
| 22 | + log.Fatalf("could not determine Trivy version: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + newHelmVersion, err := bumpHelmChart(chartFile, trivyVersion) |
| 26 | + if err != nil { |
| 27 | + log.Fatalf("could not bump Trivy version to %q: %v", trivyVersion, err) |
| 28 | + } |
| 29 | + |
| 30 | + log.Printf("Current helm version will bump up %q with Trivy %q", newHelmVersion, trivyVersion) |
| 31 | + |
| 32 | + newBranch := fmt.Sprintf("ci/helm-chart/bump-trivy-to-%s", trivyVersion) |
| 33 | + title := fmt.Sprintf("ci(helm): bump Trivy version to %s for Trivy Helm Chart %s", trivyVersion, newHelmVersion) |
| 34 | + description := fmt.Sprintf("This PR bumps Trivy up to the %s version for the Trivy Helm chart %s.", |
| 35 | + trivyVersion, newHelmVersion) |
| 36 | + |
| 37 | + cmds := [][]string{ |
| 38 | + []string{"git", "switch", "-c", newBranch}, |
| 39 | + []string{"git", "add", chartFile}, |
| 40 | + []string{"git", "commit", "-m", title}, |
| 41 | + []string{"git", "push", "origin", newBranch}, |
| 42 | + []string{"gh", "pr", "create", "--base", "main", "--head", newBranch, "--title", title, "--body", description, "--repo", "$GITHUB_REPOSITORY"}, |
| 43 | + } |
| 44 | + |
| 45 | + if err := runShCommands(cmds); err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + log.Print("Successfully created PR with a new helm version") |
| 49 | +} |
| 50 | + |
| 51 | +type Chart struct { |
| 52 | + Version string `yaml:"version"` |
| 53 | + AppVersion string `yaml:"appVersion"` |
| 54 | +} |
| 55 | + |
| 56 | +// bumpHelmChart bumps up helm and trivy versions inside a file (Chart.yaml) |
| 57 | +// it returns a new helm version and error |
| 58 | +func bumpHelmChart(filename, trivyVersion string) (string, error) { |
| 59 | + input, err := os.ReadFile(filename) |
| 60 | + if err != nil { |
| 61 | + return "", xerrors.Errorf("could not read file %q: %w", filename, err) |
| 62 | + } |
| 63 | + currentHelmChart := &Chart{} |
| 64 | + if err := yaml.Unmarshal(input, currentHelmChart); err != nil { |
| 65 | + return "", xerrors.Errorf("could not unmarshal helm chart %q: %w", filename, err) |
| 66 | + } |
| 67 | + |
| 68 | + newHelmVersion, err := buildNewHelmVersion(currentHelmChart.Version, currentHelmChart.AppVersion, trivyVersion) |
| 69 | + if err != nil { |
| 70 | + return "", xerrors.Errorf("could not build new helm version: %v", err) |
| 71 | + } |
| 72 | + cmds := [][]string{ |
| 73 | + []string{"sed", "-i", "-e", fmt.Sprintf("s/appVersion: %s/appVersion: %s/g", currentHelmChart.AppVersion, trivyVersion), filename}, |
| 74 | + []string{"sed", "-i", "-e", fmt.Sprintf("s/version: %s/version: %s/g", currentHelmChart.Version, newHelmVersion), filename}, |
| 75 | + } |
| 76 | + |
| 77 | + if err := runShCommands(cmds); err != nil { |
| 78 | + return "", xerrors.Errorf("could not update Helm Chart %q: %w", newHelmVersion, err) |
| 79 | + } |
| 80 | + return newHelmVersion, nil |
| 81 | +} |
| 82 | + |
| 83 | +func runShCommands(cmds [][]string) error { |
| 84 | + for _, cmd := range cmds { |
| 85 | + if err := sh.Run(cmd[0], cmd[1:]...); err != nil { |
| 86 | + return xerrors.Errorf("failed to run %v: %w", cmd, err) |
| 87 | + } |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func buildNewHelmVersion(currentHelm, currentTrivy, newTrivy string) (string, error) { |
| 93 | + currentHelmVersion, err := semver.Parse(currentHelm) |
| 94 | + if err != nil { |
| 95 | + return "", xerrors.Errorf("could not parse current helm version: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + currentTrivyVersion, err := semver.Parse(currentTrivy) |
| 99 | + if err != nil { |
| 100 | + return "", xerrors.Errorf("could not parse current trivy version: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + newTrivyVersion, err := semver.Parse(newTrivy) |
| 104 | + if err != nil { |
| 105 | + return "", xerrors.Errorf("could not parse new trivy version: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + if newTrivyVersion.Major().Compare(currentTrivyVersion.Major()) > 0 { |
| 109 | + return currentHelmVersion.IncMajor().String(), nil |
| 110 | + } |
| 111 | + |
| 112 | + if newTrivyVersion.Minor().Compare(currentTrivyVersion.Minor()) > 0 { |
| 113 | + return currentHelmVersion.IncMinor().String(), nil |
| 114 | + } |
| 115 | + |
| 116 | + return currentHelmVersion.IncPatch().String(), nil |
| 117 | +} |
0 commit comments