Skip to content

Commit 50006a0

Browse files
committed
Make GitLab and GitHub plugins customizable
1 parent 06d0c88 commit 50006a0

File tree

5 files changed

+109
-41
lines changed

5 files changed

+109
-41
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Main
22

3-
Nothing so far
3+
#### Changes
4+
* Make GitLab and GitHub plugins customizable
45

56
## 0.4.5
67

lib/rodbot/plugins/github_webhook/README.github_webhook.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ To authenticate the webhook calls from GitHub, create a new random secret token:
1818
ruby -r securerandom -e "puts SecureRandom.alphanumeric(20)"
1919
```
2020

21-
## Activation
22-
23-
Activate and configure this plugin in `config/rodbot.rb`:
21+
Configure this plugin in `config/rodbot.rb`:
2422

2523
```ruby
2624
plugin :github_webhook do
@@ -30,7 +28,7 @@ end
3028

3129
You can set any number of secure tokens here separated with colons.
3230

33-
## Add Repositories
31+
## Activation
3432

3533
Add a webhook to every GitHub repository you'd like to see pipeline event announcements for. Go to `https://github.com/<USER>/<REPO>/settings/hooks` and create a new webhook with the following properties:
3634

@@ -40,3 +38,29 @@ Add a webhook to every GitHub repository you'd like to see pipeline event announ
4038
* SSL verification: (o) Enable SSL verification
4139
* Which events? (o) Let me select individual events: [x] Workflow runs
4240
* And... [x] Active
41+
42+
Use the test tool to verify your setup and to see what the JSON payloads look like in case you'd like to customize the handler.
43+
44+
## Customization
45+
46+
You can change how the plugin reacts to which webhook requests by configuring a custom handler proc. Here's the default one:
47+
48+
```ruby
49+
plugin :github_webhook do
50+
handler: ->(request) do
51+
if request.env['HTTP_X_GITHUB_EVENT'] == 'workflow_run'
52+
json = JSON.parse(request.body.read)
53+
project = json.dig('repository', 'full_name')
54+
status = json.dig('workflow_run', 'status')
55+
status = json.dig('workflow_run', 'conclusion') if status == 'completed'
56+
emoji = case status
57+
when 'requested' then '🟡'
58+
when 'success' then '🟢'
59+
when 'failure' then '🔴'
60+
else '⚪️'
61+
end
62+
[emoji, project, status.gsub('_', ' ')].join(' ')
63+
end
64+
end
65+
end
66+
```

lib/rodbot/plugins/github_webhook/app.rb

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@ class GithubWebhook
66
module App
77

88
class Routes < ::App
9-
route do |r|
10-
r.post '' do
11-
r.halt 200 if request.env['HTTP_X_GITHUB_EVENT'] == 'ping'
12-
r.halt 400 unless request.env['HTTP_X_GITHUB_EVENT'] == 'workflow_run'
13-
r.halt 401 unless authorized?
9+
DEFAULT_HANDLER = ->(request) do
10+
if request.env['HTTP_X_GITHUB_EVENT'] == 'workflow_run'
1411
json = JSON.parse(request.body.read)
1512
project = json.dig('repository', 'full_name')
1613
status = json.dig('workflow_run', 'status')
1714
status = json.dig('workflow_run', 'conclusion') if status == 'completed'
18-
Rodbot.say [emoji_for(status), project, status.gsub('_', ' ')].join(' ')
19-
r.halt 200
15+
emoji = case status
16+
when 'requested' then '🟡'
17+
when 'success' then '🟢'
18+
when 'failure' then '🔴'
19+
else '⚪️'
20+
end
21+
[emoji, project, status.gsub('_', ' ')].join(' ')
22+
end
23+
end
24+
25+
route do |r|
26+
r.post '' do
27+
r.halt 200 if request.env['HTTP_X_GITHUB_EVENT'] == 'ping'
28+
r.halt 401 unless authorized?
29+
handler = Rodbot.config(:plugin, :github_webhook, :handler) || DEFAULT_HANDLER
30+
message = handler.call(r)
31+
if message&.empty?
32+
r.halt 204
33+
else
34+
Rodbot.say message
35+
r.halt 200
36+
end
2037
end
38+
rescue => error
39+
r.halt 500, error.message
2140
end
2241

2342
private
@@ -30,15 +49,6 @@ def authorized?
3049
end
3150
end
3251

