hackpad-migration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eb98deb53e9023cf28e9fa815598194d89750dd6
4
+ data.tar.gz: 88731023ca6dd3e3735a58c87d8757cad700626a
5
+ SHA512:
6
+ metadata.gz: 21c3370ca12d23e39907a4da7194a9d44adc4638969dfa2d10362db6769f4daabd30808e746cd8b984c7a8ddf081a653f4dce221c9f389cd448e253a4be43da7
7
+ data.tar.gz: 325c58de13b44ec4fad89ca60f99cc53afd898c3d16aa10b7b3e6d2f1ee80612ff8210d715b79f9c648adac33061442e217561492a1c18ee3d010153b80c9afc
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /result.json
11
+ /hackpad-migration-*.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hackpad-migration.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Doni Leung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # Hackpad::Migration
2
+
3
+ A tool for migrate Hackpad from one Hackpad site to other Hackpad site via Hackpad's APIs.
4
+
5
+ ## Installation
6
+
7
+ $ gem install hackpad-migration
8
+
9
+ ## Usage
10
+
11
+ After installed, Please use command below then follow the note to use it.
12
+
13
+ $ hackpad-migrate
14
+
15
+ The migration can execute multiple times as you want, it will store the result in the disk after the migrateion is done for each pad, So the next time to be executed, this tool will update all the pad from source site.
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kudelabs/hackpad-migration.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hackpad/migration"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/hackpad/migration'
4
+
5
+ Hackpad::Migration::Runner.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hackpad/migration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hackpad-migration"
8
+ spec.version = Hackpad::Migration::VERSION
9
+ spec.authors = ["Doni Leung"]
10
+ spec.email = ["d@ii2d.com"]
11
+
12
+ spec.summary = %q{Hackpad migrator}
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.description = %q{A tool for migrate Hackpad from one Hackpad site to other Hackpad site via Hackpad's APIs.}
15
+ spec.homepage = "https://github.com/kudelabs/hackpad-migration.git"
16
+ spec.license = "MIT"
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = 'http://mygemserver.com'
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ # spec.bindir = "exe"
27
+ spec.executables = ['hackpad-migrate']#spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency 'oauth', "~> 0.5.1"
31
+ spec.add_dependency 'thor', "~> 0.19.1"
32
+ spec.add_dependency 'nokogiri', "~> 1.6"
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.11"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "byebug", "~> 8.2"
38
+ end
@@ -0,0 +1,14 @@
1
+ require 'thor'
2
+ require 'oauth'
3
+ require 'json'
4
+ require 'nokogiri'
5
+ require "hackpad/migration/version"
6
+ require 'hackpad/migration/result'
7
+ require 'hackpad/migration/runner'
8
+ require 'hackpad/migration/api'
9
+ require 'hackpad/migration/migrator'
10
+
11
+ module Hackpad
12
+ module Migration
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ module Hackpad
2
+ module Migration
3
+ class API
4
+ attr_reader :client
5
+ def initialize(site, client_id, secret)
6
+ @client = OAuth::Consumer.new(client_id, secret, site: site)
7
+ end
8
+
9
+ def list
10
+ JSON.parse(client.request(:get, '/api/1.0/pads/all').body)
11
+ end
12
+
13
+ def get(id)
14
+ body = client.request(:get, "/api/1.0/pad/#{id}/content/latest.html").body
15
+ html_doc = Nokogiri::HTML(body)
16
+ fix_data_missing(body)
17
+ %Q(#{html_doc.at_css('h1:first').text}\n#{body})
18
+ end
19
+
20
+ def create(body)
21
+ JSON.parse(client.request(:post, '/api/1.0/pad/create', nil, {}, body, {
22
+ 'Content-Type' => 'text/html'
23
+ }).body)
24
+ end
25
+
26
+ def update(pid, body)
27
+ JSON.parse(client.request(:post, "/api/1.0/pad/#{pid}/content", nil, {}, body, {
28
+ 'Content-Type' => 'text/html'
29
+ }).body)
30
+ end
31
+
32
+ private
33
+ def fix_data_missing(body)
34
+ body.gsub!('class="taskdone"', 'class="listtype-taskdone listindent2 list-taskdone2"')
35
+ body.gsub!('class="task"', 'class="listtype-task listindent2 list-task2"')
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ module Hackpad
2
+ module Migration
3
+ class Migrator
4
+ attr_reader :source, :target, :result
5
+ def initialize(last_result)
6
+ options = last_result.user_options
7
+ @source = API.new(options['source_site'], options['source_client_id'], options['source_secret'])
8
+ @target = API.new(options['target_site'], options['target_client_id'], options['target_secret'])
9
+ @result = last_result
10
+ end
11
+
12
+ def migrate
13
+ source.list.each do |source_pid|
14
+ body = source.get(source_pid)
15
+ # pad {"padId"=>"RAtutOXxMGF1", "globalPadId"=>"2$RAtutOXxMGF1"}
16
+ target_pid = result.source_and_target_map[source_pid]
17
+ if target_pid.nil?
18
+ pad = target.create(body)
19
+ result.add_source_and_target(source_pid, pad['padId'])
20
+ puts "created pad #{pad['padId']} from source pad #{source_pid}"
21
+ result.write!
22
+ else
23
+ puts "Updating pad #{target_pid} from source pad #{source_pid}"
24
+ pad = target.update(target_pid, body)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ module Hackpad
2
+ module Migration
3
+ class Result
4
+ def initialize(out_file)
5
+ @out_file = out_file
6
+ end
7
+
8
+ def result
9
+ @result ||= File.exist?(@out_file) ? JSON.parse(open(@out_file).read) : {}
10
+ end
11
+
12
+ def write!
13
+ open(@out_file, 'w+') do |f|
14
+ f.write(to_json)
15
+ end
16
+ end
17
+
18
+ def user_options
19
+ @user_options ||= result['user_options'] || {}
20
+ end
21
+
22
+ def source_and_target_map
23
+ @source_and_target_map ||= result['source_and_target_map'] || {}
24
+ end
25
+
26
+ def add_user_option(key, vlaue)
27
+ user_options[key] = value
28
+ end
29
+
30
+ def add_source_and_target(source, target)
31
+ source_and_target_map[source] = target
32
+ end
33
+
34
+ def to_json
35
+ JSON.pretty_generate(
36
+ user_options: user_options,
37
+ source_and_target_map: source_and_target_map,
38
+ updated_at: Time.now
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ module Hackpad
2
+ module Migration
3
+ class Runner < Thor
4
+ include Thor::Actions
5
+
6
+ default_task :migrate
7
+ desc 'migrate', 'start migration with output file'
8
+ # method_option :source_site, desc: 'The source site for migration', required: true
9
+ # method_option :source_client_id, desc: 'Hackpad client id for source site', required: true
10
+ # method_option :source_secret, desc: 'Hackpad secret key for source site', required: true
11
+ # method_option :target_site, desc: 'The target site for migration', required: true
12
+ # method_option :target_client_id, desc: 'Hackpad client id for target site', required: true
13
+ # method_option :target_secret, desc: 'Hackpad secret key for target site', required: true
14
+ method_option :output, desc: 'file path for the result'
15
+ def migrate
16
+ out_file = options['output'].nil? ? 'result.json' : options['output']
17
+ last_result = Result.new(out_file)
18
+ if last_result.user_options['source_site'].nil?
19
+ last_result.user_options['source_site'] = ask 'Please entry the source site that you want to migrate (example: https://example.hackpad.com): '
20
+ last_result.write!
21
+ end
22
+ if last_result.user_options['source_client_id'].nil?
23
+ last_result.user_options['source_client_id'] = ask 'Please entry the client id that you want to migrate (example: xLR8qInsYLG): '
24
+ last_result.write!
25
+ end
26
+ if last_result.user_options['source_secret'].nil?
27
+ last_result.user_options['source_secret'] = ask 'Please entry the secret that you want to migrate (example: XsDYswLE1QogALkdFgsfvhRW4YJUvGre): '
28
+ last_result.write!
29
+ end
30
+ if last_result.user_options['target_site'].nil?
31
+ last_result.user_options['target_site'] = ask 'Please entry the target site that you want to migrate (example: https://example.hackpad.com): '
32
+ last_result.write!
33
+ end
34
+ if last_result.user_options['target_client_id'].nil?
35
+ last_result.user_options['target_client_id'] = ask 'Please entry the client id that you want to migrate (example: xLR8qInsYLG): '
36
+ last_result.write!
37
+ end
38
+ if last_result.user_options['target_secret'].nil?
39
+ last_result.user_options['target_secret'] = ask 'Please entry the secret that you want to migrate (example: XsDYswLE1QogALkdFgsfvhRW4YJUvGre): '
40
+ last_result.write!
41
+ end
42
+ Hackpad::Migration::Migrator.new(last_result).migrate
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Hackpad
2
+ module Migration
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hackpad-migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Doni Leung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.19.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.19.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '8.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '8.2'
111
+ description: A tool for migrate Hackpad from one Hackpad site to other Hackpad site
112
+ via Hackpad's APIs.
113
+ email:
114
+ - d@ii2d.com
115
+ executables:
116
+ - hackpad-migrate
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/hackpad-migrate
129
+ - bin/setup
130
+ - hackpad-migration.gemspec
131
+ - lib/hackpad/migration.rb
132
+ - lib/hackpad/migration/api.rb
133
+ - lib/hackpad/migration/migrator.rb
134
+ - lib/hackpad/migration/result.rb
135
+ - lib/hackpad/migration/runner.rb
136
+ - lib/hackpad/migration/version.rb
137
+ homepage: https://github.com/kudelabs/hackpad-migration.git
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.4.8
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Hackpad migrator
161
+ test_files: []
162
+ has_rdoc: