|
| 1 | +/* |
| 2 | +Copyright 2026 The Kube Bind Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Command release computes and creates the next v2 release-candidate tag. |
| 18 | +// |
| 19 | +// It inspects the tags already present on an upstream remote, finds the highest |
| 20 | +// release candidate for the target version (default v2.0.0), and creates the |
| 21 | +// next one (e.g. v2.0.0-rc3 if rc2 is the highest upstream). Pushing the tag to |
| 22 | +// the remote triggers the Image workflow, so pushing is gated behind -push. |
| 23 | +// |
| 24 | +// Usage: |
| 25 | +// |
| 26 | +// go run ./cmd/release # compute + create the next rc tag locally |
| 27 | +// go run ./cmd/release -dry-run # only print what would be created |
| 28 | +// go run ./cmd/release -push # create and push the tag to the remote |
| 29 | +package main |
| 30 | + |
| 31 | +import ( |
| 32 | + "context" |
| 33 | + "flag" |
| 34 | + "fmt" |
| 35 | + "os" |
| 36 | + "os/exec" |
| 37 | + "os/signal" |
| 38 | + "regexp" |
| 39 | + "sort" |
| 40 | + "strconv" |
| 41 | + "strings" |
| 42 | +) |
| 43 | + |
| 44 | +// options holds the release tool's flags. |
| 45 | +type options struct { |
| 46 | + remote string |
| 47 | + version string |
| 48 | + push bool |
| 49 | + dryRun bool |
| 50 | +} |
| 51 | + |
| 52 | +func newFlagSet(o *options) *flag.FlagSet { |
| 53 | + fs := flag.NewFlagSet("release", flag.ContinueOnError) |
| 54 | + fs.StringVar(&o.remote, "remote", "origin", "git remote to read existing tags from and push to") |
| 55 | + fs.StringVar(&o.version, "version", "v2.0.0", "target GA version the release candidates lead up to") |
| 56 | + fs.BoolVar(&o.push, "push", false, "push the created tag to the remote (triggers the Image workflow)") |
| 57 | + fs.BoolVar(&o.dryRun, "dry-run", false, "only print the tag that would be created") |
| 58 | + return fs |
| 59 | +} |
| 60 | + |
| 61 | +func main() { |
| 62 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) |
| 63 | + err := run(ctx) |
| 64 | + stop() |
| 65 | + if err != nil { |
| 66 | + fmt.Fprintln(os.Stderr, "error:", err) |
| 67 | + os.Exit(1) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func run(ctx context.Context) error { |
| 72 | + var o options |
| 73 | + fs := newFlagSet(&o) |
| 74 | + if err := fs.Parse(os.Args[1:]); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + if !strings.HasPrefix(o.version, "v2.") { |
| 79 | + return fmt.Errorf("target version %q is not a v2 version", o.version) |
| 80 | + } |
| 81 | + |
| 82 | + tags, err := remoteTags(ctx, o.remote) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + next := nextRC(o.version, tags) |
| 88 | + fmt.Printf("remote %q: highest rc for %s -> next tag %s\n", o.remote, o.version, next) |
| 89 | + |
| 90 | + if o.dryRun { |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + if localTagExists(ctx, next) { |
| 95 | + return fmt.Errorf("tag %s already exists locally; delete it first or bump the version", next) |
| 96 | + } |
| 97 | + |
| 98 | + if err := gitRun(ctx, "tag", "-a", next, "-m", next); err != nil { |
| 99 | + return fmt.Errorf("creating tag %s: %w", next, err) |
| 100 | + } |
| 101 | + fmt.Printf("created annotated tag %s on HEAD\n", next) |
| 102 | + |
| 103 | + if !o.push { |
| 104 | + fmt.Printf("not pushed. To publish (triggers the Image workflow):\n\n git push %s %s\n", o.remote, next) |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + if err := gitRun(ctx, "push", o.remote, next); err != nil { |
| 109 | + return fmt.Errorf("pushing tag %s to %s: %w", next, o.remote, err) |
| 110 | + } |
| 111 | + fmt.Printf("pushed %s to %s — the Image workflow will build ghcr.io/<owner>/konnector:%s\n", next, o.remote, next) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +// rcTagRE matches a release-candidate tag and captures the rc number, e.g. |
| 116 | +// "v2.0.0-rc3" -> "3". |
| 117 | +var rcTagRE = regexp.MustCompile(`^(v\d+\.\d+\.\d+)-rc(\d+)$`) |
| 118 | + |
| 119 | +// nextRC returns the next release-candidate tag for version, given the set of |
| 120 | +// existing tags. If no rc exists for version yet it returns "<version>-rc1". |
| 121 | +func nextRC(version string, tags []string) string { |
| 122 | + highest := 0 |
| 123 | + for _, t := range tags { |
| 124 | + m := rcTagRE.FindStringSubmatch(t) |
| 125 | + if m == nil || m[1] != version { |
| 126 | + continue |
| 127 | + } |
| 128 | + n, err := strconv.Atoi(m[2]) |
| 129 | + if err != nil { |
| 130 | + continue |
| 131 | + } |
| 132 | + if n > highest { |
| 133 | + highest = n |
| 134 | + } |
| 135 | + } |
| 136 | + return fmt.Sprintf("%s-rc%d", version, highest+1) |
| 137 | +} |
| 138 | + |
| 139 | +// remoteTags lists the tag names present on the given remote via |
| 140 | +// `git ls-remote --tags`, so no local fetch is required. Dereferenced tag refs |
| 141 | +// (the "^{}" suffix) are collapsed to their base tag name. |
| 142 | +func remoteTags(ctx context.Context, remote string) ([]string, error) { |
| 143 | + out, err := gitOutput(ctx, "ls-remote", "--tags", remote) |
| 144 | + if err != nil { |
| 145 | + return nil, fmt.Errorf("listing tags on remote %q: %w", remote, err) |
| 146 | + } |
| 147 | + seen := map[string]struct{}{} |
| 148 | + var tags []string |
| 149 | + for line := range strings.SplitSeq(strings.TrimSpace(out), "\n") { |
| 150 | + fields := strings.Fields(line) |
| 151 | + if len(fields) != 2 { |
| 152 | + continue |
| 153 | + } |
| 154 | + ref := strings.TrimPrefix(fields[1], "refs/tags/") |
| 155 | + ref = strings.TrimSuffix(ref, "^{}") |
| 156 | + if _, ok := seen[ref]; ok { |
| 157 | + continue |
| 158 | + } |
| 159 | + seen[ref] = struct{}{} |
| 160 | + tags = append(tags, ref) |
| 161 | + } |
| 162 | + sort.Strings(tags) |
| 163 | + return tags, nil |
| 164 | +} |
| 165 | + |
| 166 | +// localTagExists reports whether tag already exists in the local repository. |
| 167 | +func localTagExists(ctx context.Context, tag string) bool { |
| 168 | + return gitRun(ctx, "rev-parse", "--verify", "--quiet", "refs/tags/"+tag) == nil |
| 169 | +} |
| 170 | + |
| 171 | +func gitRun(ctx context.Context, args ...string) error { |
| 172 | + cmd := exec.CommandContext(ctx, "git", args...) |
| 173 | + cmd.Stdout = os.Stdout |
| 174 | + cmd.Stderr = os.Stderr |
| 175 | + return cmd.Run() |
| 176 | +} |
| 177 | + |
| 178 | +func gitOutput(ctx context.Context, args ...string) (string, error) { |
| 179 | + cmd := exec.CommandContext(ctx, "git", args...) |
| 180 | + cmd.Stderr = os.Stderr |
| 181 | + out, err := cmd.Output() |
| 182 | + return string(out), err |
| 183 | +} |
0 commit comments