Skip to content
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

feat: add main branch tagging from release #189

Merged
merged 15 commits into from
Nov 19, 2024
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
80 changes: 79 additions & 1 deletion internal/actions/release.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
29 changes: 21 additions & 8 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
Loading