From 80b899edcbd1851365d19d4382652a5e7530414d Mon Sep 17 00:00:00 2001 From: Artyom Grigoriev Date: Thu, 15 Jan 2015 23:30:43 +0300 Subject: [PATCH 1/3] Using args_parser --- Gemfile | 1 + Gemfile.lock | 2 ++ README.md | 12 +++++++++++- cli.rb | 28 ++++++++++++++++------------ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index 3e585dc..84d139a 100644 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,5 @@ gem 'backports' gem 'octokit' gem 'rubyzip' gem 'json_pure' +gem 'args_parser' diff --git a/Gemfile.lock b/Gemfile.lock index 893f7b8..dc96d88 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ GEM remote: https://rubygems.org/ specs: addressable (2.3.3) + args_parser (0.2.0) backports (3.1.1) faraday (0.8.7) multipart-post (~> 1.1) @@ -25,6 +26,7 @@ PLATFORMS ruby DEPENDENCIES + args_parser backports json_pure octokit diff --git a/README.md b/README.md index fa8ac12..d2d13c5 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,19 @@ Export issues from a Github repository to the [Bitbucket Issue Data Format](https://confluence.atlassian.com/display/BITBUCKET/Export+or+Import+Issue+Data) +## CLI Options +``` +options: + -repository Repository username/reponame + -username (-u) Github login + -password (-p) Github password + -filename (-o) Output file name (default is ./export.zip) : default - export.zip + -help (-h) Show help +``` + ## Usage ``` bundle install -bundle exec ruby cli.rb githubuser/repo username password exportfilename.zip +bundle exec ruby cli.rb --repository user/repo --username user --password *** --filename out.zip ``` diff --git a/cli.rb b/cli.rb index 47a7a7f..6f9c6b0 100644 --- a/cli.rb +++ b/cli.rb @@ -1,17 +1,21 @@ require './lib/github-to-bitbucket-issues' +require 'args_parser' -if ARGV.size <= 2 - options = { - :repository => ARGV[0], - :filename => ARGV[1] || "export.zip" - } -else - options = { - :repository => ARGV[0], - :username => ARGV[1], - :password => ARGV[2], - :filename => ARGV[3] || "export.zip" - } +options = ArgsParser.parse ARGV do + arg :repository, 'Repository username/reponame' + arg :username, 'Github login', :alias => :u + arg :password, 'Github password', :alias => :p + arg :filename, 'Output file name (default is ./export.zip)', :alias => :o, :default => 'export.zip' + arg :help, 'Show help', :alias => :h + + validate :repository, "Invalid repository path" do |r| + r =~ /^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$/ + end +end + +if options.has_option? :help or !options.has_param?(:repository, :username, :password) + STDERR.puts options.help + exit 1 end GTBI::Export.new(options).generate From 7b9c7e312a09d59b8700583e0c5ba3c634d74fa9 Mon Sep 17 00:00:00 2001 From: Artyom Grigoriev Date: Thu, 15 Jan 2015 23:31:41 +0300 Subject: [PATCH 2/3] Fix README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d2d13c5..c1063d4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Export issues from a Github repository to the [Bitbucket -Issue Data Format](https://confluence.atlassian.com/display/BITBUCKET/Export+or+Import+Issue+Data) +Export issues from a Github repository to the +[Bitbucket Issue Data Format](https://confluence.atlassian.com/display/BITBUCKET/Export+or+Import+Issue+Data) ## CLI Options ``` @@ -15,5 +15,5 @@ options: ``` bundle install -bundle exec ruby cli.rb --repository user/repo --username user --password *** --filename out.zip +bundle exec ruby cli.rb -repository user/repo -username user -password *** -filename out.zip ``` From 00704cc0890a0e31d9c4f3828bcb3805f8b74b9c Mon Sep 17 00:00:00 2001 From: Artyom Grigoriev Date: Fri, 16 Jan 2015 00:41:06 +0300 Subject: [PATCH 3/3] Refactoring & Option for skipping export for Pull Requests --- README.md | 1 + cli.rb | 1 + lib/github-to-bitbucket-issues/export.rb | 19 ++++++---- .../formatters/base.rb | 8 +++- .../formatters/comment.rb | 14 +++---- .../formatters/issue.rb | 38 ++++++++++--------- .../formatters/milestone.rb | 4 +- 7 files changed, 49 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index c1063d4..0ff3d1d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ options: -username (-u) Github login -password (-p) Github password -filename (-o) Output file name (default is ./export.zip) : default - export.zip + -prskip Skip exporting pull requests -help (-h) Show help ``` diff --git a/cli.rb b/cli.rb index 6f9c6b0..38fa4aa 100644 --- a/cli.rb +++ b/cli.rb @@ -6,6 +6,7 @@ arg :username, 'Github login', :alias => :u arg :password, 'Github password', :alias => :p arg :filename, 'Output file name (default is ./export.zip)', :alias => :o, :default => 'export.zip' + arg :prskip, 'Skip exporting pull requests' arg :help, 'Show help', :alias => :h validate :repository, "Invalid repository path" do |r| diff --git a/lib/github-to-bitbucket-issues/export.rb b/lib/github-to-bitbucket-issues/export.rb index 6d5ddb9..4fd6d78 100644 --- a/lib/github-to-bitbucket-issues/export.rb +++ b/lib/github-to-bitbucket-issues/export.rb @@ -24,6 +24,7 @@ def initialize(options) }) @repository = options[:repository] @filename = options[:filename] + @skip_pull_requests = options.has_option? :prskip @issues = [] @comments = [] @milestones = [] @@ -58,9 +59,11 @@ def to_json private def download_issues - %w(open closed).each do |state| - @issues += download_all_of("issue", {:state => state}) - end + @issues = download_all_of( + "issue", + {:state => "all"}, + {:skip_pull_requests => @skip_pull_requests} + ) end def download_comments @@ -71,11 +74,11 @@ def download_milestones @milestones = download_all_of("milestone") end - def download_all_of(type, options = {}) - items = downloader(type).new(@github_client, @repository, options).fetch - items.map do |item| - formatter(type).new(item).formatted - end + def download_all_of(type, download_options = {}, accept_options = {}) + items = downloader(type).new(@github_client, @repository, download_options).fetch + formatter = formatter(type).new + items = items.select { |item| formatter.accept(item, accept_options) } + items.map { |item| formatter.format(item) } end def downloader(type) diff --git a/lib/github-to-bitbucket-issues/formatters/base.rb b/lib/github-to-bitbucket-issues/formatters/base.rb index 32b2bfd..a240994 100644 --- a/lib/github-to-bitbucket-issues/formatters/base.rb +++ b/lib/github-to-bitbucket-issues/formatters/base.rb @@ -1,8 +1,12 @@ module GTBI module Formatters class Base - def initialize(raw) - @raw = raw + def format(item) + {} + end + + def accept(item, options) + true end end end diff --git a/lib/github-to-bitbucket-issues/formatters/comment.rb b/lib/github-to-bitbucket-issues/formatters/comment.rb index 0afc6f1..bd7cb96 100644 --- a/lib/github-to-bitbucket-issues/formatters/comment.rb +++ b/lib/github-to-bitbucket-issues/formatters/comment.rb @@ -1,14 +1,14 @@ module GTBI module Formatters class Comment < Base - def formatted + def format(item) { - :content => @raw.body, - :created_on => @raw.created_at, - :id => @raw.id, - :issue => get_issue(@raw), - :updated_on => @raw.updated_at, - :user => @raw.user.login + :content => item.body, + :created_on => item.created_at, + :id => item.id, + :issue => get_issue(item), + :updated_on => item.updated_at, + :user => item.user.login } end diff --git a/lib/github-to-bitbucket-issues/formatters/issue.rb b/lib/github-to-bitbucket-issues/formatters/issue.rb index dda21ff..aa6fb32 100644 --- a/lib/github-to-bitbucket-issues/formatters/issue.rb +++ b/lib/github-to-bitbucket-issues/formatters/issue.rb @@ -1,27 +1,31 @@ module GTBI module Formatters class Issue < Base - def formatted + def format(item) { - :assignee => get_assignee(@raw), + :assignee => get_assignee(item), :component => nil, - :content => @raw.body || " ", - :content_updated_on => @raw.updated_at, - :created_on => @raw.updated_at, - :edited_on => @raw.updated_at, - :id => @raw.number, - :kind => get_kind(@raw), - :milestone => get_milestone(@raw), - :priority => get_priority(@raw), - :reporter => @raw.user.login, - :status => get_status(@raw), - :title => @raw.title, - :updated_on => @raw.updated_at, + :content => item.body || " ", + :content_updated_on => item.updated_at, + :created_on => item.updated_at, + :edited_on => item.updated_at, + :id => item.number, + :kind => get_kind(item), + :milestone => get_milestone(item), + :priority => get_priority(item), + :reporter => item.user.login, + :status => get_status(item), + :title => item.title, + :updated_on => item.updated_at, :version => nil, :watchers => [] } end + def accept(issue, options) + !options[:skip_pull_requests] || issue.pull_request.nil? || issue.pull_request.patch_url.nil? + end + private def get_assignee(issue) @@ -64,10 +68,10 @@ def get_priority(issue) def get_milestone(issue) if issue.milestone - milestone = issue.milestone.title + issue.milestone.title + else + nil end - - milestone end end end diff --git a/lib/github-to-bitbucket-issues/formatters/milestone.rb b/lib/github-to-bitbucket-issues/formatters/milestone.rb index 56a987f..f76fced 100644 --- a/lib/github-to-bitbucket-issues/formatters/milestone.rb +++ b/lib/github-to-bitbucket-issues/formatters/milestone.rb @@ -1,9 +1,9 @@ module GTBI module Formatters class Milestone < Base - def formatted + def format(item) { - :name => @raw.title + :name => item.title } end end