diff --git a/github/data_source_github_release.go b/github/data_source_github_release.go new file mode 100644 index 0000000000..91f4606d86 --- /dev/null +++ b/github/data_source_github_release.go @@ -0,0 +1,54 @@ +package github + +import ( + "context" + "log" + "strconv" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGithubRelease() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubReleaseRead, + + Schema: map[string]*schema.Schema{ + "repository": { + Type: schema.TypeString, + Required: true, + }, + "owner": { + Type: schema.TypeString, + Required: true, + }, + "release_id": { + Type: schema.TypeInt, + Required: true, + }, + "url": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceGithubReleaseRead(d *schema.ResourceData, meta interface{}) error { + repository := d.Get("repository").(string) + owner := d.Get("owner").(string) + releaseID := int64(d.Get("release_id").(int)) + log.Printf("[INFO] Refreshing GitHub release %s from repository %s", releaseID, repository) + + client := meta.(*Organization).client + ctx := context.Background() + + release, _, err := client.Repositories.GetRelease(ctx, owner, repository, releaseID) + if err != nil { + return err + } + + d.SetId(strconv.FormatInt(release.GetID(), 10)) + d.Set("url", release.GetURL()) + + return nil +} diff --git a/github/provider.go b/github/provider.go index 1bcd2a8c8d..17049531cf 100644 --- a/github/provider.go +++ b/github/provider.go @@ -70,6 +70,7 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "github_collaborators": dataSourceGithubCollaborators(), "github_ip_ranges": dataSourceGithubIpRanges(), + "github_release": dataSourceGithubRelease(), "github_repositories": dataSourceGithubRepositories(), "github_repository": dataSourceGithubRepository(), "github_team": dataSourceGithubTeam(),