|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-github/v61/github" |
| 14 | + "golang.org/x/mod/semver" |
| 15 | + "golang.org/x/xerrors" |
| 16 | + |
| 17 | + "cdr.dev/slog" |
| 18 | + "cdr.dev/slog/sloggers/sloghuman" |
| 19 | + "github.com/coder/coder/v2/cli/cliui" |
| 20 | + "github.com/coder/serpent" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + owner = "coder" |
| 25 | + repo = "coder" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + logger := slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelDebug) |
| 30 | + |
| 31 | + var ghToken string |
| 32 | + var dryRun bool |
| 33 | + |
| 34 | + cmd := serpent.Command{ |
| 35 | + Use: "release <subcommand>", |
| 36 | + Short: "Prepare, create and publish releases.", |
| 37 | + Options: serpent.OptionSet{ |
| 38 | + { |
| 39 | + Flag: "gh-token", |
| 40 | + Description: "GitHub personal access token.", |
| 41 | + Env: "GH_TOKEN", |
| 42 | + Value: serpent.StringOf(&ghToken), |
| 43 | + }, |
| 44 | + { |
| 45 | + Flag: "dry-run", |
| 46 | + FlagShorthand: "n", |
| 47 | + Description: "Do not make any changes, only print what would be done.", |
| 48 | + Value: serpent.BoolOf(&dryRun), |
| 49 | + }, |
| 50 | + }, |
| 51 | + Children: []*serpent.Command{ |
| 52 | + { |
| 53 | + Use: "promote <version>", |
| 54 | + Short: "Promote version to stable.", |
| 55 | + Handler: func(inv *serpent.Invocation) error { |
| 56 | + ctx := inv.Context() |
| 57 | + if len(inv.Args) == 0 { |
| 58 | + return xerrors.New("version argument missing") |
| 59 | + } |
| 60 | + if !dryRun && ghToken == "" { |
| 61 | + return xerrors.New("GitHub personal access token is required, use --gh-token or GH_TOKEN") |
| 62 | + } |
| 63 | + |
| 64 | + err := promoteVersionToStable(ctx, inv, logger, ghToken, dryRun, inv.Args[0]) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + err := cmd.Invoke().WithOS().Run() |
| 76 | + if err != nil { |
| 77 | + if errors.Is(err, cliui.Canceled) { |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + logger.Error(context.Background(), "release command failed", "err", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +//nolint:revive // Allow dryRun control flag. |
| 86 | +func promoteVersionToStable(ctx context.Context, inv *serpent.Invocation, logger slog.Logger, ghToken string, dryRun bool, version string) error { |
| 87 | + client := github.NewClient(nil) |
| 88 | + if ghToken != "" { |
| 89 | + client = client.WithAuthToken(ghToken) |
| 90 | + } |
| 91 | + |
| 92 | + logger = logger.With(slog.F("dry_run", dryRun), slog.F("version", version)) |
| 93 | + |
| 94 | + logger.Info(ctx, "checking current stable release") |
| 95 | + |
| 96 | + // Check if the version is already the latest stable release. |
| 97 | + currentStable, _, err := client.Repositories.GetLatestRelease(ctx, "coder", "coder") |
| 98 | + if err != nil { |
| 99 | + return xerrors.Errorf("get latest release failed: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + logger = logger.With(slog.F("stable_version", currentStable.GetTagName())) |
| 103 | + logger.Info(ctx, "found current stable release") |
| 104 | + |
| 105 | + if currentStable.GetTagName() == version { |
| 106 | + return xerrors.Errorf("version %q is already the latest stable release", version) |
| 107 | + } |
| 108 | + |
| 109 | + // Ensure the version is a valid release. |
| 110 | + perPage := 20 |
| 111 | + latestReleases, _, err := client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{ |
| 112 | + Page: 0, |
| 113 | + PerPage: perPage, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return xerrors.Errorf("list releases failed: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + var releaseVersions []string |
| 120 | + var newStable *github.RepositoryRelease |
| 121 | + for _, r := range latestReleases { |
| 122 | + releaseVersions = append(releaseVersions, r.GetTagName()) |
| 123 | + if r.GetTagName() == version { |
| 124 | + newStable = r |
| 125 | + } |
| 126 | + } |
| 127 | + semver.Sort(releaseVersions) |
| 128 | + slices.Reverse(releaseVersions) |
| 129 | + |
| 130 | + switch { |
| 131 | + case len(releaseVersions) == 0: |
| 132 | + return xerrors.Errorf("no releases found") |
| 133 | + case newStable == nil: |
| 134 | + return xerrors.Errorf("version %q is not found in the last %d releases", version, perPage) |
| 135 | + } |
| 136 | + |
| 137 | + logger = logger.With(slog.F("mainline_version", releaseVersions[0])) |
| 138 | + |
| 139 | + if version != releaseVersions[0] { |
| 140 | + logger.Warn(ctx, "selected version is not the latest mainline release") |
| 141 | + } |
| 142 | + |
| 143 | + if reply, err := cliui.Prompt(inv, cliui.PromptOptions{ |
| 144 | + Text: "Are you sure you want to promote this version to stable?", |
| 145 | + Default: "no", |
| 146 | + IsConfirm: true, |
| 147 | + }); err != nil { |
| 148 | + if reply == cliui.ConfirmNo { |
| 149 | + return nil |
| 150 | + } |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + logger.Info(ctx, "promoting selected version to stable") |
| 155 | + |
| 156 | + // Update the release to latest. |
| 157 | + updatedNewStable := cloneRelease(newStable) |
| 158 | + |
| 159 | + updatedBody := removeMainlineBlurb(newStable.GetBody()) |
| 160 | + updatedBody = addStableSince(time.Now().UTC(), updatedBody) |
| 161 | + updatedNewStable.Body = github.String(updatedBody) |
| 162 | + updatedNewStable.Prerelease = github.Bool(false) |
| 163 | + updatedNewStable.Draft = github.Bool(false) |
| 164 | + if !dryRun { |
| 165 | + _, _, err = client.Repositories.EditRelease(ctx, owner, repo, newStable.GetID(), newStable) |
| 166 | + if err != nil { |
| 167 | + return xerrors.Errorf("edit release failed: %w", err) |
| 168 | + } |
| 169 | + logger.Info(ctx, "selected version promoted to stable", "url", newStable.GetHTMLURL()) |
| 170 | + } else { |
| 171 | + logger.Info(ctx, "dry-run: release not updated", "uncommitted_changes", cmp.Diff(newStable, updatedNewStable)) |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func cloneRelease(r *github.RepositoryRelease) *github.RepositoryRelease { |
| 178 | + rr := *r |
| 179 | + return &rr |
| 180 | +} |
| 181 | + |
| 182 | +// addStableSince adds a stable since note to the release body. |
| 183 | +// |
| 184 | +// Example: |
| 185 | +// |
| 186 | +// > ## Stable (since April 23, 2024) |
| 187 | +func addStableSince(date time.Time, body string) string { |
| 188 | + return fmt.Sprintf("> ## Stable (since %s)\n\n", date.Format("January 02, 2006")) + body |
| 189 | +} |
| 190 | + |
| 191 | +// removeMainlineBlurb removes the mainline blurb from the release body. |
| 192 | +// |
| 193 | +// Example: |
| 194 | +// |
| 195 | +// > [!NOTE] |
| 196 | +// > This is a mainline Coder release. We advise enterprise customers without a staging environment to install our [latest stable release](https://github.com/coder/coder/releases/latest) while we refine this version. Learn more about our [Release Schedule](https://coder.com/docs/v2/latest/install/releases). |
| 197 | +func removeMainlineBlurb(body string) string { |
| 198 | + lines := strings.Split(body, "\n") |
| 199 | + |
| 200 | + var newBody, clip []string |
| 201 | + var found bool |
| 202 | + for _, line := range lines { |
| 203 | + if strings.HasPrefix(strings.TrimSpace(line), "> [!NOTE]") { |
| 204 | + clip = append(clip, line) |
| 205 | + found = true |
| 206 | + continue |
| 207 | + } |
| 208 | + if found { |
| 209 | + clip = append(clip, line) |
| 210 | + found = strings.HasPrefix(strings.TrimSpace(line), ">") |
| 211 | + continue |
| 212 | + } |
| 213 | + if !found && len(clip) > 0 { |
| 214 | + if !strings.Contains(strings.ToLower(strings.Join(clip, "\n")), "this is a mainline coder release") { |
| 215 | + newBody = append(newBody, clip...) // This is some other note, restore it. |
| 216 | + } |
| 217 | + clip = nil |
| 218 | + } |
| 219 | + newBody = append(newBody, line) |
| 220 | + } |
| 221 | + |
| 222 | + return strings.Join(newBody, "\n") |
| 223 | +} |
0 commit comments