Skip to content

Commit f2efde7

Browse files
authored
Drop external dependencies (#55)
1 parent be6c104 commit f2efde7

File tree

6 files changed

+135
-42
lines changed

6 files changed

+135
-42
lines changed

.github/workflows/unit.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ jobs:
3838
- 'ubuntu-latest'
3939
- 'windows-latest'
4040
- 'macos-latest'
41-
go:
42-
- '1.18'
4341

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

4644
steps:
47-
- uses: 'actions/checkout@v3'
45+
- uses: 'actions/checkout@v4'
4846

49-
- uses: 'actions/setup-go@v3'
47+
- uses: 'actions/setup-go@v5'
5048
with:
51-
go-version: '${{ matrix.go }}'
49+
go-version-file: 'go.mod'
5250

5351
- run: 'make test-acc'

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test:
1616
@go test \
1717
-count=1 \
1818
-short \
19+
-shuffle=on \
1920
-timeout=5m \
2021
./...
2122
.PHONY: test
@@ -24,6 +25,7 @@ test-acc:
2425
@go test \
2526
-count=1 \
2627
-race \
28+
-shuffle=on \
2729
-timeout=10m \
2830
./...
2931
.PHONY: test-acc

actions.go

Lines changed: 123 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ import (
2121
"bytes"
2222
"context"
2323
"encoding/json"
24+
"errors"
2425
"fmt"
2526
"html/template"
2627
"io"
2728
"net/http"
2829
"net/url"
2930
"os"
31+
"strconv"
3032
"strings"
3133
"time"
32-
33-
"github.com/sethvargo/go-envconfig"
3434
)
3535

3636
var (
@@ -536,15 +536,130 @@ func (c *GitHubContext) Repo() (string, string) {
536536
return ownerName, repoName
537537
}
538538

539+
func parseBool(v string) (bool, error) {
540+
if v == "" {
541+
return false, nil
542+
}
543+
return strconv.ParseBool(v)
544+
}
545+
546+
func parseInt(v string) (int64, error) {
547+
if v == "" {
548+
return 0, nil
549+
}
550+
return strconv.ParseInt(v, 10, 64)
551+
}
552+
539553
// Context returns the context of current action with the payload object
540554
// that triggered the workflow
541555
func (c *Action) Context() (*GitHubContext, error) {
542-
ctx := context.Background()
543-
lookuper := &wrappedLookuper{f: c.getenv}
556+
var merr error
557+
githubContext := &GitHubContext{
558+
APIURL: "https://api.github.com",
559+
GraphqlURL: "https://api.github.com/graphql",
560+
ServerURL: "https://github.com",
561+
}
544562

545-
var githubContext GitHubContext
546-
if err := envconfig.ProcessWith(ctx, &githubContext, lookuper); err != nil {
547-
return nil, fmt.Errorf("could not process github context variables: %w", err)
563+
if v := c.getenv("GITHUB_ACTION"); v != "" {
564+
githubContext.Action = v
565+
}
566+
if v := c.getenv("GITHUB_ACTION_PATH"); v != "" {
567+
githubContext.ActionPath = v
568+
}
569+
if v := c.getenv("GITHUB_ACTION_REPOSITORY"); v != "" {
570+
githubContext.ActionRepository = v
571+
}
572+
if v, err := parseBool(c.getenv("GITHUB_ACTIONS")); err == nil {
573+
githubContext.Actions = v
574+
} else {
575+
merr = errors.Join(merr, err)
576+
}
577+
if v := c.getenv("GITHUB_ACTOR"); v != "" {
578+
githubContext.Actor = v
579+
}
580+
if v := c.getenv("GITHUB_API_URL"); v != "" {
581+
githubContext.APIURL = v
582+
}
583+
if v := c.getenv("GITHUB_BASE_REF"); v != "" {
584+
githubContext.BaseRef = v
585+
}
586+
if v := c.getenv("GITHUB_ENV"); v != "" {
587+
githubContext.Env = v
588+
}
589+
if v := c.getenv("GITHUB_EVENT_NAME"); v != "" {
590+
githubContext.EventName = v
591+
}
592+
if v := c.getenv("GITHUB_EVENT_PATH"); v != "" {
593+
githubContext.EventPath = v
594+
}
595+
if v := c.getenv("GITHUB_GRAPHQL_URL"); v != "" {
596+
githubContext.GraphqlURL = v
597+
}
598+
if v := c.getenv("GITHUB_HEAD_REF"); v != "" {
599+
githubContext.HeadRef = v
600+
}
601+
if v := c.getenv("GITHUB_JOB"); v != "" {
602+
githubContext.Job = v
603+
}
604+
if v := c.getenv("GITHUB_PATH"); v != "" {
605+
githubContext.Path = v
606+
}
607+
if v := c.getenv("GITHUB_REF"); v != "" {
608+
githubContext.Ref = v
609+
}
610+
if v := c.getenv("GITHUB_REF_NAME"); v != "" {
611+
githubContext.RefName = v
612+
}
613+
if v, err := parseBool(c.getenv("GITHUB_REF_PROTECTED")); err == nil {
614+
githubContext.RefProtected = v
615+
} else {
616+
merr = errors.Join(merr, err)
617+
}
618+
if v := c.getenv("GITHUB_REF_TYPE"); v != "" {
619+
githubContext.RefType = v
620+
}
621+
622+
if v := c.getenv("GITHUB_REPOSITORY"); v != "" {
623+
githubContext.Repository = v
624+
}
625+
if v := c.getenv("GITHUB_REPOSITORY_OWNER"); v != "" {
626+
githubContext.RepositoryOwner = v
627+
}
628+
629+
if v, err := parseInt(c.getenv("GITHUB_RETENTION_DAYS")); err == nil {
630+
githubContext.RetentionDays = v
631+
} else {
632+
merr = errors.Join(merr, err)
633+
}
634+
if v, err := parseInt(c.getenv("GITHUB_RUN_ATTEMPT")); err == nil {
635+
githubContext.RunAttempt = v
636+
} else {
637+
merr = errors.Join(merr, err)
638+
}
639+
if v, err := parseInt(c.getenv("GITHUB_RUN_ID")); err == nil {
640+
githubContext.RunID = v
641+
} else {
642+
merr = errors.Join(merr, err)
643+
}
644+
if v, err := parseInt(c.getenv("GITHUB_RUN_NUMBER")); err == nil {
645+
githubContext.RunNumber = v
646+
} else {
647+
merr = errors.Join(merr, err)
648+
}
649+
if v := c.getenv("GITHUB_SERVER_URL"); v != "" {
650+
githubContext.ServerURL = v
651+
}
652+
if v := c.getenv("GITHUB_SHA"); v != "" {
653+
githubContext.SHA = v
654+
}
655+
if v := c.getenv("GITHUB_STEP_SUMMARY"); v != "" {
656+
githubContext.StepSummary = v
657+
}
658+
if v := c.getenv("GITHUB_WORKFLOW"); v != "" {
659+
githubContext.Workflow = v
660+
}
661+
if v := c.getenv("GITHUB_WORKSPACE"); v != "" {
662+
githubContext.Workspace = v
548663
}
549664

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

562-
return &githubContext, nil
563-
}
564-
565-
// wrappedLookuper creates a lookuper that wraps a given getenv func.
566-
type wrappedLookuper struct {
567-
f GetenvFunc
568-
}
569-
570-
// Lookup implements a custom lookuper.
571-
func (w *wrappedLookuper) Lookup(key string) (string, bool) {
572-
if v := w.f(key); v != "" {
573-
return v, true
574-
}
575-
return "", false
677+
return githubContext, merr
576678
}

actions_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"reflect"
2626
"strings"
2727
"testing"
28-
29-
"github.com/google/go-cmp/cmp"
3028
)
3129

3230
func TestNew(t *testing.T) {
@@ -301,14 +299,16 @@ func TestAction_AddStepSummaryTemplate(t *testing.T) {
301299
defer os.Remove(file.Name())
302300
fakeGetenvFunc := newFakeGetenvFunc(t, "GITHUB_STEP_SUMMARY", file.Name())
303301
a := New(WithWriter(&b), WithGetenv(fakeGetenvFunc))
304-
a.AddStepSummaryTemplate(`
302+
if err := a.AddStepSummaryTemplate(`
305303
## This is
306304
307305
{{.Input}}
308306
- content
309307
`, map[string]string{
310308
"Input": "some markdown",
311-
})
309+
}); err != nil {
310+
t.Fatal(err)
311+
}
312312

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

773-
if diff := cmp.Diff(tc.exp, got); diff != "" {
774-
t.Fatalf("mismatch (-want, +got):\n%s", diff)
773+
if !reflect.DeepEqual(got, tc.exp) {
774+
t.Errorf("expected\n\n%#v\n\nto be\n\n%#v\n", got, tc.exp)
775775
}
776776
})
777777
}

go.mod

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,4 @@
1414

1515
module github.com/sethvargo/go-githubactions
1616

17-
go 1.18
18-
19-
require (
20-
github.com/google/go-cmp v0.5.8
21-
github.com/sethvargo/go-envconfig v0.8.0
22-
)
17+
go 1.21

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
2-
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3-
github.com/sethvargo/go-envconfig v0.8.0 h1:AcmdAewSFAc7pQ1Ghz+vhZkilUtxX559QlDuLLiSkdI=
4-
github.com/sethvargo/go-envconfig v0.8.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=

0 commit comments

Comments
 (0)