Skip to content

Commit

Permalink
add milestone resource and data source
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 authored and Jeremy Udit committed Nov 22, 2020
1 parent 11e1973 commit 2951fd6
Show file tree
Hide file tree
Showing 8 changed files with 810 additions and 0 deletions.
66 changes: 66 additions & 0 deletions github/data_source_github_repository_milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package github

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strconv"
)

func dataSourceGithubRepositoryMilestone() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryMilestoneRead,

Schema: map[string]*schema.Schema{
"owner": {
Type: schema.TypeString,
Required: true,
},
"repository": {
Type: schema.TypeString,
Required: true,
},
"number": {
Type: schema.TypeInt,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"due_date": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"title": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubRepositoryMilestoneRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*Organization).v3client
ctx := context.Background()

owner := d.Get("owner").(string)
repoName := d.Get("repository").(string)

number := d.Get("number").(int)
milestone, _, err := conn.Issues.GetMilestone(ctx, owner, repoName, number)
if err != nil {
return err
}

d.SetId(strconv.FormatInt(milestone.GetID(), 10))
d.Set("description", milestone.GetDescription())
d.Set("due_date", milestone.GetDueOn().Format(layoutISO))
d.Set("state", milestone.GetState())
d.Set("title", milestone.GetTitle())

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

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"regexp"
"testing"
"time"

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

func TestAccGithubRepositoryMilestoneDataSource_noMatchReturnsError(t *testing.T) {
repo := "nonExistentRepo"
owner := "no-user"
number := "1"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryMilestoneDataSourceNonExistentConfig(repo, owner, number),
ExpectError: regexp.MustCompile(`Not Found`),
},
},
})
}

func TestAccGithubRepositoryMilestoneDataSource_existing(t *testing.T) {
repo := acctest.RandomWithPrefix("tf-acc-test")
title := acctest.RandomWithPrefix("ms")
description := acctest.RandomWithPrefix("tf-acc-test-desc")
dueDate := time.Now().UTC().Format(layoutISO)

rn := "github_repository_milestone.test"
dataSource := "data.github_repository_milestone.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryMilestoneDataSourceConfig(repo, title, description, dueDate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSource, "title", rn, "title"),
resource.TestCheckResourceAttrPair(dataSource, "description", rn, "description"),
resource.TestCheckResourceAttrPair(dataSource, "due_date", rn, "due_date"),
resource.TestCheckResourceAttrPair(dataSource, "state", rn, "state"),
resource.TestCheckResourceAttrPair(dataSource, "number", rn, "number"),
resource.TestCheckResourceAttrPair(dataSource, "owner", rn, "owner"),
resource.TestCheckResourceAttrPair(dataSource, "repository", rn, "repository"),
),
},
},
})
}

func testAccCheckGithubRepositoryMilestoneDataSourceNonExistentConfig(owner, repo, number string) string {
return fmt.Sprintf(`
data "github_repository_milestone" "test" {
owner = "%s"
repository = "%s"
number = "%s"
}
`, owner, repo, number)
}

func testAccCheckGithubRepositoryMilestoneDataSourceConfig(repo, title, description, dueDate string) string {
return fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}
resource "github_repository_milestone" "test" {
owner = split("/", "${github_repository.test.full_name}")[0]
repository = github_repository.test.name
title = "%s"
description = "%s"
due_date = "%s"
}
data "github_repository_milestone" "test" {
owner = github_repository_milestone.test.owner
repository = github_repository_milestone.test.repository
number = github_repository_milestone.test.number
}
`, repo, title, description, dueDate)
}
2 changes: 2 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Provider() terraform.ResourceProvider {
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
"github_repository_file": resourceGithubRepositoryFile(),
"github_repository_milestone": resourceGithubRepositoryMilestone(),
"github_repository_project": resourceGithubRepositoryProject(),
"github_repository_webhook": resourceGithubRepositoryWebhook(),
"github_repository": resourceGithubRepository(),
Expand All @@ -76,6 +77,7 @@ func Provider() terraform.ResourceProvider {
"github_release": dataSourceGithubRelease(),
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
"github_repository_milestone": dataSourceGithubRepositoryMilestone(),
"github_team": dataSourceGithubTeam(),
"github_user": dataSourceGithubUser(),
},
Expand Down
Loading

0 comments on commit 2951fd6

Please sign in to comment.