Skip to content

Commit

Permalink
Convert to TF 0.12. Add tests. Add Codefresh test pipeline (cloudposs…
Browse files Browse the repository at this point in the history
…e#10)

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline
  • Loading branch information
aknysh authored Oct 29, 2019
1 parent 85b4ee0 commit 31dc1d6
Show file tree
Hide file tree
Showing 22 changed files with 441 additions and 69 deletions.
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# terraform-github-repository-webhooks

[![Build Status](https://travis-ci.org/cloudposse/terraform-github-repository-webhooks.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-github-repository-webhooks) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-github-repository-webhooks.svg)](https://github.com/cloudposse/terraform-github-repository-webhooks/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
[![Codefresh Build Status](https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-github-repository-webhooks?type=cf-1)](https://g.codefresh.io/public/accounts/cloudposse/pipelines/5d1bcc1a1bde727a7172235e) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-github-repository-webhooks.svg)](https://github.com/cloudposse/terraform-github-repository-webhooks/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)


Terraform module to provision webhooks on a set of GitHub repositories.
Expand Down Expand Up @@ -52,13 +52,15 @@ Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest re

Create a GitHub Personal Access Token that has `admin:repo_hook` for full control of repository hooks; in otherwords, we need `write:repo_hook` to write repository hooks and `read:repo_hook` to read repository hooks.

For a complete example, see [examples/complete](examples/complete).

```hcl
module "github_webhooks" {
source = "git::https://github.com/cloudposse/terraform-github-repository-webhooks.git?ref=master"
github_organization = "cloudposse"
github_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
github_repositories = "geodesic"
webhook_url = "https://atlantis.prod.company.com/"
github_repositories = ["geodesic"]
webhook_url = "https://atlantis.prod.company.com"
webhook_content_type = "application/json"
events = ["issues"]
}
Expand All @@ -83,17 +85,23 @@ Available targets:

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| active | Indicate of the webhook should receive events | string | `true` | no |
| enabled | Whether or not to enable this module | string | `true` | no |
| events | A list of events which should trigger the webhook. | list | `<list>` | no |
| active | Indicate of the webhook should receive events | bool | `true` | no |
| enabled | Whether or not to enable this module | bool | `true` | no |
| events | A list of events which should trigger the webhook. | list(string) | `<list>` | no |
| github_organization | GitHub organization to use when creating webhooks | string | - | yes |
| github_repositories | List of repository names which should be associated with the webhook | list | `<list>` | no |
| github_repositories | List of repository names which should be associated with the webhook | list(string) | `<list>` | no |
| github_token | GitHub token used for API access. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable | string | `` | no |
| webhook_content_type | Webhook Content Type (E.g. json) | string | `json` | no |
| webhook_insecure_ssl | Webhook Insecure SSL (E.g. trust self-signed certificates) | string | `false` | no |
| webhook_content_type | Webhook Content Type (e.g. `json`) | string | `json` | no |
| webhook_insecure_ssl | Webhook Insecure SSL (e.g. trust self-signed certificates) | bool | `false` | no |
| webhook_secret | Webhook secret | string | `` | no |
| webhook_url | Webhook URL | string | - | yes |

## Outputs

| Name | Description |
|------|-------------|
| webhook_url | Webhook URL |




Expand Down Expand Up @@ -243,11 +251,11 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
|---|---|---|

[osterman_homepage]: https://github.com/osterman
[osterman_avatar]: https://github.com/osterman.png?size=150
[osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png
[goruha_homepage]: https://github.com/goruha
[goruha_avatar]: https://github.com/goruha.png?size=150
[goruha_avatar]: https://img.cloudposse.com/150x150/https://github.com/goruha.png
[aknysh_homepage]: https://github.com/aknysh
[aknysh_avatar]: https://github.com/aknysh.png?size=150
[aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png



Expand Down
12 changes: 7 additions & 5 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ description: |-
# Badges to display
badges:
- name: "Build Status"
image: "https://travis-ci.org/cloudposse/terraform-github-repository-webhooks.svg?branch=master"
url: "https://travis-ci.org/cloudposse/terraform-github-repository-webhooks"
- name: "Codefresh Build Status"
image: "https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-github-repository-webhooks?type=cf-1"
url: "https://g.codefresh.io/public/accounts/cloudposse/pipelines/5d1bcc1a1bde727a7172235e"
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-github-repository-webhooks.svg"
url: "https://github.com/cloudposse/terraform-github-repository-webhooks/releases/latest"
Expand All @@ -34,13 +34,15 @@ badges:
usage: |-
Create a GitHub Personal Access Token that has `admin:repo_hook` for full control of repository hooks; in otherwords, we need `write:repo_hook` to write repository hooks and `read:repo_hook` to read repository hooks.
For a complete example, see [examples/complete](examples/complete).
```hcl
module "github_webhooks" {
source = "git::https://github.com/cloudposse/terraform-github-repository-webhooks.git?ref=master"
github_organization = "cloudposse"
github_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
github_repositories = "geodesic"
webhook_url = "https://atlantis.prod.company.com/"
github_repositories = ["geodesic"]
webhook_url = "https://atlantis.prod.company.com"
webhook_content_type = "application/json"
events = ["issues"]
}
Expand Down
74 changes: 74 additions & 0 deletions codefresh/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: '1.0'

stages:
- Prepare
- Test

steps:
wait:
title: Wait
stage: Prepare
image: codefresh/cli:latest
commands:
- codefresh get builds --pipeline=${{CF_REPO_NAME}} --status running --limit 1000 -o json | jq --arg id ${{CF_BUILD_ID}} -ser 'flatten|.[-1].id==$id'
retry:
maxAttempts: 10
delay: 20
exponentialFactor: 1.1

main_clone:
title: "Clone repository"
type: git-clone
stage: Prepare
description: "Initialize"
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
git: CF-default
revision: ${{CF_REVISION}}

clean_init:
title: Prepare build-harness and test-harness
image: ${{TEST_IMAGE}}
stage: Prepare
commands:
- cf_export PATH="/usr/local/terraform/0.12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- make init
- git -C build-harness checkout master
- make -C test/ clean init TEST_HARNESS_BRANCH=master
- make -C test/src clean init
- find . -type d -name '.terraform' | xargs rm -rf
- find . -type f -name 'terraform.tfstate*' -exec rm -f {} \;

test:
type: "parallel"
title: "Run tests"
description: "Run all tests in parallel"
stage: Test
steps:
test_readme_lint:
title: "Test README.md updated"
stage: "Test"
image: ${{TEST_IMAGE}}
description: Test "readme/lint"
commands:
- make readme/lint

test_module:
title: Test module with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ module

test_examples_complete:
title: Test "examples/complete" with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ examples/complete

# test_examples_complete_terratest:
# title: Test "examples/complete" with terratest
# image: ${{TEST_IMAGE}}
# stage: Test
# commands:
# - make -C test/src
18 changes: 12 additions & 6 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| active | Indicate of the webhook should receive events | string | `true` | no |
| enabled | Whether or not to enable this module | string | `true` | no |
| events | A list of events which should trigger the webhook. | list | `<list>` | no |
| active | Indicate of the webhook should receive events | bool | `true` | no |
| enabled | Whether or not to enable this module | bool | `true` | no |
| events | A list of events which should trigger the webhook. | list(string) | `<list>` | no |
| github_organization | GitHub organization to use when creating webhooks | string | - | yes |
| github_repositories | List of repository names which should be associated with the webhook | list | `<list>` | no |
| github_repositories | List of repository names which should be associated with the webhook | list(string) | `<list>` | no |
| github_token | GitHub token used for API access. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable | string | `` | no |
| webhook_content_type | Webhook Content Type (E.g. json) | string | `json` | no |
| webhook_insecure_ssl | Webhook Insecure SSL (E.g. trust self-signed certificates) | string | `false` | no |
| webhook_content_type | Webhook Content Type (e.g. `json`) | string | `json` | no |
| webhook_insecure_ssl | Webhook Insecure SSL (e.g. trust self-signed certificates) | bool | `false` | no |
| webhook_secret | Webhook secret | string | `` | no |
| webhook_url | Webhook URL | string | - | yes |

## Outputs

| Name | Description |
|------|-------------|
| webhook_url | Webhook URL |

9 changes: 9 additions & 0 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
region = "us-west-1"

github_organization = "cloudposse"

github_repositories = ["terraform-github-repository-webhooks"]

webhook_url = "https://archive.sweetops.com"

events = ["issue_comment", "pull_request", "pull_request_review", "pull_request_review_comment"]
13 changes: 13 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "aws" {
region = var.region
}

module "webhooks" {
source = "../../"
active = false
github_token = var.github_token
github_organization = var.github_organization
github_repositories = var.github_repositories
webhook_url = var.webhook_url
events = var.events
}
4 changes: 4 additions & 0 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "webhook_url" {
description = "Webhook URL"
value = module.webhooks.webhook_url
}
32 changes: 32 additions & 0 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "region" {
type = string
description = "AWS region"
}

variable "github_token" {
type = string
default = ""
description = "GitHub token used for API access. If not provided, can be sourced from the `GITHUB_TOKEN` environment variable"
}

variable "github_organization" {
type = string
description = "GitHub organization to use when creating webhooks"
}

variable "github_repositories" {
type = list(string)
description = "List of repository names which should be associated with the webhook"
default = []
}

variable "webhook_url" {
type = string
description = "Webhook URL"
}

variable "events" {
# Full list of events available here: https://developer.github.com/v3/activity/events/types/
type = list(string)
description = "A list of events which should trigger the webhook."
}
13 changes: 0 additions & 13 deletions examples/simple/main.tf

This file was deleted.

22 changes: 11 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
provider "github" {
token = "${var.github_token}"
organization = "${var.github_organization}"
token = var.github_token != "" ? var.github_token : null
organization = var.github_organization
}

resource "github_repository_webhook" "default" {
count = "${var.enabled == "true" && length(var.github_repositories) > 0 ? length(var.github_repositories) : 0}"
count = var.enabled && length(var.github_repositories) > 0 ? length(var.github_repositories) : 0

repository = "${var.github_repositories[count.index]}"
active = "${var.active}"
repository = var.github_repositories[count.index]
active = var.active

configuration {
url = "${var.webhook_url}"
content_type = "${var.webhook_content_type}"
secret = "${var.webhook_secret}"
insecure_ssl = "${var.webhook_insecure_ssl}"
url = var.webhook_url
content_type = var.webhook_content_type
secret = var.webhook_secret
insecure_ssl = var.webhook_insecure_ssl
}

events = "${var.events}"
events = var.events

lifecycle {
# This is required for idempotency
ignore_changes = ["configuration.secret"]
ignore_changes = ["configuration[0].secret"]
}
}
5 changes: 4 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@

output "webhook_url" {
description = "Webhook URL"
value = join("", github_repository_webhook.default.*.url)
}
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.test-harness
43 changes: 43 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
TEST_HARNESS ?= https://github.com/cloudposse/test-harness.git
TEST_HARNESS_BRANCH ?= master
TEST_HARNESS_PATH = $(realpath .test-harness)
BATS_ARGS ?= --tap
BATS_LOG ?= test.log

# Define a macro to run the tests
define RUN_TESTS
@echo "Running tests in $(1)"
@cd $(1) && bats $(BATS_ARGS) $(addsuffix .bats,$(addprefix $(TEST_HARNESS_PATH)/test/terraform/,$(TESTS)))
endef

default: all

-include Makefile.*

## Provision the test-harnesss
.test-harness:
[ -d $@ ] || git clone --depth=1 -b $(TEST_HARNESS_BRANCH) $(TEST_HARNESS) $@

## Initialize the tests
init: .test-harness

## Install all dependencies (OS specific)
deps::
@exit 0

## Clean up the test harness
clean:
[ "$(TEST_HARNESS_PATH)" == "/" ] || rm -rf $(TEST_HARNESS_PATH)

## Run all tests
all: module examples/complete

## Run basic sanity checks against the module itself
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
module: deps
$(call RUN_TESTS, ../)

## Run tests against example
examples/complete: export TESTS ?= installed lint get-modules get-plugins validate
examples/complete: deps
$(call RUN_TESTS, ../$@)
5 changes: 5 additions & 0 deletions test/Makefile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ifneq (,$(wildcard /sbin/apk))
## Install all dependencies for alpine
deps:: init
@apk add --update terraform-docs@cloudposse json2hcl@cloudposse
endif
2 changes: 2 additions & 0 deletions test/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.gopath
vendor/
Loading

0 comments on commit 31dc1d6

Please sign in to comment.