diff --git a/.codeclimate.yml b/.codeclimate.yml index 2be8e63e84223b7bdb00b5e2f9d15aae5e1d81a2..ccc09fcc77574ec7bf3f1d05ddbe3aa42bb98ee1 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -38,3 +38,4 @@ exclude_paths: - backups/ - coverage-javascript/ - plugins/ + - file_hooks/ diff --git a/.rubocop.yml b/.rubocop.yml index 719d1c4a2b6315218ca1a0a5593c6d82c3aa9cec..c98d027cee34c57f454c2e3accbfb47b7a4cd4cb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -25,6 +25,7 @@ AllCops: - 'generator_templates/**/*' - 'builds/**/*' - 'plugins/**/*' + - 'file_hooks/**/*' CacheRootDirectory: tmp Cop/StaticTranslationDefinition: diff --git a/changelogs/unreleased/dz-file-hooks-dir-support.yml b/changelogs/unreleased/dz-file-hooks-dir-support.yml new file mode 100644 index 0000000000000000000000000000000000000000..424b0eb01ef782db7fe0e93291b9b613a0a9c552 --- /dev/null +++ b/changelogs/unreleased/dz-file-hooks-dir-support.yml @@ -0,0 +1,5 @@ +--- +title: Add support for /file_hooks directory +merge_request: 29675 +author: +type: changed diff --git a/doc/administration/file_hooks.md b/doc/administration/file_hooks.md index 7d935af8e370d1d0f75de801ec232d319705c2c3..21ade36a2a5a72ad8c1766da677b70d0b5ef037f 100644 --- a/doc/administration/file_hooks.md +++ b/doc/administration/file_hooks.md @@ -23,22 +23,22 @@ see the [system hooks] documentation. ## Setup -The file hooks must be placed directly into the `plugins` directory, subdirectories +The file hooks must be placed directly into the `file_hooks` directory, subdirectories will be ignored. There is an -[`example` directory inside `plugins`](https://gitlab.com/gitlab-org/gitlab/tree/master/plugins/examples) +[`example` directory inside `file_hooks`](https://gitlab.com/gitlab-org/gitlab/tree/master/file_hooks/examples) where you can find some basic examples. Follow the steps below to set up a custom hook: 1. On the GitLab server, navigate to the plugin directory. For an installation from source the path is usually - `/home/git/gitlab/plugins/`. For Omnibus installs the path is - usually `/opt/gitlab/embedded/service/gitlab-rails/plugins`. + `/home/git/gitlab/file_hooks/`. For Omnibus installs the path is + usually `/opt/gitlab/embedded/service/gitlab-rails/file_hooks`. For [highly available] configurations, your hook file should exist on each application server. -1. Inside the `plugins` directory, create a file with a name of your choice, +1. Inside the `file_hooks` directory, create a file with a name of your choice, without spaces or special characters. 1. Make the hook file executable and make sure it's owned by the Git user. 1. Write the code to make the file hook function as expected. That can be @@ -106,9 +106,9 @@ bundle exec rake file_hooks:validate RAILS_ENV=production Example of output: ```plaintext -Validating file hooks from /plugins directory -* /home/git/gitlab/plugins/save_to_file.clj succeed (zero exit code) -* /home/git/gitlab/plugins/save_to_file.rb failure (non-zero exit code) +Validating file hooks from /file_hooks directory +* /home/git/gitlab/file_hooks/save_to_file.clj succeed (zero exit code) +* /home/git/gitlab/file_hooks/save_to_file.rb failure (non-zero exit code) ``` [system hooks]: ../system_hooks/system_hooks.md diff --git a/file_hooks/.gitignore b/file_hooks/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e4ccdc9e2ecd3fa003c76539fa7d88fed46cb39e --- /dev/null +++ b/file_hooks/.gitignore @@ -0,0 +1,5 @@ +* +!*/ +!.gitignore +!.gitkeep +!examples/* diff --git a/plugins/examples/save_to_file.clj b/file_hooks/examples/save_to_file.clj similarity index 100% rename from plugins/examples/save_to_file.clj rename to file_hooks/examples/save_to_file.clj diff --git a/plugins/examples/save_to_file.rb b/file_hooks/examples/save_to_file.rb similarity index 100% rename from plugins/examples/save_to_file.rb rename to file_hooks/examples/save_to_file.rb diff --git a/lib/gitlab/file_hook.rb b/lib/gitlab/file_hook.rb index 38c19ff506f6e148be7090b9b5919480c4727583..f23ef2921d70c0a5475170b651c218098be8217a 100644 --- a/lib/gitlab/file_hook.rb +++ b/lib/gitlab/file_hook.rb @@ -3,16 +3,17 @@ module Gitlab module FileHook def self.any? - plugin_glob.any? { |entry| File.file?(entry) } + dir_glob.any? { |entry| File.file?(entry) } end def self.files - plugin_glob.select { |entry| File.file?(entry) } + dir_glob.select { |entry| File.file?(entry) } end - def self.plugin_glob - Dir.glob(Rails.root.join('plugins/*')) + def self.dir_glob + Dir.glob([Rails.root.join('file_hooks/*'), Rails.root.join('plugins/*')]) end + private_class_method :dir_glob def self.execute_all_async(data) args = files.map { |file| [file, data] } diff --git a/lib/tasks/file_hooks.rake b/lib/tasks/file_hooks.rake index 20a726de65b0a9b8d4223af6ba0f79b3bffdb717..66d382db6126ac6ed447de61af824b8efa78f0ef 100644 --- a/lib/tasks/file_hooks.rake +++ b/lib/tasks/file_hooks.rake @@ -1,7 +1,7 @@ namespace :file_hooks do - desc 'Validate existing plugins' + desc 'Validate existing file hooks' task validate: :environment do - puts 'Validating file hooks from /plugins directory' + puts 'Validating file hooks from /file_hooks and /plugins directories' Gitlab::FileHook.files.each do |file| success, message = Gitlab::FileHook.execute(file, Gitlab::DataBuilder::Push::SAMPLE_DATA) diff --git a/spec/lib/gitlab/file_hook_spec.rb b/spec/lib/gitlab/file_hook_spec.rb index d184eb483d4edaa05f64223d3c6ae459901469ee..fda3583289bb4f1dcf98b9a2d90b33c338edd1ea 100644 --- a/spec/lib/gitlab/file_hook_spec.rb +++ b/spec/lib/gitlab/file_hook_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Gitlab::FileHook do - let(:file_hook) { Rails.root.join('plugins', 'test.rb') } + let(:file_hook) { Rails.root.join('file_hooks', 'test.rb') } let(:tmp_file) { Tempfile.new('file_hook-dump') } let(:file_hook_source) do