Class: Gitorious
- Inherits:
-
Object
- Object
- Gitorious
- Defined in:
- lib/rock/gitorious_api.rb
Defined Under Namespace
Classes: NamePath
Instance Attribute Summary collapse
-
#hostname ⇒ Object
Returns the value of attribute hostname.
Instance Method Summary collapse
-
#ask_for(message, array_of_namepath) ⇒ Object
asks the user to choose on of namepath.
-
#ask_for_branch(git, message) ⇒ Object
asks user to select brnach.
-
#ask_for_item(message, array) ⇒ Object
asks the user to choose on of namepath.
-
#ask_for_package_set(message) ⇒ Object
asks user to select a package set.
-
#ask_for_project(message) ⇒ Object
asks user to select a project.
-
#authorization?(project, user_name, password) ⇒ Boolean
check for authorization.
-
#create_git(project, git_name, description, user_name, password) ⇒ Object
creates a new git at @http_uri/project.
-
#dir_git(git, branch = 'master', reg = nil) ⇒ Object
returns a list of all root files.
-
#git?(project_path, git_name) ⇒ Boolean
returns true if the project has a git with the given name.
-
#git_branches(git) ⇒ Object
return the branches of the git.
-
#gits(project) ⇒ Object
returns all gits which are hosted at @http_uri/project.
- #http_uri ⇒ Object
-
#initialize(hostname) ⇒ Gitorious
constructor
A new instance of Gitorious.
-
#log_in?(user_name, password) ⇒ Boolean
check for authonticatin.
-
#package_set_name(path, branch = 'master') ⇒ Object
returns the name of the package set which is set in source.yml.
-
#projects(filter = nil) ⇒ Object
returns all projects which are hosted at @http_uri.
-
#raw_file(path) ⇒ Object
returns the raw file from http address.
-
#raw_file_from_git(git, file, branch = 'master') ⇒ Object
returns the raw file from git and branch.
-
#repository_url(repositiory) ⇒ Object
return http url to repositiory.
Constructor Details
#initialize(hostname) ⇒ Gitorious
Returns a new instance of Gitorious
8 9 10 |
# File 'lib/rock/gitorious_api.rb', line 8 def initialize(hostname) @hostname = hostname end |
Instance Attribute Details
#hostname ⇒ Object
Returns the value of attribute hostname
6 7 8 |
# File 'lib/rock/gitorious_api.rb', line 6 def hostname @hostname end |
Instance Method Details
#ask_for(message, array_of_namepath) ⇒ Object
asks the user to choose on of namepath
113 114 115 116 117 118 119 120 121 |
# File 'lib/rock/gitorious_api.rb', line 113 def ask_for(,array_of_namepath) result = choose do || .prompt = array_of_namepath.each do |x| .choice x.name.to_sym end end array_of_namepath.find {|x| x.name==result.to_s} end |
#ask_for_branch(git, message) ⇒ Object
asks user to select brnach
140 141 142 143 144 |
# File 'lib/rock/gitorious_api.rb', line 140 def ask_for_branch(git,) branch = git_branches(git) return branch[0] if branch.size == 1 ask_for_item(,branch) end |
#ask_for_item(message, array) ⇒ Object
asks the user to choose on of namepath
124 125 126 127 128 129 130 131 132 |
# File 'lib/rock/gitorious_api.rb', line 124 def ask_for_item(,array) result = choose do || .prompt = array.each do |x| .choice x.to_sym end end result.to_s end |
#ask_for_package_set(message) ⇒ Object
asks user to select a package set
135 136 137 |
# File 'lib/rock/gitorious_api.rb', line 135 def ask_for_package_set() ask_for(,package_sets) end |
#ask_for_project(message) ⇒ Object
asks user to select a project
147 148 149 |
# File 'lib/rock/gitorious_api.rb', line 147 def ask_for_project() ask_for(,projects) end |
#authorization?(project, user_name, password) ⇒ Boolean
check for authorization
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/rock/gitorious_api.rb', line 171 def (project,user_name, password) url = URI.parse(http_uri+project) req = Net::HTTP::Get.new(url.path) req.basic_auth user_name, password res = Net::HTTP.new(url.host, url.port).start{|http| http.request(req)} case res when Net::HTTPSuccess , Net::HTTPRedirection if nil != res.body.match(/edit/) return 1 else return 0 end end end |
#create_git(project, git_name, description, user_name, password) ⇒ Object
creates a new git at @http_uri/project
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rock/gitorious_api.rb', line 187 def create_git(project,git_name,description,user_name,password) url = URI.parse(http_uri+project+'/repositories') req = Net::HTTP::Post.new(url.path) req.basic_auth user_name, password req.set_form_data({'repository[name]'=> git_name, 'repository[description]'=>description, 'repository[merge_requests_enabled]'=> 1}, ';') res = Net::HTTP.new(url.host, url.port).start do |http| http.request(req) end case res when Net::HTTPSuccess, Net::HTTPRedirection return 0 if nil == res.body.match(/redirected/) sleep 5 else return 0 end return 1 end |
#dir_git(git, branch = 'master', reg = nil) ⇒ Object
returns a list of all root files
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rock/gitorious_api.rb', line 88 def dir_git(git,branch='master',reg=nil) path = http_uri + git + '/trees/master' raw = raw_file(path) # find all files array = Array.new reg = Regexp.new(reg) if reg.is_a?(String) raw.scan(/file.*\n.*<a href="\/.*>(.*)<\/a>.*<\/td>/) do |name| if reg array << name.to_s if name.to_s.match(reg) else array << name.to_s end end return array end |
#git?(project_path, git_name) ⇒ Boolean
returns true if the project has a git with the given name
51 52 53 54 55 56 |
# File 'lib/rock/gitorious_api.rb', line 51 def git?(project_path,git_name) result = gits(project_path) result.find do |x| x.name.to_s == git_name end end |
#git_branches(git) ⇒ Object
return the branches of the git
59 60 61 62 63 64 65 66 |
# File 'lib/rock/gitorious_api.rb', line 59 def git_branches(git) array = Array.new body = raw_file(http_uri + git) body.scan(/" title=".*>(.*)<\/a><\/li>/) do |branch| array << branch end array.flatten end |
#gits(project) ⇒ Object
returns all gits which are hosted at @http_uri/project
41 42 43 44 45 46 47 48 |
# File 'lib/rock/gitorious_api.rb', line 41 def gits(project) array = Array.new body = raw_file(http_uri+"/"+project) body.scan(/<h3 mainline>.*\n.*<a href="(.*)">(.*)<\/a>/) do |path,name| array << NamePath.new(name,path) end return array end |
#http_uri ⇒ Object
12 13 14 |
# File 'lib/rock/gitorious_api.rb', line 12 def http_uri "http://#{hostname}" end |
#log_in?(user_name, password) ⇒ Boolean
check for authonticatin
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rock/gitorious_api.rb', line 152 def log_in?(user_name, password) url = URI.parse(http_uri+'/') req = Net::HTTP::Post.new(url.path) req.basic_auth user_name , password res = Net::HTTP.new(url.host, url.port).start{|http| http.request(req)} case res when Net::HTTPSuccess , Net::HTTPRedirection if nil != res.body.match(/Logout/) return 1 else return 0 end else res.error! return 2 end end |
#package_set_name(path, branch = 'master') ⇒ Object
returns the name of the package set which is set in source.yml
105 106 107 108 109 110 |
# File 'lib/rock/gitorious_api.rb', line 105 def package_set_name(path, branch='master') file = dir_git(path,branch,/source\.yml/) raw = raw_file_from_git(path,file[0]) yaml = YAML.load(raw) yaml["name"] end |
#projects(filter = nil) ⇒ Object
returns all projects which are hosted at @http_uri
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rock/gitorious_api.rb', line 17 def projects(filter=nil) array = Array.new i = 1; new_data = true while(new_data) new_data = false body = nil if(filter) body = raw_file(http_uri+"/search?page=#{i}&q=#{filter}") else body = raw_file(http_uri+"/projects?page=#{i}") end reg = Regexp.new(filter) if filter body.scan(/<h3><a href="(.*)">(.*)<\/a>/) do |path,name| puts "Found project with name #{name}" array << NamePath.new(name,path) new_data = true end i = i + 1 end return array end |
#raw_file(path) ⇒ Object
returns the raw file from http address
80 81 82 83 84 85 |
# File 'lib/rock/gitorious_api.rb', line 80 def raw_file(path) good_path = path.gsub(/ /, '-') url = URI.parse(good_path) res = Net::HTTP.get_response(url) return res.body end |
#raw_file_from_git(git, file, branch = 'master') ⇒ Object
returns the raw file from git and branch
69 70 71 72 |
# File 'lib/rock/gitorious_api.rb', line 69 def raw_file_from_git(git,file,branch ='master') path = http_uri + git + "/blobs/raw/#{branch}/" + file raw_file(path) end |
#repository_url(repositiory) ⇒ Object
return http url to repositiory
75 76 77 |
# File 'lib/rock/gitorious_api.rb', line 75 def repository_url(repositiory) http_uri + repositiory.path end |