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

New Data Source: github_repository #109

Merged
merged 1 commit into from
Aug 3, 2018
Merged
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
167 changes: 167 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package github

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceGithubRepository() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryRead,

Schema: map[string]*schema.Schema{
"full_name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"full_name"},
},

"description": {
Type: schema.TypeString,
Computed: true,
},
"homepage_url": {
Type: schema.TypeString,
Computed: true,
},
"private": {
Type: schema.TypeBool,
Computed: true,
},
"has_issues": {
Type: schema.TypeBool,
Computed: true,
},
"has_projects": {
Type: schema.TypeBool,
Computed: true,
},
"has_downloads": {
Type: schema.TypeBool,
Computed: true,
},
"has_wiki": {
Type: schema.TypeBool,
Computed: true,
},
"allow_merge_commit": {
Type: schema.TypeBool,
Computed: true,
},
"allow_squash_merge": {
Type: schema.TypeBool,
Computed: true,
},
"allow_rebase_merge": {
Type: schema.TypeBool,
Computed: true,
},
"default_branch": {
Type: schema.TypeString,
Computed: true,
},
"archived": {
Type: schema.TypeBool,
Computed: true,
},
"topics": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"html_url": {
Type: schema.TypeString,
Computed: true,
},
"ssh_clone_url": {
Type: schema.TypeString,
Computed: true,
},
"svn_url": {
Type: schema.TypeString,
Computed: true,
},
"git_clone_url": {
Type: schema.TypeString,
Computed: true,
},
"http_clone_url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client

orgName := meta.(*Organization).name
var repoName string

if fullName, ok := d.GetOk("full_name"); ok {
var err error
orgName, repoName, err = splitRepoFullName(fullName.(string))
if err != nil {
return err
}
}
if name, ok := d.GetOk("name"); ok {
repoName = name.(string)
}

if repoName == "" {
return fmt.Errorf("One of %q or %q has to be provided", "full_name", "name")
}

log.Printf("[DEBUG] Reading GitHub repository %s/%s", orgName, repoName)
repo, _, err := client.Repositories.Get(context.TODO(), orgName, repoName)
if err != nil {
return err
}

d.SetId(repoName)

d.Set("name", repoName)
d.Set("description", repo.Description)
d.Set("homepage_url", repo.Homepage)
d.Set("private", repo.Private)
d.Set("has_issues", repo.HasIssues)
d.Set("has_wiki", repo.HasWiki)
d.Set("allow_merge_commit", repo.AllowMergeCommit)
d.Set("allow_squash_merge", repo.AllowSquashMerge)
d.Set("allow_rebase_merge", repo.AllowRebaseMerge)
d.Set("has_downloads", repo.HasDownloads)
d.Set("full_name", repo.FullName)
d.Set("default_branch", repo.DefaultBranch)
d.Set("html_url", repo.HTMLURL)
d.Set("ssh_clone_url", repo.SSHURL)
d.Set("svn_url", repo.SVNURL)
d.Set("git_clone_url", repo.GitURL)
d.Set("http_clone_url", repo.CloneURL)
d.Set("archived", repo.Archived)

err = d.Set("topics", flattenStringList(repo.Topics))
if err != nil {
return err
}

return nil
}

func splitRepoFullName(fullName string) (string, string, error) {
parts := strings.Split(fullName, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("Unexpected full name format (%q), expected org/repo_name", fullName)
}
return parts[0], parts[1], nil
}
116 changes: 116 additions & 0 deletions github/data_source_github_repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package github

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccGithubRepositoryDataSource_fullName_noMatchReturnsError(t *testing.T) {
fullName := "klsafj_23434_doesnt_exist/not-exists"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryDataSourceConfig_fullName(fullName),
ExpectError: regexp.MustCompile(`Not Found`),
},
},
})
}

func TestAccGithubRepositoryDataSource_name_noMatchReturnsError(t *testing.T) {
name := "not-exists"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryDataSourceConfig_name(name),
ExpectError: regexp.MustCompile(`Not Found`),
},
},
})
}

func TestAccGithubRepositoryDataSource_fullName_existing(t *testing.T) {
fullName := "terraformtesting/test-repo"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryDataSourceConfig_fullName(fullName),
Check: testRepoCheck(),
},
},
})
}

func TestAccGithubRepositoryDataSource_name_existing(t *testing.T) {
name := "test-repo"
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryDataSourceConfig_name(name),
Check: testRepoCheck(),
},
},
})
}