33-
def emoji_for(status)
34-
case status
35-
when 'requested' then '🟡'
36-
when 'success' then '🟢'
37-
when 'failure' then '🔴'
38-
else '⚪️'
39-
end
40-
end
41-
4252
end
4353
end
4454
end

lib/rodbot/plugins/gitlab_webhook/README.gitlab_webhook.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ To authenticate the webhook calls from GitLab, create a new random secret token:
1818
ruby -r securerandom -e "puts SecureRandom.alphanumeric(20)"
1919
```
2020

21-
## Activation
22-
23-
Activate and configure this plugin in `config/rodbot.rb`:
21+
Configure this plugin in `config/rodbot.rb`:
2422

2523
```ruby
2624
plugin :gitlab_webhook do
@@ -30,11 +28,36 @@ end
3028

3129
You can set any number of secure tokens here separated with colons.
3230

33-
## Add Repositories
31+
## Activation
3432

35-
Add a webhook to every GitLab repository you'd like to see pipeline event announcements for. Go to `https://gitlab.com/<USER>/<REPO>/-/hooks` and create a new webhook with the following properties:
33+
Set up a webhook to every GitLab repository you'd like to see pipeline event announcements for. Go to `https://gitlab.com/<USER>/<REPO>/-/hooks` and create a new webhook with the following properties:
3634

3735
* URL: `https://<RODBOT-APP>/gitlab_webhook`
3836
* Secret token: `<TOKEN>`
3937
* Trigger: [x] Pipeline events
4038
* SSL verification: [x] Enable SSL verification
39+
40+
Use the test tool to verify your setup and to see what the JSON payloads look like in case you'd like to customize the handler.
41+
42+
## Customization
43+
44+
You can change how the plugin reacts to which webhook requests by configuring a custom handler proc. Here's the default one:
45+
46+
```ruby
47+
plugin :gitlab_webhook do
48+
handler: ->(request) do
49+
json = JSON.parse(request.body.read)
50+
if json['object_kind'] == 'pipeline'
51+
project = json.dig('project', 'path_with_namespace')
52+
status = json.dig('object_attributes', 'detailed_status')
53+
emoji = case status
54+
when 'running' then '🟡'
55+
when 'passed' then '🟢'
56+
when 'failed' then '🔴'
57+
else '⚪️'
58+
end
59+
[emoji, project, status.gsub('_', ' ')].join(' ')
60+
end
61+
end
62+
end
63+
```

lib/rodbot/plugins/gitlab_webhook/app.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,35 @@ class GitlabWebhook
66
module App
77

88
class Routes < ::App
9+
DEFAULT_HANDLER = ->(request) do
10+
json = JSON.parse(request.body.read)
11+
if json['object_kind'] == 'pipeline'
12+
project = json.dig('project', 'path_with_namespace')
13+
status = json.dig('object_attributes', 'detailed_status')
14+
emoji = case status
15+
when 'running' then '🟡'
16+
when 'passed' then '🟢'
17+
when 'failed' then '🔴'
18+
else '⚪️'
19+
end
20+
[emoji, project, status.gsub('_', ' ')].join(' ')
21+
end
22+
end
23+
924
route do |r|
1025
r.post '' do
1126
r.halt 401 unless authorized?
12-
json = JSON.parse(request.body.read)
13-
r.halt 400 unless json['object_kind'] == 'pipeline'
14-
project = json.dig('project', 'path_with_namespace')
15-
status = json.dig('object_attributes', 'detailed_status')
16-
Rodbot.say [emoji_for(status), project, status.gsub('_', ' ')].join(' ')
17-
r.halt 200
27+
handler = Rodbot.config(:plugin, :gitlab_webhook, :handler) || DEFAULT_HANDLER
28+
message = handler.call(r)
29+
if message&.empty?
30+
r.halt 204
31+
else
32+
Rodbot.say message
33+
r.halt 200
34+
end
1835
end
36+
rescue => error
37+
r.halt 500, error.message
1938
end
2039

2140
private
@@ -24,15 +43,6 @@ def authorized?
2443
Rodbot.config(:plugin, :gitlab_webhook, :secret_tokens).to_s.split(':').include?(request.env['HTTP_X_GITLAB_TOKEN'])
2544
end
2645

27-
def emoji_for(status)
28-
case status
29-
when 'running' then '🟡'
30-
when 'passed' then '🟢'
31-
when 'failed' then '🔴'
32-
else '⚪️'
33-
end
34-
end
35-
3646
end
3747
end
3848
end

0 commit comments

Comments
 (0)