2024-07-21 10:15:55 +00:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GitHubContext of current workflow.
|
|
|
|
// See: https://docs.github.com/en/actions/learn-github-actions/environment-variables
|
|
|
|
type GitHubContext struct {
|
|
|
|
Action string `env:"GITHUB_ACTION"`
|
|
|
|
ActionPath string `env:"GITHUB_ACTION_PATH"`
|
|
|
|
ActionRepository string `env:"GITHUB_ACTION_REPOSITORY"`
|
2024-11-01 22:38:54 +00:00
|
|
|
Actions string `env:"GITHUB_ACTIONS"`
|
2024-07-21 10:15:55 +00:00
|
|
|
Actor string `env:"GITHUB_ACTOR"`
|
|
|
|
APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"`
|
|
|
|
BaseRef string `env:"GITHUB_BASE_REF"`
|
|
|
|
Env string `env:"GITHUB_ENV"`
|
|
|
|
EventName string `env:"GITHUB_EVENT_NAME"`
|
|
|
|
EventPath string `env:"GITHUB_EVENT_PATH"`
|
|
|
|
GraphqlURL string `env:"GITHUB_GRAPHQL_URL,default=https://api.github.com/graphql"`
|
|
|
|
HeadRef string `env:"GITHUB_HEAD_REF"`
|
|
|
|
Job string `env:"GITHUB_JOB"`
|
|
|
|
Path string `env:"GITHUB_PATH"`
|
|
|
|
Ref string `env:"GITHUB_REF"`
|
|
|
|
RefName string `env:"GITHUB_REF_NAME"`
|
2024-11-01 22:38:54 +00:00
|
|
|
RefProtected string `env:"GITHUB_REF_PROTECTED"`
|
2024-07-21 10:15:55 +00:00
|
|
|
RefType string `env:"GITHUB_REF_TYPE"`
|
|
|
|
|
|
|
|
Repository string `env:"GITHUB_REPOSITORY"`
|
|
|
|
RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"`
|
|
|
|
|
2024-11-01 22:38:54 +00:00
|
|
|
RetentionDays string `env:"GITHUB_RETENTION_DAYS"`
|
|
|
|
RunAttempt string `env:"GITHUB_RUN_ATTEMPT"`
|
|
|
|
RunID string `env:"GITHUB_RUN_ID"`
|
|
|
|
RunNumber string `env:"GITHUB_RUN_NUMBER"`
|
2024-07-21 10:15:55 +00:00
|
|
|
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
|
|
|
|
SHA string `env:"GITHUB_SHA"`
|
|
|
|
StepSummary string `env:"GITHUB_STEP_SUMMARY"`
|
|
|
|
Workflow string `env:"GITHUB_WORKFLOW"`
|
|
|
|
Workspace string `env:"GITHUB_WORKSPACE"`
|
|
|
|
|
2024-11-01 22:38:54 +00:00
|
|
|
Token string `env:"GITHUB_WORKSPACE"`
|
|
|
|
|
2024-07-21 10:15:55 +00:00
|
|
|
// Event is populated by parsing the file at EventPath, if it exists.
|
2024-11-01 22:38:54 +00:00
|
|
|
event map[string]any
|
2024-07-21 10:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns the context of current action with the payload object
|
|
|
|
// that triggered the workflow
|
2024-11-01 22:38:54 +00:00
|
|
|
func (c *Action) Context() *GitHubContext {
|
2024-07-21 10:15:55 +00:00
|
|
|
context := &GitHubContext{
|
|
|
|
APIURL: "https://api.github.com",
|
|
|
|
GraphqlURL: "https://api.github.com/graphql",
|
|
|
|
ServerURL: "https://github.com",
|
2024-11-01 22:38:54 +00:00
|
|
|
event: map[string]any{},
|
2024-07-21 10:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v := c.env("GITHUB_ACTION"); v != "" {
|
|
|
|
context.Action = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_ACTION_PATH"); v != "" {
|
|
|
|
context.ActionPath = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_ACTION_REPOSITORY"); v != "" {
|
|
|
|
context.ActionRepository = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_ACTIONS"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.Actions = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_ACTOR"); v != "" {
|
|
|
|
context.Actor = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_API_URL"); v != "" {
|
|
|
|
context.APIURL = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_BASE_REF"); v != "" {
|
|
|
|
context.BaseRef = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_ENV"); v != "" {
|
|
|
|
context.Env = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_EVENT_NAME"); v != "" {
|
|
|
|
context.EventName = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_EVENT_PATH"); v != "" {
|
|
|
|
context.EventPath = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_GRAPHQL_URL"); v != "" {
|
|
|
|
context.GraphqlURL = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_HEAD_REF"); v != "" {
|
|
|
|
context.HeadRef = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_JOB"); v != "" {
|
|
|
|
context.Job = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_PATH"); v != "" {
|
|
|
|
context.Path = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_REF"); v != "" {
|
|
|
|
context.Ref = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_REF_NAME"); v != "" {
|
|
|
|
context.RefName = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_REF_PROTECTED"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.RefProtected = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_REF_TYPE"); v != "" {
|
|
|
|
context.RefType = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_REPOSITORY"); v != "" {
|
|
|
|
context.Repository = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_REPOSITORY_OWNER"); v != "" {
|
|
|
|
context.RepositoryOwner = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_RETENTION_DAYS"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.RetentionDays = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_RUN_ATTEMPT"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.RunAttempt = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_RUN_ID"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.RunID = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_RUN_NUMBER"); v != "" {
|
2024-07-21 10:15:55 +00:00
|
|
|
context.RunNumber = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_SERVER_URL"); v != "" {
|
|
|
|
context.ServerURL = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_SHA"); v != "" {
|
|
|
|
context.SHA = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_STEP_SUMMARY"); v != "" {
|
|
|
|
context.StepSummary = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_WORKFLOW"); v != "" {
|
|
|
|
context.Workflow = v
|
|
|
|
}
|
|
|
|
if v := c.env("GITHUB_WORKSPACE"); v != "" {
|
|
|
|
context.Workspace = v
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
if v := c.env("GITHUB_TOKEN"); v != "" {
|
|
|
|
context.Token = v
|
|
|
|
}
|
2024-07-21 10:15:55 +00:00
|
|
|
|
2024-11-01 22:38:54 +00:00
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *GitHubContext) Event() (map[string]any, error) {
|
|
|
|
if c.EventPath != "" {
|
|
|
|
eventData, err := os.ReadFile(c.EventPath)
|
2024-07-21 10:15:55 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("could not read event file: %w", err)
|
|
|
|
}
|
|
|
|
if eventData != nil {
|
2024-11-01 22:38:54 +00:00
|
|
|
if err := json.Unmarshal(eventData, &c.event); err != nil {
|
2024-07-21 10:15:55 +00:00
|
|
|
return nil, fmt.Errorf("failed to unmarshal event payload: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-01 22:38:54 +00:00
|
|
|
return c.event, nil
|
2024-07-21 10:15:55 +00:00
|
|
|
}
|