2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-19 17:20:53 +00:00
|
|
|
module Gitlab
|
2020-01-16 15:08:41 +00:00
|
|
|
module FileHook
|
2020-01-06 09:07:42 +00:00
|
|
|
def self.any?
|
2020-04-21 15:21:10 +00:00
|
|
|
dir_glob.any? { |entry| File.file?(entry) }
|
2020-01-06 09:07:42 +00:00
|
|
|
end
|
|
|
|
|
2018-02-19 17:55:54 +00:00
|
|
|
def self.files
|
2020-04-21 15:21:10 +00:00
|
|
|
dir_glob.select { |entry| File.file?(entry) }
|
2020-01-06 09:07:42 +00:00
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def self.dir_glob
|
2021-05-19 18:10:39 +00:00
|
|
|
Dir.glob(Rails.root.join('file_hooks/*'))
|
2018-02-19 17:55:54 +00:00
|
|
|
end
|
2020-04-21 15:21:10 +00:00
|
|
|
private_class_method :dir_glob
|
2018-02-19 17:20:53 +00:00
|
|
|
|
2018-02-19 17:55:54 +00:00
|
|
|
def self.execute_all_async(data)
|
2018-02-26 12:15:51 +00:00
|
|
|
args = files.map { |file| [file, data] }
|
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
FileHookWorker.bulk_perform_async(args) # rubocop:disable Scalability/BulkPerformWithContext
|
2018-02-19 17:20:53 +00:00
|
|
|
end
|
|
|
|
|
2018-02-19 17:55:54 +00:00
|
|
|
def self.execute(file, data)
|
2018-02-27 16:08:38 +00:00
|
|
|
result = Gitlab::Popen.popen_with_detail([file]) do |stdin|
|
2018-02-26 11:42:25 +00:00
|
|
|
stdin.write(data.to_json)
|
2018-02-23 13:58:57 +00:00
|
|
|
end
|
|
|
|
|
2018-02-27 16:08:38 +00:00
|
|
|
exit_status = result.status&.exitstatus
|
2020-08-05 15:09:59 +00:00
|
|
|
[exit_status == 0, result.stderr]
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => e
|
2018-02-28 10:16:23 +00:00
|
|
|
[false, e.message]
|
2018-02-19 17:20:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|