From 5d797644935a0b41fbd65ad8f5846f6dfe877ce8 Mon Sep 17 00:00:00 2001 From: galargh Date: Wed, 27 Apr 2022 23:19:35 +0200 Subject: [PATCH 1/3] Add a data source for labels. --- github/data_source_github_issue_labels.go | 104 ++++++++++++++++++ .../data_source_github_issue_labels_test.go | 57 ++++++++++ github/provider.go | 1 + website/docs/d/issue_labels.html.markdown | 30 +++++ website/github.erb | 3 + 5 files changed, 195 insertions(+) create mode 100644 github/data_source_github_issue_labels.go create mode 100644 github/data_source_github_issue_labels_test.go create mode 100644 website/docs/d/issue_labels.html.markdown diff --git a/github/data_source_github_issue_labels.go b/github/data_source_github_issue_labels.go new file mode 100644 index 0000000000..96d5a0ea88 --- /dev/null +++ b/github/data_source_github_issue_labels.go @@ -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 +} diff --git a/github/data_source_github_issue_labels_test.go b/github/data_source_github_issue_labels_test.go new file mode 100644 index 0000000000..2d9c621a44 --- /dev/null +++ b/github/data_source_github_issue_labels_test.go @@ -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) + }) + }) +} diff --git a/github/provider.go b/github/provider.go index bb13345e3f..2a9ac1e8ff 100644 --- a/github/provider.go +++ b/github/provider.go @@ -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(), diff --git a/website/docs/d/issue_labels.html.markdown b/website/docs/d/issue_labels.html.markdown new file mode 100644 index 0000000000..de43a01ba9 --- /dev/null +++ b/website/docs/d/issue_labels.html.markdown @@ -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. diff --git a/website/github.erb b/website/github.erb index 4e971bff46..4b51c109d5 100644 --- a/website/github.erb +++ b/website/github.erb @@ -22,6 +22,9 @@
  • github_collaborators
  • +
  • + github_issue_labels +
  • github_ip_ranges
  • From 55874a493768b3a389669a9d18f915062ae91eed Mon Sep 17 00:00:00 2001 From: galargh Date: Sat, 1 Apr 2023 10:54:49 +0200 Subject: [PATCH 2/3] chore: go fmt --- github/data_source_github_issue_labels_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_issue_labels_test.go b/github/data_source_github_issue_labels_test.go index 2d9c621a44..94b9caa13b 100644 --- a/github/data_source_github_issue_labels_test.go +++ b/github/data_source_github_issue_labels_test.go @@ -2,8 +2,8 @@ package github import ( "fmt" - "testing" "regexp" + "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" From 1e9ff707076c59a31e93b2edec30117ecf8f3b1d Mon Sep 17 00:00:00 2001 From: galargh Date: Sat, 1 Apr 2023 10:59:28 +0200 Subject: [PATCH 3/3] fix: github client import in the label data source --- github/data_source_github_issue_labels.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_issue_labels.go b/github/data_source_github_issue_labels.go index 96d5a0ea88..7c1f07362a 100644 --- a/github/data_source_github_issue_labels.go +++ b/github/data_source_github_issue_labels.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/google/go-github/v43/github" + "github.com/google/go-github/v50/github" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" )