<commit_before>require 'fileutils'
module Lighthouse::GitHooks
    
  class Base
    
    def initialize
      @repo = Grit::Repo.new(Configuration[:repository_path])
    end
    
  end # Base

  class TicketUpdate < Base

    attr_accessor :message
    attr_reader :tickets
    attr_reader :real_tickets

    AUTHORIZED_KEYS = [ 'assign', 'state', 'tags', 'untag']

    def initialize(old_rev, new_rev)
      super()
      @commits = @repo.commits_between(old_rev, new_rev)
      @message = ""
      @changes = []
    end
    
    def parse
      @commits.each do |commit|
      
        commit.message.scan(/\[#([0-9]+)\s+(.*?)\]/) do |match|
          @changes << parse_ticket(commit, match[0], match[1])
        end
      end
      self
    end
    
    def send_changes
      @changes.each do |hash_ticket|
        commit = hash_ticket.delete 'commit'
        begin
          puts "updating ticket ##{hash_ticket['number']}"
          ticket = Lighthouse::Ticket.find(hash_ticket['number'], :params => {:project_id => Configuration[:project_id]})

          hash_ticket.each_pair do |key, value|
            case key
            when 'number'
              # do nothing
            when 'assign'
              if user_id = find_user(value.strip)
                ticket.assigned_user_id = user_id
              end
            when 'tags'
              ticket.tags = (ticket.tags + value.split(' ')).uniq
              puts "tags: #{ticket.tags.inspect}"
            when 'untag'
              ticket.tags.delete value
            else
              ticket.send "#{key}=".to_sym, value 
            end
          end
          ticket.body = message
          ticket.save
        rescue ActiveResource::ResourceNotFound => e
          puts "ticket not found ##{hash_ticket['number']} : #{e.message}"
          next
        end
      end
    end

    protected

    def parse_ticket(commit, number, params)
      puts "#{number} , #{params}"
      ticket = {'number' => number.to_i, 'commit' => commit}
      params.scan(/(\w+):(\w+|'.*?')/) do |key, value|
        ticket[key] = value.gsub(/^["'](.*)["']$/, '\1') if AUTHORIZED_KEYS.include?(key)
      end
      ticket
    end
    
    def find_user(nick)
      user = Configuration.users[nick.to_sym]
      raise "user not found #{nick}" unless user
      user.id
    end
    
  end
  
end<commit_msg>fix login<commit_after>require 'fileutils'
module Lighthouse::GitHooks
    
  class Base
    
    def initialize
      @repo = Grit::Repo.new(Configuration[:repository_path])
    end
    
  end # Base

  class TicketUpdate < Base

    attr_accessor :message
    attr_reader :tickets
    attr_reader :real_tickets

    AUTHORIZED_KEYS = [ 'assign', 'state', 'tags', 'untag']

    def initialize(old_rev, new_rev)
      super()
      @commits = @repo.commits_between(old_rev, new_rev)
      @message = ""
      @changes = []
    end
    
    def parse
      @commits.each do |commit|
        commit.message.scan(/\[#([0-9]+)\s+(.*?)\]/) do |match|
          @changes << parse_ticket(commit, match[0], match[1])
        end
      end
      self
    end
    
    def send_changes
      
      @changes.each do |hash_ticket|
        commit = hash_ticket.delete 'commit'
        
        Configuration.login(commit.committer.email)
        
        begin
          puts "updating ticket ##{hash_ticket['number']}"
          ticket = Lighthouse::Ticket.find(hash_ticket['number'], :params => {:project_id => Configuration[:project_id]})

          hash_ticket.each_pair do |key, value|
            case key
            when 'number'
              # do nothing
            when 'assign'
              if user_id = find_user(value.strip)
                ticket.assigned_user_id = user_id
              end
            when 'tags'
              ticket.tags = (ticket.tags + value.split(' ')).uniq
              puts "tags: #{ticket.tags.inspect}"
            when 'untag'
              ticket.tags.delete value
            else
              ticket.send "#{key}=".to_sym, value 
            end
          end
          ticket.body = message
          ticket.save
        rescue ActiveResource::ResourceNotFound => e
          puts "ticket not found ##{hash_ticket['number']} : #{e.message}"
          next
        end
      end
    end

    protected

    def parse_ticket(commit, number, params)
      ticket = {'number' => number.to_i, 'commit' => commit}
      params.scan(/(\w+):(\w+|'.*?')/) do |key, value|
        ticket[key] = value.gsub(/^["'](.*)["']$/, '\1') if AUTHORIZED_KEYS.include?(key)
      end
      ticket
    end
    
    def find_user(nick)
      user = Configuration.users[nick.to_sym]
      raise "user not found #{nick}" unless user
      user.id
    end
    
  end
  
end
{'fraction_non_alphanumeric': 0.12608053974277883, 'fraction_numerical': 0.002108370229812355, 'mean_word_length': 1.8543922984356198, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1447954', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1432, 'n_words': 326}