Skip to content

Commit

Permalink
Add a data source for labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
galargh committed Apr 27, 2022
1 parent fdfd3c6 commit 1a97d57
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
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/v43/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"
"testing"
"regexp"

"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 @@ -133,6 +133,7 @@ func Provider() terraform.ResourceProvider {
"github_branch": dataSourceGithubBranch(),
"github_collaborators": dataSourceGithubCollaborators(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_issue_labels": dataSourceGithubIssueLabels(),
"github_membership": dataSourceGithubMembership(),
"github_organization": dataSourceGithubOrganization(),
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
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.

0 comments on commit 1a97d57

Please sign in to comment.