class NG1BuildHelpers::JIRA::Parser

Constants

Options

Public Class Methods

commandline_jira_update(args) click to toggle source
# File lib/jira/jira_parser.rb, line 107
def self.commandline_jira_update(args)
    options = args ? parse(ARGV) : Options.new

    pull_ci_options(options)
    pull_common_options(options)
    
    if validate_options(options)  
        logs = get_git_history(options.commit_range)
        issue_set = find_jira_issues(logs, options.regex)
        
        if issue_set.size > 0
            puts "Updating #{issue_set.size} issues"
            jira_info = NG1BuildHelpers::JIRA::Jira.new options
            jira_info.jira_update(issue_set, options)
        else
            puts "Zero issues found"
        end
    end
end
find_jira_issues(logs, regex) click to toggle source
# File lib/jira/jira_parser.rb, line 127
def self.find_jira_issues(logs, regex)
    issue_set = Set.new
    logs.each do |log|
        #If provided a JIRA Regex to find issues
        if regex
            issues = log.scan(/#{regex}/)
        elsif
            issues = log.scan(/[A-Za-z][A-Za-z]+-[1-9][0-9]*/)
        end

        issue_set.merge(issues)
    end
    
    return issue_set
end
get_git_history(commit_range) click to toggle source
# File lib/jira/jira_parser.rb, line 143
def self.get_git_history(commit_range)
    puts "Getting git history for #{commit_range}"
    happyface = "\u263A".encode('utf-8')
    extra_params = commit_range.index('..') == nil ? "-n 1" : ""
    git_log = `git --no-pager log --format="%B#{happyface}" #{commit_range} #{extra_params}`
    puts(git_log)

    #parse out the log
    logs = git_log.split(happyface)
    
    return logs
end
parse(options) click to toggle source
# File lib/jira/jira_parser.rb, line 20
def self.parse(options)
    args = Options.new()

    opt_parser = OptionParser.new do |opts|
        opts.banner = "Usage: jira_update_fix_version [options]"
    
        opts.on("-c", "--commits COMMIT_RANGE", "Specify a commit range") do |commit_range|
            args.commit_range = commit_range
        end
        
        opts.on("-u", "--username USERNAME", "Specify the user to connecto to JIRA") do |username|
            args.username = username
        end
        
        opts.on("-p", "--password PASSWORD", "Specify the password to connect to JIRA") do |password|
            args.password = password
        end
        
        opts.on("-s", "--site SITE", "Specify the base location of the JIRA instance") do |site|
            site.slice!("http://")
            site.insert(0, "https://") if not site.start_with?("https://")
            args.site = site
        end
        
        opts.on("-b", "--build_number BUILD", "The build number for which this script is executing") do |build|
            args.build = build
        end
    
        opts.on("-r", "--regex REGEX", "JIRA Story Regex [Optional]") do |regex|
            args.regex = regex
        end
    
        opts.on("-v", "--version VERSION", "App Version") do |version|
            args.version = version
        end
    
        opts.on("-n", "--version_name VERSION NAME", "JIRA Version Name") do |version_name|
            args.version_name = version_name
        end

        opts.on("-p", "--project PROJECT", "JIRA Project Name") do |project|
            args.project = project
        end

        opts.on_tail("-h", "--help", "Show this message") do
            puts opts
            exit
        end
    end

    opt_parser.parse!(options)
    return args   
end
print_help() click to toggle source
pull_ci_options(options) click to toggle source
# File lib/jira/jira_parser.rb, line 156
def self.pull_ci_options(options) 
    if ENV["CIRCLECI"]
        pull_circleci_options(options)
    else
        puts("Not running on CircleCI")
    end
end
pull_circleci_options(options) click to toggle source
# File lib/jira/jira_parser.rb, line 170
def self.pull_circleci_options(options)
    puts "Pulling parameters from CircleCI environment variables where needed...."
    options.commit_range = ENV["CIRCLECI_COMMIT_RANGE"] if options.commit_range == nil
    options.build = ENV["CIRCLE_BUILD_NUM"] if options.build == nil
end
pull_common_options(options) click to toggle source
# File lib/jira/jira_parser.rb, line 164
def self.pull_common_options(options)
    options.site = ENV["JIRA_URL"] if options.site == nil
    options.username = ENV["JIRA_USERNAME"] if options.username == nil
    options.password = ENV["JIRA_PASSWORD"] if options.password == nil
end
validate_options(options) click to toggle source
# File lib/jira/jira_parser.rb, line 74
def self.validate_options(options)
    valid_options = true
    if options.commit_range == nil || options.commit_range.empty?
        puts "Commit range is required. None specified."
        valid_options = false
    end
    if options.site == nil
        puts "Site parameter is required, or use JIRA_URL environment variable on build servers"
        valid_options = false
    end
    if options.username == nil
        puts "No username for JIRA provided. Use JIRA_USERNAME environment variable on build servers"
        valid_options = false
    end
    if options.password == nil
        puts "No password for JIRA provided. Use JIRA_PASSWORD environment variable on build servers"
        valid_options = false
    end
    if options.version == nil
        puts "No version provided."
        valid_options = false
    end
    if options.project == nil
        puts "No project name provided."
        valid_options = false
    end
    if options.version_name == nil
        puts "No version name provided."
        valid_options = false
    end
    valid_options
end