Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for repository name and owner on the github context #45

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,54 @@ type GitHubContext struct {
ServerURL string `env:"GITHUB_SERVER_URL,default=https://github.com"`
GraphqlURL string `env:"GITHUB_GRAPHQL_URL,default=https://api.github.com/graphql"`

// Repository is the owner and repository name. For example, octocat/Hello-World
// It is not recommended to use this field to acquire the repository name
// but to use the Repo method instead.
Repository string `env:"GITHUB_REPOSITORY"`

// RepositoryOwner is the repository owner. For example, octocat
// It is not recommended to use this field to acquire the repository owner
// but to use the Repo method instead.
RepositoryOwner string `env:"GITHUB_REPOSITORY_OWNER"`

// Event is populated by parsing the file at EventPath, if it exists.
Event map[string]any
}

// Repo returns the username of the repository owner and repository name.
func (c *GitHubContext) Repo() (string, string) {
if c == nil {
return "", ""
}

// Based on https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts
if c.Repository != "" {
parts := strings.SplitN(c.Repository, "/", 2)
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}

// If c.Repository is empty attempt to get the repo from the Event data.
var repoName string
// NOTE: differs from context.ts. Fall back to GITHUB_REPOSITORY_OWNER
ownerName := c.RepositoryOwner
if c.Event != nil {
if repo, ok := c.Event["repository"].(map[string]any); ok {
if name, ok := repo["name"].(string); ok {
repoName = name
}
if owner, ok := repo["owner"].(map[string]any); ok {
if name, ok := owner["name"].(string); ok {
ownerName = name
}
}
}
}
return ownerName, repoName
}

// Context returns the context of current action with the payload object
// that triggered the workflow
func (c *Action) Context() (*GitHubContext, error) {
Expand Down
127 changes: 103 additions & 24 deletions actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,32 +631,36 @@ func TestAction_Context(t *testing.T) {
{
name: "no_payload",
env: map[string]string{
"GITHUB_EVENT_NAME": "event_name",
"GITHUB_SHA": "abcd1234",
"GITHUB_REF": "main",
"GITHUB_WORKFLOW": "test",
"GITHUB_ACTION": "foo/bar@v0",
"GITHUB_ACTOR": "sethvargo",
"GITHUB_JOB": "12",
"GITHUB_RUN_NUMBER": "34",
"GITHUB_RUN_ID": "56",
"GITHUB_API_URL": "https://foo.com",
"GITHUB_SERVER_URL": "https://bar.com",
"GITHUB_GRAPHQL_URL": "https://baz.com",
"GITHUB_EVENT_NAME": "event_name",
"GITHUB_SHA": "abcd1234",
"GITHUB_REF": "main",
"GITHUB_WORKFLOW": "test",
"GITHUB_ACTION": "foo/bar@v0",
"GITHUB_REPOSITORY": "sethvargo/baz",
"GITHUB_REPOSITORY_OWNER": "sethvargo",
"GITHUB_ACTOR": "sethvargo",
"GITHUB_JOB": "12",
"GITHUB_RUN_NUMBER": "34",
"GITHUB_RUN_ID": "56",
"GITHUB_API_URL": "https://foo.com",
"GITHUB_SERVER_URL": "https://bar.com",
"GITHUB_GRAPHQL_URL": "https://baz.com",
},
exp: &GitHubContext{
EventName: "event_name",
SHA: "abcd1234",
Ref: "main",
Workflow: "test",
Action: "foo/bar@v0",
Actor: "sethvargo",
Job: "12",
RunNumber: 34,
RunID: 56,
APIURL: "https://foo.com",
ServerURL: "https://bar.com",
GraphqlURL: "https://baz.com",
EventName: "event_name",
SHA: "abcd1234",
Ref: "main",
Workflow: "test",
Action: "foo/bar@v0",
Actor: "sethvargo",
Job: "12",
RunNumber: 34,
RunID: 56,
APIURL: "https://foo.com",
ServerURL: "https://bar.com",
GraphqlURL: "https://baz.com",
Repository: "sethvargo/baz",
RepositoryOwner: "sethvargo",
},
},
{
Expand Down Expand Up @@ -700,6 +704,81 @@ func TestAction_Context(t *testing.T) {
}
}

func TestGitHubContext_Repo(t *testing.T) {
t.Parallel()

cases := []struct {
name string
context *GitHubContext
expOwner string
expRepo string
}{
{
name: "empty",
context: &GitHubContext{},
expOwner: "",
expRepo: "",
},
{
name: "GITHUB_REPOSITORY env",
context: &GitHubContext{
Repository: "sethvargo/foo",
},
expOwner: "sethvargo",
expRepo: "foo",
},
{
name: "event",
context: &GitHubContext{
Event: map[string]any{
"repository": map[string]any{
"name": "foo",
"owner": map[string]any{
"name": "sethvargo",
},
},
},
},
expOwner: "sethvargo",
expRepo: "foo",
},
{
name: "event invalid type",
context: &GitHubContext{
Event: map[string]any{
"repository": "sethvargo/foo",
},
},
expOwner: "",
expRepo: "",
},
{
name: "owner fallback",
context: &GitHubContext{
RepositoryOwner: "sethvargo",
},
expOwner: "sethvargo",
expRepo: "",
},
}

for _, tc := range cases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

owner, repo := tc.context.Repo()
if want, got := tc.expOwner, owner; want != got {
t.Errorf("unexpected owner, want: %q, got: %q", want, got)
}
if want, got := tc.expRepo, repo; want != got {
t.Errorf("unexpected repository, want: %q, got: %q", want, got)
}
})
}
}

// newFakeGetenvFunc returns a new GetenvFunc that is expected to be called with
// the provided key. It returns the provided value if the call matches the
// provided key. It reports an error on test t otherwise.
Expand Down