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
63 changes: 63 additions & 0 deletions internal/actions/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package actions
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"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 {
logging.Debug("failed to tag registry images: %v", err)
}
}

return nil
}

Expand Down Expand Up @@ -146,3 +155,57 @@ 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
}

if specificTarget := environment.SpecifiedTarget(); specificTarget != "" {
if target, ok := workflow.Targets[specificTarget]; ok {
sources = append(sources, target.Source)
targets = append(targets, specificTarget)
}
} else {
for name, target := range workflow.Targets {
if releaseInfo, ok := latestRelease[target.Target]; ok {
releasePath, err := filepath.Rel(".", releaseInfo.Path)
if err != nil {
return err
}

// check for no SDK output path
if (releasePath == "" || releasePath == ".") && target.Output == nil {
sources = append(sources, target.Source)
targets = append(targets, name)
}

if target.Output != nil {
outputPath, err := filepath.Rel(".", *target.Output)
if err != nil {
return err
}
outputPath = filepath.Join(environment.GetWorkingDirectory(), outputPath)
if outputPath == releasePath {
sources = append(sources, target.Source)
targets = append(targets, name)
}
}
}
}
}

if len(sources) > 0 && len(targets) > 0 && branch != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need sources AND targets?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just weird naming with the way the underlying tagging lib is currently written. Sources is basically the spec source and targets is codeSamples. I'll leave a comment here

return cli.Tag([]string{branch}, sources, targets)
}

return nil
}
Loading