<commit_before>require 'pathname'

module Saddler
  module Reporter
    module Github
      class CommitReviewComment
        include Support
        include Helper

        # https://developer.github.com/v3/repos/comments/#create-a-commit-comment
        def report(messages, options)
          repo_path = '.'
          repo = GitRepository.new(repo_path)

          sha = options['sha'] || repo.head.sha
          data = parse(messages)

          client = Client.new(repo)
          # fetch commit_comments
          commit_comments = client.commit_comments(sha)

          # build comment
          comments = build_comments(data, client, sha)
          return if comments.empty?

          posting_comments = comments - commit_comments
          return if posting_comments.empty?
          # create commit_comment
          client.create_commit_comment(posting_comments)
        end

        def build_comments(data, client, sha)
          patches = client.commit_patches(sha)
          comments = []
          files = data['checkstyle']['file'] ||= []
          files.each do |file|
            errors = file['error'] ||= []
            file_name = file['@name'] ||= ''
            patch = patches.find_patch_by_file(file_relative_path_string(file_name))
            next unless patch

            errors.each do |error|
              line_no = error['@line'] && error['@line'].to_i
              next unless patch.changed_line_numbers.include?(line_no)

              severity = error['@severity'] && error['@severity'].upcase
              message = error['@message']
              position = patch.find_patch_position_by_line_number(line_no)
<commit_msg>fix(commitreview): adjust nori result<commit_after>require 'pathname'

module Saddler
  module Reporter
    module Github
      class CommitReviewComment
        include Support
        include Helper

        # https://developer.github.com/v3/repos/comments/#create-a-commit-comment
        def report(messages, options)
          repo_path = '.'
          repo = GitRepository.new(repo_path)

          sha = options['sha'] || repo.head.sha
          data = parse(messages)

          client = Client.new(repo)
          # fetch commit_comments
          commit_comments = client.commit_comments(sha)

          # build comment
          comments = build_comments(data, client, sha)
          return if comments.empty?

          posting_comments = comments - commit_comments
          return if posting_comments.empty?
          # create commit_comment
          client.create_commit_comment(posting_comments)
        end

        def build_comments(data, client, sha)
          patches = client.commit_patches(sha)
          comments = []
          files = data['checkstyle']['file'] ||= []
          files = [files] if files.kind_of?(Hash)
          files.each do |file|
            errors = file['error'] ||= []
            errors = [file['error']] if errors.kind_of?(Hash)
            file_name = file['@name'] ||= ''
            patch = patches.find_patch_by_file(file_relative_path_string(file_name))
            next unless patch

            errors.each do |error|
              line_no = error['@line'] && error['@line'].to_i
              next unless patch.changed_line_numbers.include?(line_no)

              severity = error['@severity'] && error['@severity'].upcase
              message = error['@message']
              position = patch.find_patch_position_by_line_number(line_no)

{'fraction_non_alphanumeric': 0.10306004618937645, 'fraction_numerical': 0.0005773672055427252, 'mean_word_length': 2.15, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1785735', 'n_tokens_mistral': 938, 'n_tokens_neox': 917, 'n_words': 187}