Skip to content

Instantly share code, notes, and snippets.

@johnduarte
Created July 11, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnduarte/c37da5a228b69faebfe6fee5be491c41 to your computer and use it in GitHub Desktop.
Save johnduarte/c37da5a228b69faebfe6fee5be491c41 to your computer and use it in GitHub Desktop.
PoC script to create Jira tickets from a given YAML file
# frozen_string_literal: true
# This file needs to be in the winston project
# Requires a yaml file with ticket information in the following
# format:
#
# ---
# issuetype: "1" # global: bug
#
# issues:
# -
# summary: Delete Me
# description: |
# Ticket description for delete me
# * Item 1
# * Item 2
# [link|url]
# -
# summary: Delete Me2
# description: |
# Ticket description for delete me2
LIBDIR = File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
$LOAD_PATH.unshift(LIBDIR)
require 'tickets'
require 'yaml'
USAGE = <<~ENDUSAGE
Usage:
bundle exec ruby create_issue.rb [-h] [-n] -p project yaml_file
ENDUSAGE
HELP = <<ENDHELP
-h, --help Show this help.
-n, --noop No-op mode
-p, --project JIRA project to create ticket for
ENDHELP
ARGS = {} # Setting default values
UNFLAGGED_ARGS = [:yaml_file] # Bare arguments (no flag)
next_arg = UNFLAGGED_ARGS.first
ARGV.each do |arg|
case arg
when '-h', '--help' then ARGS[:help] = true
when '-n', '--noop' then ARGS[:noop] = true
when '-p', '--project' then next_arg = :project
else
if next_arg
ARGS[next_arg] = arg
UNFLAGGED_ARGS.delete(next_arg)
end
next_arg = UNFLAGGED_ARGS.first
end
end
if ARGS[:help] || !ARGS[:yaml_file] || !ARGS[:project]
puts USAGE
puts HELP if ARGS[:help]
exit
end
issues_data = YAML.load_file(ARGS[:yaml_file])
global_type = issues_data['issuetype'] if issues_data.key?('issuetype')
issues = issues_data['issues'] if issues_data.key?('issues')
jira, _authentication = Util::Jira.authenticate unless ARGS[:noop]
issues.each do |i|
issue = jira.client.Issue.build unless ARGS[:noop]
issue_type = i['issuetype'] if i.key?('issuetype')
issue_type ||= global_type
fields = { 'fields' => {
'summary' => i['summary'],
'project' => { 'key' => ARGS[:project] },
'issuetype' => { 'id' => issue_type.to_s },
'description' => i['description']
} }
if ARGS[:noop]
puts "Issue fields: #{fields['fields']}"
next
end
issue.save(fields)
puts "Created: #{issue.key} - #{issue.summary}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment