From d774c509666274381e877a6d2943549344bfdd9b Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Tue, 16 Jan 2018 16:48:39 +0800 Subject: [PATCH 1/5] Prohibited Keyword under PreCommit Hook Created this out of my own frustration because there was too many times I've committed things such as `console.log` or `binding.pry` to my Pull Requests. This is a simple check on prohibited keywords based on the list of keywords that we set in config yml. This is useful for projects that need to blacklist few keywords/functions from being committed. --- config/default.yml | 8 +++ .../hook/pre_commit/prohibited_keyword.rb | 27 ++++++++ .../pre_commit/prohibited_keyword_spec.rb | 67 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/prohibited_keyword.rb create mode 100644 spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb diff --git a/config/default.yml b/config/default.yml index 9d95fd4e..b149248f 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1017,6 +1017,14 @@ PrePush: required: false quiet: false + ProhibitedKeyword: + enabled: false + description: 'Check for prohibited keywords' + keywords: + - console.log( + - binding.pry + - eval( + ProtectedBranches: enabled: false description: 'Check for illegal pushes to protected branches' diff --git a/lib/overcommit/hook/pre_commit/prohibited_keyword.rb b/lib/overcommit/hook/pre_commit/prohibited_keyword.rb new file mode 100644 index 00000000..bcbc41cb --- /dev/null +++ b/lib/overcommit/hook/pre_commit/prohibited_keyword.rb @@ -0,0 +1,27 @@ +module Overcommit::Hook::PreCommit + class ProhibitedKeyword < Base + def run + errors = [] + + applicable_files.each do |file| + if File.read(file) =~ /(#{formatted_keywords})/ + errors << "#{file}: contains prohibited keyword.`" + end + end + + return :fail, errors.join("\n") if errors.any? + + :pass + end + + private + + def formatted_keywords + prohibited_keywords.map { |keyword| Regexp.quote(keyword) }.join('|') + end + + def prohibited_keywords + @prohibited_keywords ||= Array(config[:keywords]) + end + end +end diff --git a/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb new file mode 100644 index 00000000..ff110eb9 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' +require 'overcommit/hook_context/pre_push' + +describe Overcommit::Hook::PreCommit::ProhibitedKeyword do + let(:hook_config) { { keywords: ['console.log(', 'eval('] } } + let(:config) do + Overcommit::ConfigurationLoader.default_configuration.merge( + Overcommit::Configuration.new( + 'PreCommit' => { 'ProhibitedKeyword' => hook_config } + ) + ) + end + let(:context) { double('context') } + subject { described_class.new(config, context) } + + context 'with blacklisted keyword' do + let(:file) { create_file('console.log("hello")') } + + context 'with matching file' do + before do + subject.stub(:applicable_files).and_return([file.path]) + end + + it { should fail_hook /contains prohibited keyword./ } + end + + context 'without matching file' do + before do + subject.stub(:applicable_files).and_return([]) + end + + it { should pass } + end + end + + context 'without blacklisted keyword' do + let(:file) { create_file('alert("hello")') } + before do + subject.stub(:applicable_files).and_return([file.path]) + end + + context 'with matching file' do + before do + subject.stub(:applicable_files).and_return([file.path]) + end + + it { should pass } + end + + context 'without matching file' do + before do + subject.stub(:applicable_files).and_return([]) + end + + it { should pass } + end + end + + private + + def create_file(content) + Tempfile.new('index.html').tap do |file| + file.write(content) + file.close + end + end +end From e6df95e8f226f8fe6220922e73a0796d4a427757 Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Wed, 17 Jan 2018 06:35:35 +0800 Subject: [PATCH 2/5] Refactor test --- .../pre_commit/prohibited_keyword_spec.rb | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb index ff110eb9..ed751e0f 100644 --- a/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb +++ b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb @@ -10,24 +10,19 @@ ) ) end - let(:context) { double('context') } subject { described_class.new(config, context) } context 'with blacklisted keyword' do let(:file) { create_file('console.log("hello")') } context 'with matching file' do - before do - subject.stub(:applicable_files).and_return([file.path]) - end + let(:context) { double('context', modified_files: [file.path]) } it { should fail_hook /contains prohibited keyword./ } end context 'without matching file' do - before do - subject.stub(:applicable_files).and_return([]) - end + let(:context) { double('context', modified_files: []) } it { should pass } end @@ -35,22 +30,15 @@ context 'without blacklisted keyword' do let(:file) { create_file('alert("hello")') } - before do - subject.stub(:applicable_files).and_return([file.path]) - end context 'with matching file' do - before do - subject.stub(:applicable_files).and_return([file.path]) - end + let(:context) { double('context', modified_files: [file.path]) } it { should pass } end context 'without matching file' do - before do - subject.stub(:applicable_files).and_return([]) - end + let(:context) { double('context', modified_files: []) } it { should pass } end From 95e1edcfbc1b65da82d11a0373e74bd0f54912c9 Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Wed, 17 Jan 2018 06:41:25 +0800 Subject: [PATCH 3/5] Move to PreCommit --- config/default.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/config/default.yml b/config/default.yml index b149248f..18b2477b 100644 --- a/config/default.yml +++ b/config/default.yml @@ -484,6 +484,14 @@ PreCommit: install_command: 'gem install pronto' flags: ['run', '--staged', '--exit-code'] + ProhibitedKeyword: + enabled: false + description: 'Check for prohibited keywords' + keywords: + - console.log( + - binding.pry + - eval( + PuppetLint: enabled: false description: 'Analyze with puppet-lint' @@ -1017,14 +1025,6 @@ PrePush: required: false quiet: false - ProhibitedKeyword: - enabled: false - description: 'Check for prohibited keywords' - keywords: - - console.log( - - binding.pry - - eval( - ProtectedBranches: enabled: false description: 'Check for illegal pushes to protected branches' From eb1e9abd8f7c30783c00de854f84f5453574385c Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Wed, 17 Jan 2018 06:45:35 +0800 Subject: [PATCH 4/5] Fix test --- lib/overcommit/hook/pre_commit/prohibited_keyword.rb | 2 +- .../overcommit/hook/pre_commit/prohibited_keyword_spec.rb | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/prohibited_keyword.rb b/lib/overcommit/hook/pre_commit/prohibited_keyword.rb index bcbc41cb..d6ad87d4 100644 --- a/lib/overcommit/hook/pre_commit/prohibited_keyword.rb +++ b/lib/overcommit/hook/pre_commit/prohibited_keyword.rb @@ -21,7 +21,7 @@ def formatted_keywords end def prohibited_keywords - @prohibited_keywords ||= Array(config[:keywords]) + @prohibited_keywords ||= Array(config['keywords']) end end end diff --git a/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb index ed751e0f..736f8899 100644 --- a/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb +++ b/spec/overcommit/hook/pre_commit/prohibited_keyword_spec.rb @@ -3,13 +3,7 @@ describe Overcommit::Hook::PreCommit::ProhibitedKeyword do let(:hook_config) { { keywords: ['console.log(', 'eval('] } } - let(:config) do - Overcommit::ConfigurationLoader.default_configuration.merge( - Overcommit::Configuration.new( - 'PreCommit' => { 'ProhibitedKeyword' => hook_config } - ) - ) - end + let(:config) { Overcommit::ConfigurationLoader.default_configuration } subject { described_class.new(config, context) } context 'with blacklisted keyword' do From 10ab632a2ed7760b0fc026b2698200614da8eb70 Mon Sep 17 00:00:00 2001 From: Taufek Johar Date: Wed, 17 Jan 2018 06:46:26 +0800 Subject: [PATCH 5/5] Alphabetical order --- config/default.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/config/default.yml b/config/default.yml index 18b2477b..c3f74939 100644 --- a/config/default.yml +++ b/config/default.yml @@ -477,13 +477,6 @@ PreCommit: flags: ['--standard=PSR2', '--report=csv'] include: '**/*.php' - Pronto: - enabled: false - description: 'Analyzing with pronto' - required_executable: 'pronto' - install_command: 'gem install pronto' - flags: ['run', '--staged', '--exit-code'] - ProhibitedKeyword: enabled: false description: 'Check for prohibited keywords' @@ -492,6 +485,13 @@ PreCommit: - binding.pry - eval( + Pronto: + enabled: false + description: 'Analyzing with pronto' + required_executable: 'pronto' + install_command: 'gem install pronto' + flags: ['run', '--staged', '--exit-code'] + PuppetLint: enabled: false description: 'Analyze with puppet-lint'