From 07d9b036a9b7b3502238cac0fdb4980bfe02e128 Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Sun, 24 Jun 2018 19:12:07 +0900 Subject: Add dummy Google Hangouts Chat integration --- app/models/project.rb | 1 + .../project_services/hangouts_chat_service.rb | 41 ++++++++++++++++++++++ app/models/service.rb | 1 + lib/api/services.rb | 9 +++++ spec/lib/gitlab/import_export/all_models.yml | 1 + spec/models/project_spec.rb | 1 + 6 files changed, 54 insertions(+) create mode 100644 app/models/project_services/hangouts_chat_service.rb diff --git a/app/models/project.rb b/app/models/project.rb index e29bca365a4..a0744b0c58e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -154,6 +154,7 @@ class Project < ActiveRecord::Base has_one :mock_monitoring_service has_one :microsoft_teams_service has_one :packagist_service + has_one :hangouts_chat_service # TODO: replace these relations with the fork network versions has_one :forked_project_link, foreign_key: "forked_to_project_id" diff --git a/app/models/project_services/hangouts_chat_service.rb b/app/models/project_services/hangouts_chat_service.rb new file mode 100644 index 00000000000..99a6afcc11a --- /dev/null +++ b/app/models/project_services/hangouts_chat_service.rb @@ -0,0 +1,41 @@ +class HangoutsChatService < ChatNotificationService + def title + 'Hangouts Chat' + end + + def description + 'Receive event notifications in Google Hangouts Chat' + end + + def self.to_param + 'hangouts_chat' + end + + def help + 'This service sends notifications about projects events to Google Hangouts Chat room.
+ To set up this service: +
    +
  1. Set up an incoming webhook for your room. All notifications will come to this room.
  2. +
  3. Paste the Webhook URL into the field below.
  4. +
  5. Select events below to enable notifications.
  6. +
