Compare commits
No commits in common. "main" and "v1.0.0" have entirely different histories.
3 changed files with 58 additions and 74 deletions
33
client.go
33
client.go
|
@ -1,33 +0,0 @@
|
||||||
package sdk
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (a *Action) Client() *Client {
|
|
||||||
c := &Client{Client: &http.Client{}}
|
|
||||||
context := a.Context()
|
|
||||||
c.base = context.APIURL
|
|
||||||
c.token = fmt.Sprintf("Bearer %s", context.Token)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
|
||||||
*http.Client
|
|
||||||
base string
|
|
||||||
token string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|
||||||
req.Header.Set("Authorization", c.token)
|
|
||||||
if !req.URL.IsAbs() {
|
|
||||||
u, err := url.Parse(fmt.Sprintf("%s%s", c.base, req.URL))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.URL = u
|
|
||||||
}
|
|
||||||
return c.Client.Do(req)
|
|
||||||
}
|
|
78
context.go
78
context.go
|
@ -2,8 +2,10 @@ package sdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GitHubContext of current workflow.
|
// GitHubContext of current workflow.
|
||||||
|
@ -12,7 +14,7 @@ type GitHubContext struct {
|
||||||
Action string `env:"GITHUB_ACTION"`
|
Action string `env:"GITHUB_ACTION"`
|
||||||
ActionPath string `env:"GITHUB_ACTION_PATH"`
|
ActionPath string `env:"GITHUB_ACTION_PATH"`
|
||||||
ActionRepository string `env:"GITHUB_ACTION_REPOSITORY"`
|
ActionRepository string `env:"GITHUB_ACTION_REPOSITORY"`
|
||||||
Actions string `env:"GITHUB_ACTIONS"`
|
Actions bool `env:"GITHUB_ACTIONS"`
|
||||||
Actor string `env:"GITHUB_ACTOR"`
|
Actor string `env:"GITHUB_ACTOR"`
|
||||||
APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"`
|
APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"`
|
||||||
BaseRef string `env:"GITHUB_BASE_REF"`
|
BaseRef string `env:"GITHUB_BASE_REF"`
|
||||||
|
@ -25,36 +27,34 @@ type GitHubContext struct {
|
||||||
Path string `env:"GITHUB_PATH"`
|
Path string `env:"GITHUB_PATH"`
|
||||||
Ref string `env:"GITHUB_REF"`
|
Ref string `env:"GITHUB_REF"`
|
||||||
RefName string `env:"GITHUB_REF_NAME"`
|
RefName string `env:"GITHUB_REF_NAME"`
|
||||||
RefProtected string `env:"GITHUB_REF_PROTECTED"`
|
RefProtected bool `env:"GITHUB_REF_PROTECTED"`
|
||||||
RefType string `env:"GITHUB_REF_TYPE"`
|
RefType string `env:"GITHUB_REF_TYPE"`
|
||||||
|
|
||||||
Repository string `env:"GITHUB_REPOSITORY"`
|
Repository string `env:"GITHUB_REPOSITORY"`
|
||||||
RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"`
|
RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"`
|
||||||
|
|
||||||
RetentionDays string `env:"GITHUB_RETENTION_DAYS"`
|
RetentionDays int64 `env:"GITHUB_RETENTION_DAYS"`
|
||||||
RunAttempt string `env:"GITHUB_RUN_ATTEMPT"`
|
RunAttempt int64 `env:"GITHUB_RUN_ATTEMPT"`
|
||||||
RunID string `env:"GITHUB_RUN_ID"`
|
RunID int64 `env:"GITHUB_RUN_ID"`
|
||||||
RunNumber string `env:"GITHUB_RUN_NUMBER"`
|
RunNumber int64 `env:"GITHUB_RUN_NUMBER"`
|
||||||
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
|
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
|
||||||
SHA string `env:"GITHUB_SHA"`
|
SHA string `env:"GITHUB_SHA"`
|
||||||
StepSummary string `env:"GITHUB_STEP_SUMMARY"`
|
StepSummary string `env:"GITHUB_STEP_SUMMARY"`
|
||||||
Workflow string `env:"GITHUB_WORKFLOW"`
|
Workflow string `env:"GITHUB_WORKFLOW"`
|
||||||
Workspace string `env:"GITHUB_WORKSPACE"`
|
Workspace string `env:"GITHUB_WORKSPACE"`
|
||||||
|
|
||||||
Token string `env:"GITHUB_WORKSPACE"`
|
|
||||||
|
|
||||||
// Event is populated by parsing the file at EventPath, if it exists.
|
// 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
|
// Context returns the context of current action with the payload object
|
||||||
// that triggered the workflow
|
// that triggered the workflow
|
||||||
func (c *Action) Context() *GitHubContext {
|
func (c *Action) Context() (*GitHubContext, error) {
|
||||||
|
var merr error
|
||||||
context := &GitHubContext{
|
context := &GitHubContext{
|
||||||
APIURL: "https://api.github.com",
|
APIURL: "https://api.github.com",
|
||||||
GraphqlURL: "https://api.github.com/graphql",
|
GraphqlURL: "https://api.github.com/graphql",
|
||||||
ServerURL: "https://github.com",
|
ServerURL: "https://github.com",
|
||||||
event: map[string]any{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v := c.env("GITHUB_ACTION"); v != "" {
|
if v := c.env("GITHUB_ACTION"); v != "" {
|
||||||
|
@ -66,8 +66,10 @@ func (c *Action) Context() *GitHubContext {
|
||||||
if v := c.env("GITHUB_ACTION_REPOSITORY"); v != "" {
|
if v := c.env("GITHUB_ACTION_REPOSITORY"); v != "" {
|
||||||
context.ActionRepository = v
|
context.ActionRepository = v
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_ACTIONS"); v != "" {
|
if v, err := parseBool(c.env("GITHUB_ACTIONS")); err == nil {
|
||||||
context.Actions = v
|
context.Actions = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_ACTOR"); v != "" {
|
if v := c.env("GITHUB_ACTOR"); v != "" {
|
||||||
context.Actor = v
|
context.Actor = v
|
||||||
|
@ -105,29 +107,41 @@ func (c *Action) Context() *GitHubContext {
|
||||||
if v := c.env("GITHUB_REF_NAME"); v != "" {
|
if v := c.env("GITHUB_REF_NAME"); v != "" {
|
||||||
context.RefName = v
|
context.RefName = v
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_REF_PROTECTED"); v != "" {
|
if v, err := parseBool(c.env("GITHUB_REF_PROTECTED")); err == nil {
|
||||||
context.RefProtected = v
|
context.RefProtected = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_REF_TYPE"); v != "" {
|
if v := c.env("GITHUB_REF_TYPE"); v != "" {
|
||||||
context.RefType = v
|
context.RefType = v
|
||||||
}
|
}
|
||||||
|
|
||||||
if v := c.env("GITHUB_REPOSITORY"); v != "" {
|
if v := c.env("GITHUB_REPOSITORY"); v != "" {
|
||||||
context.Repository = v
|
context.Repository = v
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_REPOSITORY_OWNER"); v != "" {
|
if v := c.env("GITHUB_REPOSITORY_OWNER"); v != "" {
|
||||||
context.RepositoryOwner = v
|
context.RepositoryOwner = v
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_RETENTION_DAYS"); v != "" {
|
|
||||||
|
if v, err := parseInt(c.env("GITHUB_RETENTION_DAYS")); err == nil {
|
||||||
context.RetentionDays = v
|
context.RetentionDays = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_RUN_ATTEMPT"); v != "" {
|
if v, err := parseInt(c.env("GITHUB_RUN_ATTEMPT")); err == nil {
|
||||||
context.RunAttempt = v
|
context.RunAttempt = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_RUN_ID"); v != "" {
|
if v, err := parseInt(c.env("GITHUB_RUN_ID")); err == nil {
|
||||||
context.RunID = v
|
context.RunID = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_RUN_NUMBER"); v != "" {
|
if v, err := parseInt(c.env("GITHUB_RUN_NUMBER")); err == nil {
|
||||||
context.RunNumber = v
|
context.RunNumber = v
|
||||||
|
} else {
|
||||||
|
merr = errors.Join(merr, err)
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_SERVER_URL"); v != "" {
|
if v := c.env("GITHUB_SERVER_URL"); v != "" {
|
||||||
context.ServerURL = v
|
context.ServerURL = v
|
||||||
|
@ -144,24 +158,32 @@ func (c *Action) Context() *GitHubContext {
|
||||||
if v := c.env("GITHUB_WORKSPACE"); v != "" {
|
if v := c.env("GITHUB_WORKSPACE"); v != "" {
|
||||||
context.Workspace = v
|
context.Workspace = v
|
||||||
}
|
}
|
||||||
if v := c.env("GITHUB_TOKEN"); v != "" {
|
|
||||||
context.Token = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return context
|
if context.EventPath != "" {
|
||||||
}
|
eventData, err := os.ReadFile(context.EventPath)
|
||||||
|
|
||||||
func (c *GitHubContext) Event() (map[string]any, error) {
|
|
||||||
if c.EventPath != "" {
|
|
||||||
eventData, err := os.ReadFile(c.EventPath)
|
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return nil, fmt.Errorf("could not read event file: %w", err)
|
return nil, fmt.Errorf("could not read event file: %w", err)
|
||||||
}
|
}
|
||||||
if eventData != nil {
|
if eventData != nil {
|
||||||
if err := json.Unmarshal(eventData, &c.event); err != nil {
|
if err := json.Unmarshal(eventData, &context.Event); err != nil {
|
||||||
return nil, fmt.Errorf("failed to unmarshal event payload: %w", err)
|
return nil, fmt.Errorf("failed to unmarshal event payload: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.event, nil
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,6 @@ func TestAction_Context(t *testing.T) {
|
||||||
APIURL: "https://api.github.com",
|
APIURL: "https://api.github.com",
|
||||||
ServerURL: "https://github.com",
|
ServerURL: "https://github.com",
|
||||||
GraphqlURL: "https://api.github.com/graphql",
|
GraphqlURL: "https://api.github.com/graphql",
|
||||||
event: map[string]any{},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -71,13 +70,12 @@ func TestAction_Context(t *testing.T) {
|
||||||
"GITHUB_STEP_SUMMARY": "/path/to/summary",
|
"GITHUB_STEP_SUMMARY": "/path/to/summary",
|
||||||
"GITHUB_WORKFLOW": "test",
|
"GITHUB_WORKFLOW": "test",
|
||||||
"GITHUB_WORKSPACE": "/path/to/workspace",
|
"GITHUB_WORKSPACE": "/path/to/workspace",
|
||||||
"GITHUB_TOKEN": "somerandomtoken",
|
|
||||||
},
|
},
|
||||||
exp: &GitHubContext{
|
exp: &GitHubContext{
|
||||||
Action: "__repo-owner_name-of-action-repo",
|
Action: "__repo-owner_name-of-action-repo",
|
||||||
ActionPath: "/path/to/action",
|
ActionPath: "/path/to/action",
|
||||||
ActionRepository: "repo-owner/name-of-action-repo",
|
ActionRepository: "repo-owner/name-of-action-repo",
|
||||||
Actions: "true",
|
Actions: true,
|
||||||
Actor: "sethvargo",
|
Actor: "sethvargo",
|
||||||
APIURL: "https://foo.com",
|
APIURL: "https://foo.com",
|
||||||
BaseRef: "main",
|
BaseRef: "main",
|
||||||
|
@ -90,21 +88,19 @@ func TestAction_Context(t *testing.T) {
|
||||||
Path: "/path/to/path",
|
Path: "/path/to/path",
|
||||||
Ref: "refs/tags/v1.0",
|
Ref: "refs/tags/v1.0",
|
||||||
RefName: "v1.0",
|
RefName: "v1.0",
|
||||||
RefProtected: "true",
|
RefProtected: true,
|
||||||
RefType: "tag",
|
RefType: "tag",
|
||||||
Repository: "sethvargo/baz",
|
Repository: "sethvargo/baz",
|
||||||
RepositoryOwner: "sethvargo",
|
RepositoryOwner: "sethvargo",
|
||||||
RetentionDays: "90",
|
RetentionDays: 90,
|
||||||
RunAttempt: "6",
|
RunAttempt: 6,
|
||||||
RunID: "56",
|
RunID: 56,
|
||||||
RunNumber: "34",
|
RunNumber: 34,
|
||||||
ServerURL: "https://bar.com",
|
ServerURL: "https://bar.com",
|
||||||
SHA: "abcd1234",
|
SHA: "abcd1234",
|
||||||
StepSummary: "/path/to/summary",
|
StepSummary: "/path/to/summary",
|
||||||
Workflow: "test",
|
Workflow: "test",
|
||||||
Workspace: "/path/to/workspace",
|
Workspace: "/path/to/workspace",
|
||||||
Token: "somerandomtoken",
|
|
||||||
event: map[string]any{},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -120,7 +116,7 @@ func TestAction_Context(t *testing.T) {
|
||||||
ServerURL: "https://github.com",
|
ServerURL: "https://github.com",
|
||||||
GraphqlURL: "https://api.github.com/graphql",
|
GraphqlURL: "https://api.github.com/graphql",
|
||||||
|
|
||||||
event: map[string]any{
|
Event: map[string]any{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -135,8 +131,7 @@ func TestAction_Context(t *testing.T) {
|
||||||
|
|
||||||
a := New()
|
a := New()
|
||||||
a.env = func(s string) string { return tc.env[s] }
|
a.env = func(s string) string { return tc.env[s] }
|
||||||
got := a.Context()
|
got, err := a.Context()
|
||||||
_, err := got.Event()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue