<commit_before>require 'uri'
require 'net/http'
require 'net/http/post/multipart'
require 'json'
require 'date'
require 'pp'
require 'mime/types'
require 'JiraMule/Config'
require 'JiraMule/Passwords'
require 'JiraMule/http'
require 'JiraMule/verbosing'

module JiraMule
    class JiraUtilsException < Exception
        attr_accessor :request, :response
    end

    class JiraUtils
        include Verbose
        include Http

        # TODO: all params are now optional.
        def initialize(args=nil, options=nil, cfg=nil)
            acc = Account.new
            up = acc.loginInfo
            @username = up[:email]
            @password = up[:password]
        end
        attr_reader :username, :password

        def jiraEndPoint
            endPoint()
        end

        def endPoint(path='')
          URI($cfg['net.url'] + '/rest/api/2/' + path.to_s)
        end

        def project
            return @project unless @project.nil?
            @project = $cfg['jira.project']
            return @project
        end

        ##
        # Given an array of issues keys that may or may not have the project prefixed
        # Return an array with the project prefixed.
        #
        # So on project APP, from %w{1 56 BUG-78} you get %w{APP-1 APP-56 BUG-78}
        def expandKeys(keys)
            return keys.map do |k|
                k.match(/([a-zA-Z0-9]+-)?(\d+)/) do |m|
                    if m[1].nil? then
                        "#{project}-#{m[2]}"
                    else
                        m[0]
                    end
                end
            end.compact
        end

        ##
        # Allow for some sloppy matching.
        # +transition+:: The transition hash to match against
        # +couldBe+:: The string from a human to match for
        def fuzzyMatchStatus(transition, couldBe)
            return transition[:id] == couldBe if couldBe =~ /^\d+$/

            # Build a regexp for all sorts of variations.

            # Replace whitespace with a rex for dashes, whitespace, or nospace.
            cb = couldBe.gsub(/\s+/, '[-_\s]*')

            matcher = Regexp.new(cb, Regexp::IGNORECASE)
            debug "Fuzzing: #{transition[:name]} =~ #{cb}"
            return transition[:name] =~ matcher
        end

        ##
        # Lookup a path from one state to another in a map
        # +at+:: The starting state
        # +to+:: The stopping state
        def getPath(at, to)
            verbose "Getting path from '#{at}' to '#{to}'"

            # [goto-map]
            # at-to: each, step, to, end
            tr = $cfg["goto-maps.#{at.gsub(/\W+/,'_')}-#{to.gsub(/\W+/,'_')}"]
            (tr or "").split(/,/).map{|p| p.strip}
        end

        ##
        # Run a JQL query and get issues with the selected fields
        def getIssues(query, fields=[ 'key', 'summary' ])
            verbose "Get keys: #{query}"
            data = post('search', {:jql=>query, :fields=>fields})
            data[:issues]
        end

        ##
        # make sure #user is an actual user in the system.
        def checkUser(user, keyMatch=true)
            verbose "Get user: #{user}"
            users = get("user/search", {:username=>user})
            return [] if users.empty?
            userKeys = users.map{|i| i[:key]}
            return [user] if keyMatch and userKeys.index(user)
            return userKeys
        end

        ##
        # Create a new version for release.
        # TODO: test this.
        def createVersion(project, version)
            verbose "Creating #{version} in #{project}"
            unless $cfg['tool.dry'] then
                data = post('version', {
                    'name' => version,
                    'archived' => false,
                    'released' => true,
                    'releaseDate' => DateTime.now.strftime('%Y-%m-%d'),
                    'project' => project,
                })
                unless data[:released] then
                    # Sometimes setting released on create doesn't work.
                    # So modify it.
                    put("version/#{data[:id]}", {:released=>true})<commit_msg>Return empty array if not hash<commit_after>require 'uri'
require 'net/http'
require 'net/http/post/multipart'
require 'json'
require 'date'
require 'pp'
require 'mime/types'
require 'JiraMule/Config'
require 'JiraMule/Passwords'
require 'JiraMule/http'
require 'JiraMule/verbosing'

module JiraMule
    class JiraUtilsException < Exception
        attr_accessor :request, :response
    end

    class JiraUtils
        include Verbose
        include Http

        # TODO: all params are now optional.
        def initialize(args=nil, options=nil, cfg=nil)
            acc = Account.new
            up = acc.loginInfo
            @username = up[:email]
            @password = up[:password]
        end
        attr_reader :username, :password

        def jiraEndPoint
            endPoint()
        end

        def endPoint(path='')
          URI($cfg['net.url'] + '/rest/api/2/' + path.to_s)
        end

        def project
            return @project unless @project.nil?
            @project = $cfg['jira.project']
            return @project
        end

        ##
        # Given an array of issues keys that may or may not have the project prefixed
        # Return an array with the project prefixed.
        #
        # So on project APP, from %w{1 56 BUG-78} you get %w{APP-1 APP-56 BUG-78}
        def expandKeys(keys)
            return keys.map do |k|
                k.match(/([a-zA-Z0-9]+-)?(\d+)/) do |m|
                    if m[1].nil? then
                        "#{project}-#{m[2]}"
                    else
                        m[0]
                    end
                end
            end.compact
        end

        ##
        # Allow for some sloppy matching.
        # +transition+:: The transition hash to match against
        # +couldBe+:: The string from a human to match for
        def fuzzyMatchStatus(transition, couldBe)
            return transition[:id] == couldBe if couldBe =~ /^\d+$/

            # Build a regexp for all sorts of variations.

            # Replace whitespace with a rex for dashes, whitespace, or nospace.
            cb = couldBe.gsub(/\s+/, '[-_\s]*')

            matcher = Regexp.new(cb, Regexp::IGNORECASE)
            debug "Fuzzing: #{transition[:name]} =~ #{cb}"
            return transition[:name] =~ matcher
        end

        ##
        # Lookup a path from one state to another in a map
        # +at+:: The starting state
        # +to+:: The stopping state
        def getPath(at, to)
            verbose "Getting path from '#{at}' to '#{to}'"

            # [goto-map]
            # at-to: each, step, to, end
            tr = $cfg["goto-maps.#{at.gsub(/\W+/,'_')}-#{to.gsub(/\W+/,'_')}"]
            (tr or "").split(/,/).map{|p| p.strip}
        end

        ##
        # Run a JQL query and get issues with the selected fields
        def getIssues(query, fields=[ 'key', 'summary' ])
            verbose "Get keys: #{query}"
            data = post('search', {:jql=>query, :fields=>fields})
            return data[:issues] if data.kind_of?(Hash) and data.has_key?(:issues)
            []
        end

        ##
        # make sure #user is an actual user in the system.
        def checkUser(user, keyMatch=true)
            verbose "Get user: #{user}"
            users = get("user/search", {:username=>user})
            return [] if users.empty?
            userKeys = users.map{|i| i[:key]}
            return [user] if keyMatch and userKeys.index(user)
            return userKeys
        end

        ##
        # Create a new version for release.
        # TODO: test this.
        def createVersion(project, version)
            verbose "Creating #{version} in #{project}"
            unless $cfg['tool.dry'] then
                data = post('version', {
                    'name' => version,
                    'archived' => false,
                    'released' => true,
                    'releaseDate' => DateTime.now.strftime('%Y-%m-%d'),
                    'project' => project,
                })
                unless data[:released] then
                    # Sometimes setting released on create doesn't work.
                    # So modify it.
                    put("version/#{data[:id]}", {:released=>true})
{'fraction_non_alphanumeric': 0.12247580356075995, 'fraction_numerical': 0.0038236348428725055, 'mean_word_length': 1.5674846625766872, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '4300536', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2321, 'n_words': 701}