From cb9f2d191ca183d7239e34c9d3817fcf7dac97d7 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 7 May 2024 21:58:43 +0300 Subject: [PATCH 1/5] chore(scripts): fix stable release promote script --- scripts/release/main.go | 10 +++++++--- scripts/release/main_internal_test.go | 13 ++++++++++--- scripts/release_promote_stable.sh | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/scripts/release/main.go b/scripts/release/main.go index 919205b76db65..8eaeb20825a92 100644 --- a/scripts/release/main.go +++ b/scripts/release/main.go @@ -62,9 +62,9 @@ func main() { Value: serpent.BoolOf(&r.debug), }, { - Flag: "gh-token", + Flag: "github-token", Description: "GitHub personal access token.", - Env: "GH_TOKEN", + Env: "GITHUB_TOKEN", Value: serpent.StringOf(&r.ghToken), }, { @@ -245,7 +245,7 @@ func (r *releaseCommand) promoteVersionToStable(ctx context.Context, inv *serpen updatedNewStable.Prerelease = github.Bool(false) updatedNewStable.Draft = github.Bool(false) if !r.dryRun { - _, _, err = client.Repositories.EditRelease(ctx, owner, repo, newStable.GetID(), newStable) + _, _, err = client.Repositories.EditRelease(ctx, owner, repo, newStable.GetID(), updatedNewStable) if err != nil { return xerrors.Errorf("edit release failed: %w", err) } @@ -268,6 +268,10 @@ func cloneRelease(r *github.RepositoryRelease) *github.RepositoryRelease { // // > ## Stable (since April 23, 2024) func addStableSince(date time.Time, body string) string { + // Protect against adding twice. + if strings.Contains(body, "> ## Stable (since") { + return body + } return fmt.Sprintf("> ## Stable (since %s)\n\n", date.Format("January 02, 2006")) + body } diff --git a/scripts/release/main_internal_test.go b/scripts/release/main_internal_test.go index 74a6d46d05c8a..d5d10706683a2 100644 --- a/scripts/release/main_internal_test.go +++ b/scripts/release/main_internal_test.go @@ -131,12 +131,19 @@ func Test_addStableSince(t *testing.T) { date := time.Date(2024, time.April, 23, 0, 0, 0, 0, time.UTC) body := "## Changelog" - expected := "> ## Stable (since April 23, 2024)\n\n## Changelog" - result := addStableSince(date, body) + want := "> ## Stable (since April 23, 2024)\n\n## Changelog" + got := addStableSince(date, body) - if diff := cmp.Diff(expected, result); diff != "" { + if diff := cmp.Diff(want, got); diff != "" { require.Fail(t, "addStableSince() mismatch (-want +got):\n%s", diff) } + + // Test that it doesn't add twice. + got = addStableSince(date, got) + + if diff := cmp.Diff(want, got); diff != "" { + require.Fail(t, "addStableSince() mismatch (-want +got):\n%s", diff, "addStableSince() should not add twice") + } } func Test_release_autoversion(t *testing.T) { diff --git a/scripts/release_promote_stable.sh b/scripts/release_promote_stable.sh index 33b55d3855d41..1ac0f8318d749 100755 --- a/scripts/release_promote_stable.sh +++ b/scripts/release_promote_stable.sh @@ -4,6 +4,9 @@ set -euo pipefail # shellcheck source=scripts/lib.sh source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" +# Make sure GITHUB_TOKEN is set for the release command. +gh_auth + # This script is a convenience wrapper around the release promote command. # # Sed hack to make help text look like this script. From daf03f8d70cec593d3b39d4b0245940e5c32ff4e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 8 May 2024 13:59:11 +0300 Subject: [PATCH 2/5] set gh token outside workspace if gh is logged in --- scripts/lib.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/lib.sh b/scripts/lib.sh index 78ec22d503fbf..c23f61aecf498 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -144,6 +144,8 @@ gh_auth() { GITHUB_TOKEN=$(coder external-auth access-token github) export GITHUB_TOKEN fi + elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then + export GITHUB_TOKEN=$token else log "Please authenticate gh CLI by running 'gh auth login'" fi From 9417866c9fb3bb8c892e4b66d500e08dee496121 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 8 May 2024 16:00:03 +0300 Subject: [PATCH 3/5] expand use-cases in `gh_auth` --- scripts/lib.sh | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/scripts/lib.sh b/scripts/lib.sh index c23f61aecf498..dad2b77e11552 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -134,20 +134,22 @@ requiredenvs() { } gh_auth() { - local fail=0 - if [[ "${CODER:-}" == "true" ]]; then - if ! output=$(coder external-auth access-token github 2>&1); then - log "ERROR: Could not authenticate with GitHub." - log "$output" - fail=1 + if [[ -z ${GITHUB_TOKEN:-} ]]; then + if [[ -n ${GH_TOKEN:-} ]]; then + export GITHUB_TOKEN=${GH_TOKEN} + elif [[ ${CODER:-} == true ]]; then + if ! output=$(coder external-auth access-token github 2>&1); then + # TODO(maf): We could allow checking `gh auth token` here. + log "${output}" + error "Could not authenticate with GitHub using Coder external auth." + else + export GITHUB_TOKEN=${output} + fi + elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then + export GITHUB_TOKEN=${token} else - GITHUB_TOKEN=$(coder external-auth access-token github) - export GITHUB_TOKEN + error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or authenticate run 'gh auth login'." fi - elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then - export GITHUB_TOKEN=$token - else - log "Please authenticate gh CLI by running 'gh auth login'" fi } From a3b254d3c446a6ff579285ec1c22b6e05f58cabd Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 8 May 2024 16:01:28 +0300 Subject: [PATCH 4/5] redri --- scripts/lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lib.sh b/scripts/lib.sh index dad2b77e11552..bc793a3d1126d 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -139,7 +139,7 @@ gh_auth() { export GITHUB_TOKEN=${GH_TOKEN} elif [[ ${CODER:-} == true ]]; then if ! output=$(coder external-auth access-token github 2>&1); then - # TODO(maf): We could allow checking `gh auth token` here. + # TODO(mafredri): We could allow checking `gh auth token` here. log "${output}" error "Could not authenticate with GitHub using Coder external auth." else From 7573dfc6994522981030f9559f3a4f11e608d498 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 8 May 2024 16:03:04 +0300 Subject: [PATCH 5/5] s/authenticate // --- scripts/lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lib.sh b/scripts/lib.sh index bc793a3d1126d..e245fb4ab8cc7 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -148,7 +148,7 @@ gh_auth() { elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then export GITHUB_TOKEN=${token} else - error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or authenticate run 'gh auth login'." + error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or run 'gh auth login'." fi fi }