Skip to content

Commit

Permalink
Merge pull request #3928 from mdeniz/refactor_backend_api_methods
Browse files Browse the repository at this point in the history
Refactor Backend::Api methods
  • Loading branch information
David Kang committed Oct 2, 2017
2 parents ded6629 + 28937e1 commit f7e60e8
Show file tree
Hide file tree
Showing 55 changed files with 11,347 additions and 148 deletions.
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/api/app/assets/javascripts/webui/application/cm2/
src/api/vendor/assets/javascripts/*.min.js
src/api/vendor/bundle/
src/api/lib/backend/doc
1 change: 1 addition & 0 deletions src/api/lib/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yardoc
5 changes: 0 additions & 5 deletions src/api/lib/backend/api.rb

This file was deleted.

7 changes: 0 additions & 7 deletions src/api/lib/backend/api/build_results.rb

This file was deleted.

31 changes: 14 additions & 17 deletions src/api/lib/backend/api/build_results/binaries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,48 @@ module Backend
module Api
module BuildResults
class Binaries
extend Backend::ConnectionHelper

# Returns a file list of binaries
def self.files(project, repository, arch, package)
Backend::Connection.get("/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(arch)}/#{CGI.escape(package)}").body
get(["/build/:project/:repository/:arch/:package", project, repository, arch, package])
end

# Returns the jobs history for a project
def self.job_history(project, repository, arch)
Backend::Connection.get("/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(arch)}/_jobhistory?code=lastfailures").body
def self.job_history(project, repository, architecture)
get(["/build/:project/:repository/:architecture/_jobhistory", project, repository, architecture], params: { code: :lastfailures })
end

# Returns the download url for a file of a package
def self.download_url_for_file(project, repository, package, architecture, file)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/#{CGI.escape(package)}/#{CGI.escape(file)}"
Backend::Connection.get("#{path}?view=publishedpath").body
get(["/build/:project/:repository/:architecture/:package/:file", project, repository, architecture, package, file],
params: { view: :publishedpath })
end

# Returns the RPMlint log
def self.rpmlint_log(project, package, repository, architecture)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/#{CGI.escape(package)}/rpmlint.log"
Backend::Connection.get(path).body.force_encoding("UTF-8")
get(["/build/:project/:repository/:architecture/:package/rpmlint.log", project, repository, architecture, package])
end

# Returns the build dependency information
def self.build_dependency_info(project, package, repository, architecture)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/_builddepinfo"
path += "?package=#{CGI.escape(package)}&view=pkgnames"
Backend::Connection.get(path).body.force_encoding("UTF-8")
get(["/build/:project/:repository/:architecture/_builddepinfo", project, repository, architecture],
params: { package: package, view: :pkgnames })
end

# Returns the available binaries for the project
def self.available_in_project(project)
path = "/build/#{CGI.escape(project)}/_availablebinaries"
transform_binary_packages_response(Backend::Connection.get(path).body.force_encoding("UTF-8"))
transform_binary_packages_response(get(["/build/:project/_availablebinaries", project]))
end

# Returns the available binaries for the repositories given
def self.available_in_repositories(project, urls, repositories)
return {} if repositories.empty? && urls.empty?
path = "/build/#{CGI.escape(project)}/_availablebinaries"
query = urls.map { |value| value.to_query(:url) }
query += repositories.map { |value| value.to_query(:path) }
path += "?#{query.join('&')}"
transform_binary_packages_response(Backend::Connection.get(path).body.force_encoding("UTF-8"))
transform_binary_packages_response(get(["/build/:project/_availablebinaries", project],
params: { url: urls, path: repositories }, expand: [:url, :path]))
end

# TODO: Move this method that transforms the output into another module
# Transforms the output of the available_in_repositories, available_in_urls and available_in_project methods to a hash containing
# the name of the binary as keys and the architectures as the value
def self.transform_binary_packages_response(response)
Expand Down
20 changes: 8 additions & 12 deletions src/api/lib/backend/api/build_results/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,32 @@ module Backend
module Api
module BuildResults
class Status
extend Backend::ConnectionHelper

# Returns a chunk of the build's log
def self.log_chunk(project, package, repository, architecture, starting, ending)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/#{CGI.escape(package)}/_log"
path += "?nostream=1&start=#{starting.to_i}&end=#{ending.to_i}"
Backend::Connection.get(path).body.force_encoding("UTF-8")
endpoint = ["/build/:project/:repository/:architecture/:package/_log", project, repository, architecture, package]
get(endpoint, params: { nostream: 1, start: starting.to_i, end: ending.to_i })
end

# Returns the job status of a build
def self.job_status(project, package, repository, architecture)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/#{CGI.escape(package)}/_jobstatus"
Backend::Connection.get(path).body
get(["/build/:project/:repository/:architecture/:package/_jobstatus", project, repository, architecture, package])
end

# Returns the result view for a build
def self.build_result(project, package, repository, architecture)
path = "/build/#{CGI.escape(project)}/_result"
path += "?view=status&package=#{CGI.escape(package)}&arch=#{CGI.escape(architecture)}&repository=#{CGI.escape(repository)}"
Backend::Connection.get(path).body.force_encoding("UTF-8")
get(["/build/:project/_result", project], params: { view: :status, package: package, arch: architecture, repository: repository })
end

# Returns the log's size for a build
def self.build_log_size(project, package, repository, architecture)
path = "/build/#{CGI.escape(project)}/#{CGI.escape(repository)}/#{CGI.escape(architecture)}/#{CGI.escape(package)}/_log?view=entry"
Backend::Connection.get(path).body
get(["/build/:project/:repository/:architecture/:package/_log", project, repository, architecture, package], params: { view: :entry })
end

# Returns the log's size for a build
def self.build_problems(project)
path = "/build/#{CGI.escape(project)}/_result?view=status&code=failed&code=broken&code=unresolvable"
Backend::Connection.get(path).body
get(["/build/:project/_result", project], params: { view: :status, code: [:failed, :broken, :unresolvable] }, expand: [:code])
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/api/lib/backend/api/build_results/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Backend
module Api
module BuildResults
class Worker
extend Backend::ConnectionHelper

# Returns the worker status
def self.status
Backend::Connection.get('/build/_workerstatus').body.force_encoding("UTF-8")
get('/build/_workerstatus')
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/api/lib/backend/api/published.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
module Backend
module Api
class Published
extend Backend::ConnectionHelper

# Returns the download url for a repository
def self.download_url_for_repository(project, repository)
Backend::Connection.get("/published/#{CGI.escape(project)}/#{CGI.escape(repository)}?view=publishedpath").body
get(['/published/:project/:repository', project, repository], params: { view: :publishedpath })
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/api/lib/backend/api/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
module Backend
module Api
class Search
extend Backend::ConnectionHelper

# Performs a search of the binary in a project list
def self.binary(projects, name)
project_list = projects.map { |project| "@project='#{CGI.escape(project.name)}'" }.join('+or+')
Backend::Connection.post("/search/published/binary/id?match=(@name='#{CGI.escape(name)}'+and+(#{project_list}))").body
post("/search/published/binary/id?match=(@name='#{CGI.escape(name)}'+and+(#{project_list}))")
end
end
end
Expand Down
26 changes: 14 additions & 12 deletions src/api/lib/backend/api/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
module Backend
module Api
class Server
# Returns the notification payload for that key (from src/api/app/models/binary_release.rb)
def self.notification_payload(key)
Backend::Connection.get("/notificationpayload/#{key}").body
extend Backend::ConnectionHelper

# Returns the notification payload
def self.notification_payload(notification)
get(["/notificationpayload/:notification", notification])
end

# Deletes the notification payload for that key (from src/api/app/models/binary_release.rb)
def self.delete_notification_payload(key)
Backend::Connection.delete("/notificationpayload/#{key}")
# Deletes the notification payload
def self.delete_notification_payload(notification)
delete(["/notificationpayload/:notification", notification])
end

# It writes the configuration XML
def self.write_configuration(xml)
Backend::Connection.put('/configuration', xml)
# It writes the configuration
def self.write_configuration(configuration)
put('/configuration', data: configuration)
end

# Returns the latest notifications specifying a starting point
def self.last_notifications(start)
Backend::Connection.get("/lastnotifications?start=#{CGI.escape(start.to_s)}&block=1").body
get("/lastnotifications", params: { start: start, block: 1 })
end

# Notifies a certain plugin with the payload
def self.notify_plugin(plugin, payload)
Backend::Connection.post("/notify_plugins/#{plugin}", Yajl::Encoder.encode(payload), 'Content-Type' => 'application/json').body
post(["/notify_plugins/:plugin", plugin], data: Yajl::Encoder.encode(payload), headers: { 'Content-Type' => 'application/json' })
end

# Pings the root of the backend
def self.root
Backend::Connection.get('/').body.force_encoding("UTF-8")
get('/')
end
end
end
Expand Down
7 changes: 0 additions & 7 deletions src/api/lib/backend/api/sources.rb

This file was deleted.

82 changes: 37 additions & 45 deletions src/api/lib/backend/api/sources/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,96 @@ module Backend
module Api
module Sources
module Package
# Returns the attribute content
extend Backend::ConnectionHelper

# Returns the attributes content
def self.attributes(project, package, revision)
path = "/source/#{CGI.escape(project)}/#{CGI.escape(package || '_project')}/_attribute?meta=1"
path += "&rev=#{CGI.escape(revision)}" if revision
Backend::Connection.get(path).body
params = { meta: 1 }
params[:rev] = revision if revision
get(["/source/:project/:package/_attribute", project, package || '_project'], params: params)
end

# Writes the xml for attributes
def self.write_attributes(project, package, login, xml, comment)
path = "/source/#{CGI.escape(project)}/#{CGI.escape(package || '_project')}/_attribute?meta=1&user=#{CGI.escape(login)}"
path += "&comment=#{CGI.escape(comment)}" if comment
Backend::Connection.put(path, xml)
# Writes the content in xml for attributes
def self.write_attributes(project, package, user, content, comment)
params = { meta: 1, user: user }
params[:comment] = comment if comment
put(["/source/:project/:package/_attribute", project, package || '_project'],
data: content, params: params)
end

# Returns a file list of the sources for a package
def self.files(project, package, options = {})
path = "/source/#{CGI.escape(project)}/#{CGI.escape(package)}"
path += "?#{options.to_query}" if options.present?
Backend::Connection.get(path).body.force_encoding("UTF-8")
get(["/source/:project/:package", project, package], params: options)
end

# Returns the revisions (mrev) list for a package
def self.revisions(project, package)
Backend::Connection.get("/source/#{CGI.escape(project)}/#{CGI.escape(package)}/_history?deleted=1&meta=1").body
get(["/source/:project/:package/_history", project, package], params: { meta: 1, deleted: 1 })
end

# Returns the meta file from a package
def self.meta(project, package)
Backend::Connection.get("/source/#{CGI.escape(project)}/#{CGI.escape(package)}/_meta").body.force_encoding('UTF-8')
get(["/source/:project/:package/_meta", project, package])
end

# It triggers all the services of a package (from src/api/app/controllers/webui/package_controller.rb)
# It triggers all the services of a package
def self.trigger_services(project, package, user)
Backend::Connection.post("/source/#{CGI.escape(project)}/#{CGI.escape(package)}?cmd=runservice&user=#{CGI.escape(user)}")
post(["/source/:project/:package", project, package], params: { cmd: :runservice, user: user })
end

# Writes the patchinfo
def self.write_patchinfo(project, package, login, xml, comment = nil)
path = "/source/#{CGI.escape(project)}/#{CGI.escape(package)}/_patchinfo?user=#{CGI.escape(login)}"
path += "&comment=#{CGI.escape(comment)}" if comment
Backend::Connection.put(path, xml)
def self.write_patchinfo(project, package, user, content, comment = nil)
params = { user: user }
params[:comment] = comment if comment
put(["/source/:project/:package/_patchinfo", project, package], data: content, params: params)
end

# Runs the command waitservice for that project/package
def self.wait_service(project, package)
Backend::Connection.post("/source/#{CGI.escape(project)}/#{CGI.escape(package)}?cmd=waitservice")
post(["/source/:project/:package", project, package], params: { cmd: :waitservice })
end

# Runs the command mergeservice for that project/package
def self.merge_service(project, package, login)
Backend::Connection.post("/source/#{CGI.escape(project)}/#{CGI.escape(package)}?cmd=mergeservice&user=#{CGI.escape(login)}")
def self.merge_service(project, package, user)
post(["/source/:project/:package", project, package], params: { cmd: :mergeservice, user: user })
end

# Runs the command runservice for that project/package
def self.run_service(project, package, login)
Backend::Connection.post("/source/#{CGI.escape(project)}/#{CGI.escape(package)}?cmd=runservice&user=#{CGI.escape(login)}")
def self.run_service(project, package, user)
post(["/source/:project/:package", project, package], params: { cmd: :runservice, user: user })
end

# Copy a package into another project
def self.copy(target_project, target_package, source_project, source_package, login, options = {})
path = "/source/#{CGI.escape(target_project)}/#{CGI.escape(target_package)}"
query_hash = { cmd: :copy, oproject: source_project, opackage: source_package, user: login }
query_hash.merge!(options.slice(:keeplink, :expand, :comment))
path += "?#{query_hash.to_query}"
Backend::Connection.post(path)
def self.copy(target_project, target_package, source_project, source_package, user, options = {})
post(["/source/:project/:package", target_project, target_package],
defaults: { cmd: :copy, oproject: source_project, opackage: source_package, user: user },
params: options, accepted: [:keeplink, :expand, :comment])
end

# Writes the link information of a package
def self.write_link(project, package, login, xml)
Backend::Connection.put("/source/#{CGI.escape(project)}/#{CGI.escape(package)}/_link?user=#{CGI.escape(login)}", xml)
def self.write_link(project, package, user, content)
put(["/source/:project/:package/_link", project, package], data: content, params: { user: user })
end

# Returns the source diff
def self.source_diff(project, package, options = {})
path = "/source/#{CGI.escape(project)}/#{CGI.escape(package)}"
query_hash = { cmd: :diff, view: :xml, withissues: 1 }
query_hash.merge!(options.slice(:rev, :orev, :opackage, :oproject, :linkrev, :olinkrev, :expand))
path += "?#{query_hash.to_query}"
Backend::Connection.post(path).body.force_encoding('UTF-8')
post(["/source/:project/:package", project, package], defaults: { cmd: :diff, view: :xml, withissues: 1 },
params: options, accepted: [:rev, :orev, :opackage, :oproject, :linkrev, :olinkrev, :expand])
end

# Runs the command rebuild for that project/package
def self.rebuild(project, package, options = {})
path = "/build/#{CGI.escape(project)}"
query_hash = { cmd: :rebuild, package: package }
query_hash.merge!(options.slice(:repository, :arch))
path += "?#{query_hash.to_query}"
Backend::Connection.post(path)
post(["/build/:project", project], defaults: { cmd: :rebuild, package: package }, params: options, accepted: [:repository, :arch])
end

# Returns the content of the source file
def self.file(project, package, filename)
Backend::Connection.get("/source/#{CGI.escape(project)}/#{CGI.escape(package)}/#{CGI.escape(filename)}").body.force_encoding('UTF-8')
get(["/source/:project/:package/:filename", project, package, filename])
end

# Writes the content of the source file
def self.write_file(project, package, filename, content = '')
Backend::Connection.put("/source/#{CGI.escape(project)}/#{CGI.escape(package)}/#{CGI.escape(filename)}", content)
put(["/source/:project/:package/:filename", project, package, filename], data: content)
end
end
end
Expand Down
Loading

0 comments on commit f7e60e8

Please sign in to comment.