Skip to content

Commit

Permalink
feat: registry tagging action (#136)
Browse files Browse the repository at this point in the history
* feat: registry tagging action

* fix: import cycle

* Update internal/cli/speakeasy.go

Co-authored-by: David Alberto Adler <dalberto.adler@gmail.com>

* fix: tag command args

---------

Co-authored-by: David Alberto Adler <dalberto.adler@gmail.com>
  • Loading branch information
chase-crumbaugh and mfbx9da4 authored Jun 11, 2024
1 parent 77062f9 commit 2451b10
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 30 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Speakeasy Registry Tagger

on:
workflow_call:
inputs:
sources:
description: "The sources to tag (comma or newline separated)"
required: false
type: string
code_samples:
description: "The targets to tag code samples for (comma or newline separated)"
required: false
type: string
registry_tags:
description: "Multi-line or single-line string input of tags to apply to speakeasy registry builds"
required: false
type: string
secrets:
speakeasy_api_key:
description: The API key to use to authenticate the Speakeasy CLI
required: true
jobs:
run-workflow:
name: Apply Tags in Speakeasy Registry
runs-on: ubuntu-latest
steps:
- name: Tune GitHub-hosted runner network
uses: smorimoto/tune-github-hosted-runner-network@v1
- id: apply-tags
name: Apply Tags
uses: speakeasy-api/sdk-generation-action@v15
with:
action: "tag"
sources: ${{ inputs.sources }}
code_samples: ${{ inputs.code_samples }}
registry_tags: ${{ inputs.registry_tags }}
speakeasy_api_key: ${{ secrets.speakeasy_api_key }}
15 changes: 10 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ inputs:
description: "Force the SDK to be regenerated"
default: "false"
required: false
sources:
description: "The sources to tag (comma or newline separated)"
required: false
target:
description: "Generate a specific target by name"
required: false
code_samples:
description: "The targets to tag code samples for (comma or newline separated)"
required: false
registry_tags:
description: "Multi-line or single-line string input of tags to apply to speakeasy registry builds"
required: false
Expand All @@ -40,14 +46,11 @@ inputs:
required: false
action:
description: |-
The current action step to run, valid options are 'validate', 'run-workflow', 'suggest', 'finalize', 'finalize-suggestion', or 'release', defaults to 'run-workflow'.
The current action step to run, valid options are 'run-workflow', 'release', or 'tag', defaults to 'run-workflow'.
This is intended to be used along with the `mode` input to determine the current action step to run.
- 'validate' will validate the OpenAPI document and return addressable warnings and errors.
- 'run-workflow' will generate the SDK and commit the changes to the branch.
- 'suggest' will apply suggestions to the OpenAPI document and commit the changes to the branch.
- 'finalize' depending on mode will either merge the branch to the branch the workflow is configure to run on (normally 'main' or 'master') or create a pull request.
- 'finalize-suggestion' will create a pull request with the suggestions from the LLM model.
- 'release' will create a release on Github.
- 'tag' will tag the registry images with the provided tags.
branch_name:
description: "The name of the branch to finalize, only used for the 'finalize' action step."
required: false
Expand Down Expand Up @@ -174,4 +177,6 @@ runs:
- ${{ inputs.registry_tags }}
- ${{ inputs.registry_name }}
- ${{ inputs.target_directory }}
- ${{ inputs.sources }}
- ${{ inputs.code_samples }}

40 changes: 40 additions & 0 deletions internal/actions/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package actions

import (
"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/logging"
"github.com/speakeasy-api/sdk-generation-action/internal/registry"
"golang.org/x/exp/maps"
)

func Tag() error {
g, err := initAction()
if err != nil {
return err
}

if _, err = cli.Download("latest", g); err != nil {
return err
}

tags := registry.ProcessRegistryTags()

sources := environment.SpecifiedSources()
targets := environment.SpecifiedCodeSamplesTargets()

if len(sources) == 0 && len(targets) == 0 {
wf, err := configuration.GetWorkflowAndValidateLanguages(false)
if err != nil {
return err
}

sources = maps.Keys(wf.Sources)
targets = maps.Keys(wf.Targets)

logging.Info("No sources or targets specified, using all sources and targets from workflow")
}

return cli.Tag(tags, sources, targets)
}
27 changes: 2 additions & 25 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"encoding/json"
"fmt"
"github.com/speakeasy-api/sdk-generation-action/internal/registry"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -47,7 +48,7 @@ func Run(sourcesOnly bool, installationURLs map[string]string, repoURL string, r
args = append(args, "-r", repoURL)
}

tags := processRegistryTags()
tags := registry.ProcessRegistryTags()
if len(tags) > 0 {
tagString := strings.Join(tags, ",")
args = append(args, "--registry-tags", tagString)
Expand Down Expand Up @@ -113,27 +114,3 @@ func getChangesReportURL(out string) string {

return ""
}

func processRegistryTags() []string {
var tags []string
tagsInput := environment.RegistryTags()
if len(strings.Replace(tagsInput, " ", "", -1)) == 0 {
return tags
}

var processedTags []string
if strings.Contains(tagsInput, "\n") {
processedTags = strings.Split(tagsInput, "\n")
} else {
processedTags = strings.Split(tagsInput, ",")
}

for _, tag := range processedTags {
tag = strings.Replace(tag, " ", "", -1)
if len(tag) > 0 {
tags = append(tags, tag)
}
}

return tags
}
27 changes: 27 additions & 0 deletions internal/cli/speakeasy.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,30 @@ func CheckFreeUsageAccess() (bool, error) {

return accessDetails.GenerationAllowed, nil
}

func Tag(tags, sources, codeSamples []string) error {
args := []string{"tag", "promote"}

if len(tags) == 0 {
return fmt.Errorf("please specify at least one tag")
}
if len(sources) == 0 && len(codeSamples) == 0 {
return fmt.Errorf("please specify at least one source or target (codeSamples) to tag")
}

if len(sources) > 0 {
args = append(args, "-s", strings.Join(sources, ","))
}
if len(codeSamples) > 0 {
args = append(args, "-c", strings.Join(codeSamples, ","))
}

args = append(args, "-t", strings.Join(tags, ","))
out, err := runSpeakeasyCommand(args...)
if err != nil {
return fmt.Errorf("error running speakeasy tag: %w\n %s", err, out)
}

fmt.Println(out)
return nil
}
21 changes: 21 additions & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
ActionRelease Action = "release"
ActionLog Action = "log-result"
ActionPublishEvent Action = "publish-event"
ActionTag Action = "tag"
)

const (
Expand Down Expand Up @@ -69,6 +70,14 @@ func SpecifiedTarget() string {
return os.Getenv("INPUT_TARGET")
}

func SpecifiedSources() []string {
return parseArrayInput(os.Getenv("INPUT_SOURCES"))
}

func SpecifiedCodeSamplesTargets() []string {
return parseArrayInput(os.Getenv("INPUT_CODE_SAMPLES"))
}

func GetMode() Mode {
mode := os.Getenv("INPUT_MODE")
if mode == "" {
Expand Down Expand Up @@ -228,3 +237,15 @@ func ShouldOutputTests() bool {
func SetCLIVersionToUse(version string) error {
return os.Setenv("PINNED_VERSION", version)
}

func parseArrayInput(input string) []string {
if input == "" {
return []string{}
}

if strings.Contains(input, "\n") {
return strings.Split(input, "\n")
}

return strings.Split(input, ",")
}
30 changes: 30 additions & 0 deletions internal/registry/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package registry

import (
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"strings"
)

func ProcessRegistryTags() []string {
var tags []string
tagsInput := environment.RegistryTags()
if len(strings.Replace(tagsInput, " ", "", -1)) == 0 {
return tags
}

var processedTags []string
if strings.Contains(tagsInput, "\n") {
processedTags = strings.Split(tagsInput, "\n")
} else {
processedTags = strings.Split(tagsInput, ",")
}

for _, tag := range processedTags {
tag = strings.Replace(tag, " ", "", -1)
if len(tag) > 0 {
tags = append(tags, tag)
}
}

return tags
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func main() {
return actions.LogActionResult()
case environment.ActionPublishEvent:
return actions.PublishEvent()
case environment.ActionTag:
return actions.Tag()
default:
return fmt.Errorf("unknown action: %s", environment.GetAction())
}
Expand Down

0 comments on commit 2451b10

Please sign in to comment.