Skip to content

Commit c3221ce

Browse files
mafredrikylecarbs
authored andcommitted
chore(scripts): fix stable release promote script (#13204)
(cherry picked from commit f66d044)
1 parent d53c94b commit c3221ce

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

scripts/lib.sh

+14-10
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,22 @@ requiredenvs() {
131131
}
132132

133133
gh_auth() {
134-
local fail=0
135-
if [[ "${CODER:-}" == "true" ]]; then
136-
if ! output=$(coder external-auth access-token github 2>&1); then
137-
log "ERROR: Could not authenticate with GitHub."
138-
log "$output"
139-
fail=1
134+
if [[ -z ${GITHUB_TOKEN:-} ]]; then
135+
if [[ -n ${GH_TOKEN:-} ]]; then
136+
export GITHUB_TOKEN=${GH_TOKEN}
137+
elif [[ ${CODER:-} == true ]]; then
138+
if ! output=$(coder external-auth access-token github 2>&1); then
139+
# TODO(mafredri): We could allow checking `gh auth token` here.
140+
log "${output}"
141+
error "Could not authenticate with GitHub using Coder external auth."
142+
else
143+
export GITHUB_TOKEN=${output}
144+
fi
145+
elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then
146+
export GITHUB_TOKEN=${token}
140147
else
141-
GITHUB_TOKEN=$(coder external-auth access-token github)
142-
export GITHUB_TOKEN
148+
error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or run 'gh auth login'."
143149
fi
144-
else
145-
log "Please authenticate gh CLI by running 'gh auth login'"
146150
fi
147151
}
148152

scripts/release/main.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ func main() {
6262
Value: serpent.BoolOf(&r.debug),
6363
},
6464
{
65-
Flag: "gh-token",
65+
Flag: "github-token",
6666
Description: "GitHub personal access token.",
67-
Env: "GH_TOKEN",
67+
Env: "GITHUB_TOKEN",
6868
Value: serpent.StringOf(&r.ghToken),
6969
},
7070
{
@@ -245,7 +245,7 @@ func (r *releaseCommand) promoteVersionToStable(ctx context.Context, inv *serpen
245245
updatedNewStable.Prerelease = github.Bool(false)
246246
updatedNewStable.Draft = github.Bool(false)
247247
if !r.dryRun {
248-
_, _, err = client.Repositories.EditRelease(ctx, owner, repo, newStable.GetID(), newStable)
248+
_, _, err = client.Repositories.EditRelease(ctx, owner, repo, newStable.GetID(), updatedNewStable)
249249
if err != nil {
250250
return xerrors.Errorf("edit release failed: %w", err)
251251
}
@@ -268,6 +268,10 @@ func cloneRelease(r *github.RepositoryRelease) *github.RepositoryRelease {
268268
//
269269
// > ## Stable (since April 23, 2024)
270270
func addStableSince(date time.Time, body string) string {
271+
// Protect against adding twice.
272+
if strings.Contains(body, "> ## Stable (since") {
273+
return body
274+
}
271275
return fmt.Sprintf("> ## Stable (since %s)\n\n", date.Format("January 02, 2006")) + body
272276
}
273277

scripts/release/main_internal_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,19 @@ func Test_addStableSince(t *testing.T) {
131131
date := time.Date(2024, time.April, 23, 0, 0, 0, 0, time.UTC)
132132
body := "## Changelog"
133133

134-
expected := "> ## Stable (since April 23, 2024)\n\n## Changelog"
135-
result := addStableSince(date, body)
134+
want := "> ## Stable (since April 23, 2024)\n\n## Changelog"
135+
got := addStableSince(date, body)
136136

137-
if diff := cmp.Diff(expected, result); diff != "" {
137+
if diff := cmp.Diff(want, got); diff != "" {
138138
require.Fail(t, "addStableSince() mismatch (-want +got):\n%s", diff)
139139
}
140+
141+
// Test that it doesn't add twice.
142+
got = addStableSince(date, got)
143+
144+
if diff := cmp.Diff(want, got); diff != "" {
145+
require.Fail(t, "addStableSince() mismatch (-want +got):\n%s", diff, "addStableSince() should not add twice")
146+
}
140147
}
141148

142149
func Test_release_autoversion(t *testing.T) {

scripts/release_promote_stable.sh

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set -euo pipefail
44
# shellcheck source=scripts/lib.sh
55
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
66

7+
# Make sure GITHUB_TOKEN is set for the release command.
8+
gh_auth
9+
710
# This script is a convenience wrapper around the release promote command.
811
#
912
# Sed hack to make help text look like this script.

0 commit comments

Comments
 (0)