From 8e216c642e11dfa8dba7aab040bb09274c48b5a6 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Sun, 8 May 2022 10:23:19 -0400 Subject: [PATCH] feat: support `GITHUB_REF_NAME` & `GITHUB_REF_TYPE` (#1142) * feat: support `_REF_NAME` & `_REF_TYPE` * chore: fix `step_test::TestSetupEnv` * fix: logic & test * test: delete `GITHUB_REF_NAME`, `GITHUB_REF_TYPE` --- pkg/model/github_context.go | 2 ++ pkg/runner/run_context.go | 11 +++++++++++ pkg/runner/step_test.go | 2 ++ 3 files changed, 15 insertions(+) diff --git a/pkg/model/github_context.go b/pkg/model/github_context.go index c68aaa1..26e61ae 100644 --- a/pkg/model/github_context.go +++ b/pkg/model/github_context.go @@ -18,6 +18,8 @@ type GithubContext struct { EventName string `json:"event_name"` Sha string `json:"sha"` Ref string `json:"ref"` + RefName string `json:"ref_name"` + RefType string `json:"ref_type"` HeadRef string `json:"head_ref"` BaseRef string `json:"base_ref"` Token string `json:"token"` diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 06c0740..07f62d8 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -549,6 +549,15 @@ func (rc *RunContext) getGithubContext() *model.GithubContext { ghc.SetRefAndSha(rc.Config.DefaultBranch, repoPath) + // https://docs.github.com/en/actions/learn-github-actions/environment-variables + if strings.HasPrefix(ghc.Ref, "refs/tags/") { + ghc.RefType = "tag" + ghc.RefName = ghc.Ref[len("refs/tags/"):] + } else if strings.HasPrefix(ghc.Ref, "refs/heads/") { + ghc.RefType = "branch" + ghc.RefName = ghc.Ref[len("refs/heads/"):] + } + return ghc } @@ -624,6 +633,8 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string { env["GITHUB_WORKSPACE"] = github.Workspace env["GITHUB_SHA"] = github.Sha env["GITHUB_REF"] = github.Ref + env["GITHUB_REF_NAME"] = github.RefName + env["GITHUB_REF_TYPE"] = github.RefType env["GITHUB_TOKEN"] = github.Token env["GITHUB_SERVER_URL"] = "https://github.com" env["GITHUB_API_URL"] = "https://api.github.com" diff --git a/pkg/runner/step_test.go b/pkg/runner/step_test.go index c8f3a0c..b87efeb 100644 --- a/pkg/runner/step_test.go +++ b/pkg/runner/step_test.go @@ -154,6 +154,8 @@ func TestSetupEnv(t *testing.T) { // These are commit or system specific delete((env), "GITHUB_REF") + delete((env), "GITHUB_REF_NAME") + delete((env), "GITHUB_REF_TYPE") delete((env), "GITHUB_SHA") delete((env), "GITHUB_WORKSPACE") delete((env), "GITHUB_REPOSITORY")