Skip to content

chore(scripts): fix stable release promote script #13204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +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
Comment on lines +137 to +138
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always get the new token. The token stored in GITHUB_TOKEN may be stale, so I opted to refresh this even though it's set.

coder external-auth access-token github handles the auto-refreshing automatically.

export GITHUB_TOKEN=${GH_TOKEN}
elif [[ ${CODER:-} == true ]]; then
if ! output=$(coder external-auth access-token github 2>&1); then
# TODO(mafredri): 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 run 'gh auth login'."
fi
else
log "Please authenticate gh CLI by running 'gh auth login'"
fi
}

Expand Down
10 changes: 7 additions & 3 deletions scripts/release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
{
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down
13 changes: 10 additions & 3 deletions scripts/release/main_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/release_promote_stable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works when CODER=true i.e. inside a Coder workspaces.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we suggest adding an extra "echo" to indicate it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s the expected use-case. And if it isn’t set outside a workspace it’ll print a notice to do gh auth login. Just realized that won’t work though, but we could update lib.sh to fetch the token via gh auth token in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we also shouldn't override GITHUB_TOKEN if it's set? And maybe support GH_TOKEN too (in lib.sh)? Thoughts @matifali?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes as far it works and make it more robust. Let's do this. I don't fully understand the difference between GH_ and GITHUB_ prefix for tokens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think GH_ is legacy, sometimes still used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored it a bit @matifali, if it still looks alright to you, I'll go ahead and merge: 9417866

gh_auth

# This script is a convenience wrapper around the release promote command.
#
# Sed hack to make help text look like this script.
Expand Down
Loading