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

Workflow context #40

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/sethvargo/go-envconfig"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -415,3 +416,44 @@ func (c *Action) Getenv(key string) string {
// GetenvFunc is an abstraction to make tests feasible for commands that
// interact with environment variables.
type GetenvFunc func(key string) string

// Context of current workflow
type Context struct {
Event map[string]interface{}
EventPayloadPath string `env:"GITHUB_EVENT_PATH,required"`
EventName string `env:"GITHUB_EVENT_NAME"`
SHA string `env:"GITHUB_SHA"`
Ref string `env:"GITHUB_REF"`
Workflow string `env:"GITHUB_WORKFLOW"`
Action string `env:"GITHUB_ACTION"`
Actor string `env:"GITHUB_ACTOR"`
Job string `env:"GITHUB_JOB"`
RunNumber int `env:"GITHUB_RUN_NUMBER"`
RunID int `env:"GITHUB_RUN_ID"`
APIURL string `env:"GITHUB_API_URL,default=https://api.github.com"`
bernardoVale marked this conversation as resolved.
Show resolved Hide resolved
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
GraphqlURL string `env:"GITHUB_GRAPHQL_URL,default=https://api.github.com/graphql"`
}

// Context returns the context of current action with the payload object that triggered the workflow
func (c *Action) Context(ctx context.Context) (*Context, error) {
var (
bernardoVale marked this conversation as resolved.
Show resolved Hide resolved
rawEvent map[string]interface{}
githubCtx Context
)

if err := envconfig.Process(ctx, &githubCtx); err != nil {
return nil, fmt.Errorf("could not process context variables: %w", err)
}

eventData, err := ioutil.ReadFile(githubCtx.EventPayloadPath)
if err != nil {
return nil, fmt.Errorf("could not read event file: %w", err)
}

if err = json.Unmarshal(eventData, &rawEvent); err != nil {
return nil, fmt.Errorf("could not unmarshall event payload: %w", err)
}

return &githubCtx, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
module github.com/sethvargo/go-githubactions

go 1.13

require github.com/sethvargo/go-envconfig v0.5.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/sethvargo/go-envconfig v0.5.0 h1:puRfYHj1HUe03MSPVBW8zc2BtRyn/KySgZcICXOzpVg=
github.com/sethvargo/go-envconfig v0.5.0/go.mod h1:00S1FAhRUuTNJazWBWcJGvEHOM+NO6DhoRMAOX7FY5o=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=