-
Notifications
You must be signed in to change notification settings - Fork 741
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |