diff --git a/go.mod b/go.mod index 2e8b807..fcdd3be 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,9 @@ require ( github.com/hashicorp/go-version v1.6.0 github.com/joho/godotenv v1.5.1 github.com/pb33f/libopenapi v0.15.14 + github.com/pkg/errors v0.9.1 github.com/speakeasy-api/git-diff-parser v0.0.3 - github.com/speakeasy-api/sdk-gen-config v1.7.4 + github.com/speakeasy-api/sdk-gen-config v1.28.0 github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.15.4 github.com/speakeasy-api/versioning-reports v0.6.0 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index 0c20b2a..b995eeb 100644 --- a/go.sum +++ b/go.sum @@ -138,8 +138,8 @@ github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2 github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/speakeasy-api/git-diff-parser v0.0.3 h1:LL12d+HMtSyj6O/hQqIn/lgDPYI6ci/DEhk0la/xA+0= github.com/speakeasy-api/git-diff-parser v0.0.3/go.mod h1:P46HmmVVmwA9P8h2wa0fDpmRM8/grbVQ+uKhWDtpkIY= -github.com/speakeasy-api/sdk-gen-config v1.7.4 h1:hk3GaKiL6zxx3SulnxdunvU2V7bUSQ4YviXtIiUmWuo= -github.com/speakeasy-api/sdk-gen-config v1.7.4/go.mod h1:4R+8FTyM6UdLHltOVAigIoR5D2UfPsGMmEFzPOP1yCs= +github.com/speakeasy-api/sdk-gen-config v1.28.0 h1:Gm3WqJCxs7ExEJcRVhMsUq2mC7S23m18zpvtFaPEuys= +github.com/speakeasy-api/sdk-gen-config v1.28.0/go.mod h1:e9PjnCRHGa4K4EFKVU+kKmihOZjJ2V4utcU+274+bnQ= github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.15.4 h1:lPVNakwHrrRWRaNIdIHE6BK7RI6B/jpdwbtvI/xPEYo= github.com/speakeasy-api/speakeasy-client-sdk-go/v3 v3.15.4/go.mod h1:b4fiZ1Wid0JHwwiYqhaPifDwjmC15uiN7A8Cmid+9kw= github.com/speakeasy-api/versioning-reports v0.6.0 h1:oLokEQ7xnDXqWAQk60Sk+ifwYaRbq3BrLX2KyT8gWxE= diff --git a/internal/actions/release.go b/internal/actions/release.go index f582e01..ced654d 100644 --- a/internal/actions/release.go +++ b/internal/actions/release.go @@ -1,13 +1,16 @@ package actions import ( - "errors" "fmt" + "os" "path/filepath" "strings" + "github.com/pkg/errors" + "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" @@ -90,6 +93,12 @@ func Release() error { return err } + if os.Getenv("SPEAKEASY_API_KEY") != "" { + if err = addCurrentBranchTagging(g, latestRelease.Languages); err != nil { + return errors.Wrap(err, "failed to tag registry images") + } + } + return nil } @@ -146,3 +155,72 @@ func addPublishOutputs(dir string, outputs map[string]string) error { return nil } + +func addCurrentBranchTagging(g *git.Git, latestRelease map[string]releases.LanguageReleaseInfo) error { + _, err := cli.Download("latest", g) + if err != nil { + return err + } + + var sources, targets []string + branch := strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/heads/") + workflow, err := configuration.GetWorkflowAndValidateLanguages(true) + if err != nil { + return err + } + + // the tagging library treats targets synonymously with code samples + if specificTarget := environment.SpecifiedTarget(); specificTarget != "" { + if target, ok := workflow.Targets[specificTarget]; ok { + if source, ok := workflow.Sources[target.Source]; ok && source.Registry != nil { + sources = append(sources, target.Source) + } + + if target.CodeSamples != nil && target.CodeSamples.Registry != nil { + targets = append(targets, specificTarget) + } + } + } else { + for name, target := range workflow.Targets { + if releaseInfo, ok := latestRelease[target.Target]; ok { + var targetIsMatched bool + releasePath, err := filepath.Rel(".", releaseInfo.Path) + if err != nil { + return err + } + + // check for no SDK output path + if (releasePath == "" || releasePath == ".") && target.Output == nil { + targetIsMatched = true + } + + if target.Output != nil { + outputPath, err := filepath.Rel(".", *target.Output) + if err != nil { + return err + } + outputPath = filepath.Join(environment.GetWorkingDirectory(), outputPath) + if outputPath == releasePath { + targetIsMatched = true + } + } + + if targetIsMatched { + if source, ok := workflow.Sources[target.Source]; ok && source.Registry != nil { + sources = append(sources, target.Source) + } + + if target.CodeSamples != nil && target.CodeSamples.Registry != nil { + targets = append(targets, name) + } + } + } + } + } + + if (len(sources) > 0 || len(targets) > 0) && branch != "" { + return cli.Tag([]string{branch}, sources, targets) + } + + return nil +} diff --git a/internal/actions/runWorkflow.go b/internal/actions/runWorkflow.go index 72fef9d..7146eab 100644 --- a/internal/actions/runWorkflow.go +++ b/internal/actions/runWorkflow.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/google/go-github/v63/github" + "github.com/pkg/errors" "github.com/speakeasy-api/sdk-generation-action/internal/versionbumps" "github.com/speakeasy-api/versioning-reports/versioning" @@ -282,12 +283,13 @@ func finalize(inputs finalizeInputs) error { } } + inputs.Outputs["commit_hash"] = commitHash + // add merging branch registry tag if err = addDirectModeBranchTagging(); err != nil { - logging.Debug("failed to tag registry images: %v", err) + return errors.Wrap(err, "failed to tag registry images") } - inputs.Outputs["commit_hash"] = commitHash } return nil @@ -302,18 +304,29 @@ func addDirectModeBranchTagging() error { branch := strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/heads/") var sources, targets []string + // the tagging library treats targets synonymously with code samples if specificTarget := environment.SpecifiedTarget(); specificTarget != "" { - if target, ok := wf.Targets[environment.SpecifiedTarget()]; ok { - sources = append(sources, target.Source) - targets = append(targets, specificTarget) + if target, ok := wf.Targets[specificTarget]; ok { + if source, ok := wf.Sources[target.Source]; ok && source.Registry != nil { + sources = append(sources, target.Source) + } + + if target.CodeSamples != nil && target.CodeSamples.Registry != nil { + targets = append(targets, specificTarget) + } } } else { for name, target := range wf.Targets { - sources = append(sources, target.Source) - targets = append(targets, name) + if source, ok := wf.Sources[target.Source]; ok && source.Registry != nil { + sources = append(sources, target.Source) + } + + if target.CodeSamples != nil && target.CodeSamples.Registry != nil { + targets = append(targets, name) + } } } - if len(sources) > 0 && len(targets) > 0 && branch != "" { + if (len(sources) > 0 || len(targets) > 0) && branch != "" { return cli.Tag([]string{branch}, sources, targets) }