require 'open-uri' require_relative 'finder' module Danger # Automatically mention potential reviewers on pull requests. # It downloads and parses the blame information of changed files # to figure out who may be a good reviewer. # # @example Specifying max reviewers. # # # Find maximum two reviewers without specifying # # ignored files and users # mention.run(2, [], []) # class DangerMention < Plugin # Mention potential reviewers. # # @param Integer max_reviewers # Maximum number of people to ping in the PR message, default is 3. # @param [String] file_blacklist # Regexes of ignored files. # @param [String] user_blacklist # List of users that will never be mentioned. # @return [void] # def run(max_reviewers = 3, file_blacklist = [], user_blacklist = []) files = select_files(file_blacklist) return if files.empty? authors = {} compose_urls(files).each do |url| result = parse_blame(url) authors.merge!(result) { |_, m, n| m + n } end reviewers = find_reviewers(authors, user_blacklist, max_reviewers) if reviewers.count > 0 reviewers = reviewers.map { |r| '@' + r } result = format('By analyzing the blame information on this pull '\ 'request, we identified %s to be potential reviewer%s.', reviewers.join(', '), reviewers.count > 1 ? 's' : '') markdown result end end def select_files(file_blacklist) files = Finder.parse(env.scm.diff) file_blacklist = file_blacklist.map { |f| /#{f}/ } re = Regexp.union(file_blacklist) files = files.select { |f| !f.match(re) } files[0...3] end def compose_urls(files) host = 'https://' + env.request_source.host repo_slug = env.ci_source.repo_slug path = host + '/' + repo_slug + '/' + 'blame' + '/' + branch_for_base urls = [] files.each do |file| urls << path + '/' + file end urls end def parse_blame(url) regex = %r{(?:rel="(?:author|contributor)">([^<]+) authored|(?: