-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from speakeasy-api/add_logging_to_actions
feat: logging in github actions
- Loading branch information
Showing
12 changed files
with
440 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters