From 06b08d75fae0d90621eb639872346a227539035c Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 9 Jul 2019 10:37:06 +1200 Subject: [PATCH 1/3] Update Markdown lint output message --- spec/overcommit/hook/pre_commit/mdl_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/overcommit/hook/pre_commit/mdl_spec.rb b/spec/overcommit/hook/pre_commit/mdl_spec.rb index dc2aa6cd..5fc663c0 100644 --- a/spec/overcommit/hook/pre_commit/mdl_spec.rb +++ b/spec/overcommit/hook/pre_commit/mdl_spec.rb @@ -27,7 +27,11 @@ let(:success) { false } context 'and it reports an error' do - let(:stdout) { 'file1.md:1: MD013 Line length' } + let(:stdout) { <<-STDOUT } + file1.md:1: MD013 Line length + + A detailed description of the rules is available at https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md + STDOUT let(:stderr) { '' } it { should fail_hook } From 765efd66d43d51f53b12e1340604a6f67730fba1 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 9 Jul 2019 10:48:31 +1200 Subject: [PATCH 2/3] Change Markdown lint test output to be in JSON --- spec/overcommit/hook/pre_commit/mdl_spec.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spec/overcommit/hook/pre_commit/mdl_spec.rb b/spec/overcommit/hook/pre_commit/mdl_spec.rb index 5fc663c0..e84b52cc 100644 --- a/spec/overcommit/hook/pre_commit/mdl_spec.rb +++ b/spec/overcommit/hook/pre_commit/mdl_spec.rb @@ -27,11 +27,10 @@ let(:success) { false } context 'and it reports an error' do - let(:stdout) { <<-STDOUT } - file1.md:1: MD013 Line length - - A detailed description of the rules is available at https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md - STDOUT + let(:stdout) do + '[{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"],'\ + '"description":"Line length"}]' + end let(:stderr) { '' } it { should fail_hook } From 5f5027a6f06c552600592abd4bc17dea38be5c1d Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 9 Jul 2019 11:07:22 +1200 Subject: [PATCH 3/3] Use JSON output for Markdown lint Markdown lint 0.5.0 added a link to the rules after printing the results. This caused the Mdl hook to fail as it was not expecting this additional output. Using the JSON output prevents the link to the rules from being displayed and provided a more structured output for parsing. --- CHANGELOG.md | 1 + config/default.yml | 1 + lib/overcommit/hook/pre_commit/mdl.rb | 18 +++++++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78d6feb9..d4e240ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Add `skipped_commit_types` option to `ReplaceBranch` prepare-commit-msg hook * Add `RubySyntax` pre-commit hook * Relax `childprocess` dependency to allow version 1.x +* Fix `Mdl` hook to use JSON output and not fail on unexpected output ## 0.48.1 diff --git a/config/default.yml b/config/default.yml index dcbd34a4..eabeb464 100644 --- a/config/default.yml +++ b/config/default.yml @@ -477,6 +477,7 @@ PreCommit: enabled: false description: 'Analyze markdown files with mdl' required_executable: 'mdl' + flags: ['--json'] install_command: 'gem install mdl' include: '**/*.md' diff --git a/lib/overcommit/hook/pre_commit/mdl.rb b/lib/overcommit/hook/pre_commit/mdl.rb index 17eccc78..4759936e 100644 --- a/lib/overcommit/hook/pre_commit/mdl.rb +++ b/lib/overcommit/hook/pre_commit/mdl.rb @@ -5,8 +5,6 @@ module Overcommit::Hook::PreCommit # # @see https://github.com/mivok/markdownlint class Mdl < Base - MESSAGE_REGEX = /^(?(?:\w:)?[^:]+):(?\d+):\s(?.+)/ - def run result = execute(command, args: applicable_files) output = result.stdout.chomp @@ -15,11 +13,17 @@ def run return [:fail, result.stderr] unless result.stderr.empty? # example message: - # path/to/file.md:1: MD001 Error message - extract_messages( - output.split("\n"), - MESSAGE_REGEX - ) + # [{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"], + # "description":"Line length"}] + json_messages = JSON.parse(output) + json_messages.map do |message| + Overcommit::Hook::Message.new( + :error, + message[:filename], + message[:line], + message[:description] + ) + end end end end