Skip to content

Commit

Permalink
feat: add link to linting report to PR summary (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy authored Apr 18, 2024
1 parent 34fba85 commit 7b991aa
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 94 deletions.
64 changes: 44 additions & 20 deletions internal/actions/runWorkflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"fmt"

"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 @@ -65,7 +66,7 @@ func RunWorkflow() error {
}
}()

genInfo, outputs, err := run.Run(g, wf)
runRes, outputs, err := run.Run(g, wf)
if err != nil {
if err := setOutputs(outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
Expand All @@ -76,15 +77,15 @@ func RunWorkflow() error {

anythingRegenerated := false

if genInfo != nil {
docVersion := genInfo.OpenAPIDocVersion
speakeasyVersion := genInfo.SpeakeasyVersion
if runRes.GenInfo != nil {
docVersion := runRes.GenInfo.OpenAPIDocVersion
speakeasyVersion := runRes.GenInfo.SpeakeasyVersion

releaseInfo := releases.ReleasesInfo{
ReleaseTitle: environment.GetInvokeTime().Format("2006-01-02 15:04:05"),
DocVersion: docVersion,
SpeakeasyVersion: speakeasyVersion,
GenerationVersion: genInfo.GenerationVersion,
GenerationVersion: runRes.GenInfo.GenerationVersion,
DocLocation: environment.GetOpenAPIDocLocation(),
Languages: map[string]releases.LanguageReleaseInfo{},
LanguagesGenerated: map[string]releases.GenerationInfo{},
Expand All @@ -96,7 +97,7 @@ func RunWorkflow() error {
}

for _, lang := range supportedLanguages {
langGenInfo, ok := genInfo.Languages[lang]
langGenInfo, ok := runRes.GenInfo.Languages[lang]

if ok && outputs[fmt.Sprintf("%s_regenerated", lang)] == "true" {
anythingRegenerated = true
Expand Down Expand Up @@ -138,7 +139,14 @@ func RunWorkflow() error {
}
}

if err = finalize(outputs, branchName, anythingRegenerated, sourcesOnly, g); err != nil {
if err := finalize(finalizeInputs{
Outputs: outputs,
BranchName: branchName,
AnythingRegenerated: anythingRegenerated,
SourcesOnly: sourcesOnly,
Git: g,
LintingReport: runRes.LintingReport,
}); err != nil {
return err
}

Expand All @@ -147,65 +155,81 @@ func RunWorkflow() error {
return nil
}

type finalizeInputs struct {
Outputs map[string]string
BranchName string
AnythingRegenerated bool
SourcesOnly bool
Git *git.Git
LintingReport string
}

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

branchName, err := g.FindAndCheckoutBranch(branchName)
branchName, err := inputs.Git.FindAndCheckoutBranch(inputs.BranchName)
if err != nil {
return err
}

defer func() {
outputs["branch_name"] = branchName
inputs.Outputs["branch_name"] = branchName

if err := setOutputs(outputs); err != nil {
if err := setOutputs(inputs.Outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
}
}()

switch environment.GetMode() {
case environment.ModePR:
branchName, pr, err := g.FindExistingPR(branchName, environment.ActionFinalize, sourcesOnly)
branchName, pr, err := inputs.Git.FindExistingPR(branchName, environment.ActionFinalize, inputs.SourcesOnly)
if err != nil {
return err
}

var releaseInfo *releases.ReleasesInfo
if !sourcesOnly {
if !inputs.SourcesOnly {
releaseInfo, err = getReleasesInfo()
if err != nil {
return err
}
}

if err := g.CreateOrUpdatePR(branchName, releaseInfo, environment.GetPreviousGenVersion(), pr, sourcesOnly); err != nil {
if err := inputs.Git.CreateOrUpdatePR(git.PRInfo{
BranchName: branchName,
ReleaseInfo: releaseInfo,
PreviousGenVersion: environment.GetPreviousGenVersion(),
PR: pr,
SourceGeneration: inputs.SourcesOnly,
LintingReport: inputs.LintingReport,
}); err != nil {
return err
}
case environment.ModeDirect:
var releaseInfo *releases.ReleasesInfo
if !sourcesOnly {
if !inputs.SourcesOnly {
releaseInfo, err = getReleasesInfo()
if err != nil {
return err
}
}

commitHash, err := g.MergeBranch(branchName)
commitHash, err := inputs.Git.MergeBranch(branchName)
if err != nil {
return err
}

if !sourcesOnly && environment.CreateGitRelease() {
if err := g.CreateRelease(*releaseInfo); err != nil {
if !inputs.SourcesOnly && environment.CreateGitRelease() {
if err := inputs.Git.CreateRelease(*releaseInfo); err != nil {
return err
}
}

outputs["commit_hash"] = commitHash
inputs.Outputs["commit_hash"] = commitHash
}

return nil
Expand Down
49 changes: 0 additions & 49 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -40,54 +39,6 @@ func GetSupportedLanguages() ([]string, error) {
return strings.Split(langs, ", "), nil
}

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

if sourcesOnly {
args = append(args, "-s", "all")
} else {
specifiedTarget := environment.SpecifiedTarget()
if specifiedTarget != "" {
args = append(args, "-t", specifiedTarget)
} else {
args = append(args, "-t", "all")
}
urls, err := json.Marshal(installationURLs)
if err != nil {
return fmt.Errorf("error marshalling installation urls: %w", err)
}
args = append(args, "--installationURLs", string(urls))

subdirs, err := json.Marshal(repoSubdirectories)
if err != nil {
return fmt.Errorf("error marshalling repo subdirectories: %w", err)
}
args = append(args, "--repo-subdirs", string(subdirs))
}

if repoURL != "" {
args = append(args, "-r", repoURL)
}

if environment.ForceGeneration() {
fmt.Println("force input enabled - setting SPEAKEASY_FORCE_GENERATION=true")
os.Setenv("SPEAKEASY_FORCE_GENERATION", "true")
}

if environment.ShouldOutputTests() {
// TODO: Add CLI flag for outputting tests
}

out, err := runSpeakeasyCommand(args...)
if err != nil {
return fmt.Errorf("error running workflow: %w - %s", err, out)
}
fmt.Println(out)
return nil
}

func TriggerGoGenerate() error {
tidyCmd := exec.Command("go", "mod", "tidy")
tidyCmd.Dir = filepath.Join(environment.GetWorkspace(), "repo", environment.GetWorkingDirectory())
Expand Down
78 changes: 78 additions & 0 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cli

import (
"encoding/json"
"fmt"
"os"
"regexp"

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

type RunResults struct {
LintingReport string
}

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

if sourcesOnly {
args = append(args, "-s", "all")
} else {
specifiedTarget := environment.SpecifiedTarget()
if specifiedTarget != "" {
args = append(args, "-t", specifiedTarget)
} else {
args = append(args, "-t", "all")
}
urls, err := json.Marshal(installationURLs)
if err != nil {
return nil, fmt.Errorf("error marshalling installation urls: %w", err)
}
args = append(args, "--installationURLs", string(urls))

subdirs, err := json.Marshal(repoSubdirectories)
if err != nil {
return nil, fmt.Errorf("error marshalling repo subdirectories: %w", err)
}
args = append(args, "--repo-subdirs", string(subdirs))
}

if repoURL != "" {
args = append(args, "-r", repoURL)
}

if environment.ForceGeneration() {
fmt.Println("force input enabled - setting SPEAKEASY_FORCE_GENERATION=true")
os.Setenv("SPEAKEASY_FORCE_GENERATION", "true")
}

//if environment.ShouldOutputTests() {
// TODO: Add CLI flag for outputting tests
//}

out, err := runSpeakeasyCommand(args...)
if err != nil {
return nil, fmt.Errorf("error running workflow: %w - %s", err, out)
}

lintingReportURL := getLintingReportURL(out)

fmt.Println(out)
return &RunResults{
LintingReport: lintingReportURL,
}, nil
}

var lintingReportRegex = regexp.MustCompile(`(?m).*?(https:\/\/app.speakeasyapi.dev\/org\/.*?\/.*?\/linting-report\/.*?)\s`)

func getLintingReportURL(out string) string {
matches := lintingReportRegex.FindStringSubmatch(out)
if len(matches) > 1 {
return matches[1]
}

return ""
}
39 changes: 39 additions & 0 deletions internal/cli/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_getLintingReportURL(t *testing.T) {
type args struct {
out string
}
tests := []struct {
name string
args args
want string
}{
{
name: "success",
args: args{
out: `INFO Running source validationtest...
INFO Validating OpenAPI spec...
INFO Using ruleset thisRuleset from validationtest/.speakeasy/lint.yaml
INFO Validation report available to view at: https://app.speakeasyapi.dev/org/test/test/linting-report/7aebdf7581f7f04430709644c2e304b7
WARN validation warn: [line 12] any-paths - More than a single path exists, there are 1
INFO
OpenAPI spec validation complete. 0 errors, 1 warnings, 0 hints`,
},
want: "https://app.speakeasyapi.dev/org/test/test/linting-report/7aebdf7581f7f04430709644c2e304b7",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url := getLintingReportURL(tt.args.out)
assert.Equal(t, tt.want, url)
})
}
}
Loading

0 comments on commit 7b991aa

Please sign in to comment.