module GitHubBackup::GitHub

Attributes

opts[RW]

Public Class Methods

backup_repo(repo) click to toggle source
# 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_repos() click to toggle source
# 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) click to toggle source
# File lib/github/repos.rb, line 47
def clone(repo)
    %x{git clone #{repo['ssh_url']}}
end
create_all_branches(repo) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 git@github.com:#{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) click to toggle source
# File lib/github/repos.rb, line 51
def fetch_changes(repo)
    Dir.chdir(repo['repo_path']) 
    %x{git fetch origin}
end
get_forks(repo) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/github/repos.rb, line 8
def options=(v)
    self.opts = v
end
repack(repo) click to toggle source
# File lib/github/repos.rb, line 113
def repack(repo)
    Dir.chdir(repo['repo_path'])
    %x{git gc --aggressive --auto}
end