summaryrefslogtreecommitdiff
path: root/lib/flowdock/git.rb
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2018-10-15 17:03:14 +0100
committerNick Thomas <nick@gitlab.com>2018-10-16 11:54:55 +0100
commit04aaf71932646efd99f2abd74fc59e3129fcbe1d (patch)
treea6de51762eff3787ed262f7192b0408e80ee8b44 /lib/flowdock/git.rb
parente347170cc59dfa1e48de451f7c48ccb65d3e581a (diff)
downloadgitlab-ce-04aaf71932646efd99f2abd74fc59e3129fcbe1d.tar.gz
Inline the gitlab-flowdock-git-hooks gem
This allows us to avoid one transitive dependency on gitlab-grit. The aim is to remove all transitive dependencies.
Diffstat (limited to 'lib/flowdock/git.rb')
-rw-r--r--lib/flowdock/git.rb96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/flowdock/git.rb b/lib/flowdock/git.rb
new file mode 100644
index 00000000000..43e729c27d7
--- /dev/null
+++ b/lib/flowdock/git.rb
@@ -0,0 +1,96 @@
+
+require "multi_json"
+require "cgi"
+require "flowdock"
+require "flowdock/git/builder"
+
+module Flowdock
+ class Git
+ class TokenError < StandardError; end
+
+ class << self
+ def post(ref, from, to, options = {})
+ Git.new(ref, from, to, options).post
+ end
+
+ def background_post(ref, from, to, options = {})
+ Git.new(ref, from, to, options).background_post
+ end
+ end
+
+ def initialize(ref, from, to, options = {})
+ @ref = ref
+ @from = from
+ @to = to
+ @options = options
+ @token = options[:token] || config["flowdock.token"] || raise(TokenError.new("Flowdock API token not found"))
+ @commit_url = options[:commit_url] || config["flowdock.commit-url-pattern"] || nil
+ @diff_url = options[:diff_url] || config["flowdock.diff-url-pattern"] || nil
+ @repo_url = options[:repo_url] || config["flowdock.repository-url"] || nil
+ @repo_name = options[:repo_name] || config["flowdock.repository-name"] || nil
+ @permanent_refs = options[:permanent_refs] ||
+ (config["flowdock.permanent-references"] || "refs/heads/master")
+ .split(",")
+ .map(&:strip)
+ .map {|exp| Regexp.new(exp) }
+ end
+
+ # Send git push notification to Flowdock
+ def post
+ messages.each do |message|
+ Flowdock::Client.new(flow_token: @token).post_to_thread(message)
+ end
+ end
+
+ # Create and post notification in background process. Avoid blocking the push notification.
+ def background_post
+ pid = Process.fork
+ if pid.nil?
+ Grit::Git.with_timeout(600) do
+ post
+ end
+ else
+ Process.detach(pid) # Parent
+ end
+ end
+
+ def repo
+ @repo ||= Grit::Repo.new(
+ @options[:repo] || Dir.pwd,
+ is_bare: @options[:is_bare] || false
+ )
+ end
+
+ private
+
+ def messages
+ Git::Builder.new(repo: @repo,
+ 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
+ if @options[:tags]
+ @options[:tags]
+ else
+ config["flowdock.tags"].to_s.split(",").map(&:strip)
+ end.map do |t|
+ CGI.escape(t)
+ end
+ end
+
+ def config
+ @config ||= Grit::Config.new(repo)
+ end
+ end
+end