Skip to content

Instantly share code, notes, and snippets.

@janpaul123
Created April 8, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janpaul123/f607fada739578ce5a8cfd41a1b24538 to your computer and use it in GitHub Desktop.
Save janpaul123/f607fada739578ce5a8cfd41a1b24538 to your computer and use it in GitHub Desktop.
Custom pronto matcher
#!/usr/bin/env ruby
require 'pronto'
module Pronto
class RegexWarnings < Runner
# Matchers arguments: path, text, addition, first_changed_line
def matchers
{
->(path, text, addition, _) { path.match(/\.js$/) && text.include?('http:') && addition } =>
':thought_balloon: Is this a cross-domain request? Might want to use `https:` then.',
->(path, _, _, first_changed_line) { path.include?('__external_dependency') && path.include?('.approved.json') && first_changed_line } =>
':scroll: JSON approval for one or more external dependencies has changed have you updated them accordingly? :thinking:',
}
end
def run
paths_seen = {}
@patches.map(&:lines).flatten.map do |line|
path = line.patch.delta.new_file[:path]
first_changed_line = paths_seen[path].nil?
paths_seen[path] = true
matchers.map do |matcher, message|
# path, text, addition, first_changed_line
if matcher.call(path, line.content, line.addition?, first_changed_line)
new_message(message, line)
end
end
end.flatten.compact
end
def new_message(message, line)
level = :warning
path = line.patch.delta.new_file[:path]
if File.exist?(path)
Message.new(path, line, level, message, nil, self.class)
end
end
end
end
# Used by Pronto::Github.
if ENV['CI_PULL_REQUEST']
ENV['PULL_REQUEST_ID'] = ENV['CI_PULL_REQUEST'].split('/').last
end
if ENV['GITHUB_BOT_TOKEN']
ENV['PRONTO_GITHUB_ACCESS_TOKEN'] = ENV['GITHUB_BOT_TOKEN']
end
# Uncomment this for testing locally:
# Pronto.run(`git show HEAD~10 --pretty=format:%H`[0..30], '.')
if ENV['PULL_REQUEST_ID'].to_i > 0 # CircleCI sets this to 0 in master.
Pronto.run('origin/master', '.', [Pronto::Formatter::GithubPullRequestFormatter.new])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment