Class: Gitorious

Inherits:
Object
  • Object
show all
Defined in:
lib/rock/gitorious_api.rb

Defined Under Namespace

Classes: NamePath

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#hostnameObject

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(message,array_of_namepath)
    result = choose do |menu|
        menu.prompt = message
        array_of_namepath.each do |x|
            menu.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,message)
    branch = git_branches(git)
    return branch[0] if branch.size == 1
    ask_for_item(message,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(message,array)
    result = choose do |menu|
        menu.prompt = message
        array.each do |x|
            menu.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(message)
    ask_for(message,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(message)
    ask_for(message,projects)
end

#authorization?(project, user_name, password) ⇒ Boolean

check for authorization

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rock/gitorious_api.rb', line 171

def authorization?(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

Returns:

  • (Boolean)


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_uriObject



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

Returns:

  • (Boolean)


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