summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab_access.rb43
-rw-r--r--lib/gitlab_custom_hook.rb98
-rw-r--r--lib/gitlab_net.rb1
-rw-r--r--lib/gitlab_post_receive.rb125
4 files changed, 0 insertions, 267 deletions
diff --git a/lib/gitlab_access.rb b/lib/gitlab_access.rb
deleted file mode 100644
index 72abd14..0000000
--- a/lib/gitlab_access.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-require_relative 'gitlab_init'
-require_relative 'gitlab_net'
-require_relative 'gitlab_access_status'
-require_relative 'gitlab_metrics'
-require_relative 'object_dirs_helper'
-require 'json'
-
-class GitlabAccess
- class AccessDeniedError < StandardError; end
-
- attr_reader :config, :gl_repository, :repo_path, :changes, :protocol
-
- def initialize(gl_repository, repo_path, actor, changes, protocol)
- @config = GitlabConfig.new
- @gl_repository = gl_repository
- @repo_path = repo_path.strip
- @actor = actor
- @changes = changes.lines
- @protocol = protocol
- end
-
- def exec
- status = GitlabMetrics.measure('check-access:git-receive-pack') do
- api.check_access('git-receive-pack', @gl_repository, @repo_path, @actor, @changes, @protocol, env: ObjectDirsHelper.all_attributes.to_json)
- end
-
- raise AccessDeniedError, status.message unless status.allowed?
-
- true
- rescue GitlabNet::ApiUnreachableError
- $stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable"
- false
- rescue AccessDeniedError => ex
- $stderr.puts "GitLab: #{ex.message}"
- false
- end
-
- protected
-
- def api
- GitlabNet.new
- end
-end
diff --git a/lib/gitlab_custom_hook.rb b/lib/gitlab_custom_hook.rb
deleted file mode 100644
index 67096df..0000000
--- a/lib/gitlab_custom_hook.rb
+++ /dev/null
@@ -1,98 +0,0 @@
-require 'open3'
-require_relative 'gitlab_init'
-require_relative 'gitlab_metrics'
-
-class GitlabCustomHook
- attr_reader :vars, :config
-
- def initialize(repo_path, key_id)
- @repo_path = repo_path
- @vars = { 'GL_ID' => key_id }
- @config = GitlabConfig.new
- end
-
- def pre_receive(changes)
- GitlabMetrics.measure("pre-receive-hook") do
- find_hooks('pre-receive').all? do |hook|
- call_receive_hook(hook, changes)
- end
- end
- end
-
- def post_receive(changes)
- GitlabMetrics.measure("post-receive-hook") do
- find_hooks('post-receive').all? do |hook|
- call_receive_hook(hook, changes)
- end
- end
- end
-
- def update(ref_name, old_value, new_value)
- GitlabMetrics.measure("update-hook") do
- find_hooks('update').all? do |hook|
- system(vars, hook, ref_name, old_value, new_value)
- end
- end
- end
-
- private
-
- def call_receive_hook(hook, changes)
- # Prepare the hook subprocess. Attach a pipe to its stdin, and merge
- # both its stdout and stderr into our own stdout.
- stdin_reader, stdin_writer = IO.pipe
- hook_pid = spawn(vars, hook, in: stdin_reader, err: :out)
- stdin_reader.close
-
- # Submit changes to the hook via its stdin.
- begin
- IO.copy_stream(StringIO.new(changes), stdin_writer)
- rescue Errno::EPIPE # rubocop:disable Lint/HandleExceptions
- # It is not an error if the hook does not consume all of its input.
- end
-
- # Close the pipe to let the hook know there is no further input.
- stdin_writer.close
-
- Process.wait(hook_pid)
- $?.success?
- end
-
- # lookup hook files in this order:
- #
- # 1. <repository>.git/custom_hooks/<hook_name> - per project hook
- # 2. <repository>.git/custom_hooks/<hook_name>.d/* - per project hooks
- # 3. <repository>.git/hooks/<hook_name>.d/* - global hooks
- #
- def find_hooks(hook_name)
- hook_files = []
-
- # <repository>.git/custom_hooks/<hook_name>
- project_custom_hook_file = File.join(@repo_path, 'custom_hooks', hook_name)
- hook_files.push(project_custom_hook_file) if File.executable?(project_custom_hook_file)
-
- # <repository>.git/custom_hooks/<hook_name>.d/*
- project_custom_hooks_dir = File.join(@repo_path, 'custom_hooks', "#{hook_name}.d")
- hook_files += match_hook_files(project_custom_hooks_dir)
-
- # <repository>.git/hooks/<hook_name>.d/* OR <custom_hook_dir>/<hook_name>.d/*
- global_custom_hooks_parent = config.custom_hooks_dir(default: File.join(@repo_path, 'hooks'))
- global_custom_hooks_dir = File.join(global_custom_hooks_parent, "#{hook_name}.d")
- hook_files += match_hook_files(global_custom_hooks_dir)
-
- hook_files
- end
-
- # match files from path:
- # 1. file must be executable
- # 2. file must not match backup file
- #
- # the resulting list is sorted
- def match_hook_files(path)
- return [] unless Dir.exist?(path)
-
- Dir["#{path}/*"].select do |f|
- !f.end_with?('~') && File.executable?(f)
- end.sort
- end
-end
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb
index a84ba84..07fef22 100644
--- a/lib/gitlab_net.rb
+++ b/lib/gitlab_net.rb
@@ -3,7 +3,6 @@ require 'openssl'
require 'json'
require_relative 'gitlab_config'
-require_relative 'gitlab_access'
require_relative 'gitlab_lfs_authentication'
require_relative 'http_helper'
diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb
deleted file mode 100644
index 7c5bd19..0000000
--- a/lib/gitlab_post_receive.rb
+++ /dev/null
@@ -1,125 +0,0 @@
-require_relative 'gitlab_init'
-require_relative 'gitlab_net'
-require_relative 'gitlab_metrics'
-require 'json'
-require 'base64'
-require 'securerandom'
-
-class GitlabPostReceive
- attr_reader :config, :gl_repository, :repo_path, :changes, :jid
-
- def initialize(gl_repository, repo_path, actor, changes, push_options)
- @config = GitlabConfig.new
- @gl_repository = gl_repository
- @repo_path = repo_path.strip
- @actor = actor
- @changes = changes
- @push_options = push_options
- @jid = SecureRandom.hex(12)
- end
-
- def exec
- response = GitlabMetrics.measure("post-receive") do
- api.post_receive(gl_repository, @actor, changes, @push_options)
- end
-
- return false unless response
- print_formatted_alert_message(response['broadcast_message']) if response['broadcast_message']
- print_merge_request_links(response['merge_request_urls']) if response['merge_request_urls']
- puts response['redirected_message'] if response['redirected_message']
- puts response['project_created_message'] if response['project_created_message']
- print_warnings(response['warnings']) if response['warnings']
-
- response['reference_counter_decreased']
- rescue GitlabNet::ApiUnreachableError
- false
- end
-
- protected
-
- def api
- @api ||= GitlabNet.new
- end
-
- def print_merge_request_links(merge_request_urls)
- return if merge_request_urls.empty?
- puts
- merge_request_urls.each { |mr| print_merge_request_link(mr) }
- end
-
- def print_merge_request_link(merge_request)
- message =
- if merge_request["new_merge_request"]
- "To create a merge request for #{merge_request['branch_name']}, visit:"
- else
- "View merge request for #{merge_request['branch_name']}:"
- end
-
- puts message
- puts((" " * 2) + merge_request["url"])
- puts
- end
-
- def print_warnings(warnings)
- message = "WARNINGS:\n#{warnings}"
- print_formatted_alert_message(message)
- end
-
- def print_formatted_alert_message(message)
- # A standard terminal window is (at least) 80 characters wide.
- total_width = 80
-
- # Git prefixes remote messages with "remote: ", so this width is subtracted
- # from the width available to us.
- total_width -= "remote: ".length # rubocop:disable Performance/FixedSize
-
- # Our centered text shouldn't start or end right at the edge of the window,
- # so we add some horizontal padding: 2 chars on either side.
- text_width = total_width - 2 * 2
-
- # Automatically wrap message at text_width (= 68) characters:
- # Splits the message up into the longest possible chunks matching
- # "<between 0 and text_width characters><space or end-of-line>".
-
- msg_start_idx = 0
- lines = []
- while msg_start_idx < message.length
- parsed_line = parse_broadcast_msg(message[msg_start_idx..-1], text_width)
- msg_start_idx += parsed_line.length
- lines.push(parsed_line.strip)
- end
-
- puts
- puts "=" * total_width
- puts
-
- lines.each do |line|
- line.strip!
-
- # Center the line by calculating the left padding measured in characters.
- line_padding = [(total_width - line.length) / 2, 0].max
- puts((" " * line_padding) + line)
- end
-
- puts
- puts "=" * total_width
- end
-
- private
-
- def parse_broadcast_msg(msg, text_length)
- msg ||= ""
- # just return msg if shorter than or equal to text length
- return msg if msg.length <= text_length
-
- # search for word break shorter than text length
- truncate_to_space = msg.match(/\A(.{,#{text_length}})(?=\s|$)(\s*)/).to_s
-
- if truncate_to_space.empty?
- # search for word break longer than text length
- truncate_to_space = msg.match(/\A\S+/).to_s
- end
-
- truncate_to_space
- end
-end