func testRepoCheck() resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.github_repository.test", "id", "test-repo"),
resource.TestCheckResourceAttr("data.github_repository.test", "name", "test-repo"),
resource.TestCheckResourceAttr("data.github_repository.test", "private", "false"),
resource.TestCheckResourceAttr("data.github_repository.test", "description", "Test description, used in GitHub Terraform provider acceptance test."),
resource.TestCheckResourceAttr("data.github_repository.test", "homepage_url", "http://www.example.com"),
resource.TestCheckResourceAttr("data.github_repository.test", "has_issues", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "has_wiki", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "allow_merge_commit", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "allow_squash_merge", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "allow_rebase_merge", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "has_downloads", "true"),
resource.TestCheckResourceAttr("data.github_repository.test", "full_name", "terraformtesting/test-repo"),
resource.TestCheckResourceAttr("data.github_repository.test", "default_branch", "master"),
resource.TestCheckResourceAttr("data.github_repository.test", "html_url", "https://github.com/terraformtesting/test-repo"),
resource.TestCheckResourceAttr("data.github_repository.test", "ssh_clone_url", "git@github.com:terraformtesting/test-repo.git"),
resource.TestCheckResourceAttr("data.github_repository.test", "svn_url", "https://github.com/terraformtesting/test-repo"),
resource.TestCheckResourceAttr("data.github_repository.test", "git_clone_url", "git://github.com/terraformtesting/test-repo.git"),
resource.TestCheckResourceAttr("data.github_repository.test", "http_clone_url", "https://github.com/terraformtesting/test-repo.git"),
resource.TestCheckResourceAttr("data.github_repository.test", "archived", "false"),
resource.TestCheckResourceAttr("data.github_repository.test", "topics.#", "2"),
resource.TestCheckResourceAttr("data.github_repository.test", "topics.0", "second-test-topic"),
resource.TestCheckResourceAttr("data.github_repository.test", "topics.1", "test-topic"),
)
}

func testAccCheckGithubRepositoryDataSourceConfig_fullName(fullName string) string {
return fmt.Sprintf(`
data "github_repository" "test" {
full_name = "%s"
}
`, fullName)
}

func testAccCheckGithubRepositoryDataSourceConfig_name(name string) string {
return fmt.Sprintf(`
data "github_repository" "test" {
name = "%s"
}
`, name)
}
7 changes: 4 additions & 3 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"github_user": dataSourceGithubUser(),
"github_team": dataSourceGithubTeam(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_user": dataSourceGithubUser(),
"github_team": dataSourceGithubTeam(),
"github_ip_ranges": dataSourceGithubIpRanges(),
"github_repository": dataSourceGithubRepository(),
},
}

Expand Down
69 changes: 69 additions & 0 deletions website/docs/d/repository.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
layout: "github"
page_title: "GitHub: github_repository"
sidebar_current: "docs-github-datasource-repository"
description: |-
Get details about GitHub repository
---

# github_repository

Use this data source to retrieve information about a GitHub repository.

## Example Usage

```hcl
data "github_repository" "example" {
full_name = "hashicorp/terraform"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Optional) The name of the repository.

* `full_name` - (Optional) Full name of the repository (in `org/name` format).

## Attributes Reference

* `description` - A description of the repository.

* `homepage_url` - URL of a page describing the project.

* `private` - Whether the repository is private.

* `has_issues` - Whether the repository has GitHub Issues enabled.

* `has_projects` - Whether the repository has the GitHub Projects enabled.

* `has_wiki` - Whether the repository has the GitHub Wiki enabled.

* `allow_merge_commit` - Whether the repository allows merge commits.

* `allow_squash_merge` - Whether the repository allows squash merges.

* `allow_rebase_merge` - Whether the repository allows rebase merges.

* `has_downloads` - Whether the repository has Downloads feature enabled.

* `default_branch` - The name of the default branch of the repository.

* `archived` - Whether the repository is archived.

* `topics` - The list of topics of the repository.

* `html_url` - URL to the repository on the web.

* `ssh_clone_url` - URL that can be provided to `git clone` to clone the
repository via SSH.

* `http_clone_url` - URL that can be provided to `git clone` to clone the
repository via HTTPS.

* `git_clone_url` - URL that can be provided to `git clone` to clone the
repository anonymously via the git protocol.

* `svn_url` - URL that can be provided to `svn checkout` to check out
the repository via GitHub's Subversion protocol emulation.
5 changes: 4 additions & 1 deletion website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
<li<%= sidebar_current("docs-github-datasource") %>>
<a href="#">Data Sources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-github-datasource-ip-ranges") %>>
<li<%= sidebar_current("docs-github-datasource-ip-ranges") %>>
<a href="/docs/providers/github/d/ip_ranges.html">github_ip_ranges</a>
</li>
<li<%= sidebar_current("docs-github-datasource-repository") %>>
<a href="/docs/providers/github/d/repository.html">github_repository</a>
</li>
<li<%= sidebar_current("docs-github-datasource-user") %>>
<a href="/docs/providers/github/d/user.html">github_user</a>
</li>
Expand Down