Skip to content

Commit

Permalink
Add gitlab wiki page hook as well
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Mar 2, 2019
1 parent 1951a70 commit 79028ed
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/codebot/formatters.rb
Expand Up @@ -19,6 +19,7 @@
require 'codebot/formatters/gitlab_pipeline_hook'
require 'codebot/formatters/gitlab_note_hook'
require 'codebot/formatters/gitlab_merge_request_hook'
require 'codebot/formatters/gitlab_wiki_page_hook'
require 'codebot/shortener'

module Codebot
Expand Down Expand Up @@ -88,6 +89,9 @@ def self.create_formatter(event, payload, integration) # rubocop:disable Metrics
when :gitlab_merge_request_hook
Formatters::Gitlab::MergeRequestHook.new(payload,
shortener(integration))
when :gitlab_wiki_page_hook
Formatters::Gitlab::WikiPageHook.new(payload,
shortener(integration))
else "Error: missing formatter for #{event.inspect}"
end
end
Expand Down
56 changes: 56 additions & 0 deletions lib/codebot/formatters/gitlab_wiki_page_hook.rb
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module Codebot
module Formatters
module Gitlab
# Triggers on a Wiki Page Hook event
class WikiPageHook < Formatter
def format
["#{summary}: #{format_url url}"]
end

def summary
default_format % {
repository: format_repository(repository_name),
sender: format_user(sender_name),
action: action,
title: wiki_title
}
end

def default_format
'[%<repository>s] %<sender>s %<action>s page \'%<title>s\''
end

def repository_name
extract(:project, :path_with_namespace)
end

def sender_name
extract(:user, :name)
end

def summary_url
extract(:object_attributes, :url)
end

def action
case wiki_action
when 'create' then 'created'
when 'delete' then 'deleted'
when 'update', nil then 'updated'
else wiki_action
end
end

def wiki_action
extract(:object_attributes, :action)
end

def wiki_title
extract(:object_attributes, :title)
end
end
end
end
end
18 changes: 18 additions & 0 deletions spec/codebot/formatters/gitlab_wiki_page_hook_spec.rb
@@ -1 +1,19 @@
# frozen_string_literal: true

require 'codebot/formatters/gitlab_helpers'

RSpec.describe Codebot::Formatters::Gitlab::WikiPageHook do
describe '.format' do
it 'formats correctly one entry' do
result = do_format_test('gitlab_wiki_page_hook_1',
Codebot::Formatters::Gitlab::WikiPageHook)
expect(result).to eq ['[root/awesome-project] Administrator created page \'Awesome\': shortened://http://example.com/root/awesome-project/wikis/awesome']
end

it 'formats correctly another entry' do
result = do_format_test('gitlab_wiki_page_hook_2',
Codebot::Formatters::Gitlab::WikiPageHook)
expect(result).to eq ['[infrastructure/management] Ola Bini created page \'Infrastructure Onboarding\': shortened://https://gitlab.autonomia.digital/infrastructure/management/wikis/Infrastructure-Onboarding']
end
end
end

0 comments on commit 79028ed

Please sign in to comment.