Skip to content

Commit

Permalink
Drop external dependencies (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jan 12, 2024
1 parent be6c104 commit f2efde7
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 42 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/unit.yml
Expand Up @@ -38,16 +38,14 @@ jobs:
- 'ubuntu-latest'
- 'windows-latest'
- 'macos-latest'
go:
- '1.18'

runs-on: '${{ matrix.os }}'

steps:
- uses: 'actions/checkout@v3'
- uses: 'actions/checkout@v4'

- uses: 'actions/setup-go@v3'
- uses: 'actions/setup-go@v5'
with:
go-version: '${{ matrix.go }}'
go-version-file: 'go.mod'

- run: 'make test-acc'
2 changes: 2 additions & 0 deletions Makefile
Expand Up @@ -16,6 +16,7 @@ test:
@go test \
-count=1 \
-short \
-shuffle=on \
-timeout=5m \
./...
.PHONY: test
Expand All @@ -24,6 +25,7 @@ test-acc:
@go test \
-count=1 \
-race \
-shuffle=on \
-timeout=10m \
./...
.PHONY: test-acc
144 changes: 123 additions & 21 deletions actions.go
Expand Up @@ -21,16 +21,16 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/sethvargo/go-envconfig"
)

var (
Expand Down Expand Up @@ -536,15 +536,130 @@ func (c *GitHubContext) Repo() (string, string) {
return ownerName, repoName
}

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)
}

