summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2019-01-03 15:46:12 +0100
committerJames Lopez <james@jameslopez.es>2019-02-12 15:44:53 +0100
commit63a1e32dad9ccb59bdb3f65b4619b96317e9cd25 (patch)
tree6c8a48dcf465cf3636b58e94c06792e1f844a642
parentd29e81b2aa7fc26736eb09309bbbf2ab5a5d5050 (diff)
downloadgitlab-ce-feature/alex-danger-review.tar.gz
Add alex as a Danger filefeature/alex-danger-review
Adds a new step in Danger to warn on non-inclusive and profane language. This affects: - Commits - Documentation
-rw-r--r--Dangerfile1
-rw-r--r--danger/alex/Dangerfile25
-rw-r--r--danger/alex/alex_command.rb34
-rw-r--r--danger/commit_messages/Dangerfile7
4 files changed, 67 insertions, 0 deletions
diff --git a/Dangerfile b/Dangerfile
index 6a2c5cf2773..34264e4e442 100644
--- a/Dangerfile
+++ b/Dangerfile
@@ -11,3 +11,4 @@ danger.import_dangerfile(path: 'danger/commit_messages')
danger.import_dangerfile(path: 'danger/duplicate_yarn_dependencies')
danger.import_dangerfile(path: 'danger/prettier')
danger.import_dangerfile(path: 'danger/eslint')
+danger.import_dangerfile(path: 'danger/alex')
diff --git a/danger/alex/Dangerfile b/danger/alex/Dangerfile
new file mode 100644
index 00000000000..b3174dd2ddb
--- /dev/null
+++ b/danger/alex/Dangerfile
@@ -0,0 +1,25 @@
+require_relative './alex_command'
+
+# frozen_string_literal: true
+
+EXTENSIONS = %w[md txt].freeze
+
+def paths_to_review(files)
+ files.select do |file|
+ EXTENSIONS.any? { |pattern| file.end_with?(pattern) }
+ end
+end
+
+paths = paths_to_review(helper.all_changed_files)
+
+result = AlexCommand.from_paths(paths)
+
+unless result.status.success?
+ markdown(<<~MARKDOWN)
+## Inconsiderate words review
+
+```sh
+#{result.stderr}
+```
+ MARKDOWN
+end
diff --git a/danger/alex/alex_command.rb b/danger/alex/alex_command.rb
new file mode 100644
index 00000000000..3e724bb44c5
--- /dev/null
+++ b/danger/alex/alex_command.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+require_relative '../../lib/gitlab/popen'
+
+class AlexCommand
+ Result = Struct.new(:stdout, :stderr, :status)
+
+ def self.from_paths(paths)
+ execute(%W[alex #{paths.join(' ')}])
+ end
+
+ def self.from_text(text)
+ execute(%w[alex --stdin], text)
+ end
+
+ def self.execute(cmd, input = nil)
+ Open3.popen3({}, *cmd) do |stdin, stdout, stderr, wait_thr|
+ stdin.puts(input)
+ stdin.flush
+
+ # stderr and stdout pipes can block if stderr/stdout aren't drained: https://bugs.ruby-lang.org/issues/9082
+ # Mimic what Ruby does with capture3: https://github.com/ruby/ruby/blob/1ec544695fa02d714180ef9c34e755027b6a2103/lib/open3.rb#L257-L273
+ out_reader = Thread.new { stdout.read }
+ err_reader = Thread.new { stderr.read }
+
+ yield(stdin) if block_given?
+ stdin.close
+
+ Result.new(out_reader.value, err_reader.value, wait_thr.value)
+ end
+ end
+
+ private_class_method :execute
+end
diff --git a/danger/commit_messages/Dangerfile b/danger/commit_messages/Dangerfile
index c20c8b77e6a..297728e70c6 100644
--- a/danger/commit_messages/Dangerfile
+++ b/danger/commit_messages/Dangerfile
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'json'
+require_relative '../alex/alex_command'
URL_LIMIT_SUBJECT = "https://chris.beams.io/posts/git-commit/#limit-50"
URL_GIT_COMMIT = "https://chris.beams.io/posts/git-commit/"
@@ -188,6 +189,12 @@ def lint_commits(commits)
failures = true
end
+
+ result = AlexCommand.from_text(commit.message)
+
+ message = result.stderr[8..-1]&.force_encoding('utf-8')
+
+ warn_commit(commit, message) unless result.status.success?
end
if failures