Module: GitHubBackup::GitHub

Defined in:
lib/github/repos.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optsObject

Returns the value of attribute opts.



7
8
9
# File 'lib/github/repos.rb', line 7

def opts
  @opts
end

Class Method Details

.backup_repo(repo) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/github/repos.rb', line 33

def backup_repo(repo)
    Dir.chdir(opts[:bakdir])

    repo['repo_path'] = "#{opts[:bakdir]}/#{repo['name']}"

    clone repo unless File.exists?(repo['repo_path'])
    fetch_changes repo
    get_forks repo if opts[:forks] and repo['forks'] > 1
    create_all_branches repo if opts[:init_branches]
    dump_issues repo if opts[:issues] && repo['has_issues']
    dump_wiki repo if opts[:wiki] && repo['has_wiki']
    repack repo if opts[:repack]
end

.backup_reposObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/github/repos.rb', line 12

def backup_repos()
    # get all repos
    (1..100).each do |i|
        if opts[:organization]
          url = "/orgs/#{opts[:organization]}/repos"
        elsif opts[:passwd]
          url ="/user/repos"
        else
          url = "/users/#{opts[:username]}/repos"
        end
        repos = json("#{url}?page=#{i}per_page=100")
        repos.each do |f|
            # do we limit to a specific repo?
            next unless f['name'] == opts[:reponame] if opts[:reponame]
            backup_repo f
        end
        break if repos.size == 0
    end

end

.clone(repo) ⇒ Object



47
48
49
# File 'lib/github/repos.rb', line 47

def clone(repo)
    %x{git clone #{repo['ssh_url']}}
end

.create_all_branches(repo) ⇒ Object



77
78
79
80
# File 'lib/github/repos.rb', line 77

def create_all_branches(repo)
    Dir.chdir(repo['repo_path']) 
    %x{for remote in `git branch -r`; do git branch --track $remote; done}
end

.dump_issues(repo) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/github/repos.rb', line 82

def dump_issues(repo)
    Dir.chdir(repo['repo_path'])

    filename = repo['repo_path'] + "/issues_dump.txt"
    FileUtils.rm  filename if File.exists?(filename)

    content = ''
    (1..100).each do |i|
        if opts[:organization]
          url = "/repos/#{opts[:organization]}/#{repo['name']}/issues"
        else
          url = "/repos/#{opts[:username]}/#{repo['name']}/issues"
        end
        issues = json("#{url}?page=#{i}&per_page=100")
        content += issues.join("")
        break if issues.size == 0
    end

    File.open(filename, 'w') {|f| f.write(content)}
end

.dump_wiki(repo) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/github/repos.rb', line 103

def dump_wiki(repo)
    Dir.chdir(opts[:bakdir])
    wiki_path = "#{opts[:bakdir]}/#{repo['name']}.wiki"
    %x{git clone [email protected]:#{repo['owner']['login']}/#{repo['name']}.wiki.git} unless File.exists?(wiki_path)
    if File.exists? wiki_path
        Dir.chdir(wiki_path)
        %x{git fetch origin}
    end
end

.fetch_changes(repo) ⇒ Object



51
52
53
54
# File 'lib/github/repos.rb', line 51

def fetch_changes(repo)
    Dir.chdir(repo['repo_path']) 
    %x{git fetch origin}
end

.get_forks(repo) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/github/repos.rb', line 56

def get_forks(repo)
    Dir.chdir(repo['repo_path'])

    # do we get all forks
    (1..100).each do |i|
        if opts[:organization]
          url = "/repos/#{opts[:organization]}/#{repo['name']}/forks"
        else
          url = "/repos/#{opts[:username]}/#{repo['name']}/forks"
        end
        forks = json("#{url}?page=#{i}&per_page=100")
        pp forks
        forks.each do |f|
          puts "Adding remote #{f['owner']['login']} from #{f['ssh_url']}.."
            %x{git remote add #{f['owner']['login']} #{f['ssh_url']}}
            %x{git fetch #{f['owner']['login']}}
        end
        break if forks.size == 0
    end
end

.json(url) ⇒ Object



118
119
120
121
# File 'lib/github/repos.rb', line 118

def json(url)
    auth = {:username => opts[:username], :password => opts[:passwd]} if opts[:username] and opts[:passwd]
    HTTParty.get('https://api.github.com' << url, :basic_auth => auth, :headers => { "User-Agent" => "Get out of the way, Github" }).parsed_response
end

.options=(v) ⇒ Object



8
9
10
# File 'lib/github/repos.rb', line 8

def options=(v)
    self.opts = v
end

.repack(repo) ⇒ Object



113
114
115
116
# File 'lib/github/repos.rb', line 113

def repack(repo)
    Dir.chdir(repo['repo_path'])
    %x{git gc --aggressive --auto}
end