From 425da0c61219ed1f582e72f8727ffdcee1f17562 Mon Sep 17 00:00:00 2001 From: Louis Seubert Date: Fri, 1 Nov 2024 23:38:54 +0100 Subject: [PATCH] feat: change github context parsing --- client.go | 5 ++-- context.go | 78 ++++++++++++++++++------------------------------- context_test.go | 21 ++++++++----- 3 files changed, 44 insertions(+), 60 deletions(-) diff --git a/client.go b/client.go index 2682a63..9898adb 100644 --- a/client.go +++ b/client.go @@ -8,8 +8,9 @@ import ( func (a *Action) Client() *Client { c := &Client{Client: &http.Client{}} - c.base = a.env("GITHUB_API_URL") - c.token = fmt.Sprintf("Bearer %s", a.env("GITHUB_TOKEN")) + context := a.Context() + c.base = context.APIURL + c.token = fmt.Sprintf("Bearer %s", context.Token) return c } diff --git a/context.go b/context.go index 76477d2..838c088 100644 --- a/context.go +++ b/context.go @@ -2,10 +2,8 @@ package sdk import ( "encoding/json" - "errors" "fmt" "os" - "strconv" ) // GitHubContext of current workflow. @@ -14,7 +12,7 @@ type GitHubContext struct { Action string `env:"GITHUB_ACTION"` ActionPath string `env:"GITHUB_ACTION_PATH"` ActionRepository string `env:"GITHUB_ACTION_REPOSITORY"` - Actions bool `env:"GITHUB_ACTIONS"` + Actions string `env:"GITHUB_ACTIONS"` Actor string `env:"GITHUB_ACTOR"` APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"` BaseRef string `env:"GITHUB_BASE_REF"` @@ -27,34 +25,36 @@ type GitHubContext struct { Path string `env:"GITHUB_PATH"` Ref string `env:"GITHUB_REF"` RefName string `env:"GITHUB_REF_NAME"` - RefProtected bool `env:"GITHUB_REF_PROTECTED"` + RefProtected string `env:"GITHUB_REF_PROTECTED"` RefType string `env:"GITHUB_REF_TYPE"` Repository string `env:"GITHUB_REPOSITORY"` RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"` - RetentionDays int64 `env:"GITHUB_RETENTION_DAYS"` - RunAttempt int64 `env:"GITHUB_RUN_ATTEMPT"` - RunID int64 `env:"GITHUB_RUN_ID"` - RunNumber int64 `env:"GITHUB_RUN_NUMBER"` + RetentionDays string `env:"GITHUB_RETENTION_DAYS"` + RunAttempt string `env:"GITHUB_RUN_ATTEMPT"` + RunID string `env:"GITHUB_RUN_ID"` + RunNumber string `env:"GITHUB_RUN_NUMBER"` 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"` + Token string `env:"GITHUB_WORKSPACE"` + // Event is populated by parsing the file at EventPath, if it exists. - Event map[string]any + event map[string]any } // Context returns the context of current action with the payload object // that triggered the workflow -func (c *Action) Context() (*GitHubContext, error) { - var merr error +func (c *Action) Context() *GitHubContext { context := &GitHubContext{ APIURL: "https://api.github.com", GraphqlURL: "https://api.github.com/graphql", ServerURL: "https://github.com", + event: map[string]any{}, } if v := c.env("GITHUB_ACTION"); v != "" { @@ -66,10 +66,8 @@ func (c *Action) Context() (*GitHubContext, error) { if v := c.env("GITHUB_ACTION_REPOSITORY"); v != "" { context.ActionRepository = v } - if v, err := parseBool(c.env("GITHUB_ACTIONS")); err == nil { + if v := c.env("GITHUB_ACTIONS"); v != "" { context.Actions = v - } else { - merr = errors.Join(merr, err) } if v := c.env("GITHUB_ACTOR"); v != "" { context.Actor = v @@ -107,41 +105,29 @@ func (c *Action) Context() (*GitHubContext, error) { if v := c.env("GITHUB_REF_NAME"); v != "" { context.RefName = v } - if v, err := parseBool(c.env("GITHUB_REF_PROTECTED")); err == nil { + if v := c.env("GITHUB_REF_PROTECTED"); v != "" { context.RefProtected = v - } else { - merr = errors.Join(merr, err) } 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 } - - if v, err := parseInt(c.env("GITHUB_RETENTION_DAYS")); err == nil { + if v := c.env("GITHUB_RETENTION_DAYS"); v != "" { context.RetentionDays = v - } else { - merr = errors.Join(merr, err) } - if v, err := parseInt(c.env("GITHUB_RUN_ATTEMPT")); err == nil { + if v := c.env("GITHUB_RUN_ATTEMPT"); v != "" { context.RunAttempt = v - } else { - merr = errors.Join(merr, err) } - if v, err := parseInt(c.env("GITHUB_RUN_ID")); err == nil { + if v := c.env("GITHUB_RUN_ID"); v != "" { context.RunID = v - } else { - merr = errors.Join(merr, err) } - if v, err := parseInt(c.env("GITHUB_RUN_NUMBER")); err == nil { + if v := c.env("GITHUB_RUN_NUMBER"); v != "" { context.RunNumber = v - } else { - merr = errors.Join(merr, err) } if v := c.env("GITHUB_SERVER_URL"); v != "" { context.ServerURL = v @@ -158,32 +144,24 @@ func (c *Action) Context() (*GitHubContext, error) { if v := c.env("GITHUB_WORKSPACE"); v != "" { context.Workspace = v } + if v := c.env("GITHUB_TOKEN"); v != "" { + context.Token = v + } - if context.EventPath != "" { - eventData, err := os.ReadFile(context.EventPath) + return context +} + +func (c *GitHubContext) Event() (map[string]any, error) { + if c.EventPath != "" { + eventData, err := os.ReadFile(c.EventPath) if err != nil && !os.IsNotExist(err) { return nil, fmt.Errorf("could not read event file: %w", err) } if eventData != nil { - if err := json.Unmarshal(eventData, &context.Event); err != nil { + if err := json.Unmarshal(eventData, &c.event); err != nil { return nil, fmt.Errorf("failed to unmarshal event payload: %w", err) } } } - - return context, merr -} - -func parseBool(v string) (bool, error) { - if v == "" { - return false, nil - } - return strconv.ParseBool(v) -} - -func parseInt(v string) (int64, error) { - if v == "" { - return 0, nil - } - return strconv.ParseInt(v, 10, 64) + return c.event, nil } diff --git a/context_test.go b/context_test.go index f261d42..89f3ab2 100644 --- a/context_test.go +++ b/context_test.go @@ -37,6 +37,7 @@ func TestAction_Context(t *testing.T) { APIURL: "https://api.github.com", ServerURL: "https://github.com", GraphqlURL: "https://api.github.com/graphql", + event: map[string]any{}, }, }, { @@ -70,12 +71,13 @@ func TestAction_Context(t *testing.T) { "GITHUB_STEP_SUMMARY": "/path/to/summary", "GITHUB_WORKFLOW": "test", "GITHUB_WORKSPACE": "/path/to/workspace", + "GITHUB_TOKEN": "somerandomtoken", }, exp: &GitHubContext{ Action: "__repo-owner_name-of-action-repo", ActionPath: "/path/to/action", ActionRepository: "repo-owner/name-of-action-repo", - Actions: true, + Actions: "true", Actor: "sethvargo", APIURL: "https://foo.com", BaseRef: "main", @@ -88,19 +90,21 @@ func TestAction_Context(t *testing.T) { Path: "/path/to/path", Ref: "refs/tags/v1.0", RefName: "v1.0", - RefProtected: true, + RefProtected: "true", RefType: "tag", Repository: "sethvargo/baz", RepositoryOwner: "sethvargo", - RetentionDays: 90, - RunAttempt: 6, - RunID: 56, - RunNumber: 34, + RetentionDays: "90", + RunAttempt: "6", + RunID: "56", + RunNumber: "34", ServerURL: "https://bar.com", SHA: "abcd1234", StepSummary: "/path/to/summary", Workflow: "test", Workspace: "/path/to/workspace", + Token: "somerandomtoken", + event: map[string]any{}, }, }, { @@ -116,7 +120,7 @@ func TestAction_Context(t *testing.T) { ServerURL: "https://github.com", GraphqlURL: "https://api.github.com/graphql", - Event: map[string]any{ + event: map[string]any{ "foo": "bar", }, }, @@ -131,7 +135,8 @@ func TestAction_Context(t *testing.T) { a := New() a.env = func(s string) string { return tc.env[s] } - got, err := a.Context() + got := a.Context() + _, err := got.Event() if err != nil { t.Fatal(err) }