// Context returns the context of current action with the payload object
// that triggered the workflow
func (c *Action) Context() (*GitHubContext, error) {
ctx := context.Background()
lookuper := &wrappedLookuper{f: c.getenv}
var merr error
githubContext := &GitHubContext{
APIURL: "https://api.github.com",
GraphqlURL: "https://api.github.com/graphql",
ServerURL: "https://github.com",
}

var githubContext GitHubContext
if err := envconfig.ProcessWith(ctx, &githubContext, lookuper); err != nil {
return nil, fmt.Errorf("could not process github context variables: %w", err)
if v := c.getenv("GITHUB_ACTION"); v != "" {
githubContext.Action = v
}
if v := c.getenv("GITHUB_ACTION_PATH"); v != "" {
githubContext.ActionPath = v
}
if v := c.getenv("GITHUB_ACTION_REPOSITORY"); v != "" {
githubContext.ActionRepository = v
}
if v, err := parseBool(c.getenv("GITHUB_ACTIONS")); err == nil {
githubContext.Actions = v
} else {
merr = errors.Join(merr, err)
}
if v := c.getenv("GITHUB_ACTOR"); v != "" {
githubContext.Actor = v
}
if v := c.getenv("GITHUB_API_URL"); v != "" {
githubContext.APIURL = v
}
if v := c.getenv("GITHUB_BASE_REF"); v != "" {
githubContext.BaseRef = v
}
if v := c.getenv("GITHUB_ENV"); v != "" {
githubContext.Env = v
}
if v := c.getenv("GITHUB_EVENT_NAME"); v != "" {
githubContext.EventName = v
}
if v := c.getenv("GITHUB_EVENT_PATH"); v != "" {
githubContext.EventPath = v
}
if v := c.getenv("GITHUB_GRAPHQL_URL"); v != "" {
githubContext.GraphqlURL = v
}
if v := c.getenv("GITHUB_HEAD_REF"); v != "" {
githubContext.HeadRef = v
}
if v := c.getenv("GITHUB_JOB"); v != "" {
githubContext.Job = v
}
if v := c.getenv("GITHUB_PATH"); v != "" {
githubContext.Path = v
}
if v := c.getenv("GITHUB_REF"); v != "" {
githubContext.Ref = v
}
if v := c.getenv("GITHUB_REF_NAME"); v != "" {
githubContext.RefName = v
}
if v, err := parseBool(c.getenv("GITHUB_REF_PROTECTED")); err == nil {
githubContext.RefProtected = v
} else {
merr = errors.Join(merr, err)
}
if v := c.getenv("GITHUB_REF_TYPE"); v != "" {
githubContext.RefType = v
}

if v := c.getenv("GITHUB_REPOSITORY"); v != "" {
githubContext.Repository = v
}
if v := c.getenv("GITHUB_REPOSITORY_OWNER"); v != "" {
githubContext.RepositoryOwner = v
}

if v, err := parseInt(c.getenv("GITHUB_RETENTION_DAYS")); err == nil {
githubContext.RetentionDays = v
} else {
merr = errors.Join(merr, err)
}
if v, err := parseInt(c.getenv("GITHUB_RUN_ATTEMPT")); err == nil {
githubContext.RunAttempt = v
} else {
merr = errors.Join(merr, err)
}
if v, err := parseInt(c.getenv("GITHUB_RUN_ID")); err == nil {
githubContext.RunID = v
} else {
merr = errors.Join(merr, err)
}
if v, err := parseInt(c.getenv("GITHUB_RUN_NUMBER")); err == nil {
githubContext.RunNumber = v
} else {
merr = errors.Join(merr, err)
}
if v := c.getenv("GITHUB_SERVER_URL"); v != "" {
githubContext.ServerURL = v
}
if v := c.getenv("GITHUB_SHA"); v != "" {
githubContext.SHA = v
}
if v := c.getenv("GITHUB_STEP_SUMMARY"); v != "" {
githubContext.StepSummary = v
}
if v := c.getenv("GITHUB_WORKFLOW"); v != "" {
githubContext.Workflow = v
}
if v := c.getenv("GITHUB_WORKSPACE"); v != "" {
githubContext.Workspace = v
}

if githubContext.EventPath != "" {
Expand All @@ -559,18 +674,5 @@ func (c *Action) Context() (*GitHubContext, error) {
}
}

return &githubContext, nil
}

// wrappedLookuper creates a lookuper that wraps a given getenv func.
type wrappedLookuper struct {
f GetenvFunc
}

// Lookup implements a custom lookuper.
func (w *wrappedLookuper) Lookup(key string) (string, bool) {
if v := w.f(key); v != "" {
return v, true
}
return "", false
return githubContext, merr
}
12 changes: 6 additions & 6 deletions actions_test.go
Expand Up @@ -25,8 +25,6 @@ import (
"reflect"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -301,14 +299,16 @@ func TestAction_AddStepSummaryTemplate(t *testing.T) {
defer os.Remove(file.Name())
fakeGetenvFunc := newFakeGetenvFunc(t, "GITHUB_STEP_SUMMARY", file.Name())
a := New(WithWriter(&b), WithGetenv(fakeGetenvFunc))
a.AddStepSummaryTemplate(`
if err := a.AddStepSummaryTemplate(`
## This is
{{.Input}}
- content
`, map[string]string{
"Input": "some markdown",
})
}); err != nil {
t.Fatal(err)
}

// expect an empty stdout buffer
if got, want := b.String(), ""; got != want {
Expand Down Expand Up @@ -770,8 +770,8 @@ func TestAction_Context(t *testing.T) {
t.Fatal(err)
}

if diff := cmp.Diff(tc.exp, got); diff != "" {
t.Fatalf("mismatch (-want, +got):\n%s", diff)
if !reflect.DeepEqual(got, tc.exp) {
t.Errorf("expected\n\n%#v\n\nto be\n\n%#v\n", got, tc.exp)
}
})
}
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Expand Up @@ -14,9 +14,4 @@

module github.com/sethvargo/go-githubactions

go 1.18

require (
github.com/google/go-cmp v0.5.8
github.com/sethvargo/go-envconfig v0.8.0
)
go 1.21
4 changes: 0 additions & 4 deletions go.sum
@@ -1,4 +0,0 @@
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/sethvargo/go-envconfig v0.8.0 h1:AcmdAewSFAc7pQ1Ghz+vhZkilUtxX559QlDuLLiSkdI=
github.com/sethvargo/go-envconfig v0.8.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=

0 comments on commit f2efde7

Please sign in to comment.