From 83177e642576e6c28c9ae2eb937249e433c4eae2 Mon Sep 17 00:00:00 2001 From: Ryan Albert <42415738+ryan-timothy-albert@users.noreply.github.com> Date: Sat, 24 Aug 2024 22:14:02 +0100 Subject: [PATCH] feat: allow non root working dir in run (#157) * feat: allow non root working dir in run * feat: allow non root working dir in run * feat: allow non root working dir in run * feat: update * feat: update * feat: update * feat: update * feat: update * feat: update --- .github/workflows/sdk-publish.yaml | 13 +++++++++++++ .github/workflows/tag.yaml | 5 +++++ .github/workflows/workflow-executor.yaml | 13 +++++++++++++ internal/actions/utils.go | 19 +++++++++++++++++-- internal/configuration/configuration.go | 2 +- internal/git/git.go | 7 +++---- internal/run/run.go | 1 + 7 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.github/workflows/sdk-publish.yaml b/.github/workflows/sdk-publish.yaml index 3d45f3da..f63e999e 100644 --- a/.github/workflows/sdk-publish.yaml +++ b/.github/workflows/sdk-publish.yaml @@ -7,6 +7,10 @@ on: required: false description: "Internal use only" type: string + working_directory: + description: "The working directory for running Speakeasy CLI commands in the action" + required: false + type: string dotnet_version: description: "The version of dotnet to use when compiling the C# SDK" required: false @@ -98,6 +102,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: "release" speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} - uses: ravsamhq/notify-slack-action@v2 if: always() && env.SLACK_WEBHOOK_URL != '' @@ -159,6 +164,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.python_directory }} registry_name: "pypi" @@ -206,6 +212,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.typescript_directory }} registry_name: "npm" @@ -268,6 +275,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.java_directory }} registry_name: "sonatype" @@ -310,6 +318,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.php_directory }} registry_name: "packagist" @@ -352,6 +361,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.csharp_directory }} registry_name: "nuget" @@ -392,6 +402,7 @@ jobs: with: github_access_token: ${{ secrets.github_access_token }} speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} gpg_fingerprint: ${{ steps.import_gpg.outputs.fingerprint }} action: "release" speakeasy_server_url: ${{ inputs.speakeasy_server_url }} @@ -404,6 +415,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.publish_terraform }} registry_name: "terraform" @@ -456,6 +468,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.release.outputs.ruby_directory }} registry_name: "gems" diff --git a/.github/workflows/tag.yaml b/.github/workflows/tag.yaml index 7eb08cf6..555583fc 100644 --- a/.github/workflows/tag.yaml +++ b/.github/workflows/tag.yaml @@ -11,6 +11,10 @@ on: description: "The targets to tag code samples for (comma or newline separated)" required: false type: string + working_directory: + description: "The working directory for running Speakeasy CLI commands in the action" + required: false + type: string registry_tags: description: "Multi-line or single-line string input of tags to apply to speakeasy registry builds" required: false @@ -36,6 +40,7 @@ jobs: action: "tag" sources: ${{ inputs.sources }} code_samples: ${{ inputs.code_samples }} + working_directory: ${{ inputs.working_directory }} registry_tags: ${{ inputs.registry_tags }} speakeasy_api_key: ${{ secrets.speakeasy_api_key }} github_access_token: ${{ secrets.github_access_token }} diff --git a/.github/workflows/workflow-executor.yaml b/.github/workflows/workflow-executor.yaml index b0a1c280..18a821c9 100644 --- a/.github/workflows/workflow-executor.yaml +++ b/.github/workflows/workflow-executor.yaml @@ -172,6 +172,7 @@ jobs: with: speakeasy_version: ${{ inputs.speakeasy_version }} github_access_token: ${{ secrets.github_access_token }} + working_directory: ${{ inputs.working_directory }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} @@ -236,6 +237,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.python_directory }} registry_name: "pypi" @@ -261,6 +263,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} @@ -304,6 +307,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.typescript_directory }} registry_name: "npm" @@ -329,6 +333,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} @@ -388,6 +393,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.java_directory }} registry_name: "sonatype" @@ -413,6 +419,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} @@ -461,6 +468,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.ruby_directory }} registry_name: "gems" @@ -486,6 +494,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} @@ -524,6 +533,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.csharp_directory }} registry_name: "nuget" @@ -549,6 +559,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} @@ -585,6 +596,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: publish-event speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} target_directory: ${{ needs.run-workflow.outputs.php_directory }} registry_name: "packagist" @@ -610,6 +622,7 @@ jobs: github_access_token: ${{ secrets.github_access_token }} action: log-result speakeasy_api_key: ${{ secrets.speakeasy_api_key }} + working_directory: ${{ inputs.working_directory }} speakeasy_server_url: ${{ inputs.speakeasy_server_url }} env: GH_ACTION_RESULT: ${{ job.status }} diff --git a/internal/actions/utils.go b/internal/actions/utils.go index f501b1eb..76bab718 100644 --- a/internal/actions/utils.go +++ b/internal/actions/utils.go @@ -1,12 +1,17 @@ package actions import ( + "path/filepath" + "github.com/speakeasy-api/sdk-generation-action/internal/configuration" "github.com/speakeasy-api/sdk-generation-action/internal/environment" ) func getReleasesDir() (string, error) { releasesDir := "." + if environment.GetWorkingDirectory() != "" { + releasesDir = environment.GetWorkingDirectory() + } // Find releases file wf, err := configuration.GetWorkflowAndValidateLanguages(false) if err != nil { @@ -16,14 +21,24 @@ func getReleasesDir() (string, error) { // Checking for multiple targets ensures backward compatibility with the code below if len(wf.Targets) > 1 && environment.SpecifiedTarget() != "" { if target, ok := wf.Targets[environment.SpecifiedTarget()]; ok && target.Output != nil { - return *target.Output, nil + if releasesDir != "." { + releasesDir = filepath.Join(releasesDir, *target.Output) + } else { + releasesDir = *target.Output + } + + return releasesDir, nil } } for _, target := range wf.Targets { // If we are only generating one language and its not in the root directory we assume this is a multi-sdk repo if len(wf.Targets) == 1 && target.Output != nil && *target.Output != "." { - releasesDir = *target.Output + if releasesDir != "." { + releasesDir = filepath.Join(releasesDir, *target.Output) + } else { + releasesDir = *target.Output + } } } diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index 8f498697..8d67a4de 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -33,7 +33,7 @@ func GetWorkflowAndValidateLanguages(checkLangSupported bool) (*workflow.Workflo func getWorkflow() (*workflow.Workflow, error) { workspace := environment.GetWorkspace() - localPath := filepath.Join(workspace, "repo") + localPath := filepath.Join(workspace, "repo", environment.GetWorkingDirectory()) wf, _, err := workflow.Load(localPath) if err != nil { diff --git a/internal/git/git.go b/internal/git/git.go index fab66baf..dca633b5 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/speakeasy-api/versioning-reports/versioning" "net/url" "os" "os/exec" @@ -17,6 +16,8 @@ import ( "strings" "time" + "github.com/speakeasy-api/versioning-reports/versioning" + "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" "github.com/go-git/go-git/v5/plumbing" @@ -759,7 +760,6 @@ func PRMetadata(m *versioning.MergedVersionReport, labelTypes map[string]github. return strings.Join(builder, " "), labels } - func (g *Git) CreateSuggestionPR(branchName, output string) (*int, string, error) { body := fmt.Sprintf(`Generated OpenAPI Suggestions by Speakeasy CLI. Outputs changes to *%s*.`, output) @@ -1023,7 +1023,7 @@ func (g *Git) UpsertLabelTypes(ctx context.Context) map[string]github.Label { desiredLabels := map[string]github.Label{} addGitHubLabel := func(name, description string) { desiredLabels[name] = github.Label{ - Name: &name, + Name: &name, Description: &description, } } @@ -1041,7 +1041,6 @@ func (g *Git) UpsertLabelTypes(ctx context.Context) map[string]github.Label { actualLabels[*label.Name] = *label } - for _, label := range desiredLabels { foundLabel, ok := actualLabels[*label.Name] if ok { diff --git a/internal/run/run.go b/internal/run/run.go index cdc4f61f..5ad4ba2b 100644 --- a/internal/run/run.go +++ b/internal/run/run.go @@ -71,6 +71,7 @@ func Run(g Git, pr *github.PullRequest, wf *workflow.Workflow) (*RunResult, map[ dir = *target.Output } + dir = filepath.Join(environment.GetWorkingDirectory(), dir) return dir, path.Join(workspace, "repo", dir) }