File: command_implementation.rb

package info (click to toggle)
ruby-gitlab-markup 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 504 kB
  • sloc: ruby: 369; python: 137; sh: 16; makefile: 15
file content (73 lines) | stat: -rw-r--r-- 2,048 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require "open3"

require "github/markup/implementation"

module GitHub
  module Markup
    class CommandError < RuntimeError
    end

    class CommandImplementation < Implementation
      DEFAULT_GITLAB_MARKUP_TIMEOUT = '10'.freeze

      attr_reader :command, :block, :name

      def initialize(regexp, command, name, &block)
        super regexp
        @command = command.to_s
        @block = block
        @name = name
      end

      def render(content)
        rendered = execute(command, content)
        rendered = rendered.to_s.empty? ? content : rendered
        call_block(rendered, content)
      end

      private

      def call_block(rendered, content)
        if block && block.arity == 2
          block.call(rendered, content)
        elsif block
          block.call(rendered)
        else
          rendered
        end
      end

      def timeout_in_seconds
        ENV.fetch('GITLAB_MARKUP_TIMEOUT', DEFAULT_GITLAB_MARKUP_TIMEOUT).to_i
      end

      def prepend_command_timeout_prefix(command)
        timeout_command_prefix = "timeout -s KILL #{timeout_in_seconds}"

        # Preserve existing support for command being either a String or an Array
        if command.is_a?(String)
          "#{timeout_command_prefix} #{command}"
        else
          timeout_command_prefix.split(' ') + command
        end
      end

      def execute(command, target)
        command_with_timeout_prefix = prepend_command_timeout_prefix(command)
        stdout_str, stderr_str, status = Open3.capture3(*command_with_timeout_prefix, stdin_data: target)
        if status.success?
          sanitize(stdout_str, target.encoding)
        elsif status.termsig == Signal.list['KILL']
          raise TimeoutError.new("Command was killed, probably due to exceeding GITLAB_MARKUP_TIMEOUT limit of #{timeout_in_seconds} seconds")
        else
          raise CommandError.new(stderr_str.strip)
        end
      end

      def sanitize(input, encoding)
        input.gsub("\r", '').force_encoding(encoding)
      end

    end
  end
end