@@ -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
3636var (
@@ -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
541555func (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}
0 commit comments