Skip to content

Instantly share code, notes, and snippets.

@oogali
Created October 17, 2012 16:21
Show Gist options
  • Save oogali/3906493 to your computer and use it in GitHub Desktop.
Save oogali/3906493 to your computer and use it in GitHub Desktop.
jira from cli
#!/usr/bin/env ruby
require 'rubygems'
require 'yaml'
require 'getoptlong'
gem 'soap4r'
require 'soap/soap'
require 'soap/rpc/driver'
require 'jira4r/jira_tool'
require 'jira4r/v2/jira_service'
require 'jira4r/v2/jira_soap_service_driver'
require 'jira4r/v2/jira_service_mapping_registry'
module Jira
VERSION = '20110817.03'
EMAIL = '[email protected]'
module Project
SystemEngineering = :SYS
NetworkEngineering = :NET
Operations = :OPS
Development = :DEV
end
module IssueType
Bug = 1
NewFeature = 2
Task = 3
Improvement = 4
Project = 5
end
module Priority
Blocker = 1
Critical = 2
Major = 3
Minor = 4
Trivial = 5
end
class Jira
attr_reader :endpoint
@jira
@auth
def initialize(username, password, url = 'http://jira')
@endpoint = url
@jira = Jira4R::JiraTool.new 2, @endpoint
@auth = @jira.login(username, password)
end
def createIssue(project, summary, description, options = {})
issue = Jira4R::V2::RemoteIssue.new
issue.project = project
issue.summary = summary
issue.description = description
issue.type = options[:type] || IssueType::Task
issue.assignee = options[:assignee]
issue.reporter = options[:reporter]
issue.environment = options[:environment]
issue.duedate = options[:duedate]
issue.priority = options[:priority]
p issue
@jira.createIssue issue
end
def logout
@jira.logout if @auth
end
def Jira.priority(priority)
case priority
when /blocker/i
priority_code = Priority::Blocker
when /critical/i
priority_code = Priority::Critical
when /major/i
priority_code = Priority::Major
when /minor/i
priority_code = Priority::Minor
when /trivial/i
priority_code = Priority::Trivial
end
raise if !priority_code
priority_code
end
def Jira.project(project)
case project
when /sys/i
project_code = Project::SystemEngineering
when /net/i
project_code = Project::NetworkEngineering
when /ops/i
project_code = Project::Operations
when /dev/i
project_code = Project::Development
else
project_code = project.upcase
puts "*** WARNING *** Unknown key, using #{project_code}"
end
project_code
end
def Jira.issue_type(issue_type)
case issue_type
when /bug/i
issue_type_code = IssueType::Bug
when /feature/i
issue_type_code = IssueType::NewFeature
when /task/i
issue_type_code = IssueType::Task
when /improve/i
issue_type_code = IssueType::Improvement
when /project/i
issue_type_code = IssueType::Project
end
raise if !issue_type_code
issue_type_code
end
end
end
def usage
puts "jira command-line issue creator // #{Jira::EMAIL} // #{Jira::VERSION}"
puts
puts 'jira [--assignee] [--priority] [--project] [--type] <issue summary>'
puts "-a, --assignee\t\tPerson to assign this new issue to"
puts "-p, --priority\t\tPriority of this issue, can be one of the following:"
puts "\t\t\t* blocker, critical, major, minor (default), trivial"
puts "-q, --project\t\tProject/ticket queue to create this new issue in, one of the following:"
puts "\t\t\t* sys (default), net, ops, dev"
puts "-t, --type\t\tType of issue we are creating"
puts "\t\t\t* bug, newfeature, task (default), improvement, project"
puts
exit 1
end
# set default config
config = {
'username' => nil,
'password' => nil,
'jira' => 'http://jira'
}
# load our configuration
config = config.update(YAML.load_file(File.join(ENV['HOME'], '.jira.yml'))) rescue nil
# set default options
issue_opts = {
:assignee => nil,
:priority => Jira::Priority::Minor,
:project => Jira::Project::SystemEngineering,
:type => Jira::IssueType::Task
}
# establish valid options
opts = GetoptLong.new(
[ '--assignee', '-a', GetoptLong::REQUIRED_ARGUMENT ],
[ '--priority', '-p', GetoptLong::REQUIRED_ARGUMENT ],
[ '--project', '-q', GetoptLong::REQUIRED_ARGUMENT ],
[ '--type', '-t', GetoptLong::REQUIRED_ARGUMENT ]
)
# parse options
opts.each { |opt, arg|
case opt
when '--assignee'
issue_opts[:assignee] = arg
when '--priority'
issue_opts[:priority] = Jira::Jira.priority arg
when '--project'
issue_opts[:project] = Jira::Jira.project arg
when '--type'
issue_opts[:type] = Jira::Jira.type arg
end
}
usage if ARGV.length <= 0
jira = Jira::Jira.new(config['username'], config['password'], config['jira'])
puts
puts ">> Enter your ticket description, followed by CTRL+D"
issue = jira.createIssue(issue_opts[:project], ARGV.join(' '), "#{STDIN.read}", issue_opts)
puts
puts "Your issue has been created, #{issue.key} << http://#{jira.endpoint}/browse/#{issue.key} >>"
puts
jira.logout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment