-
Notifications
You must be signed in to change notification settings - Fork 763
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
[Feature Request] github milestone resource and data source #470
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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.(*Owner).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 | ||
} |
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,74 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubRepositoryMilestoneDataSource(t *testing.T) { | ||
|
||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
t.Run("queries a repository milestone", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
|
||
resource "github_repository" "test" { | ||
name = "tf-acc-test-%s" | ||
} | ||
|
||
resource "github_repository_milestone" "test" { | ||
owner = split("/", "${github_repository.test.full_name}")[0] | ||
repository = github_repository.test.name | ||
title = "v1.0.0" | ||
description = "General Availability" | ||
due_date = "2020-11-22" | ||
state = "closed" | ||
} | ||
|
||
data "github_repository_milestone" "test" { | ||
owner = github_repository_milestone.test.owner | ||
repository = github_repository_milestone.test.repository | ||
number = github_repository_milestone.test.number | ||
} | ||
|
||
`, randomID) | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"github_repository_milestone.test", "state", | ||
"closed", | ||
), | ||
) | ||
|
||
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,233 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/go-github/v32/github" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func resourceGithubRepositoryMilestone() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGithubRepositoryMilestoneCreate, | ||
Read: resourceGithubRepositoryMilestoneRead, | ||
Update: resourceGithubRepositoryMilestoneUpdate, | ||
Delete: resourceGithubRepositoryMilestoneDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
parts := strings.Split(d.Id(), "/") | ||
if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" { | ||
return nil, fmt.Errorf("Invalid ID format, must be provided as OWNER/REPOSITORY/NUMBER") | ||
} | ||
d.Set("owner", parts[0]) | ||
d.Set("repository", parts[1]) | ||
number, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
d.Set("number", number) | ||
d.SetId(fmt.Sprintf("%s/%s/%d", parts[0], parts[1], number)) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
}, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"title": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"owner": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"due_date": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "in yyyy-mm-dd format", | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"open", "closed", | ||
}, true), | ||
Default: "open", | ||
}, | ||
"number": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
const ( | ||
layoutISO = "2006-01-02" | ||
) | ||
|
||
func resourceGithubRepositoryMilestoneCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recently came across this: Does the provider become more responsive to a cancel operation if we wire this context all the way through to requests like this? |
||
owner := d.Get("owner").(string) | ||
repoName := d.Get("repository").(string) | ||
|
||
milestone := &github.Milestone{ | ||
Title: github.String(d.Get("title").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("description"); ok && len(v.(string)) > 0 { | ||
milestone.Description = github.String(v.(string)) | ||
} | ||
if v, ok := d.GetOk("due_date"); ok && len(v.(string)) > 0 { | ||
dueDate, err := time.Parse(layoutISO, v.(string)) | ||
if err != nil { | ||
return err | ||
} | ||
date := time.Date(dueDate.Year(), dueDate.Month(), dueDate.Day(), 23, 39, 0, 0, time.UTC) | ||
milestone.DueOn = &date | ||
} | ||
if v, ok := d.GetOk("state"); ok && len(v.(string)) > 0 { | ||
milestone.State = github.String(v.(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] Creating milestone for repository: %s/%s", owner, repoName) | ||
milestone, _, err := conn.Issues.CreateMilestone(ctx, owner, repoName, milestone) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s/%d", owner, repoName, milestone.GetNumber())) | ||
|
||
return resourceGithubRepositoryMilestoneRead(d, meta) | ||
} | ||
|
||
func resourceGithubRepositoryMilestoneRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*Owner).v3client | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
|
||
owner := d.Get("owner").(string) | ||
repoName := d.Get("repository").(string) | ||
number, err := parseMilestoneNumber(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading milestone for repository: %s/%s", owner, repoName) | ||
milestone, _, err := conn.Issues.GetMilestone(ctx, owner, repoName, number) | ||
if err != nil { | ||
if ghErr, ok := err.(*github.ErrorResponse); ok { | ||
if ghErr.Response.StatusCode == http.StatusNotModified { | ||
return nil | ||
} | ||
if ghErr.Response.StatusCode == http.StatusNotFound { | ||
log.Printf("[WARN] Removing milestone for %s/%s from state because it no longer exists in GitHub", | ||
owner, repoName) | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
d.Set("title", milestone.GetTitle()) | ||
d.Set("description", milestone.GetDescription()) | ||
d.Set("number", milestone.GetNumber()) | ||
d.Set("state", milestone.GetState()) | ||
if dueOn := milestone.GetDueOn(); !dueOn.IsZero() { | ||
d.Set("due_date", milestone.GetDueOn().Format(layoutISO)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceGithubRepositoryMilestoneUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*Owner).v3client | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
owner := d.Get("owner").(string) | ||
repoName := d.Get("repository").(string) | ||
number, err := parseMilestoneNumber(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
milestone := &github.Milestone{} | ||
if d.HasChanges("title") { | ||
_, n := d.GetChange("title") | ||
milestone.Title = github.String(n.(string)) | ||
} | ||
|
||
if d.HasChanges("description") { | ||
_, n := d.GetChange("description") | ||
milestone.Description = github.String(n.(string)) | ||
} | ||
|
||
if d.HasChanges("due_date") { | ||
_, n := d.GetChange("due_date") | ||
dueDate, err := time.Parse(layoutISO, n.(string)) | ||
if err != nil { | ||
return err | ||
} | ||
date := time.Date(dueDate.Year(), dueDate.Month(), dueDate.Day(), 7, 0, 0, 0, time.UTC) | ||
milestone.DueOn = &date | ||
} | ||
|
||
if d.HasChanges("state") { | ||
_, n := d.GetChange("state") | ||
milestone.State = github.String(n.(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] Updating milestone for repository: %s/%s", owner, repoName) | ||
_, _, err = conn.Issues.EditMilestone(ctx, owner, repoName, number, milestone) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceGithubRepositoryMilestoneRead(d, meta) | ||
} | ||
|
||
func resourceGithubRepositoryMilestoneDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*Owner).v3client | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
owner := d.Get("owner").(string) | ||
repoName := d.Get("repository").(string) | ||
number, err := parseMilestoneNumber(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Deleting milestone for repository: %s/%s", owner, repoName) | ||
_, err = conn.Issues.DeleteMilestone(ctx, owner, repoName, number) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func parseMilestoneNumber(id string) (int, error) { | ||
parts := strings.Split(id, "/") | ||
if len(parts) != 3 { | ||
return -1, fmt.Errorf("ID not properly formatted: %s", id) | ||
} | ||
number, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return -1, err | ||
} | ||
return number, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your thoughts on new resources using the global node IDs when
SetId()
is being called?Previous experimentation in https://github.com/terraform-providers/terraform-provider-github/pull/460 takes this approach:
https://github.com/terraform-providers/terraform-provider-github/blob/23c6faefa7da8039b9015e4c85b154a7928f685b/github/resource_github_project_card.go#L67
https://github.com/terraform-providers/terraform-provider-github/blob/23c6faefa7da8039b9015e4c85b154a7928f685b/github/resource_github_project_card_test.go#L63