From c656c448747c0338879e9033c2dd86779adc5474 Mon Sep 17 00:00:00 2001 From: Ryan Albert Date: Sat, 23 Mar 2024 18:09:24 -0700 Subject: [PATCH] feat: support spec workflows in github actions --- .github/workflows/workflow-executor.yaml | 3 ++- action.yml | 6 +++++- internal/actions/finalize-suggestion.go | 2 +- internal/actions/runWorkflow.go | 23 +++++++++++++++-------- internal/actions/suggest.go | 2 +- internal/cli/cli.go | 8 ++++++-- internal/git/git.go | 14 ++++++++++++-- internal/run/run.go | 13 ++++--------- 8 files changed, 46 insertions(+), 25 deletions(-) diff --git a/.github/workflows/workflow-executor.yaml b/.github/workflows/workflow-executor.yaml index 1e15c7db..32fef3f6 100644 --- a/.github/workflows/workflow-executor.yaml +++ b/.github/workflows/workflow-executor.yaml @@ -123,13 +123,14 @@ 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.1.0 with: speakeasy_version: ${{ inputs.speakeasy_version }} github_access_token: ${{ secrets.github_access_token }} mode: ${{ inputs.mode }} force: ${{ inputs.force }} speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + openapi_doc_auth_token: ${{ secrets.openapi_doc_auth_token }} output_tests: ${{ inputs.output_tests }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} working_directory: ${{ inputs.working_directory }} diff --git a/action.yml b/action.yml index a3bb1302..0d56a7af 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,9 @@ inputs: speakeasy_api_key: description: "The Speakeasy API key to authenticate the Speakeasy CLI with" required: true + openapi_doc_auth_token: + description: The auth token to use when fetching the OpenAPI document if it is not publicly hosted. For example `Bearer ` or ``. + required: false force: description: "Force the SDK to be regenerated" default: "false" @@ -134,7 +137,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.1.0" env: SPEAKEASY_API_KEY: ${{ inputs.speakeasy_api_key }} SPEAKEASY_SERVER_URL: ${{ inputs.speakeasy_server_url }} @@ -147,6 +150,7 @@ runs: - ${{ inputs.mode }} - ${{ inputs.action }} - ${{ inputs.branch_name }} + - ${{ inputs.openapi_doc_auth_token }} - ${{ inputs.cli_output }} - ${{ inputs.previous_gen_version }} - ${{ inputs.openapi_doc_output }} diff --git a/internal/actions/finalize-suggestion.go b/internal/actions/finalize-suggestion.go index 6b753d4c..a08a2a54 100644 --- a/internal/actions/finalize-suggestion.go +++ b/internal/actions/finalize-suggestion.go @@ -44,7 +44,7 @@ func FinalizeSuggestion() error { return fmt.Errorf("suggestion action requires at least version %s of the speakeasy CLI", cli.MinimumSupportedCLIVersion) } - branchName, _, err = g.FindExistingPR(branchName, environment.ActionFinalize) + branchName, _, err = g.FindExistingPR(branchName, environment.ActionFinalize, false) if err != nil { return err } diff --git a/internal/actions/runWorkflow.go b/internal/actions/runWorkflow.go index fa9dcca1..44146756 100644 --- a/internal/actions/runWorkflow.go +++ b/internal/actions/runWorkflow.go @@ -4,6 +4,7 @@ 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" @@ -31,11 +32,17 @@ func RunWorkflow() error { mode := environment.GetMode() - branchName := "" + wf, err := configuration.GetWorkflowAndValidateLanguages(true) + if err != nil { + return err + } + + sourcesOnly := wf.Targets == nil || len(wf.Targets) == 0 + branchName := "" if mode == environment.ModePR { var err error - branchName, _, err = g.FindExistingPR("", environment.ActionRunWorkflow) + branchName, _, err = g.FindExistingPR("", environment.ActionRunWorkflow, sourcesOnly) if err != nil { return err } @@ -55,7 +62,7 @@ func RunWorkflow() error { } }() - genInfo, outputs, err := run.Run(g) + genInfo, outputs, err := run.Run(g, wf) if err != nil { if err := setOutputs(outputs); err != nil { logging.Debug("failed to set outputs: %v", err) @@ -122,7 +129,7 @@ func RunWorkflow() error { } } - if err = finalize(outputs, branchName, anythingRegenerated, g); err != nil { + if err = finalize(outputs, branchName, anythingRegenerated, sourcesOnly, g); err != nil { return err } @@ -132,9 +139,9 @@ func RunWorkflow() error { } // Sets outputs and creates or adds releases info -func finalize(outputs map[string]string, branchName string, anythingRegenerated bool, g *git.Git) error { +func finalize(outputs map[string]string, branchName string, anythingRegenerated bool, sourcesOnly bool, g *git.Git) error { // If nothing was regenerated, we don't need to do anything - if !anythingRegenerated { + if !anythingRegenerated && !sourcesOnly { return nil } @@ -153,7 +160,7 @@ func finalize(outputs map[string]string, branchName string, anythingRegenerated switch environment.GetMode() { case environment.ModePR: - branchName, pr, err := g.FindExistingPR(branchName, environment.ActionFinalize) + branchName, pr, err := g.FindExistingPR(branchName, environment.ActionFinalize, sourcesOnly) if err != nil { return err } @@ -163,7 +170,7 @@ func finalize(outputs map[string]string, branchName string, anythingRegenerated return err } - if err := g.CreateOrUpdatePR(branchName, *releaseInfo, environment.GetPreviousGenVersion(), pr); err != nil { + if err := g.CreateOrUpdatePR(branchName, *releaseInfo, environment.GetPreviousGenVersion(), pr, sourcesOnly); err != nil { return err } case environment.ModeDirect: diff --git a/internal/actions/suggest.go b/internal/actions/suggest.go index 760b408a..018dabf3 100644 --- a/internal/actions/suggest.go +++ b/internal/actions/suggest.go @@ -34,7 +34,7 @@ func Suggest() error { branchName := "" - branchName, _, err = g.FindExistingPR("", environment.ActionSuggest) + branchName, _, err = g.FindExistingPR("", environment.ActionSuggest, false) if err != nil { return err } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 11b28c84..689cd2a5 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -40,12 +40,16 @@ func GetSupportedLanguages() ([]string, error) { return strings.Split(langs, ", "), nil } -func Run(installationURLs map[string]string, repoURL string, repoSubdirectories map[string]string) error { +func Run(sourcesOnly bool, installationURLs map[string]string, repoURL string, repoSubdirectories map[string]string) error { args := []string{ "run", } - args = append(args, "-t", "all") + if sourcesOnly { + args = append(args, "-s", "all") + } else { + args = append(args, "-t", "all") + } urls, err := json.Marshal(installationURLs) if err != nil { diff --git a/internal/git/git.go b/internal/git/git.go index 5d127e47..665dcc09 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -159,7 +159,7 @@ func (g *Git) CheckDirDirty(dir string, ignoreChangePatterns map[string]string) return IsGitDiffSignificant(diffOutput, ignoreChangePatterns) } -func (g *Git) FindExistingPR(branchName string, action environment.Action) (string, *github.PullRequest, error) { +func (g *Git) FindExistingPR(branchName string, action environment.Action, sourceGeneration bool) (string, *github.PullRequest, error) { if g.repo == nil { return "", nil, fmt.Errorf("repo not cloned") } @@ -172,6 +172,9 @@ func (g *Git) FindExistingPR(branchName string, action environment.Action) (stri var prTitle string if action == environment.ActionRunWorkflow || action == environment.ActionFinalize { prTitle = getGenPRTitle() + if sourceGeneration { + prTitle = getGenSourcesTitle() + } } else if action == environment.ActionFinalize || action == environment.ActionFinalizeSuggestion { prTitle = getSuggestPRTitle() } @@ -352,7 +355,7 @@ func (g *Git) Add(arg string) error { return nil } -func (g *Git) CreateOrUpdatePR(branchName string, releaseInfo releases.ReleasesInfo, previousGenVersion string, pr *github.PullRequest) error { +func (g *Git) CreateOrUpdatePR(branchName string, releaseInfo releases.ReleasesInfo, previousGenVersion string, pr *github.PullRequest, sourceGeneration bool) error { var changelog string var err error @@ -451,6 +454,8 @@ You have exceeded the limit of one free generated SDK. Please reach out to the S title := getGenPRTitle() if environment.IsDocsGeneration() { title = getDocsPRTitle() + } else if sourceGeneration { + title = getGenSourcesTitle() } pr, _, err = g.client.PullRequests.Create(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), &github.NewPullRequest{ @@ -796,6 +801,7 @@ func getRepo() string { const ( speakeasyGenPRTitle = "chore: 🐝 Update SDK - " + speakeasyGenSpecsTitle = "chore: 🐝 Update Specs - " speakeasySuggestPRTitle = "chore: 🐝 Suggest OpenAPI changes - " speakeasyDocsPRTitle = "chore: 🐝 Update SDK Docs - " ) @@ -804,6 +810,10 @@ func getGenPRTitle() string { return speakeasyGenPRTitle + environment.GetWorkflowName() } +func getGenSourcesTitle() string { + return speakeasyGenSpecsTitle + environment.GetWorkflowName() +} + func getDocsPRTitle() string { return speakeasyDocsPRTitle + environment.GetWorkflowName() } diff --git a/internal/run/run.go b/internal/run/run.go index 3deb2152..8735c635 100644 --- a/internal/run/run.go +++ b/internal/run/run.go @@ -2,12 +2,12 @@ package run import ( "fmt" - "github.com/speakeasy-api/sdk-gen-config/workflow" - "github.com/speakeasy-api/sdk-generation-action/internal/configuration" "path" "path/filepath" "strings" + "github.com/speakeasy-api/sdk-gen-config/workflow" + config "github.com/speakeasy-api/sdk-gen-config" "github.com/speakeasy-api/sdk-generation-action/internal/cli" "github.com/speakeasy-api/sdk-generation-action/internal/environment" @@ -29,7 +29,7 @@ type Git interface { CheckDirDirty(dir string, ignoreMap map[string]string) (bool, string, error) } -func Run(g Git) (*GenerationInfo, map[string]string, error) { +func Run(g Git, wf *workflow.Workflow) (*GenerationInfo, map[string]string, error) { workspace := environment.GetWorkspace() outputs := map[string]string{} @@ -46,11 +46,6 @@ func Run(g Git) (*GenerationInfo, map[string]string, error) { globalPreviousGenVersion := "" - wf, err := configuration.GetWorkflowAndValidateLanguages(true) - if err != nil { - return nil, outputs, err - } - langConfigs := map[string]*config.LanguageConfig{} installationURLs := map[string]string{} @@ -107,7 +102,7 @@ func Run(g Git) (*GenerationInfo, map[string]string, error) { } // Run the workflow - if err := cli.Run(installationURLs, repoURL, repoSubdirectories); err != nil { + if err := cli.Run(wf.Targets == nil || len(wf.Targets) == 0, installationURLs, repoURL, repoSubdirectories); err != nil { return nil, outputs, err }