Class: Zizu::GithubLib
- Inherits:
-
Object
- Object
- Zizu::GithubLib
- Defined in:
- lib/zizu/githublib.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
Instance Method Summary collapse
- #check_exists?(repo) ⇒ Boolean
- #delete_repository(name) ⇒ Object
- #download_files(tree, dir = nil) ⇒ Object
- #fork(user, repo) ⇒ Object
- #fork_exists?(full_name) ⇒ Boolean
- #get_latest_commit ⇒ Object
- #get_tree(id) ⇒ Object
-
#initialize ⇒ GithubLib
constructor
A new instance of GithubLib.
- #login ⇒ Object
- #set_repository_name(repository, new_name) ⇒ Object
Constructor Details
#initialize ⇒ GithubLib
7 8 9 |
# File 'lib/zizu/githublib.rb', line 7 def initialize login end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5 6 7 |
# File 'lib/zizu/githublib.rb', line 5 def api @api end |
Instance Method Details
#check_exists?(repo) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/zizu/githublib.rb', line 86 def check_exists?(repo) response = @api.repos.get( @login, repo ) if response.success? return true else return false end end |
#delete_repository(name) ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/zizu/githublib.rb', line 113 def delete_repository(name) puts @login puts "rollback" #response = @api.repos.delete( @login, name ) end |
#download_files(tree, dir = nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/zizu/githublib.rb', line 148 def download_files( tree, dir=nil ) tree.each do |t| if t["type"] == "tree" if dir.nil? new_path = t["path"] else new_path = "#{dir}/#{t["path"]}" end FileUtils.mkdir_p(new_path) download_files( get_tree(t["sha"]), new_path ) next else data = open(t["url"]) json = JSON.parse(data.read()) unless json["content"].nil? if dir.nil? f = File.open( t["path"], "w" ) else f = File.open( "#{dir}/#{t["path"]}", "w" ) end contents = Base64.decode64(json["content"]) f.write(contents) f.close end end end end |
#fork(user, repo) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/zizu/githublib.rb', line 41 def fork( user, repo ) unless fork_exists?("#{user}/#{repo}") response = @api.repos.forks.create( user, repo ) if response.success? Zizu::success("repository forked to: #{response[:git_url]}") return response[:git_url] end end return nil end |
#fork_exists?(full_name) ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zizu/githublib.rb', line 58 def fork_exists?(full_name) response = @api.repos.list( :user => @login ) response.each do |r| user, repo = r[:full_name].split("/") get_r = @api.repos.get( user, repo ) if get_r.success? if get_r[:fork] and get_r[:parent][:full_name] == full_name Zizu::fatal( "repository has already been forked: #{get_r[:git_url]}") return true end else Zizu::fatal("Error: #{get_r.status}") Zizu::fatal(get_r.body) end end return false end |
#get_latest_commit ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/zizu/githublib.rb', line 122 def get_latest_commit response = @api.repos.commits.list( USER, REPOSITORY ) if response.success? return response[0][:sha] else fatal("unable to retrieve latest commit hash") end end |
#get_tree(id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/zizu/githublib.rb', line 134 def get_tree(id) #latest = get_latest_commit response = @api.git_data.trees.get( USER, REPOSITORY, id ) if response.success? return response[:tree] else fatal("unable to retrieve tree for #{id}") end end |
#login ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/zizu/githublib.rb', line 12 def login if @api.nil? config = Gitlib.config @login = ENV["ZIZU_GIT_LOGIN"] || config["user.name"] if @login == USER Zizu::fatal("cannot fork a project that you own, aborting") end @password = ENV["ZIZU_GIT_PASSWORD"] || ask("git password: ") { |q| q.echo = "*" } if @login.nil? or @password.nil? Zizu::fatal("please set git config variable user.name or env variable ZIZU_GIT_LOGIN.") end @api = Github.new( login:@login, password:@password ) if @api.nil? Zizu::fatal("login failed") end end end |
#set_repository_name(repository, new_name) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/zizu/githublib.rb', line 98 def set_repository_name( repository, new_name ) response = @api.repos.edit( @login, repository, :name => new_name ) if response.success? Zizu::success("Repository successfully renamed to #{new_name}") Zizu::success(response[:git_url]) return response[:git_url] else Zizu::fatal("GithubLib Error: #{response.status}") return nil end end |