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

feat: Add a data source for labels. #1126

Merged
merged 5 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions github/data_source_github_issue_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package github

import (
"context"
"fmt"

"github.com/google/go-github/v50/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubIssueLabels() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubIssueLabelsRead,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"labels": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"color": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGithubIssueLabelsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repository := d.Get("repository").(string)

ctx := context.Background()
opts := &github.ListOptions{
PerPage: maxPerPage,
}

d.SetId(repository)

allLabels := make([]interface{}, 0)
for {
labels, resp, err := client.Issues.ListLabels(ctx, owner, repository, opts)
if err != nil {
return err
}

result, err := flattenLabels(labels)
if err != nil {
return fmt.Errorf("unable to flatten GitHub Labels (Owner: %q/Repository: %q) : %+v", owner, repository, err)
}

allLabels = append(allLabels, result...)

if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}

d.Set("labels", allLabels)

return nil
}

func flattenLabels(labels []*github.Label) ([]interface{}, error) {
if labels == nil {
return make([]interface{}, 0), nil
}

results := make([]interface{}, 0)

for _, l := range labels {
result := make(map[string]interface{})

result["name"] = l.GetName()
result["color"] = l.GetColor()
result["description"] = l.GetDescription()
result["url"] = l.GetURL()

results = append(results, result)
}

return results, nil
}
57 changes: 57 additions & 0 deletions github/data_source_github_issue_labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package github

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubIssueLabelsDataSource(t *testing.T) {
t.Run("queries the labels for a repository", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%s"
}
data "github_issue_labels" "test" {
repository = github_repository.test.name
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.github_issue_labels.test", "id", regexp.MustCompile(randomID),
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func Provider() terraform.ResourceProvider {
"github_dependabot_secrets": dataSourceGithubDependabotSecrets(),
"github_external_groups": dataSourceGithubExternalGroups(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_issue_labels": dataSourceGithubIssueLabels(),
"github_membership": dataSourceGithubMembership(),
"github_organization": dataSourceGithubOrganization(),
"github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(),
Expand Down
30 changes: 30 additions & 0 deletions website/docs/d/issue_labels.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "github"
page_title: "GitHub: github_issue_labels"
description: |-
Get the labels for a given repository.
---

# github_labels

Use this data source to retrieve the labels for a given repository.

## Example Usage

```hcl
data "github_labels" "test" {
repository = "example_repository"
}
```

## Arguments Reference

* `repository` - (Required) The name of the repository.

## Attributes Reference

* `labels` - The list of this repository's labels. Each element of `labels` has the following attributes:
* `name` - The name of the label.
* `color` - The hexadecimal color code for the label, without the leading #.
* `description` - A short description of the label.
* `url` - The URL of the label.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<li>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
<li>
<a href="/docs/providers/github/d/issue_labels.html">github_issue_labels</a>
</li>
<li>
<a href="/docs/providers/github/d/membership.html">github_membership</a>
</li>
Expand Down