Skip to content

Commit

Permalink
feat: support spec workflows in github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert committed Mar 24, 2024
1 parent 50361b7 commit c656c44
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/workflow-executor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ jobs:
uses: smorimoto/tune-github-hosted-runner-network@v1
- id: run-workflow
name: Run Generation Workflow
uses: speakeasy-api/sdk-generation-action@v15
uses: speakeasy-api/sdk-generation-action@v15.1.0
with:
speakeasy_version: ${{ inputs.speakeasy_version }}
github_access_token: ${{ secrets.github_access_token }}
mode: ${{ inputs.mode }}
force: ${{ inputs.force }}
speakeasy_api_key: ${{ secrets.speakeasy_api_key }}
openapi_doc_auth_token: ${{ secrets.openapi_doc_auth_token }}
output_tests: ${{ inputs.output_tests }}
speakeasy_server_url: ${{ inputs.speakeasy_server_url }}
working_directory: ${{ inputs.working_directory }}
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ inputs:
speakeasy_api_key:
description: "The Speakeasy API key to authenticate the Speakeasy CLI with"
required: true
openapi_doc_auth_token:
description: The auth token to use when fetching the OpenAPI document if it is not publicly hosted. For example `Bearer <token>` or `<token>`.
required: false
force:
description: "Force the SDK to be regenerated"
default: "false"
Expand Down Expand Up @@ -134,7 +137,7 @@ outputs:
description: "The location of the OpenAPI document used for generation"
runs:
using: "docker"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15"
image: "docker://ghcr.io/speakeasy-api/sdk-generation-action:v15.1.0"
env:
SPEAKEASY_API_KEY: ${{ inputs.speakeasy_api_key }}
SPEAKEASY_SERVER_URL: ${{ inputs.speakeasy_server_url }}
Expand All @@ -147,6 +150,7 @@ runs:
- ${{ inputs.mode }}
- ${{ inputs.action }}
- ${{ inputs.branch_name }}
- ${{ inputs.openapi_doc_auth_token }}
- ${{ inputs.cli_output }}
- ${{ inputs.previous_gen_version }}
- ${{ inputs.openapi_doc_output }}
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/finalize-suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func FinalizeSuggestion() error {
return fmt.Errorf("suggestion action requires at least version %s of the speakeasy CLI", cli.MinimumSupportedCLIVersion)
}

branchName, _, err = g.FindExistingPR(branchName, environment.ActionFinalize)
branchName, _, err = g.FindExistingPR(branchName, environment.ActionFinalize, false)
if err != nil {
return err
}
Expand Down
23 changes: 15 additions & 8 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
"github.com/speakeasy-api/sdk-generation-action/internal/git"
"github.com/speakeasy-api/sdk-generation-action/internal/run"

Expand Down Expand Up @@ -31,11 +32,17 @@ func RunWorkflow() error {

mode := environment.GetMode()

branchName := ""
wf, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return err
}

sourcesOnly := wf.Targets == nil || len(wf.Targets) == 0

branchName := ""
if mode == environment.ModePR {
var err error
branchName, _, err = g.FindExistingPR("", environment.ActionRunWorkflow)
branchName, _, err = g.FindExistingPR("", environment.ActionRunWorkflow, sourcesOnly)
if err != nil {
return err
}
Expand All @@ -55,7 +62,7 @@ func RunWorkflow() error {
}
}()

