Skip to content

Commit

Permalink
[FEAT]: Get the license for a repository (integrations#2026)
Browse files Browse the repository at this point in the history
* [FEAT]: Get the license for a repository

* Remove unneeded option

* Update documentation for repository license

* Add test for license

* Fix test for github repository license

* Bump go-github from v52 to v55 in the resource_github_issue_labels

* Rename repositorylicense to repository_license

---------

Co-authored-by: Nick Floyd <[email protected]>
Co-authored-by: Keegan Campbell <[email protected]>
  • Loading branch information
3 people authored Jan 5, 2024
1 parent a6fd873 commit a304997
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 0 deletions.
123 changes: 123 additions & 0 deletions github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,117 @@ func dataSourceGithubRepository() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"repository_license": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
"license": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
"spdx_id": {
Type: schema.TypeString,
Computed: true,
},
"html_url": {
Type: schema.TypeString,
Computed: true,
},
"featured": {
Type: schema.TypeBool,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"implementation": {
Type: schema.TypeString,
Computed: true,
},
"permissions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"conditions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"limitations": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"body": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"sha": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
"html_url": {
Type: schema.TypeString,
Computed: true,
},
"git_url": {
Type: schema.TypeString,
Computed: true,
},
"download_url": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"content": {
Type: schema.TypeString,
Computed: true,
},
"encoding": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"pages": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -298,6 +409,18 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
d.Set("pages", flattenPages(nil))
}

if repo.License != nil {
repository_license, _, err := client.Repositories.License(context.TODO(), owner, repoName)
if err != nil {
return err
}
if err := d.Set("repository_license", flattenRepositoryLicense(repository_license)); err != nil {
return fmt.Errorf("error setting repository_license: %w", err)
}
} else {
d.Set("repository_license", flattenRepositoryLicense(nil))
}

if repo.TemplateRepository != nil {
d.Set("template", []interface{}{
map[string]interface{}{
Expand Down
63 changes: 63 additions & 0 deletions github/data_source_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,67 @@ func TestAccGithubRepositoryDataSource(t *testing.T) {
})

})

t.Run("queries a repository that has a license", func(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%s"
auto_init = true
}
resource "github_repository_file" "test" {
repository = github_repository.test.name
file = "LICENSE"
content = <<EOT
Copyright (c) 2011-2023 GitHub Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
EOT
}
data "github_repository" "test" {
name = github_repository_file.test.repository
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.github_repository.test", "repository_license.0.license.0.spdx_id",
"MIT",
),
)

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

})
}
36 changes: 36 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,42 @@ func flattenPages(pages *github.Pages) []interface{} {
return []interface{}{pagesMap}
}

func flattenRepositoryLicense(repositorylicense *github.RepositoryLicense) []interface{} {
if repositorylicense == nil {
return []interface{}{}
}

licenseMap := make(map[string]interface{})
licenseMap["key"] = repositorylicense.GetLicense().GetKey()
licenseMap["name"] = repositorylicense.GetLicense().GetName()
licenseMap["url"] = repositorylicense.GetLicense().GetURL()
licenseMap["spdx_id"] = repositorylicense.GetLicense().GetSPDXID()
licenseMap["html_url"] = repositorylicense.GetLicense().GetHTMLURL()
licenseMap["featured"] = repositorylicense.GetLicense().GetFeatured()
licenseMap["description"] = repositorylicense.GetLicense().GetDescription()
licenseMap["implementation"] = repositorylicense.GetLicense().GetImplementation()
licenseMap["permissions"] = repositorylicense.GetLicense().GetPermissions()
licenseMap["conditions"] = repositorylicense.GetLicense().GetConditions()
licenseMap["limitations"] = repositorylicense.GetLicense().GetLimitations()
licenseMap["body"] = repositorylicense.GetLicense().GetBody()

repositorylicenseMap := make(map[string]interface{})
repositorylicenseMap["license"] = []interface{}{licenseMap}
repositorylicenseMap["name"] = repositorylicense.GetName()
repositorylicenseMap["path"] = repositorylicense.GetPath()
repositorylicenseMap["sha"] = repositorylicense.GetSHA()
repositorylicenseMap["size"] = repositorylicense.GetSize()
repositorylicenseMap["url"] = repositorylicense.GetURL()
repositorylicenseMap["html_url"] = repositorylicense.GetHTMLURL()
repositorylicenseMap["git_url"] = repositorylicense.GetGitURL()
repositorylicenseMap["download_url"] = repositorylicense.GetDownloadURL()
repositorylicenseMap["type"] = repositorylicense.GetType()
repositorylicenseMap["content"] = repositorylicense.GetContent()
repositorylicenseMap["encoding"] = repositorylicense.GetEncoding()

return []interface{}{repositorylicenseMap}
}

func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis) []interface{} {
if securityAndAnalysis == nil {
return []interface{}{}
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,37 @@ The following arguments are supported:
* `node_id` - GraphQL global node id for use with v4 API

* `repo_id` - GitHub ID for the repository

* `repository_license` - An Array of GitHub repository licenses. Each `repository_license` block consists of the fields documented below.

___

The `repository_license` block consists of:

* `content` - Content of the license file, encoded by encoding scheme mentioned below.
* `download_url` - The URL to download the raw content of the license file.
* `encoding` - The encoding used for the content (e.g., "base64").
* `git_url` - The URL to access information about the license file as a Git blob.
* `html_url` - The URL to view the license file on GitHub.
* `license` - `license` block consists of the fields documented below.
* `name` - The name of the license file (e.g., "LICENSE").
* `path` - The path to the license file within the repository.
* `sha` - The SHA hash of the license file.
* `size` - The size of the license file in bytes.
* `type` - The type of the content, (e.g., "file").
* `url` - The URL to access information about the license file on GitHub.

The `license` block consists of:

* `body` - The text of the license.
* `conditions` - Conditions associated with the license.
* `description` - A description of the license.
* `featured` - Indicates if the license is featured.
* `html_url` - The URL to view the license details on GitHub.
* `implementation` - Details about the implementation of the license.
* `key` - A key representing the license type (e.g., "apache-2.0").
* `limitations` - Limitations associated with the license.
* `name` - The name of the license (e.g., "Apache License 2.0").
* `permissions` - Permissions associated with the license.
* `spdx_id` - The SPDX identifier for the license (e.g., "Apache-2.0").
* `url` - The URL to access information about the license on GitHub.

0 comments on commit a304997

Please sign in to comment.