Skip to content

Commit b7e63d6

Browse files
authored
[FEAT]: expose primary language in github_repository
2 parents 19c70f1 + 0027494 commit b7e63d6

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

github/data_source_github_repository.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func dataSourceGithubRepository() *schema.Resource {
110110
Type: schema.TypeString,
111111
Computed: true,
112112
},
113+
"primary_language": {
114+
Type: schema.TypeString,
115+
Computed: true,
116+
},
113117
"archived": {
114118
Type: schema.TypeBool,
115119
Computed: true,
@@ -271,6 +275,7 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
271275
d.Set("has_downloads", repo.GetHasDownloads())
272276
d.Set("full_name", repo.GetFullName())
273277
d.Set("default_branch", repo.GetDefaultBranch())
278+
d.Set("primary_language", repo.GetLanguage())
274279
d.Set("html_url", repo.GetHTMLURL())
275280
d.Set("ssh_clone_url", repo.GetSSHURL())
276281
d.Set("svn_url", repo.GetSVNURL())

github/data_source_github_repository_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,111 @@ func TestAccGithubRepositoryDataSource(t *testing.T) {
269269
})
270270

271271
})
272+
273+
t.Run("queries a repository that has no primary_language", func(t *testing.T) {
274+
275+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
276+
277+
config := fmt.Sprintf(`
278+
resource "github_repository" "test" {
279+
name = "tf-acc-%s"
280+
}
281+
282+
data "github_repository" "test" {
283+
name = github_repository.test.name
284+
}
285+
`, randomID)
286+
287+
check := resource.ComposeTestCheckFunc(
288+
resource.TestCheckResourceAttr(
289+
"data.github_repository.test", "primary_language",
290+
"",
291+
),
292+
)
293+
294+
testCase := func(t *testing.T, mode string) {
295+
resource.Test(t, resource.TestCase{
296+
PreCheck: func() { skipUnlessMode(t, mode) },
297+
Providers: testAccProviders,
298+
Steps: []resource.TestStep{
299+
{
300+
Config: config,
301+
Check: check,
302+
},
303+
},
304+
})
305+
}
306+
307+
t.Run("with an anonymous account", func(t *testing.T) {
308+
t.Skip("anonymous account not supported for this operation")
309+
})
310+
311+
t.Run("with an individual account", func(t *testing.T) {
312+
testCase(t, individual)
313+
})
314+
315+
t.Run("with an organization account", func(t *testing.T) {
316+
testCase(t, organization)
317+
})
318+
319+
})
320+
321+
t.Run("queries a repository that has go as primary_language", func(t *testing.T) {
322+
323+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
324+
325+
config := fmt.Sprintf(`
326+
resource "github_repository" "test" {
327+
name = "tf-acc-%s"
328+
auto_init = true
329+
}
330+
resource "github_repository_file" "test" {
331+
repository = github_repository.test.name
332+
file = "test.go"
333+
content = "package main"
334+
}
335+
336+
data "github_repository" "test" {
337+
name = github_repository_file.test.repository
338+
}
339+
`, randomID)
340+
341+
check := resource.ComposeTestCheckFunc(
342+
resource.TestCheckResourceAttr(
343+
"data.github_repository.test", "primary_language",
344+
"Go",
345+
),
346+
)
347+
348+
testCase := func(t *testing.T, mode string) {
349+
resource.Test(t, resource.TestCase{
350+
PreCheck: func() { skipUnlessMode(t, mode) },
351+
Providers: testAccProviders,
352+
Steps: []resource.TestStep{
353+
{
354+
// Not doing any checks since the language doesnt have time to be updated on the first apply
355+
Config: config,
356+
},
357+
{
358+
// Re-running the terraform will refresh the language since the go-file has been created
359+
Config: config,
360+
Check: check,
361+
},
362+
},
363+
})
364+
}
365+
366+
t.Run("with an anonymous account", func(t *testing.T) {
367+
t.Skip("anonymous account not supported for this operation")
368+
})
369+
370+
t.Run("with an individual account", func(t *testing.T) {
371+
testCase(t, individual)
372+
})
373+
374+
t.Run("with an organization account", func(t *testing.T) {
375+
testCase(t, organization)
376+
})
377+
378+
})
272379
}

github/resource_github_repository.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ func resourceGithubRepository() *schema.Resource {
352352
Type: schema.TypeString,
353353
Computed: true,
354354
},
355+
"primary_language": {
356+
Type: schema.TypeString,
357+
Computed: true,
358+
},
355359
"template": {
356360
Type: schema.TypeList,
357361
Optional: true,
@@ -642,6 +646,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
642646
d.Set("etag", resp.Header.Get("ETag"))
643647
d.Set("name", repoName)
644648
d.Set("description", repo.GetDescription())
649+
d.Set("primary_language", repo.GetLanguage())
645650
d.Set("homepage_url", repo.GetHomepage())
646651
d.Set("private", repo.GetPrivate())
647652
d.Set("visibility", repo.GetVisibility())

github/resource_github_repository_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,57 @@ func TestAccGithubRepositories(t *testing.T) {
865865
testCase(t, organization)
866866
})
867867
})
868+
t.Run("create a repository with go as primary_language", func(t *testing.T) {
869+
config := fmt.Sprintf(`
870+
resource "github_repository" "test" {
871+
name = "tf-acc-%s"
872+
auto_init = true
873+
}
874+
resource "github_repository_file" "test" {
875+
repository = github_repository.test.name
876+
file = "test.go"
877+
content = "package main"
878+
}
879+
`, randomID)
880+
881+
check := resource.ComposeTestCheckFunc(
882+
resource.TestCheckResourceAttr(
883+
"github_repository.test", "primary_language",
884+
"Go",
885+
),
886+
)
887+
888+
testCase := func(t *testing.T, mode string) {
889+
resource.Test(t, resource.TestCase{
890+
PreCheck: func() { skipUnlessMode(t, mode) },
891+
Providers: testAccProviders,
892+
Steps: []resource.TestStep{
893+
{
894+
// Not doing any checks since the file needs to be created before the language can be updated
895+
Config: config,
896+
},
897+
{
898+
// Re-running the terraform will refresh the language since the go-file has been created
899+
Config: config,
900+
Check: check,
901+
},
902+
},
903+
})
904+
}
905+
906+
t.Run("with an anonymous account", func(t *testing.T) {
907+
t.Skip("anonymous account not supported for this operation")
908+
})
909+
910+
t.Run("with an individual account", func(t *testing.T) {
911+
testCase(t, individual)
912+
})
913+
914+
t.Run("with an organization account", func(t *testing.T) {
915+
testCase(t, organization)
916+
})
917+
918+
})
868919

869920
}
870921
func TestAccGithubRepositoryPages(t *testing.T) {

website/docs/d/repository.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ The following arguments are supported:
6969

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

72+
* `primary_language` - The primary language used in the repository.
73+
7274
* `archived` - Whether the repository is archived.
7375

7476
* `pages` - The repository's GitHub Pages configuration.

website/docs/r/repository.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ The following additional attributes are exported:
194194

195195
* `repo_id` - GitHub ID for the repository
196196

197+
* `primary_language` - The primary language used in the repository.
198+
197199
* `pages` - The block consisting of the repository's GitHub Pages configuration with the following additional attributes:
198200
* `custom_404` - Whether the rendered GitHub Pages site has a custom 404 page.
199201
* `html_url` - The absolute URL (including scheme) of the rendered GitHub Pages site e.g. `https://username.github.io`.

0 commit comments

Comments
 (0)