2018-10-15 12:25:35 -04:00
|
|
|
# frozen_string_literal: true
|
2018-10-15 12:31:32 -04:00
|
|
|
require 'flowdock'
|
|
|
|
require 'flowdock/git/builder'
|
2018-10-15 12:03:14 -04:00
|
|
|
|
|
|
|
module Flowdock
|
|
|
|
class Git
|
2018-10-15 12:25:35 -04:00
|
|
|
TokenError = Class.new(StandardError)
|
2018-10-15 12:03:14 -04:00
|
|
|
|
2018-10-15 12:31:32 -04:00
|
|
|
DEFAULT_PERMANENT_REFS = [
|
|
|
|
Regexp.new('refs/heads/master')
|
|
|
|
].freeze
|
|
|
|
|
2018-10-15 12:03:14 -04:00
|
|
|
class << self
|
|
|
|
def post(ref, from, to, options = {})
|
|
|
|
Git.new(ref, from, to, options).post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(ref, from, to, options = {})
|
2021-05-04 11:10:36 -04:00
|
|
|
raise TokenError, "Flowdock API token not found" unless options[:token]
|
2018-10-15 12:31:32 -04:00
|
|
|
|
2018-10-15 12:03:14 -04:00
|
|
|
@ref = ref
|
|
|
|
@from = from
|
|
|
|
@to = to
|
|
|
|
@options = options
|
2018-10-15 12:31:32 -04:00
|
|
|
@token = options[:token]
|
|
|
|
@commit_url = options[:commit_url]
|
|
|
|
@diff_url = options[:diff_url]
|
|
|
|
@repo_url = options[:repo_url]
|
|
|
|
@repo_name = options[:repo_name]
|
|
|
|
@permanent_refs = options.fetch(:permanent_refs, DEFAULT_PERMANENT_REFS)
|
2018-10-15 12:03:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Send git push notification to Flowdock
|
|
|
|
def post
|
|
|
|
messages.each do |message|
|
2021-05-24 08:10:31 -04:00
|
|
|
::Flowdock::Client.new(flow_token: @token).post_to_thread(message)
|
2018-10-15 12:03:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo
|
2018-10-15 12:31:32 -04:00
|
|
|
@options[:repo]
|
2018-10-15 12:03:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def messages
|
2018-10-15 12:31:32 -04:00
|
|
|
Git::Builder.new(repo: repo,
|
2018-10-15 12:03:14 -04:00
|
|
|
ref: @ref,
|
|
|
|
before: @from,
|
|
|
|
after: @to,
|
|
|
|
commit_url: @commit_url,
|
|
|
|
branch_url: @branch_url,
|
|
|
|
diff_url: @diff_url,
|
|
|
|
repo_url: @repo_url,
|
|
|
|
repo_name: @repo_name,
|
|
|
|
permanent_refs: @permanent_refs,
|
|
|
|
tags: tags
|
|
|
|
).to_hashes
|
|
|
|
end
|
|
|
|
|
|
|
|
# Flowdock tags attached to the push notification
|
|
|
|
def tags
|
2018-10-15 12:31:32 -04:00
|
|
|
Array(@options[:tags]).map { |tag| CGI.escape(tag) }
|
2018-10-15 12:03:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|