From 921e8df6499e110ae5a6df97d156377b02099467 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:02:20 +0100 Subject: [PATCH 1/7] Align naming of test resources --- examples/public-repository-complete-example/main.tf | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index 24b020e5..da92c295 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -13,7 +13,7 @@ resource "random_pet" "suffix" { module "repository" { source = "../.." - name = "public-repository-complete-example-1-${random_pet.suffix.id}" + name = "test-public-repository-complete-example-A-${random_pet.suffix.id}" description = "A public repository created with terraform to test the terraform-github-repository module." homepage_url = "https://github.com/mineiros-io" private = false @@ -112,14 +112,14 @@ locals { module "repository-with-defaults" { source = "../.." - name = "public-repository-complete-example-2-${random_pet.suffix.id}" + name = "test-public-repository-complete-example-B-${random_pet.suffix.id}" description = "A public repository created with terraform to test the terraform-github-repository module." defaults = local.defaults } resource "github_team" "team" { - name = "private-repository-with-teams-test-team-${random_pet.suffix.id}" - description = "This team is created with terraform to test the terraformn-github-repository module." + name = "test-public-repository-complete-example-${random_pet.suffix.id}" + description = "A secret team created with terraform to test the terraformn-github-repository module." privacy = "secret" } From 9d4f9fa85dd18c852da6f1160c920adeef01360e Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:03:59 +0100 Subject: [PATCH 2/7] Create public keys on-the-fly to prevent conflicts --- examples/public-repository-complete-example/main.tf | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index da92c295..f46a82da 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -6,6 +6,13 @@ provider "github" { version = "~> 2.2" } +resource "tls_private_key" "deploy" { + count = 2 + + algorithm = "RSA" + rsa_bits = 4096 +} + resource "random_pet" "suffix" { length = 1 } @@ -76,12 +83,12 @@ module "repository" { deploy_keys = [ { title = "CI User Deploy Key" - key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCjv+wDq3BV0CTTIJYCnUv530ximNPwfrILIXRhhTZNPrDTpxYCO2ACexeX4wN1vfQqsSXwKutdBJoL42kXYHGRpEQ/1iiojJYL3wO7ktdeQuWDEhTyMzOp0FPrEWydzhCXKqcsA+RVPBAwXlsmaw7SkvjVgR9JCNC3OuIoPndMDSeT64/LHMX8UDlS+PikVRZ5rXhOI50Mqijd+xNf55hxURG3Zp1/iIjWCBHZQLOaazd09MD+HB9Hm/df8l6Mo4ahZuJFYfESIt8lR0FfKkfz4FDPil4JcK1BUs2013c4PYyt8JQR08dUIOGBQNe5mhLPJHD0+ylbQJown5Ahs+Nq5aX+1ZrpXlSO5KSsOKlq/Sj2uYyODWhhKuXlCaEU1eQcREhjP0KEx5AVcFCH3rxSC1gTp8w5fEuOeM3As0BPeo/LqLrtQcXnOZpYn/LGlcZE420YP8nfbMZ6I71KSn0+TjnUWQV93r+UMYmfjHwlq14pgPfWmi+nT/4g1n8WAR7VAKv/7fdY/e2gv5w22El1bXSvP+4fH86s+g/gGfxnjVhRB/CgxfrftyzBZOEXGQEGbiY2rJcLbdi2WY6i9rBQkEWCX8ExOK4HYLZ2qbw6jStl+yHDLgUbhwoe7ggzs5mReaMVcU0MQ6PRDJJ+AyI3bledoYFF+h8M8p2afcb3aw==" + key = tls_private_key.deploy[0].public_key_openssh read_only = true }, { title = "Test Key" - key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC7IF88OLL8j1bTvRCernM/J45aoQvOaHNTDkBnMpxWUsf+F3ZBy/ls8yOgeF+OAFCT3tPrh2drGa3cK460/zL2SxAn5mT1bcDJOk7axMcamZ0EpKWAaSbRKxCLkUsTbHiW0nHG+jT7PQCtWwx6CQNh5ztx+Ge5QwDgS7HpyMxIS5Ju4dt5Eh2+MaPMjKWd8P5FKqNCvO4VYy+/3RijeQO+6lM9rih3EECkLug44ZKtbSmQhjHS5/iY7TkfpFpOgMVprgTq5tEK13wrhgjDO5gZ+MopTv5jmuatBLUlpjVI64EHm+zPyxn5E/vehopX8uF1yM1dTt4t6t+p0SRmaCSIX6QOJt9LUmIjrHvseC/TCU8Bfv//QM3GhuaSd1YtbTxbni7R4YX8EKLuBK3bidAj1c9130aML89ckztI9RAiRj+oYyqZqoj1bNG0x1D7XFjyaLf+V+yVrACx/h3PvpG2/dNQ06PRdqAq33dQ7gfsI0qw5QnOCEZimbNigOUuqxf33k2hNkjZCyTXRHhslaY4LoOCIJZrC9dr+a4ENgWw0gVOsuP+Lr6I5UBR4B/H9PtvqAcRjpUOAoJYJiCPgWlglIyuV80u9Uwk11X09E09zEiW+CJWi4RHDWhRNcWO0z5o4nPbEExnXqpiVGPn+WOCsagpap0WXUErrCGhzwuHkQ==" + key = tls_private_key.deploy[1].public_key_openssh read_only = false } ] From 289c0c4a345f9bb75f49278d10c06fe09bbc81dc Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:05:41 +0100 Subject: [PATCH 3/7] Pin and set up provider versions --- examples/public-repository-complete-example/main.tf | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index f46a82da..55cd7bf4 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -3,7 +3,19 @@ terraform { } provider "github" { + # we want to be compatible with 2.x series of github provider version = "~> 2.2" + # credentials are read from the environment + # GITHUB_TOKEN + # GITHUB_ORGANIZATION +} + +provider "random" { + version = "= 2.2.1" +} + +provider "tls" { + version = "= 2.1.1" } resource "tls_private_key" "deploy" { From 942b4def9e43c9e0dda9b8c5b498c2fe3484e3d9 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:06:26 +0100 Subject: [PATCH 4/7] Improve comments for example --- .../main.tf | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index 55cd7bf4..9ee711c4 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -1,7 +1,15 @@ +# ----------------------------------------------------------------------------- +# TERRAFORM +# We need at least version 0.12.9 for full for_each functionality +# ----------------------------------------------------------------------------- terraform { required_version = "~> 0.12.9" } +# ----------------------------------------------------------------------------- +# PROVIDERS +# We are using specific version of different providers for consistant results +# ----------------------------------------------------------------------------- provider "github" { # we want to be compatible with 2.x series of github provider version = "~> 2.2" @@ -18,6 +26,10 @@ provider "tls" { version = "= 2.1.1" } +# ----------------------------------------------------------------------------- +# DEPENDENCIES from other providers +# We are creating some resources for easier testing +# ----------------------------------------------------------------------------- resource "tls_private_key" "deploy" { count = 2 @@ -29,6 +41,11 @@ resource "random_pet" "suffix" { length = 1 } +# ----------------------------------------------------------------------------- +# TEST A +# We are creating a repository, adding teams and setting up branch protection, +# deploy keys, issue labels and projects +# ----------------------------------------------------------------------------- module "repository" { source = "../.." @@ -117,6 +134,10 @@ module "repository" { ] } +# ----------------------------------------------------------------------------- +# TEST B +# We are creating a repository using some defaults defined in locals +# ----------------------------------------------------------------------------- locals { defaults = { homepage_url = "https://github.com/mineiros-io" @@ -136,6 +157,10 @@ module "repository-with-defaults" { defaults = local.defaults } +# ----------------------------------------------------------------------------- +# GITHUB DEPENDENCIES: TEAM +# We are creating a github team to be added to the repository +# ----------------------------------------------------------------------------- resource "github_team" "team" { name = "test-public-repository-complete-example-${random_pet.suffix.id}" description = "A secret team created with terraform to test the terraformn-github-repository module." From dea29113dc1ec50fdda18e0c3a420e743e16af93 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:13:46 +0100 Subject: [PATCH 5/7] Add random suffix to resources of other examples --- examples/public-repository-with-collaborators/main.tf | 10 +++++++++- examples/public-repository/main.tf | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/public-repository-with-collaborators/main.tf b/examples/public-repository-with-collaborators/main.tf index 4a687663..95beb7c4 100644 --- a/examples/public-repository-with-collaborators/main.tf +++ b/examples/public-repository-with-collaborators/main.tf @@ -6,10 +6,18 @@ provider "github" { version = "~> 2.2" } +provider "random" { + version = "= 2.2.1" +} + +resource "random_pet" "suffix" { + length = 1 +} + module "repository" { source = "../.." - name = "public-repository-with-collaborators" + name = "test-public-repository-with-collaborators-${random_pet.suffix.id}" description = "A public repository created with terraform to test the terraform-github-repository module." homepage_url = "https://github.com/mineiros-io" private = false diff --git a/examples/public-repository/main.tf b/examples/public-repository/main.tf index 17a36168..1aedd967 100644 --- a/examples/public-repository/main.tf +++ b/examples/public-repository/main.tf @@ -6,10 +6,18 @@ provider "github" { version = "~> 2.2" } +provider "random" { + version = "= 2.2.1" +} + +resource "random_pet" "suffix" { + length = 1 +} + module "repository" { source = "../.." - name = "public-repository" + name = "test-public-repository-${random_pet.suffix.id}" description = "A public repository created with terraform to test the terraform-github-repository module." homepage_url = "https://github.com/mineiros-io" private = false From ad1a14dc61ae03dd49f082efe6820f6a26d5a285 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:19:59 +0100 Subject: [PATCH 6/7] Remove unneeded resources from example as it leaves spurious invitations when adding a user to a team that is not member of the organization, the user will be invited to the organization. when removing the member again from the team the invitation will still be outstanding and not removed during terraform destroy. --- examples/public-repository-complete-example/main.tf | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/examples/public-repository-complete-example/main.tf b/examples/public-repository-complete-example/main.tf index 9ee711c4..b318ddd3 100644 --- a/examples/public-repository-complete-example/main.tf +++ b/examples/public-repository-complete-example/main.tf @@ -169,19 +169,10 @@ resource "github_team" "team" { # --------------------------------------------------------------------------------------------------------------------- # TEAM MEMBERSHIP -# We are adding two members to this team. terraform-test-user-1 and terraform-test-user-2 which are both existing users -# and already members of the GitHub Organization terraform-test that is an Organization managed by Mineiros.io to run -# integration tests with Terragrunt. +# We are adding one members to this team for testing branch restrictions +# terraform-test-user is permanent normal member of the test organization # --------------------------------------------------------------------------------------------------------------------- -resource "github_team_membership" "team_membership" { - count = 2 - - team_id = github_team.team.id - username = "terraform-test-user-${count.index + 1}" - role = "member" -} - resource "github_team_membership" "team_membership_permanent" { team_id = github_team.team.id username = "terraform-test-user" From 2cce4754eb21cb8278cbba4e4955638334b692bb Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Sun, 12 Jan 2020 03:48:56 +0100 Subject: [PATCH 7/7] Remove unused global variable and make them local where needed --- test/github_repository_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/github_repository_test.go b/test/github_repository_test.go index 9a5a4678..91808013 100644 --- a/test/github_repository_test.go +++ b/test/github_repository_test.go @@ -6,11 +6,9 @@ import ( "testing" ) -var githubOrganization, githubToken string - func init() { - githubOrganization = os.Getenv("GITHUB_ORGANIZATION") - githubToken = os.Getenv("GITHUB_TOKEN") + githubOrganization := os.Getenv("GITHUB_ORGANIZATION") + githubToken := os.Getenv("GITHUB_TOKEN") if githubOrganization == "" { panic("Please set a github organization using the GITHUB_ORGANIZATION environment variable.")