Skip to content

Commit

Permalink
feat: add auto-upgrade capability for speakeasyVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
chase-crumbaugh committed Apr 10, 2024
1 parent bd37d99 commit c466e3e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/workflow-executor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ on:
default: latest
required: false
type: string
auto_upgrade:
description: "Automatically upgrade Speakeasy version to the latest version"
default: "false"
required: false
type: string
mode:
description: |-
The mode to run the workflow in, valid options are 'direct' or 'pr', defaults to 'direct'.
Expand Down Expand Up @@ -127,9 +132,10 @@ jobs:
uses: smorimoto/tune-github-hosted-runner-network@v1
- id: run-workflow
name: Run Generation Workflow
uses: speakeasy-api/sdk-generation-action@v15
uses: speakeasy-api/sdk-generation-action@v15-test
with:
speakeasy_version: ${{ inputs.speakeasy_version }}
auto_upgrade: ${{ inputs.auto_upgrade }}
github_access_token: ${{ secrets.github_access_token }}
mode: ${{ inputs.mode }}
force: ${{ inputs.force }}
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ inputs:
description: The version of the Speakeasy CLI to use or "latest"
default: latest
required: false
auto_upgrade:
description: "Automatically upgrade Speakeasy version to the latest version"
default: "false"
required: false
github_access_token:
description: A GitHub access token with write access to the repo
required: true
Expand Down Expand Up @@ -140,7 +144,7 @@ outputs:
description: "The location of the OpenAPI document used for generation"
runs:
using: "docker"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15-test"
env:
SPEAKEASY_API_KEY: ${{ inputs.speakeasy_api_key }}
SPEAKEASY_SERVER_URL: ${{ inputs.speakeasy_server_url }}
Expand All @@ -161,3 +165,4 @@ runs:
- ${{ inputs.gpg_fingerprint }}
- ${{ inputs.openapi_doc_auth_token }}
- ${{ inputs.target }}
- ${{ inputs.auto_upgrade }}
35 changes: 29 additions & 6 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package actions

import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
"github.com/speakeasy-api/sdk-generation-action/internal/git"
"github.com/speakeasy-api/sdk-generation-action/internal/run"

"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"github.com/speakeasy-api/sdk-generation-action/internal/git"
"github.com/speakeasy-api/sdk-generation-action/internal/logging"
"github.com/speakeasy-api/sdk-generation-action/internal/run"
"github.com/speakeasy-api/sdk-generation-action/pkg/releases"
)

Expand All @@ -20,11 +18,36 @@ func RunWorkflow() error {
return err
}

resolvedVersion, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
pinnedVersion := cli.GetVersion(environment.GetPinnedSpeakeasyVersion())
firstRunVersion := pinnedVersion
if environment.ShouldAutoUpgradeSpeakeasyVersion() {
firstRunVersion = "latest"
}

resolvedVersion, err := cli.Download(firstRunVersion, g)
if err != nil {
return err
}

// Only bother doing the second run if it will be with a different version than the first
autoUpgradeAttempted := pinnedVersion != "latest" && pinnedVersion != resolvedVersion

err = runWorkflow(g, resolvedVersion)
if err != nil && autoUpgradeAttempted {
logging.Info("Error running workflow with version %s: %v", firstRunVersion, err)
logging.Info("Trying again with pinned version %s", pinnedVersion)

resolvedVersion, err := cli.Download(firstRunVersion, g)
if err != nil {
return err
}

return runWorkflow(g, resolvedVersion)
}
return err
}

