|
| 1 | +//go:build mage_helm |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 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 | + input, err := os.ReadFile(chartFile) |
| 25 | + if err != nil { |
| 26 | + log.Fatalf("could not find helm chart %s: %v", chartFile, err) |
| 27 | + } |
| 28 | + jsonData := map[string]interface{}{} |
| 29 | + if err := yaml.Unmarshal(input, &jsonData); err != nil { |
| 30 | + log.Fatalf("could not unmarshal helm chart %s: %v", chartFile, err) |
| 31 | + } |
| 32 | + currentAppVersion, ok := jsonData["appVersion"].(string) |
| 33 | + if !ok { |
| 34 | + log.Fatalf("could not determine current app version") |
| 35 | + } |
| 36 | + currentHelmVersion, ok := jsonData["version"].(string) |
| 37 | + if !ok { |
| 38 | + log.Fatalf("could not determine current helm version") |
| 39 | + } |
| 40 | + newHelmVersion := newHelmVersion(currentHelmVersion, currentAppVersion, trivyVersion) |
| 41 | + |
| 42 | + log.Printf("Current helm version %q with Trivy %q will bump up %q with Trivy %q", |
| 43 | + currentHelmVersion, currentAppVersion, newHelmVersion, trivyVersion) |
| 44 | + |
| 45 | + newBranch := fmt.Sprintf("ci/helm-chart/bump-trivy-to-%s", trivyVersion) |
| 46 | + title := fmt.Sprintf("\"ci(helm): bump Trivy version to %s\"", trivyVersion) |
| 47 | + description := fmt.Sprintf("\"# Description\\n\\nThis PR bumps Trivy up to the %s version for the Helm chart.\"", trivyVersion) |
| 48 | + |
| 49 | + cmds := [][]string{ |
| 50 | + []string{"sed", "-i", "-e", fmt.Sprintf("s/appVersion: %s/appVersion: %s/g", currentAppVersion, trivyVersion), chartFile}, |
| 51 | + []string{"sed", "-i", "-e", fmt.Sprintf("s/version: %s/version: %s/g", currentHelmVersion, trivyVersion), chartFile}, |
| 52 | + []string{"git", "switch", "-c", newBranch}, |
| 53 | + []string{"git", "add", "./helm/trivy/Chart.yaml"}, |
| 54 | + []string{"git", "commit", "-m", title}, |
| 55 | + []string{"git", "push", "origin", newBranch}, |
| 56 | + []string{"gh", "pr", "create", "--base", "main", "--head", newBranch, "--title", title, "--body", description, "--repo", "$GITHUB_REPOSITORY"}, |
| 57 | + } |
| 58 | + |
| 59 | + if err := runShCommands(cmds); err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | + log.Print("Successfully created PR with a new helm version") |
| 63 | +} |
| 64 | + |
| 65 | +func runShCommands(cmds [][]string) error { |
| 66 | + for _, cmd := range cmds { |
| 67 | + if err := sh.Run(cmd[0], cmd[1:]...); err != nil { |
| 68 | + return xerrors.Errorf("failed to run %v: %w", cmd, err) |
| 69 | + } |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func splitVersion(version string) []int { |
| 75 | + items := strings.Split(version, ".") |
| 76 | + result := make([]int, len(items)) |
| 77 | + for i, item := range items { |
| 78 | + result[i], _ = strconv.Atoi(item) |
| 79 | + } |
| 80 | + return result |
| 81 | +} |
| 82 | + |
| 83 | +func newHelmVersion(currentHelm, currentTrivy, newTrivy string) string { |
| 84 | + ch := splitVersion(currentHelm) |
| 85 | + ct := splitVersion(currentTrivy) |
| 86 | + tr := splitVersion(newTrivy) |
| 87 | + |
| 88 | + if len(ch) != len(ct) || len(ch) != len(tr) || len(ch) != 3 { |
| 89 | + log.Fatalf("invalid version lengths for %q, %q and %q", currentHelm, currentTrivy, newTrivy) |
| 90 | + } |
| 91 | + |
| 92 | + n := len(ch) |
| 93 | + res := make([]string, n) |
| 94 | + if tr[0] != ct[0] { |
| 95 | + res[0] = strconv.Itoa(tr[0]) |
| 96 | + res[1] = strconv.Itoa(tr[1]) |
| 97 | + res[2] = "0" |
| 98 | + return strings.Join(res, ".") |
| 99 | + } |
| 100 | + |
| 101 | + res[0] = strconv.Itoa(tr[0]) |
| 102 | + if tr[1] != ct[1] { |
| 103 | + res[1] = strconv.Itoa(ch[1] + tr[1] - ct[1]) |
| 104 | + res[2] = "0" |
| 105 | + } else { |
| 106 | + res[1] = strconv.Itoa(ch[1]) |
| 107 | + res[2] = strconv.Itoa(ch[2] + tr[2] - ct[2]) |
| 108 | + } |
| 109 | + return strings.Join(res, ".") |
| 110 | +} |
0 commit comments