Skip to content

Commit e86fd39

Browse files
Merge pull request #34 from chrispblink/feature/implement-repo-webhooks
implement repository webhooks
2 parents 552037f + 11750f8 commit e86fd39

File tree

8 files changed

+400
-2
lines changed

8 files changed

+400
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ features like Branch Protection or Collaborator Management.
4646
Collaborators,
4747
Teams,
4848
Deploy Keys,
49-
Projects
49+
Projects,
50+
Repository Webhooks
5051

5152
- *Features not yet implemented*:
52-
Repository Webhooks,
5353
Project Columns support,
5454
Actions,
5555
Repository File
@@ -301,6 +301,14 @@ Default is `true`.
301301
This resource allows you to create and manage projects for GitHub repository.
302302
Default is `[]`.
303303

304+
##### Webhooks Configuration
305+
- **[`webhooks`](#webhook-object-attributes)**: *(Optional `list(webhook)`)*
306+
This resource allows you to create and manage webhooks for repositories in your organization.
307+
When applied, a webhook will be created which specifies a URL to receive events and which events
308+
to receieve. Additional constraints, such as SSL verification, pre-shared secret and content type
309+
can also be configured
310+
Default is `[]`.
311+
304312
#### [`defaults`](#repository-configuration) Object Attributes
305313
This is a special argument to set various defaults to be reused for multiple repositories.
306314
The following top-level arguments can be set as defaults:
@@ -454,6 +462,26 @@ Specifies an ID which is used to prevent resource recreation when the order in
454462
the list of projects changes.
455463
Default is `name`.
456464

465+
#### [`webhook`](#webhooks-configuration) Object Attributes
466+
- **`events`**: ***(Required `list(string)`)***
467+
A list of events which should trigger the webhook. [See a list of available events.](https://developer.github.com/v3/activity/events/types/)
468+
469+
- **`url`**: ***(Required `string`)***
470+
The URL to which the payloads will be delivered.
471+
472+
- **`active`**: *(Optional `bool`)*
473+
Indicate if the webhook should receive events. Defaults to `true`.
474+
475+
- **`content_type`**: *(Optional `string`)*
476+
The media type used to serialize the payloads. Supported values include `json` and `form`. The default is `form`.
477+
478+
- **`secret`**: *(Optional `string`)*
479+
If provided, the `secret` will be used as the `key` to generate the HMAC hex digest value in the `[X-Hub-Signature](https://developer.github.com/webhooks/#delivery-headers)` header.
480+
481+
- **`insecure_ssl`**: *(Optional `bool`)*
482+
Determines whether the SSL certificate of the host for `url` will be verified when delivering payloads. Supported values include `0` (verification is performed) and `1` (verification is not performed). The default is `0`. **We strongly recommend not setting this to `1` as you are subject to man-in-the-middle and other attacks.**
483+
484+
457485
## Module Attributes Reference
458486
The following attributes are exported by the module:
459487

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
# CREATE A REPOSITORY WITH WEBHOOK
3+
# This example will create a repository with a webhook and some basic settings.
4+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5+
6+
# ---------------------------------------------------------------------------------------------------------------------
7+
# SET TERRAFORM AND PROVIDER REQUIREMENTS FOR RUNNING THIS MODULE
8+
# ---------------------------------------------------------------------------------------------------------------------
9+
10+
provider "github" {
11+
version = "~> 2.6"
12+
}
13+
14+
# ---------------------------------------------------------------------------------------------------------------------
15+
# TEST
16+
# We are creating a repository with a single webhook while specifying only the minimum required variables
17+
# ---------------------------------------------------------------------------------------------------------------------
18+
19+
module "repository" {
20+
source = "../.."
21+
22+
name = var.name
23+
24+
webhooks = [{
25+
active = var.webhook_active
26+
events = var.webhook_events
27+
url = var.webhook_url
28+
content_type = var.webhook_content_type
29+
insecure_ssl = var.webhook_insecure_ssl
30+
secret = var.webhook_secret
31+
}]
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
output "repository" {
2+
description = "All outputs of the created repository."
3+
value = module.repository
4+
}
5+
6+
output "repository_name" {
7+
description = "The full name of the created repository"
8+
value = module.repository.full_name
9+
}
10+
11+
output "webhook_url" {
12+
description = "Events are being sent to this URL"
13+
value = module.repository.webhooks[0].configuration[0].url
14+
}
15+
16+
output "webhook_content_type" {
17+
description = "The content-type of the webhook"
18+
value = module.repository.webhooks[0].configuration[0].content_type
19+
}
20+
21+
output "webhook_insecure_ssl" {
22+
description = "TLS encryption configuration on the webhook"
23+
value = module.repository.webhooks[0].configuration[0].insecure_ssl
24+
}
25+
26+
output "webhook_secret" {
27+
description = "The shared secret for the webhook"
28+
value = module.repository.webhooks[0].configuration[0].secret
29+
}
30+
31+
output "webhook_active" {
32+
description = "Indicates if the webhook should receive events"
33+
value = module.repository.webhooks[0].active
34+
}
35+
36+
output "webhook_events" {
37+
description = "The events which will trigger this webhook"
38+
value = module.repository.webhooks[0].events
39+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# ENVIRONMENT VARIABLES
3+
# Define these secrets as environment variables.
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
# GITHUB_ORGANIZATION
7+
# GITHUB_TOKEN
8+
9+
# ---------------------------------------------------------------------------------------------------------------------
10+
# REQUIRED VARIABLES
11+
# These variables must be set when using this module.
12+
# ---------------------------------------------------------------------------------------------------------------------
13+
14+
# ---------------------------------------------------------------------------------------------------------------------
15+
# OPTIONAL VARIABLES
16+
# These variables have defaults, but may be overridden.
17+
# ---------------------------------------------------------------------------------------------------------------------
18+
19+
variable "name" {
20+
description = "The name of the created repository."
21+
type = string
22+
default = "test-public-repository-with-collaborators"
23+
}
24+
25+
variable "description" {
26+
description = "The description of the created repository."
27+
type = string
28+
default = "A public repository created with terraform to test the terraform-github-repository module."
29+
}
30+
31+
variable "url" {
32+
description = "The url of the created repository."
33+
type = string
34+
default = "https://github.com/mineiros-io"
35+
}
36+
37+
38+
variable "has_issues" {
39+
description = "Set to true to enable the GitHub Issues features on the repository."
40+
type = bool
41+
default = false
42+
}
43+
44+
variable "has_projects" {
45+
description = "Set to true to enable the GitHub Projects features on the repository."
46+
type = bool
47+
default = false
48+
}
49+
50+
variable "has_wiki" {
51+
description = "Set to true to enable the GitHub Wiki features on the repository."
52+
type = bool
53+
default = false
54+
}
55+
56+
variable "allow_merge_commit" {
57+
description = "Set to false to disable merge commits on the repository."
58+
type = bool
59+
default = true
60+
}
61+
62+
variable "allow_squash_merge" {
63+
description = "Set to true to enable squash merges on the repository."
64+
type = bool
65+
default = false
66+
}
67+
68+
variable "allow_rebase_merge" {
69+
description = "Set to true to enable rebase merges on the repository."
70+
type = bool
71+
default = false
72+
}
73+
74+
variable "has_downloads" {
75+
description = "Set to true to enable the (deprecated) downloads features on the repository."
76+
type = bool
77+
default = false
78+
}
79+
80+
variable "auto_init" {
81+
description = "Wether or not to produce an initial commit in the repository."
82+
type = bool
83+
default = true
84+
}
85+
86+
variable "gitignore_template" {
87+
description = "Use the name of the template without the extension. For example, Haskell. Available templates: https://github.com/github/gitignore"
88+
type = string
89+
default = "Terraform"
90+
}
91+
92+
variable "license_template" {
93+
description = "Use the name of the template without the extension. For example, 'mit' or 'mpl-2.0'. Available licences: https://github.com/github/choosealicense.com/tree/gh-pages/_licenses"
94+
type = string
95+
default = "mit"
96+
}
97+
98+
variable "topics" {
99+
description = "The list of topics of the repository."
100+
type = list(string)
101+
default = ["terraform", "integration-test"]
102+
}
103+
104+
variable "admin_collaborators" {
105+
description = "A list of GitHub usernames that should be added as admin collaborators to the created repository."
106+
type = list(string)
107+
default = ["terraform-test-user-1"]
108+
}
109+
110+
variable "webhook_url" {
111+
description = "Send events to this URL"
112+
type = string
113+
default = "https://example.com/events"
114+
}
115+
116+
variable "webhook_content_type" {
117+
description = "Use this content-type in the webhook"
118+
type = string
119+
default = "application/json"
120+
}
121+
122+
variable "webhook_insecure_ssl" {
123+
description = "Configure TLS encryption on the webhook"
124+
type = bool
125+
default = true
126+
}
127+
128+
variable "webhook_secret" {
129+
description = "The shared secret for the webhook"
130+
type = string
131+
default = "correct horse battery staple"
132+
}
133+
134+
variable "webhook_active" {
135+
description = "Indicate if the webhook should receive events"
136+
type = bool
137+
default = true
138+
}
139+
140+
variable "webhook_events" {
141+
description = "The events which will trigger this webhook"
142+
type = list(string)
143+
default = ["issues"]
144+
}

main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,24 @@ resource "github_repository_project" "repository_project" {
407407
name = each.value.name
408408
body = each.value.body
409409
}
410+
411+
# ---------------------------------------------------------------------------------------------------------------------
412+
# Webhooks
413+
# ---------------------------------------------------------------------------------------------------------------------
414+
415+
resource "github_repository_webhook" "repository_webhook" {
416+
count = length(var.webhooks)
417+
418+
repository = github_repository.repository.name
419+
# the optional `name` attribute causes an error so it has been removed
420+
# > Error: "name": [REMOVED] The `name` attribute is no longer necessary.
421+
active = try(var.webhooks[count.index].active, true)
422+
events = var.webhooks[count.index].events
423+
424+
configuration {
425+
url = var.webhooks[count.index].url
426+
content_type = try(var.webhooks[count.index].content_type, "json")
427+
insecure_ssl = try(var.webhooks[count.index].insecure_ssl, false)
428+
secret = try(var.webhooks[count.index].secret, null)
429+
}
430+
}

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ output "deploy_keys" {
5454
value = local.deploy_keys_output
5555
description = "A map of deploy keys keyed by input id."
5656
}
57+
58+
output "webhooks" {
59+
value = github_repository_webhook.repository_webhook
60+
description = "All attributes and arguments as returned by the github_repository_webhook resource."
61+
}

0 commit comments

Comments
 (0)