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: logging in github actions #69

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
112 changes: 112 additions & 0 deletions internal/actions/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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() error {
key := os.Getenv("SPEAKEASY_API_KEY")
if key == "" {
return fmt.Errorf("no speakeasy api key available.")
}

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]
ryan-timothy-albert marked this conversation as resolved.
Show resolved Hide resolved
}

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 {
return err
}

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 {
return err
}

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 nil
}

defer resp.Body.Close()

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

return nil
}
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"
)

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:
err = actions.LogActionResult()
ryan-timothy-albert marked this conversation as resolved.
Show resolved Hide resolved
}

if err != nil {
Expand Down