Class: Lhbackup::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/lhbackup/backup.rb

Instance Method Summary collapse

Constructor Details

#initialize(account, token) ⇒ Backup

Returns a new instance of Backup.



10
11
12
13
# File 'lib/lhbackup/backup.rb', line 10

def initialize(, token)
  Lighthouse. = 
  Lighthouse.token = token
end

Instance Method Details

#curl(url) ⇒ Object



94
95
96
97
98
# File 'lib/lhbackup/backup.rb', line 94

def curl(url)
  output = File.join(@folder, URI(url).path)
  "curl -H 'X-LighthouseToken: #{Lighthouse.token}' #{url} -o #{output} -L --create-dirs"
  # -C - would continue downloads
end

#find_attachments(data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lhbackup/backup.rb', line 69

def find_attachments(data)
  print "- finding #{data.sum(&:attachments_count)} attachments"

  attachment_data = data.map do |obj|
    next if obj.attachments_count.zero?

    # individual tickets respond to attachments, but the listed ones don't
    attachments = Lighthouse::Ticket.find(obj.number, :params => {:project_id => @project.id}).attachments
    putc '.'

    attachments.each do |a|
      if a.respond_to?(:url)
        @attachments << a.url
      elsif a.respond_to?(:image) && a.image.respond_to?(:url)
        @attachments << a.image.url
      else
        puts "Whats the url for this attachment? #{a.to_json}"
      end
    end
    {obj.number => attachments} # ticket to attachments
  end.compact

  store(attachment_data, File.join(@folder, "ticket-attachments.json"))
end

#load_pagesObject

results are paginated, get them all



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lhbackup/backup.rb', line 111

def load_pages
  p = 1
  results = []
  while true
    result = yield p
    # TODO error cases
    break if result.count.zero?
    results += result
    p = p + 1
    putc "."
  end
  puts
  results
end

#retrieve_project(project) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lhbackup/backup.rb', line 26

def retrieve_project(project)
  @project = project
  @attachments = Set.new

  # subfolder for the project
  @folder = File.join(FileUtils.pwd, @project.permalink)

  # unpaginated resources
  retrieve_resource "memberships"
  retrieve_resource "bins"

  # paginated resources
  %w{milestones tickets messages changesets}.each do |resource|
    retrieve_resource resource, :paginated
  end

  # attachments
  puts "#{@attachments.count} attachments to download"

  File.open(File.join(@folder, "attachments.sh"), "w") do |f|
    f.write(@attachments.map {|url| curl(url)}.join("\n"))
  end
  system(File.join(@folder, "attachments.sh"))
end

#retrieve_resource(resource, paginated = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lhbackup/backup.rb', line 51

def retrieve_resource(resource, paginated = false)
  print "- retreiving #{resource}"

  data = if paginated
    load_pages do |p|
      @project.send(resource, :page => p)
    end
  else
    @project.send(resource)
    puts
  end

  store(data, File.join(@folder, "#{resource}.json"))

  find_attachments(data) if resource == "tickets"
  # TODO: messages have attachments too (but on the comments)
end

#runObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/lhbackup/backup.rb', line 15

def run
  print "Retreiving projects..."
  projects = Lighthouse::Project.find(:all)
  store(projects, 'projects.json')

  projects.each do |project|
    puts "#{project.permalink}:"
    retrieve_project(project)
  end
end

#store(data, file) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/lhbackup/backup.rb', line 100

def store(data, file)
  FileUtils.mkdir_p(File.dirname(file)) if File.dirname(file).present?

  puts "  saving #{File.basename(file)}"

  File.open(file, "w") do |f|
    f.write(data.to_json)
  end
end