' + end + + def event_field(event) + end + + def default_channel_placeholder + end + + def webhook_placeholder + 'https://chat.googleapis.com/v1/spaces…' + end + + def default_fields + [ + { type: 'text', name: 'webhook', placeholder: "e.g. #{webhook_placeholder}" }, + { type: 'checkbox', name: 'notify_only_broken_pipelines' }, + { type: 'checkbox', name: 'notify_only_default_branch' } + ] + end +end diff --git a/app/models/service.rb b/app/models/service.rb index ad835293b46..cbfe0c6eedd 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -254,6 +254,7 @@ class Service < ActiveRecord::Base emails_on_push external_wiki flowdock + hangouts_chat hipchat irker jira diff --git a/lib/api/services.rb b/lib/api/services.rb index 553e8dff4b9..1f2bf546cd7 100644 --- a/lib/api/services.rb +++ b/lib/api/services.rb @@ -368,6 +368,14 @@ module API desc: "The project's slug on gemnasium.com" } ], + 'hangouts-chat' => [ + { + required: true, + name: :webhook, + type: String, + desc: 'The Hangouts Chat webhook. e.g. https://chat.googleapis.com/v1/spaces…' + } + ], 'hipchat' => [ { required: true, @@ -688,6 +696,7 @@ module API ExternalWikiService, FlowdockService, GemnasiumService, + HangoutsChatService, HipchatService, IrkerService, JiraService, diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index 084ce3066d6..db5aab0cd76 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -211,6 +211,7 @@ project: - slack_service - microsoft_teams_service - mattermost_service +- hangouts_chat_service - buildkite_service - bamboo_service - teamcity_service diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index d200e5f2e42..b0ec725bf70 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -26,6 +26,7 @@ describe Project do it { is_expected.to have_one(:slack_service) } it { is_expected.to have_one(:microsoft_teams_service) } it { is_expected.to have_one(:mattermost_service) } + it { is_expected.to have_one(:hangouts_chat_service) } it { is_expected.to have_one(:packagist_service) } it { is_expected.to have_one(:pushover_service) } it { is_expected.to have_one(:asana_service) } -- cgit v1.2.1 From cb77d939c919ac8ebf4288724febdf7bb8238399 Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Sun, 24 Jun 2018 19:35:35 +0900 Subject: Add Hangouts Chat service implementation --- Gemfile | 3 + Gemfile.lock | 2 + Gemfile.rails5.lock | 2 + .../project_services/hangouts_chat_service.rb | 25 ++ .../project_services/hangouts_chat_service_spec.rb | 311 +++++++++++++++++++++ 5 files changed, 343 insertions(+) create mode 100644 spec/models/project_services/hangouts_chat_service_spec.rb diff --git a/Gemfile b/Gemfile index d575568adaa..5aa05bf53f9 100644 --- a/Gemfile +++ b/Gemfile @@ -220,6 +220,9 @@ gem 'gemnasium-gitlab-service', '~> 0.2' # Slack integration gem 'slack-notifier', '~> 1.5.1' +# Hangouts Chat integration +gem 'hangouts-chat', '~> 0.0.5' + # Asana integration gem 'asana', '~> 0.6.0' diff --git a/Gemfile.lock b/Gemfile.lock index 7f9207d9dfe..2e711e50dfc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -385,6 +385,7 @@ GEM temple (>= 0.8.0) thor tilt + hangouts-chat (0.0.5) hashdiff (0.3.4) hashie (3.5.7) hashie-forbidden_attributes (0.1.1) @@ -1058,6 +1059,7 @@ DEPENDENCIES grpc (~> 1.11.0) haml_lint (~> 0.26.0) hamlit (~> 2.8.8) + hangouts-chat (~> 0.0.5) hashie-forbidden_attributes health_check (~> 2.6.0) hipchat (~> 1.5.0) diff --git a/Gemfile.rails5.lock b/Gemfile.rails5.lock index 766f2479ea5..9bb25e53fe1 100644 --- a/Gemfile.rails5.lock +++ b/Gemfile.rails5.lock @@ -388,6 +388,7 @@ GEM temple (>= 0.8.0) thor tilt + hangouts-chat (0.0.5) hashdiff (0.3.4) hashie (3.5.7) hashie-forbidden_attributes (0.1.1) @@ -1068,6 +1069,7 @@ DEPENDENCIES grpc (~> 1.11.0) haml_lint (~> 0.26.0) hamlit (~> 2.8.8) + hangouts-chat (~> 0.0.5) hashie-forbidden_attributes health_check (~> 2.6.0) hipchat (~> 1.5.0) diff --git a/app/models/project_services/hangouts_chat_service.rb b/app/models/project_services/hangouts_chat_service.rb index 99a6afcc11a..00e9b634dd1 100644 --- a/app/models/project_services/hangouts_chat_service.rb +++ b/app/models/project_services/hangouts_chat_service.rb @@ -1,3 +1,5 @@ +require 'hangouts_chat' + class HangoutsChatService < ChatNotificationService def title 'Hangouts Chat' @@ -38,4 +40,27 @@ class HangoutsChatService < ChatNotificationService { type: 'checkbox', name: 'notify_only_default_branch' } ] end + + private + + def notify(message, opts) + simple_text = compose_simple_message(message) + HangoutsChat::Sender.new(webhook).simple(simple_text) + end + + def compose_simple_message(message) + header = message.pretext + return header if message.attachments.empty? + + title = fetch_attachment_title(message.attachments.first) + body = message.attachments.first[:text] + [header, title, body].compact.join("\n") + end + + def fetch_attachment_title(attachment) + return nil if attachment[:title].nil? + return attachment[:title] if attachment[:title_link].nil? + + "<#{attachment[:title_link]}|#{attachment[:title]}>" + end end diff --git a/spec/models/project_services/hangouts_chat_service_spec.rb b/spec/models/project_services/hangouts_chat_service_spec.rb new file mode 100644 index 00000000000..73472700548 --- /dev/null +++ b/spec/models/project_services/hangouts_chat_service_spec.rb @@ -0,0 +1,311 @@ +require 'spec_helper' + +describe HangoutsChatService do + let(:chat_service) { described_class.new } + let(:webhook_url) { 'https://example.gitlab.com/' } + + describe 'Associations' do + it { is_expected.to belong_to :project } + it { is_expected.to have_one :service_hook } + end + + describe 'Validations' do + context 'when service is active' do + before do + subject.active = true + end + + it { is_expected.to validate_presence_of(:webhook) } + it_behaves_like 'issue tracker service URL attribute', :webhook + end + + context 'when service is inactive' do + before do + subject.active = false + end + + it { is_expected.not_to validate_presence_of(:webhook) } + end + end + + describe '#execute' do + let(:user) { create(:user) } + let(:project) { create(:project, :repository) } + + before do + allow(chat_service).to receive_messages( + project: project, + project_id: project.id, + service_hook: true, + webhook: webhook_url + ) + + WebMock.stub_request(:post, webhook_url) + end + + context 'with push events' do + let(:push_sample_data) do + Gitlab::DataBuilder::Push.build_sample(project, user) + end + + it 'calls Hangouts Chat API for push events' do + chat_service.execute(push_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + + it 'specifies the webhook when it is configured' do + expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object) + + chat_service.execute(push_sample_data) + end + end + + context 'with issue events' do + let(:opts) { { title: 'Awesome issue', description: 'please fix' } } + let(:issues_sample_data) do + service = Issues::CreateService.new(project, user, opts) + issue = service.execute + service.hook_data(issue, 'open') + end + + it 'calls Hangouts Chat API' do + chat_service.execute(issues_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'with merge events' do + let(:opts) do + { + title: 'Awesome merge_request', + description: 'please fix', + source_branch: 'feature', + target_branch: 'master' + } + end + + let(:merge_sample_data) do + service = MergeRequests::CreateService.new(project, user, opts) + merge_request = service.execute + service.hook_data(merge_request, 'open') + end + + before do + project.add_developer(user) + end + + it 'calls Hangouts Chat API' do + chat_service.execute(merge_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'with wiki page events' do + let(:opts) do + { + title: 'Awesome wiki_page', + content: 'Some text describing some thing or another', + format: 'md', + message: 'user created page: Awesome wiki_page' + } + end + let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) } + let(:wiki_page_sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') } + + it 'calls Hangouts Chat API' do + chat_service.execute(wiki_page_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + end + + describe 'Note events' do + let(:user) { create(:user) } + let(:project) { create(:project, :repository, creator: user) } + + before do + allow(chat_service).to receive_messages( + project: project, + project_id: project.id, + service_hook: true, + webhook: webhook_url + ) + + WebMock.stub_request(:post, webhook_url) + end + + context 'when commit comment event executed' do + let(:commit_note) do + create(:note_on_commit, author: user, + project: project, + commit_id: project.repository.commit.id, + note: 'a comment on a commit') + end + + it 'calls Hangouts Chat API for commit comment events' do + data = Gitlab::DataBuilder::Note.build(commit_note, user) + + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when merge request comment event executed' do + let(:merge_request_note) do + create(:note_on_merge_request, project: project, + note: 'merge request note') + end + + it 'calls Hangouts Chat API for merge request comment events' do + data = Gitlab::DataBuilder::Note.build(merge_request_note, user) + + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when issue comment event executed' do + let(:issue_note) do + create(:note_on_issue, project: project, note: 'issue note') + end + + it 'calls Hangouts Chat API for issue comment events' do + data = Gitlab::DataBuilder::Note.build(issue_note, user) + + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when snippet comment event executed' do + let(:snippet_note) do + create(:note_on_project_snippet, project: project, + note: 'snippet note') + end + + it 'calls Hangouts Chat API for snippet comment events' do + data = Gitlab::DataBuilder::Note.build(snippet_note, user) + + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + end + + describe 'Pipeline events' do + let(:user) { create(:user) } + let(:project) { create(:project, :repository) } + + let(:pipeline) do + create(:ci_pipeline, + project: project, status: status, + sha: project.commit.sha, ref: project.default_branch) + end + + before do + allow(chat_service).to receive_messages( + project: project, + service_hook: true, + webhook: webhook_url + ) + end + + shared_examples 'call Hangouts Chat API' do + before do + WebMock.stub_request(:post, webhook_url) + end + + it 'calls Hangouts Chat API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + + chat_service.execute(data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'with failed pipeline' do + let(:status) { 'failed' } + + it_behaves_like 'call Hangouts Chat API' + end + + context 'with succeeded pipeline' do + let(:status) { 'success' } + + context 'with default to notify_only_broken_pipelines' do + it 'does not call Hangouts Chat API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + result = chat_service.execute(data) + + expect(result).to be_falsy + end + end + + context 'with setting notify_only_broken_pipelines to false' do + before do + chat_service.notify_only_broken_pipelines = false + end + + it_behaves_like 'call Hangouts Chat API' + end + end + + context 'only notify for the default branch' do + context 'when enabled' do + let(:pipeline) do + create(:ci_pipeline, project: project, status: 'failed', ref: 'not-the-default-branch') + end + + before do + chat_service.notify_only_default_branch = true + WebMock.stub_request(:post, webhook_url) + end + + it 'does not call the Hangouts Chat API for pipeline events' do + data = Gitlab::DataBuilder::Pipeline.build(pipeline) + result = chat_service.execute(data) + + expect(result).to be_falsy + end + + it 'does not notify push events if they are not for the default branch' do + ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}test" + push_sample_data = Gitlab::DataBuilder::Push.build(project, user, nil, nil, ref, []) + + chat_service.execute(push_sample_data) + + expect(WebMock).not_to have_requested(:post, webhook_url) + end + + it 'notifies about push events for the default branch' do + push_sample_data = Gitlab::DataBuilder::Push.build_sample(project, user) + + chat_service.execute(push_sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + context 'when disabled' do + let(:pipeline) do + create(:ci_pipeline, :failed, project: project, ref: 'not-the-default-branch') + end + + before do + chat_service.notify_only_default_branch = false + end + + it_behaves_like 'call Hangouts Chat API' + end + end + end +end -- cgit v1.2.1 From d63a0a07f4ac80c1a3f801fd8087762847b4a018 Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Thu, 28 Jun 2018 22:53:37 +0900 Subject: Add documentation for Hangouts Chat integration --- .../unreleased/hangouts_chat_integration.yml | 5 +++ doc/api/services.md | 46 +++++++++++++++++++++ doc/user/project/integrations/hangouts_chat.md | 27 ++++++++++++ .../img/hangouts_chat_configuration.png | Bin 0 -> 101788 bytes doc/user/project/integrations/project_services.md | 1 + 5 files changed, 79 insertions(+) create mode 100644 changelogs/unreleased/hangouts_chat_integration.yml create mode 100644 doc/user/project/integrations/hangouts_chat.md create mode 100644 doc/user/project/integrations/img/hangouts_chat_configuration.png diff --git a/changelogs/unreleased/hangouts_chat_integration.yml b/changelogs/unreleased/hangouts_chat_integration.yml new file mode 100644 index 00000000000..f19d64e8ef9 --- /dev/null +++ b/changelogs/unreleased/hangouts_chat_integration.yml @@ -0,0 +1,5 @@ +--- +title: Add Hangouts Chat integration +merge_request: +author: Kukovskii Vladimir +type: added diff --git a/doc/api/services.md b/doc/api/services.md index aeb48ccc36c..29da1139fcc 100644 --- a/doc/api/services.md +++ b/doc/api/services.md @@ -443,6 +443,52 @@ Get Gemnasium service settings for a project. GET /projects/:id/services/gemnasium ``` +## Hangouts Chat + +Google GSuite team collaboration tool. + +### Create/Edit Hangouts Chat service + +Set Hangouts Chat service for a project. + +``` +PUT /projects/:id/services/hangouts_chat +``` + +>**Note:** Specific event parameters (e.g. `push_events` flag) were [introduced in v10.4][11435] + +Parameters: + +| Parameter | Type | Required | Description | +| --------- | ---- | -------- | ----------- | +| `webhook` | string | true | The Hangouts Chat webhook. e.g. https://chat.googleapis.com/v1/spaces... | +| `notify_only_broken_pipelines` | boolean | false | Send notifications for broken pipelines | +| `notify_only_default_branch` | boolean | false | Send notifications only for the default branch | +| `push_events` | boolean | false | Enable notifications for push events | +| `issues_events` | boolean | false | Enable notifications for issue events | +| `confidential_issues_events` | boolean | false | Enable notifications for confidential issue events | +| `merge_requests_events` | boolean | false | Enable notifications for merge request events | +| `tag_push_events` | boolean | false | Enable notifications for tag push events | +| `note_events` | boolean | false | Enable notifications for note events | +| `pipeline_events` | boolean | false | Enable notifications for pipeline events | +| `wiki_page_events` | boolean | false | Enable notifications for wiki page events | + +### Delete Hangouts Chat service + +Delete Hangouts Chat service for a project. + +``` +DELETE /projects/:id/services/hangouts_chat +``` + +### Get Hangouts Chat service settings + +Get Hangouts Chat service settings for a project. + +``` +GET /projects/:id/services/hangouts_chat +``` + ## HipChat Private group chat and IM diff --git a/doc/user/project/integrations/hangouts_chat.md b/doc/user/project/integrations/hangouts_chat.md new file mode 100644 index 00000000000..5d8869637c9 --- /dev/null +++ b/doc/user/project/integrations/hangouts_chat.md @@ -0,0 +1,27 @@ +# Hangouts Chat service + +The Hangouts Chat service sends notifications from GitLab to the room for which the webhook was created. + +## On Hangouts Chat + +1. Open the chat room in which you want to see the notifications. +1. From the chat room menu, select **Configure Webhooks**. +1. Click on **ADD WEBHOOK** and fill in the name of the bot that will post the messages. Optionally define avatar. +1. Click **SAVE** and copy the **Webhook URL** of your webhook. + +Details: https://developers.google.com/hangouts/chat/how-tos/webhooks + +## On GitLab + +When you have **Webhook URL** for your Hangouts Chat room webhook, you can setup the GitLab service. + +1. Navigate to the [Integrations page](project_services.md#accessing-the-project-services) in your project's settings, i.e. **Project > Settings > Integrations**. +1. Select the **Hangouts Chat** project service to configure it. +1. Check the **Active** checkbox to turn on the service. +1. Check the checkboxes corresponding to the GitLab events you want to receive. +1. Paste the **Webhook URL** that you copied from the Hangouts Chat configuration step. +1. Configure the remaining options and click `Save changes`. + +Your Hangouts Chat room will now start receiving GitLab event notifications as configured. + +![Hangouts Chat configuration](img/hangouts_chat_configuration.png) diff --git a/doc/user/project/integrations/img/hangouts_chat_configuration.png b/doc/user/project/integrations/img/hangouts_chat_configuration.png new file mode 100644 index 00000000000..33fadbe6547 Binary files /dev/null and b/doc/user/project/integrations/img/hangouts_chat_configuration.png differ diff --git a/doc/user/project/integrations/project_services.md b/doc/user/project/integrations/project_services.md index 8c51eb9915e..05ee1b4e6d7 100644 --- a/doc/user/project/integrations/project_services.md +++ b/doc/user/project/integrations/project_services.md @@ -35,6 +35,7 @@ Click on the service links to see further configuration instructions and details | External Wiki | Replaces the link to the internal wiki with a link to an external wiki | | Flowdock | Flowdock is a collaboration web app for technical teams | | Gemnasium _(Has been deprecated in GitLab 11.0)_ | Gemnasium monitors your project dependencies and alerts you about updates and security vulnerabilities | +| [Hangouts Chat](hangouts_chat.md) | Receive events notifications in Google Hangouts Chat | | [HipChat](hipchat.md) | Private group chat and IM | | [Irker (IRC gateway)](irker.md) | Send IRC messages, on update, to a list of recipients through an Irker gateway | | [JIRA](jira.md) | JIRA issue tracker | -- cgit v1.2.1 From 5b3a9d6bc20b2526483964187454b8c998b20360 Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Tue, 3 Jul 2018 21:01:56 +0900 Subject: Fix fetch_attachment_title method of Hangouts Chat service --- app/models/project_services/hangouts_chat_service.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/project_services/hangouts_chat_service.rb b/app/models/project_services/hangouts_chat_service.rb index 00e9b634dd1..53f11c424e3 100644 --- a/app/models/project_services/hangouts_chat_service.rb +++ b/app/models/project_services/hangouts_chat_service.rb @@ -58,8 +58,7 @@ class HangoutsChatService < ChatNotificationService end def fetch_attachment_title(attachment) - return nil if attachment[:title].nil? - return attachment[:title] if attachment[:title_link].nil? + return attachment[:title] unless attachment[:title_link] "<#{attachment[:title_link]}|#{attachment[:title]}>" end -- cgit v1.2.1 From 6cfb0a9e9463d54f7ad74d5ee79af36ba08be186 Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Tue, 3 Jul 2018 22:26:50 +0900 Subject: Rework spec for Hangouts Chat service --- .../project_services/hangouts_chat_service_spec.rb | 279 ++++++++------------- 1 file changed, 107 insertions(+), 172 deletions(-) diff --git a/spec/models/project_services/hangouts_chat_service_spec.rb b/spec/models/project_services/hangouts_chat_service_spec.rb index 73472700548..7b19edb1e51 100644 --- a/spec/models/project_services/hangouts_chat_service_spec.rb +++ b/spec/models/project_services/hangouts_chat_service_spec.rb @@ -43,37 +43,64 @@ describe HangoutsChatService do WebMock.stub_request(:post, webhook_url) end + shared_examples 'Hangouts Chat service' do + it 'calls Hangouts Chat API' do + chat_service.execute(sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + context 'with push events' do - let(:push_sample_data) do + let(:sample_data) do Gitlab::DataBuilder::Push.build_sample(project, user) end - it 'calls Hangouts Chat API for push events' do - chat_service.execute(push_sample_data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end + it_behaves_like 'Hangouts Chat service' it 'specifies the webhook when it is configured' do expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object) - chat_service.execute(push_sample_data) + chat_service.execute(sample_data) + end + + context 'with not default branch' do + let(:sample_data) do + ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}test" + Gitlab::DataBuilder::Push.build(project, user, nil, nil, ref, []) + end + + context 'when notify_only_default_branch enabled' do + before do + chat_service.notify_only_default_branch = true + end + + it 'does not call the Hangouts Chat API' do + result = chat_service.execute(sample_data) + + expect(result).to be_falsy + end + end + + context 'when notify_only_default_branch disabled' do + before do + chat_service.notify_only_default_branch = false + end + + it_behaves_like 'Hangouts Chat service' + end end end context 'with issue events' do let(:opts) { { title: 'Awesome issue', description: 'please fix' } } - let(:issues_sample_data) do + let(:sample_data) do service = Issues::CreateService.new(project, user, opts) issue = service.execute service.hook_data(issue, 'open') end - it 'calls Hangouts Chat API' do - chat_service.execute(issues_sample_data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end + it_behaves_like 'Hangouts Chat service' end context 'with merge events' do @@ -86,7 +113,7 @@ describe HangoutsChatService do } end - let(:merge_sample_data) do + let(:sample_data) do service = MergeRequests::CreateService.new(project, user, opts) merge_request = service.execute service.hook_data(merge_request, 'open') @@ -96,11 +123,7 @@ describe HangoutsChatService do project.add_developer(user) end - it 'calls Hangouts Chat API' do - chat_service.execute(merge_sample_data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end + it_behaves_like 'Hangouts Chat service' end context 'with wiki page events' do @@ -113,199 +136,111 @@ describe HangoutsChatService do } end let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) } - let(:wiki_page_sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') } - - it 'calls Hangouts Chat API' do - chat_service.execute(wiki_page_sample_data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end - end - end - - describe 'Note events' do - let(:user) { create(:user) } - let(:project) { create(:project, :repository, creator: user) } - - before do - allow(chat_service).to receive_messages( - project: project, - project_id: project.id, - service_hook: true, - webhook: webhook_url - ) + let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') } - WebMock.stub_request(:post, webhook_url) + it_behaves_like 'Hangouts Chat service' end - context 'when commit comment event executed' do - let(:commit_note) do - create(:note_on_commit, author: user, - project: project, - commit_id: project.repository.commit.id, - note: 'a comment on a commit') - end - - it 'calls Hangouts Chat API for commit comment events' do - data = Gitlab::DataBuilder::Note.build(commit_note, user) + context 'with note events' do + let(:sample_data) { Gitlab::DataBuilder::Note.build(note, user) } - chat_service.execute(data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end - end + context 'with commit comment' do + let(:note) do + create(:note_on_commit, author: user, + project: project, + commit_id: project.repository.commit.id, + note: 'a comment on a commit') + end - context 'when merge request comment event executed' do - let(:merge_request_note) do - create(:note_on_merge_request, project: project, - note: 'merge request note') + it_behaves_like 'Hangouts Chat service' end - it 'calls Hangouts Chat API for merge request comment events' do - data = Gitlab::DataBuilder::Note.build(merge_request_note, user) - - chat_service.execute(data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end - end + context 'with merge request comment' do + let(:note) do + create(:note_on_merge_request, project: project, + note: 'merge request note') + end - context 'when issue comment event executed' do - let(:issue_note) do - create(:note_on_issue, project: project, note: 'issue note') + it_behaves_like 'Hangouts Chat service' end - it 'calls Hangouts Chat API for issue comment events' do - data = Gitlab::DataBuilder::Note.build(issue_note, user) - - chat_service.execute(data) - - expect(WebMock).to have_requested(:post, webhook_url).once - end - end + context 'with issue comment' do + let(:note) do + create(:note_on_issue, project: project, note: 'issue note') + end - context 'when snippet comment event executed' do - let(:snippet_note) do - create(:note_on_project_snippet, project: project, - note: 'snippet note') + it_behaves_like 'Hangouts Chat service' end - it 'calls Hangouts Chat API for snippet comment events' do - data = Gitlab::DataBuilder::Note.build(snippet_note, user) - - chat_service.execute(data) + context 'wiht snippet comment' do + let(:note) do + create(:note_on_project_snippet, project: project, + note: 'snippet note') + end - expect(WebMock).to have_requested(:post, webhook_url).once + it_behaves_like 'Hangouts Chat service' end end - end - - describe 'Pipeline events' do - let(:user) { create(:user) } - let(:project) { create(:project, :repository) } - - let(:pipeline) do - create(:ci_pipeline, - project: project, status: status, - sha: project.commit.sha, ref: project.default_branch) - end - - before do - allow(chat_service).to receive_messages( - project: project, - service_hook: true, - webhook: webhook_url - ) - end - shared_examples 'call Hangouts Chat API' do - before do - WebMock.stub_request(:post, webhook_url) + context 'with pipeline events' do + let(:pipeline) do + create(:ci_pipeline, + project: project, status: status, + sha: project.commit.sha, ref: project.default_branch) end + let(:sample_data) { Gitlab::DataBuilder::Pipeline.build(pipeline) } - it 'calls Hangouts Chat API for pipeline events' do - data = Gitlab::DataBuilder::Pipeline.build(pipeline) + context 'with failed pipeline' do + let(:status) { 'failed' } - chat_service.execute(data) - - expect(WebMock).to have_requested(:post, webhook_url).once + it_behaves_like 'Hangouts Chat service' end - end - - context 'with failed pipeline' do - let(:status) { 'failed' } - it_behaves_like 'call Hangouts Chat API' - end + context 'with succeeded pipeline' do + let(:status) { 'success' } - context 'with succeeded pipeline' do - let(:status) { 'success' } + context 'with default notify_only_broken_pipelines' do + it 'does not call Hangouts Chat API' do + result = chat_service.execute(sample_data) - context 'with default to notify_only_broken_pipelines' do - it 'does not call Hangouts Chat API for pipeline events' do - data = Gitlab::DataBuilder::Pipeline.build(pipeline) - result = chat_service.execute(data) - - expect(result).to be_falsy + expect(result).to be_falsy + end end - end - context 'with setting notify_only_broken_pipelines to false' do - before do - chat_service.notify_only_broken_pipelines = false - end + context 'when notify_only_broken_pipelines is false' do + before do + chat_service.notify_only_broken_pipelines = false + end - it_behaves_like 'call Hangouts Chat API' + it_behaves_like 'Hangouts Chat service' + end end - end - context 'only notify for the default branch' do - context 'when enabled' do + context 'with not default branch' do let(:pipeline) do create(:ci_pipeline, project: project, status: 'failed', ref: 'not-the-default-branch') end - before do - chat_service.notify_only_default_branch = true - WebMock.stub_request(:post, webhook_url) - end - - it 'does not call the Hangouts Chat API for pipeline events' do - data = Gitlab::DataBuilder::Pipeline.build(pipeline) - result = chat_service.execute(data) - - expect(result).to be_falsy - end + context 'when notify_only_default_branch enabled' do + before do + chat_service.notify_only_default_branch = true + end - it 'does not notify push events if they are not for the default branch' do - ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}test" - push_sample_data = Gitlab::DataBuilder::Push.build(project, user, nil, nil, ref, []) + it 'does not call the Hangouts Chat API' do + result = chat_service.execute(sample_data) - chat_service.execute(push_sample_data) - - expect(WebMock).not_to have_requested(:post, webhook_url) + expect(result).to be_falsy + end end - it 'notifies about push events for the default branch' do - push_sample_data = Gitlab::DataBuilder::Push.build_sample(project, user) - - chat_service.execute(push_sample_data) + context 'when notify_only_default_branch disabled' do + before do + chat_service.notify_only_default_branch = false + end - expect(WebMock).to have_requested(:post, webhook_url).once + it_behaves_like 'Hangouts Chat service' end end - - context 'when disabled' do - let(:pipeline) do - create(:ci_pipeline, :failed, project: project, ref: 'not-the-default-branch') - end - - before do - chat_service.notify_only_default_branch = false - end - - it_behaves_like 'call Hangouts Chat API' - end end end end -- cgit v1.2.1 From d64196ddb3336bd435c5756407cabc162f9acc6c Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Sat, 14 Jul 2018 19:19:04 +0900 Subject: Fix couple of moments in HangoutsChatService and its spec --- .../project_services/hangouts_chat_service.rb | 12 ++++--- .../unreleased/hangouts_chat_integration.yml | 2 +- .../project_services/hangouts_chat_service_spec.rb | 38 ++++++++++++---------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/models/project_services/hangouts_chat_service.rb b/app/models/project_services/hangouts_chat_service.rb index 53f11c424e3..a8512c5f57c 100644 --- a/app/models/project_services/hangouts_chat_service.rb +++ b/app/models/project_services/hangouts_chat_service.rb @@ -44,20 +44,22 @@ class HangoutsChatService < ChatNotificationService private def notify(message, opts) - simple_text = compose_simple_message(message) + simple_text = parse_simple_text_message(message) HangoutsChat::Sender.new(webhook).simple(simple_text) end - def compose_simple_message(message) + def parse_simple_text_message(message) header = message.pretext return header if message.attachments.empty? - title = fetch_attachment_title(message.attachments.first) - body = message.attachments.first[:text] + attachment = message.attachments.first + title = format_attachment_title(attachment) + body = attachment[:text] + [header, title, body].compact.join("\n") end - def fetch_attachment_title(attachment) + def format_attachment_title(attachment) return attachment[:title] unless attachment[:title_link] "<#{attachment[:title_link]}|#{attachment[:title]}>" diff --git a/changelogs/unreleased/hangouts_chat_integration.yml b/changelogs/unreleased/hangouts_chat_integration.yml index f19d64e8ef9..bf3484a6d02 100644 --- a/changelogs/unreleased/hangouts_chat_integration.yml +++ b/changelogs/unreleased/hangouts_chat_integration.yml @@ -1,5 +1,5 @@ --- title: Add Hangouts Chat integration -merge_request: +merge_request: 20290 author: Kukovskii Vladimir type: added diff --git a/spec/models/project_services/hangouts_chat_service_spec.rb b/spec/models/project_services/hangouts_chat_service_spec.rb index 7b19edb1e51..8a1c4e33204 100644 --- a/spec/models/project_services/hangouts_chat_service_spec.rb +++ b/spec/models/project_services/hangouts_chat_service_spec.rb @@ -1,9 +1,6 @@ require 'spec_helper' describe HangoutsChatService do - let(:chat_service) { described_class.new } - let(:webhook_url) { 'https://example.gitlab.com/' } - describe 'Associations' do it { is_expected.to belong_to :project } it { is_expected.to have_one :service_hook } @@ -31,9 +28,10 @@ describe HangoutsChatService do describe '#execute' do let(:user) { create(:user) } let(:project) { create(:project, :repository) } + let(:webhook_url) { 'https://example.gitlab.com/' } before do - allow(chat_service).to receive_messages( + allow(subject).to receive_messages( project: project, project_id: project.id, service_hook: true, @@ -45,7 +43,7 @@ describe HangoutsChatService do shared_examples 'Hangouts Chat service' do it 'calls Hangouts Chat API' do - chat_service.execute(sample_data) + subject.execute(sample_data) expect(WebMock).to have_requested(:post, webhook_url).once end @@ -61,22 +59,28 @@ describe HangoutsChatService do it 'specifies the webhook when it is configured' do expect(HangoutsChat::Sender).to receive(:new).with(webhook_url).and_return(double(:hangouts_chat_service).as_null_object) - chat_service.execute(sample_data) + subject.execute(sample_data) + end + + it 'sends the simple text message to the Hangouts Chat API' do + subject.execute(sample_data) + + expect(WebMock).to have_requested(:post, webhook_url).once + .with { |req| req.body =~ /\A{"text":.+}\Z/ } end context 'with not default branch' do let(:sample_data) do - ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}test" - Gitlab::DataBuilder::Push.build(project, user, nil, nil, ref, []) + Gitlab::DataBuilder::Push.build(project, user, nil, nil, 'not-the-default-branch') end context 'when notify_only_default_branch enabled' do before do - chat_service.notify_only_default_branch = true + subject.notify_only_default_branch = true end it 'does not call the Hangouts Chat API' do - result = chat_service.execute(sample_data) + result = subject.execute(sample_data) expect(result).to be_falsy end @@ -84,7 +88,7 @@ describe HangoutsChatService do context 'when notify_only_default_branch disabled' do before do - chat_service.notify_only_default_branch = false + subject.notify_only_default_branch = false end it_behaves_like 'Hangouts Chat service' @@ -172,7 +176,7 @@ describe HangoutsChatService do it_behaves_like 'Hangouts Chat service' end - context 'wiht snippet comment' do + context 'with snippet comment' do let(:note) do create(:note_on_project_snippet, project: project, note: 'snippet note') @@ -201,7 +205,7 @@ describe HangoutsChatService do context 'with default notify_only_broken_pipelines' do it 'does not call Hangouts Chat API' do - result = chat_service.execute(sample_data) + result = subject.execute(sample_data) expect(result).to be_falsy end @@ -209,7 +213,7 @@ describe HangoutsChatService do context 'when notify_only_broken_pipelines is false' do before do - chat_service.notify_only_broken_pipelines = false + subject.notify_only_broken_pipelines = false end it_behaves_like 'Hangouts Chat service' @@ -223,11 +227,11 @@ describe HangoutsChatService do context 'when notify_only_default_branch enabled' do before do - chat_service.notify_only_default_branch = true + subject.notify_only_default_branch = true end it 'does not call the Hangouts Chat API' do - result = chat_service.execute(sample_data) + result = subject.execute(sample_data) expect(result).to be_falsy end @@ -235,7 +239,7 @@ describe HangoutsChatService do context 'when notify_only_default_branch disabled' do before do - chat_service.notify_only_default_branch = false + subject.notify_only_default_branch = false end it_behaves_like 'Hangouts Chat service' -- cgit v1.2.1 From b38a3485f0f6f08ce43bdf372a436ae5d73c329c Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Wed, 18 Jul 2018 20:14:55 +0900 Subject: Fix Hangouts Chat service spec shared example --- spec/models/project_services/hangouts_chat_service_spec.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spec/models/project_services/hangouts_chat_service_spec.rb b/spec/models/project_services/hangouts_chat_service_spec.rb index 8a1c4e33204..cfa55188a64 100644 --- a/spec/models/project_services/hangouts_chat_service_spec.rb +++ b/spec/models/project_services/hangouts_chat_service_spec.rb @@ -45,7 +45,10 @@ describe HangoutsChatService do it 'calls Hangouts Chat API' do subject.execute(sample_data) - expect(WebMock).to have_requested(:post, webhook_url).once + expect(WebMock) + .to have_requested(:post, webhook_url) + .with { |req| req.body =~ /\A{"text":.+}\Z/ } + .once end end @@ -62,13 +65,6 @@ describe HangoutsChatService do subject.execute(sample_data) end - it 'sends the simple text message to the Hangouts Chat API' do - subject.execute(sample_data) - - expect(WebMock).to have_requested(:post, webhook_url).once - .with { |req| req.body =~ /\A{"text":.+}\Z/ } - end - context 'with not default branch' do let(:sample_data) do Gitlab::DataBuilder::Push.build(project, user, nil, nil, 'not-the-default-branch') -- cgit v1.2.1 From 1ce57a7fba560daa66eee006b64bb9e77f7c51ac Mon Sep 17 00:00:00 2001 From: Kukovskii Vladimir Date: Sun, 22 Jul 2018 20:17:35 +0900 Subject: Fix some moments in documentation --- doc/api/services.md | 2 ++ doc/user/project/integrations/hangouts_chat.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api/services.md b/doc/api/services.md index 29da1139fcc..efa173180bb 100644 --- a/doc/api/services.md +++ b/doc/api/services.md @@ -447,6 +447,8 @@ GET /projects/:id/services/gemnasium Google GSuite team collaboration tool. +>**Note:** This service was [introduced in v11.2](https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/20290) + ### Create/Edit Hangouts Chat service Set Hangouts Chat service for a project. diff --git a/doc/user/project/integrations/hangouts_chat.md b/doc/user/project/integrations/hangouts_chat.md index 5d8869637c9..6ab44420a10 100644 --- a/doc/user/project/integrations/hangouts_chat.md +++ b/doc/user/project/integrations/hangouts_chat.md @@ -9,11 +9,11 @@ The Hangouts Chat service sends notifications from GitLab to the room for which 1. Click on **ADD WEBHOOK** and fill in the name of the bot that will post the messages. Optionally define avatar. 1. Click **SAVE** and copy the **Webhook URL** of your webhook. -Details: https://developers.google.com/hangouts/chat/how-tos/webhooks +See also [the Hangouts Chat documentation for configuring incoming webhooks](https://developers.google.com/hangouts/chat/how-tos/webhooks) ## On GitLab -When you have **Webhook URL** for your Hangouts Chat room webhook, you can setup the GitLab service. +When you have the **Webhook URL** for your Hangouts Chat room webhook, you can setup the GitLab service. 1. Navigate to the [Integrations page](project_services.md#accessing-the-project-services) in your project's settings, i.e. **Project > Settings > Integrations**. 1. Select the **Hangouts Chat** project service to configure it. -- cgit v1.2.1