#!/usr/bin/env ruby require 'rubygems' require 'io/console' require 'trollop' require 'jirasync' def prompt_for_password print("Please enter password: ") pw = STDIN.noecho(&:gets).chomp puts pw end opts = Trollop::options do banner <<-EOS Utility to sync a jira project to the local file system. Usage: jira-sync [options] [command] where command is one of the following fetch-all: fetches all tickets from the given project update: fetches all tickets that have been updated/ added since the last run where [options] are: EOS opt :baseurl, "Jira base url", :type => :string opt :project, "Project key", :type => :string opt :user, "User, defaults to the current system user", :type => :string opt :password, "Password, if not specified there, will be an interactive prompt", :type => :string opt :target, "Target directory, defaults to the project key", :type => :string opt :store_attachments, "Fetch and store attachments" end Trollop::die :baseurl, "must be speficied" if !opts[:baseurl] Trollop::die :project, "must be speficied" if !opts[:project] user = opts[:user] || ENV['USER'] pw = opts[:password] || prompt_for_password target = opts[:target] || opts[:project].chomp project = opts[:project] store_attachments = opts[:store_attachments] command = ARGV[0] if !["fetch", "update"].include?(command) STDERR.puts("'#{command}' is not a valid command") exit 1 end client = JiraSync::JiraClient.new(opts[:baseurl], user, pw) repo = JiraSync::LocalIssueRepository.new(target) syncer = JiraSync::Syncer.new(client, repo, project, store_attachments) if command == "fetch" if (repo.state_exists?) update_statement=<<-END jira-sync \ --baseurl #{opts[:baseurl]} \\ --user #{user} \\ --project #{project} \\ --target #{target} \\ --store-attachments \\ update END STDERR.puts <<-END Synchronisation state file found. Please use 'update' for incremental update or sync to an empty directory. Try to use a statement along the following lines #{update_statement} END exit 1 end syncer.fetch_all end if command == "update" if (!repo.state_exists?) fetch_statement=<<-END jira-sync \ --baseurl #{opts[:baseurl]} \\ --user #{user} \\ --project #{project} \\ --target #{target} \\ --store-attachments \\ fetch END STDERR.puts <<-END No synchronisation state file found. Please use 'fetch' for an initial download. Try to use a statement along the following lines #{fetch_statement} END exit 1 end syncer.update end