genInfo, outputs, err := run.Run(g)
genInfo, outputs, err := run.Run(g, wf)
if err != nil {
if err := setOutputs(outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
Expand Down Expand Up @@ -122,7 +129,7 @@ func RunWorkflow() error {
}
}

if err = finalize(outputs, branchName, anythingRegenerated, g); err != nil {
if err = finalize(outputs, branchName, anythingRegenerated, sourcesOnly, g); err != nil {
return err
}

Expand All @@ -132,9 +139,9 @@ func RunWorkflow() error {
}

// Sets outputs and creates or adds releases info
func finalize(outputs map[string]string, branchName string, anythingRegenerated bool, g *git.Git) error {
func finalize(outputs map[string]string, branchName string, anythingRegenerated bool, sourcesOnly bool, g *git.Git) error {
// If nothing was regenerated, we don't need to do anything
if !anythingRegenerated {
if !anythingRegenerated && !sourcesOnly {
return nil
}

Expand All @@ -153,7 +160,7 @@ func finalize(outputs map[string]string, branchName string, anythingRegenerated

switch environment.GetMode() {
case environment.ModePR:
branchName, pr, err := g.FindExistingPR(branchName, environment.ActionFinalize)
branchName, pr, err := g.FindExistingPR(branchName, environment.ActionFinalize, sourcesOnly)
if err != nil {
return err
}
Expand All @@ -163,7 +170,7 @@ func finalize(outputs map[string]string, branchName string, anythingRegenerated
return err
}

if err := g.CreateOrUpdatePR(branchName, *releaseInfo, environment.GetPreviousGenVersion(), pr); err != nil {
if err := g.CreateOrUpdatePR(branchName, *releaseInfo, environment.GetPreviousGenVersion(), pr, sourcesOnly); err != nil {
return err
}
case environment.ModeDirect:
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Suggest() error {

branchName := ""

branchName, _, err = g.FindExistingPR("", environment.ActionSuggest)
branchName, _, err = g.FindExistingPR("", environment.ActionSuggest, false)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ func GetSupportedLanguages() ([]string, error) {
return strings.Split(langs, ", "), nil
}

func Run(installationURLs map[string]string, repoURL string, repoSubdirectories map[string]string) error {
func Run(sourcesOnly bool, installationURLs map[string]string, repoURL string, repoSubdirectories map[string]string) error {
args := []string{
"run",
}

args = append(args, "-t", "all")
if sourcesOnly {
args = append(args, "-s", "all")
} else {
args = append(args, "-t", "all")
}

urls, err := json.Marshal(installationURLs)
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (g *Git) CheckDirDirty(dir string, ignoreChangePatterns map[string]string)
return IsGitDiffSignificant(diffOutput, ignoreChangePatterns)
}

func (g *Git) FindExistingPR(branchName string, action environment.Action) (string, *github.PullRequest, error) {
func (g *Git) FindExistingPR(branchName string, action environment.Action, sourceGeneration bool) (string, *github.PullRequest, error) {
if g.repo == nil {
return "", nil, fmt.Errorf("repo not cloned")
}
Expand All @@ -172,6 +172,9 @@ func (g *Git) FindExistingPR(branchName string, action environment.Action) (stri
var prTitle string
if action == environment.ActionRunWorkflow || action == environment.ActionFinalize {
prTitle = getGenPRTitle()
if sourceGeneration {
prTitle = getGenSourcesTitle()
}
} else if action == environment.ActionFinalize || action == environment.ActionFinalizeSuggestion {
prTitle = getSuggestPRTitle()
}
Expand Down Expand Up @@ -352,7 +355,7 @@ func (g *Git) Add(arg string) error {
return nil
}

func (g *Git) CreateOrUpdatePR(branchName string, releaseInfo releases.ReleasesInfo, previousGenVersion string, pr *github.PullRequest) error {
func (g *Git) CreateOrUpdatePR(branchName string, releaseInfo releases.ReleasesInfo, previousGenVersion string, pr *github.PullRequest, sourceGeneration bool) error {
var changelog string
var err error

Expand Down Expand Up @@ -451,6 +454,8 @@ You have exceeded the limit of one free generated SDK. Please reach out to the S
title := getGenPRTitle()
if environment.IsDocsGeneration() {
title = getDocsPRTitle()
} else if sourceGeneration {
title = getGenSourcesTitle()
}

pr, _, err = g.client.PullRequests.Create(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), getRepo(), &github.NewPullRequest{
Expand Down Expand Up @@ -796,6 +801,7 @@ func getRepo() string {

const (
speakeasyGenPRTitle = "chore: 🐝 Update SDK - "
speakeasyGenSpecsTitle = "chore: 🐝 Update Specs - "
speakeasySuggestPRTitle = "chore: 🐝 Suggest OpenAPI changes - "
speakeasyDocsPRTitle = "chore: 🐝 Update SDK Docs - "
)
Expand All @@ -804,6 +810,10 @@ func getGenPRTitle() string {
return speakeasyGenPRTitle + environment.GetWorkflowName()
}

func getGenSourcesTitle() string {
return speakeasyGenSpecsTitle + environment.GetWorkflowName()
}

func getDocsPRTitle() string {
return speakeasyDocsPRTitle + environment.GetWorkflowName()
}
Expand Down
13 changes: 4 additions & 9 deletions internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package run

import (
"fmt"
"github.com/speakeasy-api/sdk-gen-config/workflow"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
"path"
"path/filepath"
"strings"

"github.com/speakeasy-api/sdk-gen-config/workflow"

config "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
Expand All @@ -29,7 +29,7 @@ type Git interface {
CheckDirDirty(dir string, ignoreMap map[string]string) (bool, string, error)
}

func Run(g Git) (*GenerationInfo, map[string]string, error) {
func Run(g Git, wf *workflow.Workflow) (*GenerationInfo, map[string]string, error) {
workspace := environment.GetWorkspace()
outputs := map[string]string{}

Expand All @@ -46,11 +46,6 @@ func Run(g Git) (*GenerationInfo, map[string]string, error) {

globalPreviousGenVersion := ""

wf, err := configuration.GetWorkflowAndValidateLanguages(true)
if err != nil {
return nil, outputs, err
}

langConfigs := map[string]*config.LanguageConfig{}

installationURLs := map[string]string{}
Expand Down Expand Up @@ -107,7 +102,7 @@ func Run(g Git) (*GenerationInfo, map[string]string, error) {
}

// Run the workflow
if err := cli.Run(installationURLs, repoURL, repoSubdirectories); err != nil {
if err := cli.Run(wf.Targets == nil || len(wf.Targets) == 0, installationURLs, repoURL, repoSubdirectories); err != nil {
return nil, outputs, err
}

Expand Down

0 comments on commit c656c44

Please sign in to comment.