func runWorkflow(g *git.Git, resolvedVersion string) error {
minimumVersionForRun := version.Must(version.NewVersion("1.161.0"))
if !cli.IsAtLeastVersion(minimumVersionForRun) {
return fmt.Errorf("action requires at least version %s of the speakeasy CLI", minimumVersionForRun)
Expand Down
4 changes: 4 additions & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func GetPinnedSpeakeasyVersion() string {
return os.Getenv("INPUT_SPEAKEASY_VERSION")
}

func ShouldAutoUpgradeSpeakeasyVersion() bool {
return os.Getenv("INPUT_AUTO_UPGRADE") == "true"
}

func GetMaxSuggestions() string {
return os.Getenv("INPUT_MAX_SUGGESTIONS")
}
Expand Down
56 changes: 36 additions & 20 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,19 @@ func (g *Git) GetLatestTag() (string, error) {
}

func (g *Git) GetDownloadLink(version string) (string, string, error) {
page := 0

// Iterate through pages until we find the release, or we run out of results
for {
releases, response, err := g.client.Repositories.ListReleases(context.Background(), "speakeasy-api", "speakeasy", &github.ListOptions{Page: page})
if version != "latest" {
release, _, err := g.client.Repositories.GetReleaseByTag(context.Background(), "speakeasy-api", "speakeasy", version)
if err != nil {
return "", "", err
}
link, tag := getDownloadLinkFromRelease(release)
if link == nil || tag == nil {
return "", "", fmt.Errorf("no download link found for version %s", version)
}
return *link, *tag, nil
} else {
// Get first matching release
releases, _, err := g.client.Repositories.ListReleases(context.Background(), "speakeasy-api", "speakeasy", nil)
if err != nil {
return "", "", fmt.Errorf("failed to get speakeasy cli releases: %w", err)
}
Expand All @@ -729,8 +737,7 @@ func (g *Git) GetDownloadLink(version string) (string, string, error) {
} else {
link, tag := getDownloadLinkFromReleases(releases, version)
if link == nil || tag == nil {
page = response.NextPage
continue
return "", "", fmt.Errorf("no download link found for version %s", version)
}

return *link, *tag, nil
Expand All @@ -740,26 +747,35 @@ func (g *Git) GetDownloadLink(version string) (string, string, error) {

func getDownloadLinkFromReleases(releases []*github.RepositoryRelease, version string) (*string, *string) {
for _, release := range releases {
for _, asset := range release.Assets {
if version == "latest" || version == release.GetTagName() {
curOS := runtime.GOOS
curArch := runtime.GOARCH
downloadUrl := asset.GetBrowserDownloadURL()

// https://github.com/speakeasy-api/sdk-generation-action/pull/28#discussion_r1213129634
if curOS == "linux" && (strings.Contains(strings.ToLower(asset.GetName()), "_linux_x86_64") || strings.Contains(strings.ToLower(asset.GetName()), "_linux_amd64")) {
return &downloadUrl, release.TagName
} else if strings.Contains(strings.ToLower(asset.GetName()), curOS) &&
strings.Contains(strings.ToLower(asset.GetName()), curArch) {
return &downloadUrl, release.TagName
}
if version == "latest" || version == release.GetTagName() {
url, tag := getDownloadLinkFromRelease(release)
if url != nil {
return url, tag
}
}
}

return nil, nil
}

func getDownloadLinkFromRelease(release *github.RepositoryRelease) (*string, *string) {
for _, asset := range release.Assets {
curOS := runtime.GOOS
curArch := runtime.GOARCH
downloadUrl := asset.GetBrowserDownloadURL()

// https://github.com/speakeasy-api/sdk-generation-action/pull/28#discussion_r1213129634
if curOS == "linux" && (strings.Contains(strings.ToLower(asset.GetName()), "_linux_x86_64") || strings.Contains(strings.ToLower(asset.GetName()), "_linux_amd64")) {
return &downloadUrl, release.TagName
} else if strings.Contains(strings.ToLower(asset.GetName()), curOS) &&
strings.Contains(strings.ToLower(asset.GetName()), curArch) {
return &downloadUrl, release.TagName
}
}

return nil, nil
}

func (g *Git) GetCommitedFiles() ([]string, error) {
path := environment.GetWorkflowEventPayloadPath()

Expand Down

0 comments on commit c466e3e

Please sign in to comment.