Skip to content
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

Allow for retrieving latest prerelease as data source. #1066

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion github/data_source_github_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func dataSourceGithubRelease() *schema.Resource {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"latest_prerelease",
"latest",
"id",
"tag",
Expand Down Expand Up @@ -108,8 +109,38 @@ func dataSourceGithubReleaseRead(d *schema.ResourceData, meta interface{}) error

var err error
var release *github.RepositoryRelease

switch retrieveBy := strings.ToLower(d.Get("retrieve_by").(string)); retrieveBy {
case "latest_prerelease":
// The GitHub API doesn't specify a way to just load the most recent prerelease,
// so we'll load all releases and determine which one is the most recent
// prerelease after.
log.Printf("[INFO] Refreshing GitHub latest prerelease from repository %s", repository)
var releases []*github.RepositoryRelease
nextPage := 1
// TODO: 10 is sort of arbitrary here -- what's the best way to allow configurability
// for this to prevent the provider from becoming glacially slow on large repos?
Comment on lines +120 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent question. Do you know what the max number of releases we can retrieve per page with the API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100 is the limit.

for nextPage < 10 {
opt := &github.ListOptions{Page: nextPage}
var response *github.Response
releases, response, err = client.Repositories.ListReleases(ctx, owner, repository, opt)
for _, rel := range releases {
if *rel.Prerelease != true || *rel.Draft == true {
continue
}
if release == nil {
release = rel
} else {
if rel.PublishedAt.After(release.PublishedAt.Time) {
release = rel
}
}
}
if response.NextPage > nextPage {
nextPage = response.NextPage
} else {
break
}
}
Comment on lines +126 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little uncomfortable with this logic as-is. What do you think about refactoring it to a function like GetLatestRelease that we could write a unit test for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like that. Will take a stab at it this weekend.

case "latest":
log.Printf("[INFO] Refreshing GitHub latest release from repository %s", repository)
release, _, err = client.Repositories.GetLatestRelease(ctx, owner, repository)
Expand Down
44 changes: 44 additions & 0 deletions github/data_source_github_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestAccGithubReleaseDataSource(t *testing.T) {

testReleaseRepository := os.Getenv("GITHUB_TEMPLATE_REPOSITORY")
testReleaseID := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID")
testPrereleaseID := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_PRERELEASE_ID")
testReleaseOwner := testOrganizationFunc()

t.Run("queries latest release", func(t *testing.T) {
Expand Down Expand Up @@ -58,6 +59,49 @@ func TestAccGithubReleaseDataSource(t *testing.T) {

})

t.Run("queries latest prerelease", func(t *testing.T) {

config := fmt.Sprintf(`
data "github_release" "test" {
repository = "%s"
owner = "%s"
retrieve_by = "latest_prerelease"
}
`, testReleaseRepository, testReleaseOwner)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.github_release.test", "id", testPrereleaseID,
),
)

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) {
testCase(t, anonymous)
})

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)
})

})

t.Run("queries release by ID or tag", func(t *testing.T) {

config := fmt.Sprintf(`
Expand Down
3 changes: 3 additions & 0 deletions github/provider_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID"); v == "" {
t.Fatal("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID must be set for acceptance tests")
}
if v := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_PRERELEASE_ID"); v == "" {
t.Fatal("GITHUB_TEMPLATE_REPOSITORY_PRERELEASE_ID must be set for acceptance tests")
}
}

func skipUnlessMode(t *testing.T, providerMode string) {
Expand Down