Skip to content

Commit 59bf7d7

Browse files
committed
feat: add github_client_config data source
Adds a new `github_client_config` data source that exposes the provider's effective configuration: the resolved `owner`, whether that owner is an organization, the authenticated `username`, and the configured `base_url`. This is useful when `owner` is supplied via the `GITHUB_OWNER` environment variable (and therefore not visible in HCL) or when callers need the login of the user the provider is authenticating as. Resolves #3418
1 parent 7a217c2 commit 59bf7d7

6 files changed

Lines changed: 217 additions & 0 deletions

File tree

docs/data-sources/client_config.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
page_title: "github_client_config (Data Source) - GitHub"
3+
description: |-
4+
Get information about the configured GitHub provider client, including the resolved owner and authenticated user.
5+
---
6+
7+
# github_client_config (Data Source)
8+
9+
Use this data source to access information about the GitHub provider's client
10+
configuration. This is useful when the `owner` argument is set via the
11+
`GITHUB_OWNER` environment variable (and therefore not available in
12+
configuration), or when you need to know which user the provider is
13+
authenticating as.
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "github_client_config" "current" {}
19+
20+
output "owner" {
21+
value = data.github_client_config.current.owner
22+
}
23+
24+
output "username" {
25+
value = data.github_client_config.current.username
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
This data source has no arguments.
32+
33+
## Attributes Reference
34+
35+
- `id` - The resolved owner name, or the authenticated user's login when no
36+
owner is configured. Falls back to the API base URL in anonymous mode.
37+
- `owner` - The owner the provider is configured to manage. This reflects the
38+
value of the `owner` provider argument or the `GITHUB_OWNER` environment
39+
variable. When neither is set and the provider is authenticated, this is the
40+
login of the authenticated user.
41+
- `is_organization` - Whether the resolved `owner` is a GitHub organization
42+
(`true`) or a user (`false`).
43+
- `username` - The login of the user the provider is authenticated as. This
44+
may differ from `owner` when `owner` is an organization or another user.
45+
Empty when the provider is configured in anonymous mode.
46+
- `base_url` - The GitHub API base URL the provider is configured to use.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data "github_client_config" "current" {}
2+
3+
output "owner" {
4+
value = data.github_client_config.current.owner
5+
}
6+
7+
output "username" {
8+
value = data.github_client_config.current.username
9+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubClientConfig() *schema.Resource {
11+
return &schema.Resource{
12+
ReadContext: dataSourceGithubClientConfigRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"owner": {
16+
Type: schema.TypeString,
17+
Computed: true,
18+
},
19+
"is_organization": {
20+
Type: schema.TypeBool,
21+
Computed: true,
22+
},
23+
"username": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"base_url": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
},
32+
}
33+
}
34+
35+
func dataSourceGithubClientConfigRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
36+
owner := meta.(*Owner)
37+
client := owner.v3client
38+
39+
if err := d.Set("owner", owner.name); err != nil {
40+
return diag.FromErr(err)
41+
}
42+
if err := d.Set("is_organization", owner.IsOrganization); err != nil {
43+
return diag.FromErr(err)
44+
}
45+
if err := d.Set("base_url", client.BaseURL.String()); err != nil {
46+
return diag.FromErr(err)
47+
}
48+
49+
var username string
50+
if user, _, err := client.Users.Get(ctx, ""); err == nil {
51+
username = user.GetLogin()
52+
}
53+
if err := d.Set("username", username); err != nil {
54+
return diag.FromErr(err)
55+
}
56+
57+
id := owner.name
58+
if id == "" {
59+
id = username
60+
}
61+
if id == "" {
62+
id = client.BaseURL.String()
63+
}
64+
d.SetId(id)
65+
66+
return nil
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
)
8+
9+
func TestAccGithubClientConfigDataSource(t *testing.T) {
10+
t.Run("reads provider configuration without error", func(t *testing.T) {
11+
config := `data "github_client_config" "test" {}`
12+
13+
check := resource.ComposeTestCheckFunc(
14+
resource.TestCheckResourceAttr("data.github_client_config.test", "owner", testAccConf.owner),
15+
resource.TestCheckResourceAttr("data.github_client_config.test", "username", testAccConf.username),
16+
resource.TestCheckResourceAttr("data.github_client_config.test", "base_url", testAccConf.baseURL.String()),
17+
resource.TestCheckResourceAttrSet("data.github_client_config.test", "is_organization"),
18+
resource.TestCheckResourceAttrSet("data.github_client_config.test", "id"),
19+
)
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { skipUnauthenticated(t) },
23+
ProviderFactories: providerFactories,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: config,
27+
Check: check,
28+
},
29+
},
30+
})
31+
})
32+
33+
t.Run("reads provider configuration in anonymous mode", func(t *testing.T) {
34+
if testAccConf.authMode != anonymous {
35+
t.Skip("Skipping as test mode is not anonymous")
36+
}
37+
38+
config := `data "github_client_config" "test" {}`
39+
40+
check := resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttr("data.github_client_config.test", "owner", ""),
42+
resource.TestCheckResourceAttr("data.github_client_config.test", "username", ""),
43+
resource.TestCheckResourceAttr("data.github_client_config.test", "is_organization", "false"),
44+
resource.TestCheckResourceAttrSet("data.github_client_config.test", "base_url"),
45+
resource.TestCheckResourceAttrSet("data.github_client_config.test", "id"),
46+
)
47+
48+
resource.Test(t, resource.TestCase{
49+
ProviderFactories: providerFactories,
50+
Steps: []resource.TestStep{
51+
{
52+
Config: config,
53+
Check: check,
54+
},
55+
},
56+
})
57+
})
58+
}

