Class: GithubExport::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/github_export/command.rb

Constant Summary collapse

ASSETS_LIST_FILENAME =
'assets.txt'.freeze
ASSET_PATTERN =
/\!\[[^\[\]]+\]\(([^\(\)]+)\)/.freeze
CHECK_MSG_OK =
"\e[32mOK %s\e[0m".freeze
CHECK_MSG_NG =
"\e[31mNG %s\e[0m".freeze

Instance Method Summary collapse

Instance Method Details

#all(repo) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/github_export/command.rb', line 19

def all(repo)
  repository(repo)
  milestones(repo)
  releases(repo)
  labels(repo)
  issues(repo)
  comments(repo)
  events(repo)
  issue_events(repo)
  assets
end

#assetsObject



80
81
82
83
84
85
# File 'lib/github_export/command.rb', line 80

def assets
  files = Dir.glob(File.join(options[:output_dir], '**/*.json'))
  assets_list(*files)
  assets_download
  assets_check
end

#assets_checkObject



130
131
132
133
134
135
136
# File 'lib/github_export/command.rb', line 130

def assets_check
  read_from_output_file(ASSETS_LIST_FILENAME).lines.map(&:strip).each do |line|
    path = File.join(options[:output_dir], URI.parse(line).path)
    fmt = File.exist?(path) ? CHECK_MSG_OK : CHECK_MSG_NG
    puts fmt % path
  end
end

#assets_downloadObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/github_export/command.rb', line 99

def assets_download
  num = [options[:client_num].to_i, 1].max
  lines = read_from_output_file(ASSETS_LIST_FILENAME).lines.map(&:strip)
  tasks = lines.group_by.with_index{|_, i| i % num}
  tasks.each do |idx, task_lines|
    fork do
      task_lines.each do |url|
        uri = URI.parse(url)
        dest = File.join(options[:output_dir], uri.path)
        if options[:force] || !File.exist?(dest)
          verbose "DL #{url}"
          FileUtils.mkdir_p(File.dirname(dest))
          # Assets might be downloaded from S3 so we use curl (or httpclient?) without auth info instead of `client` object
          # File.binwrite(dest, client.get(url))
          cmd = "curl -f -o #{dest} #{url}"
          unless system(cmd)
            puts "Download Error #{cmd}"
          end
        else
          verbose "SKIP #{url}"
        end
      end
    end
    Process.waitall
  end
end

#assets_list(*files) ⇒ Object



89
90
91
92
93
94
# File 'lib/github_export/command.rb', line 89

def assets_list(*files)
  urls = files.map{|file|
    File.read(file).lines.map{|line| line.scan(ASSET_PATTERN)}.delete_if(&:empty?).flatten.uniq
  }.flatten.sort.uniq
  output_to_file(ASSETS_LIST_FILENAME, urls.join("\n"))
end

#comments(repo) ⇒ Object



58
59
60
# File 'lib/github_export/command.rb', line 58

def comments(repo)
  call_repo_method(repo, :issues_comments, 'comments.json', sort: 'created', direction: 'asc')
end

#events(repo) ⇒ Object



63
64
65
# File 'lib/github_export/command.rb', line 63

def events(repo)
  call_repo_method(repo, :repository_events, 'events.json', sort: 'created', direction: 'asc')
end

#issue_events(repo) ⇒ Object



73
74
75
# File 'lib/github_export/command.rb', line 73

def issue_events(repo)
  call_repo_method(repo, :repository_issue_events, 'issue_events.json', sort: 'created', direction: 'asc')
end

#issues(repo) ⇒ Object



53
54
55
# File 'lib/github_export/command.rb', line 53

def issues(repo)
  call_repo_method(repo, :list_issues, 'issues.json', state: 'all', sort: 'created', direction: 'asc')
end

#labels(repo) ⇒ Object



48
49
50
# File 'lib/github_export/command.rb', line 48

def labels(repo)
  call_repo_method(repo, :labels, 'labels.json')
end

#milestones(repo) ⇒ Object



38
39
40
# File 'lib/github_export/command.rb', line 38

def milestones(repo)
  call_repo_method(repo, :list_milestones, 'milestones.json')
end

#releases(repo) ⇒ Object



43
44
45
# File 'lib/github_export/command.rb', line 43

def releases(repo)
  call_repo_method(repo, :releases, 'releases.json')
end

#repository(repo) ⇒ Object



32
33
34
35
# File 'lib/github_export/command.rb', line 32

def repository(repo)
  result = client.repository(repo)
  output_to_file('repository.json', JSON.pretty_generate(result.to_attrs))
end