-
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
Allow for retrieving latest prerelease as data source. #1066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ func dataSourceGithubRelease() *schema.Resource { | |
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"latest_prerelease", | ||
"latest", | ||
"id", | ||
"tag", | ||
|
@@ -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? | ||
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
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. I'm a little uncomfortable with this logic as-is. What do you think about refactoring it to a function like 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. 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) | ||
|
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.
This is an excellent question. Do you know what the max number of releases we can retrieve per page with the API?
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.
100 is the limit.