github/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func Provider() *schema.Provider {
241241
"github_app_token": dataSourceGithubAppToken(),
242242
"github_branch": dataSourceGithubBranch(),
243243
"github_branch_protection_rules": dataSourceGithubBranchProtectionRules(),
244+
"github_client_config": dataSourceGithubClientConfig(),
244245
"github_collaborators": dataSourceGithubCollaborators(),
245246
"github_codespaces_organization_public_key": dataSourceGithubCodespacesOrganizationPublicKey(),
246247
"github_codespaces_organization_secrets": dataSourceGithubCodespacesOrganizationSecrets(),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
page_title: "{{.Name}} ({{.Type}}) - {{.RenderedProviderName}}"
3+
description: |-
4+
Get information about the configured GitHub provider client, including the resolved owner and authenticated user.
5+
---
6+
7+
# {{.Name}} ({{.Type}})
8+
9+
Use this data source to access information about the GitHub provider's client
10+
configuration. This is useful when the `owner` argument is set via the
11+
`GITHUB_OWNER` environment variable (and therefore not available in
12+
configuration), or when you need to know which user the provider is
13+
authenticating as.
14+
15+
## Example Usage
16+
17+
{{ tffile "examples/data-sources/client_config/example_1.tf" }}
18+
19+
## Argument Reference
20+
21+
This data source has no arguments.
22+
23+
## Attributes Reference
24+
25+
- `id` - The resolved owner name, or the authenticated user's login when no
26+
owner is configured. Falls back to the API base URL in anonymous mode.
27+
- `owner` - The owner the provider is configured to manage. This reflects the
28+
value of the `owner` provider argument or the `GITHUB_OWNER` environment
29+
variable. When neither is set and the provider is authenticated, this is the
30+
login of the authenticated user.
31+
- `is_organization` - Whether the resolved `owner` is a GitHub organization
32+
(`true`) or a user (`false`).
33+
- `username` - The login of the user the provider is authenticated as. This
34+
may differ from `owner` when `owner` is an organization or another user.
35+
Empty when the provider is configured in anonymous mode.
36+
- `base_url` - The GitHub API base URL the provider is configured to use.

0 commit comments

Comments
 (0)