diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2015-01-02 14:57:08 +0100 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2015-01-12 19:53:14 +0100 |
commit | 1a1f4eded6e9c65435f76aeb1e872664895c2d02 (patch) | |
tree | b944d30d1301caa38c40f9a78017817a658581b3 /app/models/project_services | |
parent | a40989d27ea8a1c5d23dfd5b82f25e09527b2c80 (diff) | |
download | gitlab-ci-1a1f4eded6e9c65435f76aeb1e872664895c2d02.tar.gz |
Added Service and SlackService specs
Diffstat (limited to 'app/models/project_services')
-rw-r--r-- | app/models/project_services/slack_message.rb | 50 | ||||
-rw-r--r-- | app/models/project_services/slack_service.rb | 29 |
2 files changed, 27 insertions, 52 deletions
diff --git a/app/models/project_services/slack_message.rb b/app/models/project_services/slack_message.rb index 82715d1..a37041a 100644 --- a/app/models/project_services/slack_message.rb +++ b/app/models/project_services/slack_message.rb @@ -1,11 +1,6 @@ require 'slack-notifier' class SlackMessage - # default_url_options[:host] = GitlabCi.config.gitlab_ci.host - # default_url_options[:protocol] = GitlabCi.config.gitlab_ci.protocol - # default_url_options[:port] = GitlabCi.config.gitlab_ci.port if GitlabCi.config.gitlab_ci_on_non_standard_port? - # default_url_options[:script_name] = GitlabCi.config.gitlab_ci.relative_url_root - def initialize(commit) @commit = commit end @@ -29,16 +24,16 @@ class SlackMessage commit.builds_without_retry.each do |build| next unless build.failed? fields << { - title: build.job_name, - value: "Build <#{build_log_link(build)}|\##{build.id}> failed in #{build.duration.to_i} second(s)." + title: build.job_name, + value: "Build <#{RoutesHelper.project_build_url(project, build)}|\##{build.id}> failed in #{build.duration.to_i} second(s)." } end end [{ - text: attachment_message, - color: attachment_color, - fields: fields + text: attachment_message, + color: attachment_color, + fields: fields }] end @@ -47,12 +42,12 @@ class SlackMessage attr_reader :commit def attachment_message - out = "<#{project_url}|#{project_name}>: " + out = "<#{RoutesHelper.project_url(project)}|#{project_name}>: " if commit.matrix? - out << "Commit <#{commit_url}|\##{commit.id}> " + out << "Commit <#{RoutesHelper.project_commit_url(project, commit)}|\##{commit.id}> " else build = commit.builds_without_retry.first - out << "Build <#{build_log_link(build)}|\##{build.id}> " + out << "Build <#{RoutesHelper.project_build_url(project, build)}|\##{build.id}> " end out << "(<#{commit_sha_link}|#{commit.short_sha}>) " out << "of <#{commit_ref_link}|#{commit.ref}> " @@ -104,33 +99,4 @@ class SlackMessage 'failed' end end - - def project_url - Rails.application.routes.url_helpers.project_url( - project, - url_helper_options - ) - end - - def commit_url - Rails.application.routes.url_helpers.project_commit_url( - project, commit, - url_helper_options - ) - end - - def build_log_link(build) - Rails.application.routes.url_helpers.project_build_url( - project, build, - url_helper_options - ) - end - - def url_helper_options - { - host: Settings.gitlab_ci['host'], - protocol: Settings.gitlab_ci['https'] ? "https" : "http", - port: Settings.gitlab_ci['port'] - } - end end diff --git a/app/models/project_services/slack_service.rb b/app/models/project_services/slack_service.rb index 3820df1..e187a9f 100644 --- a/app/models/project_services/slack_service.rb +++ b/app/models/project_services/slack_service.rb @@ -19,6 +19,8 @@ class SlackService < Service default_value_for :notify_only_broken_builds, true + NOTIFY_ONLY_BROKEN_BUILDS_ENABLED = '1' + def title 'Slack' end @@ -37,20 +39,27 @@ class SlackService < Service def fields [ - {type: 'text', name: 'webhook', label: 'Webhook URL', placeholder: ''}, - {type: 'checkbox', name: 'notify_only_broken_builds', label: 'Notify only broken builds'} + {type: 'text', name: 'webhook', label: 'Webhook URL', placeholder: ''}, + {type: 'checkbox', name: 'notify_only_broken_builds', label: 'Notify only broken builds'} ] end def notify_only_broken_builds? - notify_only_broken_builds == '1' + notify_only_broken_builds == NOTIFY_ONLY_BROKEN_BUILDS_ENABLED end def can_test? # slack notification is useful only for builds either successful or failed - builds = project.builds - return builds.failed.any? if notify_only_broken_builds? - builds.failed.any? || builds.success.any? + project.commits.order(id: desc).any? do |commit| + case commit.status.to_sym + when :failed + true + when :success + !notify_only_broken_builds? + else + false + end + end end def execute(build) @@ -68,9 +77,9 @@ class SlackService < Service message = SlackMessage.new(commit) options = default_options.merge( - color: message.color, - fallback: message.fallback, - attachments: message.attachments + color: message.color, + fallback: message.fallback, + attachments: message.attachments ) SlackNotifierWorker.perform_async(webhook, message.pretext, options) end @@ -79,7 +88,7 @@ class SlackService < Service def default_options { - username: 'GitLab CI' + username: 'GitLab CI' } end end |