summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2018-10-17 13:17:20 +0000
committerRémy Coutable <remy@rymai.me>2018-10-17 13:17:20 +0000
commit1696a3e8c36a0336e62ec990fd8b4e0517e4c93c (patch)
tree7aa2ebfb39874599ab25cf87f8a26b37f5a62e7a /lib
parent071b39a22c8a3efc4e3a0531c71e1e8161b78f47 (diff)
parentc5bff77ea4a6170a3dc1966254feac0ca1836eaa (diff)
downloadgitlab-ce-1696a3e8c36a0336e62ec990fd8b4e0517e4c93c.tar.gz
Merge branch '44361-remove-gitlab-grit' into 'master'
Remove gitlab-grit as a transitive dependency of GitLab Closes #44361 See merge request gitlab-org/gitlab-ce!22373
Diffstat (limited to 'lib')
-rw-r--r--lib/flowdock/git.rb67
-rw-r--r--lib/flowdock/git/builder.rb145
-rw-r--r--lib/gitlab/git/wiki.rb53
3 files changed, 250 insertions, 15 deletions
diff --git a/lib/flowdock/git.rb b/lib/flowdock/git.rb
new file mode 100644
index 00000000000..f165ecfc1fa
--- /dev/null
+++ b/lib/flowdock/git.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal: true
+require 'flowdock'
+require 'flowdock/git/builder'
+
+module Flowdock
+ class Git
+ TokenError = Class.new(StandardError)
+
+ DEFAULT_PERMANENT_REFS = [
+ Regexp.new('refs/heads/master')
+ ].freeze
+
+ class << self
+ def post(ref, from, to, options = {})
+ Git.new(ref, from, to, options).post
+ end
+ end
+
+ def initialize(ref, from, to, options = {})
+ raise TokenError.new("Flowdock API token not found") unless options[:token]
+
+ @ref = ref
+ @from = from
+ @to = to
+ @options = options
+ @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)
+ 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
+
+ def repo
+ @options[:repo]
+ 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
+ Array(@options[:tags]).map { |tag| CGI.escape(tag) }
+ end
+ end
+end
diff --git a/lib/flowdock/git/builder.rb b/lib/flowdock/git/builder.rb
new file mode 100644
index 00000000000..6f4428d1f42
--- /dev/null
+++ b/lib/flowdock/git/builder.rb
@@ -0,0 +1,145 @@
+# frozen_string_literal: true
+module Flowdock
+ class Git
+ class Commit
+ def initialize(external_thread_id, thread, tags, commit)
+ @commit = commit
+ @external_thread_id = external_thread_id
+ @thread = thread
+ @tags = tags
+ end
+
+ def to_hash
+ hash = {
+ external_thread_id: @external_thread_id,
+ event: "activity",
+ author: {
+ name: @commit[:author][:name],
+ email: @commit[:author][:email]
+ },
+ title: title,
+ thread: @thread,
+ body: body
+ }
+ hash[:tags] = @tags if @tags
+ encode(hash)
+ end
+
+ private
+
+ def encode(hash)
+ return hash unless "".respond_to?(:encode)
+
+ encode_as_utf8(hash)
+ end
+
+ # This only works on Ruby 1.9
+ def encode_as_utf8(obj)
+ if obj.is_a? Hash
+ obj.each_pair do |key, val|
+ encode_as_utf8(val)
+ end
+ elsif obj.is_a?(Array)
+ obj.each do |val|
+ encode_as_utf8(val)
+ end
+ elsif obj.is_a?(String) && obj.encoding != Encoding::UTF_8
+ unless obj.force_encoding("UTF-8").valid_encoding?
+ obj.force_encoding("ISO-8859-1").encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
+ end
+ end
+ end
+
+ def body
+ content = @commit[:message][first_line.size..-1]
+ content.strip! if content
+ "<pre>#{content}</pre>" unless content.empty?
+ end
+
+ def first_line
+ @first_line ||= (@commit[:message].split("\n")[0] || @commit[:message])
+ end
+
+ def title
+ commit_id = @commit[:id][0, 7]
+ if @commit[:url]
+ "<a href=\"#{@commit[:url]}\">#{commit_id}</a> #{message_title}"
+ else
+ "#{commit_id} #{message_title}"
+ end
+ end
+
+ def message_title
+ CGI.escape_html(first_line.strip)
+ end
+ end
+
+ # Class used to build Git payload
+ class Builder
+ include ::Gitlab::Utils::StrongMemoize
+
+ def initialize(opts)
+ @repo = opts[:repo]
+ @ref = opts[:ref]
+ @before = opts[:before]
+ @after = opts[:after]
+ @opts = opts
+ end
+
+ def commits
+ @repo.commits_between(@before, @after).map do |commit|
+ {
+ url: @opts[:commit_url] ? @opts[:commit_url] % [commit.sha] : nil,
+ id: commit.sha,
+ message: commit.message,
+ author: {
+ name: commit.author_name,
+ email: commit.author_email
+ }
+ }
+ end
+ end
+
+ def ref_name
+ @ref.to_s.sub(%r{\Arefs/(heads|tags)/}, '')
+ end
+
+ def to_hashes
+ commits.map do |commit|
+ Commit.new(external_thread_id, thread, @opts[:tags], commit).to_hash
+ end
+ end
+
+ private
+
+ def thread
+ @thread ||= {
+ title: thread_title,
+ external_url: @opts[:repo_url]
+ }
+ end
+
+ def permanent?
+ strong_memoize(:permanent) do
+ @opts[:permanent_refs].any? { |regex| regex.match(@ref) }
+ end
+ end
+
+ def thread_title
+ action = "updated" if permanent?
+ type = @ref =~ %r(^refs/heads/) ? "branch" : "tag"
+
+ [@opts[:repo_name], type, ref_name, action].compact.join(" ")
+ end
+
+ def external_thread_id
+ @external_thread_id ||=
+ if permanent?
+ SecureRandom.hex
+ else
+ @ref
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/git/wiki.rb b/lib/gitlab/git/wiki.rb
index 072019dfb0a..7fe56979d5c 100644
--- a/lib/gitlab/git/wiki.rb
+++ b/lib/gitlab/git/wiki.rb
@@ -1,8 +1,3 @@
-# We only need Gollum::Page so let's not load all of gollum-lib.
-require 'gollum-lib/pagination'
-require 'gollum-lib/wiki'
-require 'gollum-lib/page'
-
module Gitlab
module Git
class Wiki
@@ -16,7 +11,43 @@ module Gitlab
{ user_id: user_id, username: username, name: name, email: email, message: message }
end
end
- PageBlob = Struct.new(:name)
+
+ # GollumSlug inlines just enough knowledge from Gollum::Page to generate a
+ # slug, which is used when previewing pages that haven't been persisted
+ class GollumSlug
+ class << self
+ def cname(name, char_white_sub = '-', char_other_sub = '-')
+ if name.respond_to?(:gsub)
+ name.gsub(/\s/, char_white_sub).gsub(/[<>+]/, char_other_sub)
+ else
+ ''
+ end
+ end
+
+ def format_to_ext(format)
+ format == :markdown ? "md" : format.to_s
+ end
+
+ def canonicalize_filename(filename)
+ ::File.basename(filename, ::File.extname(filename)).tr('-', ' ')
+ end
+
+ def generate(title, format)
+ ext = format_to_ext(format.to_sym)
+ name = cname(title) + '.' + ext
+ canonical_name = canonicalize_filename(name)
+
+ path =
+ if name.include?('/')
+ name.sub(%r{/[^/]+$}, '/')
+ else
+ ''
+ end
+
+ path + cname(canonical_name, '-', '-')
+ end
+ end
+ end
attr_reader :repository
@@ -90,15 +121,7 @@ module Gitlab
end
def preview_slug(title, format)
- # Adapted from gollum gem (Gollum::Wiki#preview_page) to avoid
- # using Rugged through a Gollum::Wiki instance
- page_class = Gollum::Page
- page = page_class.new(nil)
- ext = page_class.format_to_ext(format.to_sym)
- name = page_class.cname(title) + '.' + ext
- blob = PageBlob.new(name)
- page.populate(blob)
- page.url_path
+ GollumSlug.generate(title, format)
end
def page_formatted_data(title:, dir: nil, version: nil)