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`
This commit is contained in:
Kevin Wang 2022-05-08 10:23:19 -04:00 committed by GitHub
parent dfca2c57df
commit 8e216c642e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View file

@ -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"`

View file

@ -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"

View file

@ -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")