Skip to content

Commit

Permalink
Merge pull request #69 from speakeasy-api/add_logging_to_actions
Browse files Browse the repository at this point in the history
feat: logging in github actions
  • Loading branch information
ryan-timothy-albert authored Nov 17, 2023
2 parents 573f142 + eb73a15 commit 018088e
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 14 deletions.
300 changes: 300 additions & 0 deletions .github/workflows/sdk-generation.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion internal/actions/finalize-suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"errors"

"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"github.com/speakeasy-api/sdk-generation-action/internal/logging"
Expand Down Expand Up @@ -34,7 +35,7 @@ func FinalizeSuggestion() error {
return err
}

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

Expand Down
2 changes: 1 addition & 1 deletion internal/actions/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Finalize() error {

switch environment.GetMode() {
case environment.ModePR:
if err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
if _, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/actions/finalizeDocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func FinalizeDocs() error {

switch environment.GetMode() {
case environment.ModePR:
if err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
if _, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/actions/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func Generate() error {
return err
}

if err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
resolvedVersion, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
if err != nil {
return err
}

Expand Down Expand Up @@ -47,6 +48,7 @@ func Generate() error {
}()

genInfo, outputs, err := generate.Generate(g)
outputs["resolved_speakeasy_version"] = resolvedVersion
if err != nil {
if err := setOutputs(outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
Expand Down
4 changes: 3 additions & 1 deletion internal/actions/generateDocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func GenerateDocs() error {
return err
}

if err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
resolvedVersion, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
if err != nil {
return err
}

Expand Down Expand Up @@ -45,6 +46,7 @@ func GenerateDocs() error {
}()

genInfo, outputs, err := generate.GenerateDocs(g)
outputs["resolved_speakeasy_version"] = resolvedVersion
if err != nil {
if err := setOutputs(outputs); err != nil {
logging.Debug("failed to set outputs: %v", err)
Expand Down
115 changes: 115 additions & 0 deletions internal/actions/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package actions

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/speakeasy-api/sdk-generation-action/internal/environment"
"gopkg.in/yaml.v3"
)

const defaultAPIURL = "https://api.prod.speakeasyapi.dev"

type logProxyLevel string

const (
logProxyLevelInfo logProxyLevel = "info"
logProxyLevelError logProxyLevel = "error"
)

type logProxyEntry struct {
LogLevel logProxyLevel `json:"log_level"`
Message string `json:"message"`
Source string `json:"source"`
Tags map[string]interface{} `json:"tags"`
}

func LogActionResult() {
key := os.Getenv("SPEAKEASY_API_KEY")
if key == "" {
fmt.Print("no SPEAKEASY_API_KEY provided.")
return
}

logLevel := logProxyLevelInfo
logMessage := "Success in Github Action"
if !strings.Contains(strings.ToLower(os.Getenv("GH_ACTION_RESULT")), "success") {
logLevel = logProxyLevelError
logMessage = "Failure in Github Action"
}

request := logProxyEntry{
LogLevel: logLevel,
Message: logMessage,
Source: "gh_action",
Tags: map[string]interface{}{
"target": os.Getenv("TARGET"),
"gh_repository": fmt.Sprintf("https://github.com/%s", os.Getenv("GITHUB_REPOSITORY")),
"gh_action_version": os.Getenv("GH_ACTION_VERSION"),
"gh_action_step": os.Getenv("GH_ACTION_STEP"),
"gh_action_result": os.Getenv("GH_ACTION_RESULT"),
"gh_action_run": fmt.Sprintf("https://github.com/%s/actions/runs/%s", os.Getenv("GITHUB_REPOSITORY"), os.Getenv("GITHUB_RUN_ID")),
"run_origin": "gh_action",
},
}

languages := environment.GetLanguages()
languages = strings.ReplaceAll(languages, "\\n", "\n")
langs := []string{}
if err := yaml.Unmarshal([]byte(languages), &langs); err != nil {
fmt.Println("No language provided in github actions config.")
}
if len(langs) > 0 {
request.Tags["language"] = langs[0]
}

if os.Getenv("GITHUB_REPOSITORY") != "" {
request.Tags["gh_organization"] = strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0]
}

if os.Getenv("RESOLVED_SPEAKEASY_VERSION") != "" {
request.Tags["speakeasy_version"] = os.Getenv("RESOLVED_SPEAKEASY_VERSION")
}

body, err := json.Marshal(&request)
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
}

baseURL := os.Getenv("SPEAKEASY_SERVER_URL")
if baseURL == "" {
baseURL = defaultAPIURL
}

req, err := http.NewRequest("POST", baseURL+"/v1/log/proxy", bytes.NewBuffer(body))
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Api-Key", key)

client := &http.Client{
Timeout: time.Second * 5,
}
resp, err := client.Do(req)
if err != nil {
fmt.Print("failure sending log to speakeasy.")
return
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
fmt.Printf("failure sending log to speakeasy with status %s.", resp.Status)
}

return
}
3 changes: 2 additions & 1 deletion internal/actions/suggest.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/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/document"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
Expand All @@ -15,7 +16,7 @@ func Suggest() error {
return err
}

err = cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
_, err = cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions internal/actions/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func Validate() error {
return err
}

if err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g); err != nil {
resolvedVersion, err := cli.Download(environment.GetPinnedSpeakeasyVersion(), g)
if err != nil {
return err
}

Expand All @@ -28,7 +29,8 @@ func Validate() error {
docPathPrefix += "/"
}
if err := setOutputs(map[string]string{
"openapi_doc": strings.TrimPrefix(docPath, docPathPrefix),
"resolved_speakeasy_version": resolvedVersion,
"openapi_doc": strings.TrimPrefix(docPath, docPathPrefix),
}); err != nil {
logging.Debug("failed to set outputs: %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/cli/speakeasy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Git interface {
GetDownloadLink(version string) (string, string, error)
}

func Download(pinnedVersion string, g Git) error {
func Download(pinnedVersion string, g Git) (string, error) {
if pinnedVersion == "" {
pinnedVersion = "latest"
}
Expand All @@ -36,30 +36,30 @@ func Download(pinnedVersion string, g Git) error {

link, version, err := g.GetDownloadLink(version)
if err != nil {
return err
return version, err
}

fmt.Println("Downloading speakeasy cli version: ", version)

downloadPath := filepath.Join(os.TempDir(), "speakeasy"+path.Ext(link))
if err := download.DownloadFile(link, downloadPath, "", ""); err != nil {
return fmt.Errorf("failed to download speakeasy cli: %w", err)
return version, fmt.Errorf("failed to download speakeasy cli: %w", err)
}
defer os.Remove(downloadPath)

baseDir := environment.GetBaseDir()

if err := extract(downloadPath, filepath.Join(baseDir, "bin")); err != nil {
return fmt.Errorf("failed to extract speakeasy cli: %w", err)
return version, fmt.Errorf("failed to extract speakeasy cli: %w", err)
}

if err := os.Chmod(filepath.Join(baseDir, "bin", "speakeasy"), 0o755); err != nil {
return fmt.Errorf("failed to set permissions on speakeasy cli: %w", err)
return version, fmt.Errorf("failed to set permissions on speakeasy cli: %w", err)
}

fmt.Println("Extracted speakeasy cli to: ", filepath.Join(baseDir, "bin"))

return nil
return version, nil
}

func runSpeakeasyCommand(args ...string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
ActionFinalizeSuggestion Action = "finalize-suggestion"
ActionFinalizeDocs Action = "finalize-docs"
ActionRelease Action = "release"
ActionLog Action = "log-result"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func main() {
err = actions.FinalizeDocs()
case environment.ActionRelease:
err = actions.Release()
case environment.ActionLog:
actions.LogActionResult()
}

if err != nil {
Expand Down

0 comments on commit 018088e

Please sign in to comment.