summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbackends/gitlab-ce221
-rwxr-xr-xbackends/gitlab-ee120
-rwxr-xr-xgit-mirror3
3 files changed, 192 insertions, 152 deletions
diff --git a/backends/gitlab-ce b/backends/gitlab-ce
index c91effc..84cb933 100755
--- a/backends/gitlab-ce
+++ b/backends/gitlab-ce
@@ -19,33 +19,20 @@ class GitLabCE < GitMirrorBackend
return @obj
end
end
+
def initialize()
@connections = {}
- @cache = {}
+ @projects = {}
+ @config = {}
+ end
- # API docs suck, look at `lib/api/projects.rb` instead.
- @vars = [
- :builds_enabled, # create | create-user | edit
- :container_registry_enabled, # XXX # create | | edit
- :default_branch, # | create-user | edit
- :description, # create | create-user | edit
- #:import_url, # XXX # create | create-user |
- :issues_enabled, # create | create-user | edit
- :lfs_enabled, # create | create-user | edit
- :merge_requests_enabled, # create | create-user | edit
- :name, # create | create-user | edit
- #:namespace_id, # create | |
- :only_allow_merge_if_build_succeeds, # create | create-user | edit
- #:path, # create | | edit
- :public, # create | create-user | edit
- :public_builds, # create | create-user | edit
- :request_access_enabled, # create | create-user | edit
- :shared_runners_enabled, # create | create-user | edit
- :snippets_enabled, # create | create-user | edit
- :visibility_level, # create | create-user | edit
- :wiki_enabled, # create | create-user | edit
- :only_allow_merge_if_all_discussions_are_resolved # create | create-user | edit
- ]
+ def connection(uri)
+ key=URI(uri.scheme+":")
+ key.host = uri.host
+ key.port = uri.port
+
+ @connections[key] ||= Net::HTTP::start(uri.host, uri.port, :use_ssl => uri.scheme == 'https')
+ return @connections[key]
end
def finish
@@ -56,51 +43,110 @@ class GitLabCE < GitMirrorBackend
super
end
- def _connection(uri)
- key=URI(uri.scheme+":")
- key.host = uri.host
- key.port = uri.port
+ def config
+ return @config
+ end
- @connections[key] ||= Net::HTTP::start(uri.host, uri.port, :use_ssl => uri.scheme == 'https')
- return @connections[key]
+ # Project
+ def project(project_id)
+ unless @projects.has_key?(project_id)
+ @projects[project_id] = Project.new(self, project_id)
+ end
+ return @projects[project_id]
end
+ class Project
+ def initialize(gl, project_id)
+ @gl = gl
+ @project_id = project_id
+ @cache = {}
+ end
+
+ def info
+ unless @cache.has_key?(:info)
+ req = Net::HTTP::Get.new(@gl.config['apiurl'] + "projects/" + CGI::escape(@project_id))
+ req.add_field("PRIVATE-TOKEN", @gl.config['apikey'])
+ res = @gl.connection(req.uri).request(req)
+ case res.code
+ when "200"
+ @cache[:info] = JSON::parse(res.body)
+ when "404"
+ @cache[:info] = nil
+ else
+ raise Error.new(res)
+ end
+ end
+ return @cache[:info]
+ end
+
+ def info=(i)
+ @cache[:info] = i
+ end
+
+ def get_meta
+ return self.info.select{|k,v| @vars.include?(k.to_sym)}
+ end
+
+ def set_meta(map)
+ mirror = map["mirror"]
+ map.delete("mirror")
+
+ illegal = map.select{|k,v| not @vars.include?(k.to_sym)}
+ if illegal.count > 0
+ raise Error.new(illegal)
+ end
- def _info(project_id)
- @cache[project_id] ||= {}
- unless @cache[project_id].has_key?(:info)
- req = Net::HTTP::Get.new(@api_uri + "projects/" + CGI::escape(project_id))
- req.add_field("PRIVATE-TOKEN", @api_key)
- con = _connection(req.uri)
- res = con.request(req)
- case res.code
- when "200"
- @cache[project_id][:info] = JSON::parse(res.body)
- when "404"
- @cache[project_id][:info] = nil
+ if info == nil
+ # create
+ req = Net::HTTP::Put.new(@gl.config['apiurl'] + "projects")
+ req.add_field("PRIVATE-TOKEN", @gl.config['apikey'])
+ req.add_field("Content-Type", "application/json")
+ map["path"] = @project_id
+ if not mirror.nil?
+ map["import_url"] = mirror
+ end
+ req.body = JSON::dump(map)
+ res = @gl.connection(req.uri).request(req)
+ if res.code != "201"
+ raise Error.new(res)
+ end
+ info = JSON::parse(res.body)
else
- raise Error.new(res)
+ # update
+ req = Net::HTTP::Put.new(@gl.config['apiurl'] + "projects/" + CGI::escape(info["id"].to_s))
+ req.add_field("PRIVATE-TOKEN", @gl.config['apikey'])
+ req.add_field("Content-Type", "application/json")
+ req.body = JSON::dump(map)
+ res = @gl.connection(req.uri).request(req)
+ if res.code != "200"
+ raise Error.new(res)
+ end
+ info = JSON::parse(res.body)
end
+ return self.get_meta
+ end
+
+ def repo_mode
+ return "passive"
end
- return @cache[project_id][:info]
end
+ # commands
def cmd_config(*args)
args.each do |arg|
key, val = arg.split('=', 2)
case key
when "apiurl"
- @api_uri = URI(val)
- unless @api_uri.path.end_with?("/")
- @api_uri.path += "/"
+ val = URI(val)
+ unless val.path.end_with?("/")
+ val.path += "/"
end
- when "apikey"
- @api_key = val
end
+ @config[key] = val
end
end
def cmd_get_meta(project_id)
- return _info(project_id).select{|k,v| @vars.include?(k.to_sym)}
+ return self.project(project_id).get_meta()
end
def cmd_set_meta(project_id, *pairs)
@@ -109,58 +155,45 @@ class GitLabCE < GitMirrorBackend
key, val = arg.split('=', 2)
map[key] = val
end
-
- mirror = map["mirror"]
- map.delete("mirror")
-
- illegal = map.select{|k,v| not @vars.include?(k.to_sym)}
- if illegal.count > 0
- raise Error.new(illegal)
- end
-
- info = self._info(project_id)
- if info == nil
- # create
- req = Net::HTTP::Put.new(@api_uri + "projects"))
- req.add_field("PRIVATE-TOKEN", @api_key)
- req.add_field("Content-Type", "application/json")
- map["path"] = project_id
- if not mirror.nil?
- map["import_url"] = mirror
- end
- req.body = JSON::dump(map)
- con = _connection(req.uri)
- res = con.request(req)
- if res.code != "201"
- raise Error.new(res)
- end
- @cache[project_id][:info] = JSON::parse(res.body)
- else
- # update
- req = Net::HTTP::Put.new(@api_uri + "projects/" + CGI::escape(info["id"].to_s))
- req.add_field("PRIVATE-TOKEN", @api_key)
- req.add_field("Content-Type", "application/json")
- req.body = JSON::dump(map)
- con = _connection(req.uri)
- res = con.request(req)
- if res.code != "200"
- raise Error.new(res)
- end
- @cache[project_id][:info] = JSON::parse(res.body)
- end
- return get_meta
+ return self.project(project_id).set_meta(map)
end
def cmd_push_url(project_id)
- return _info(project_id)["ssh_url_to_repo"]
+ return self.project(project_id).info["ssh_url_to_repo"]
end
def cmd_pull_url(project_id)
- return _info(project_id)["http_url_to_repo"]
+ return self.project(project_id).info["http_url_to_repo"]
end
- def cmd_repo_mode
- return "passive"
+ def cmd_repo_mode(project_id)
+ return self.project(projecT_id).repo_mode()
+ end
+
+ def vars
+ # API docs suck, look at `lib/api/projects.rb` instead.
+ return [
+ :builds_enabled, # create | create-user | edit
+ :container_registry_enabled, # XXX # create | | edit
+ :default_branch, # | create-user | edit
+ :description, # create | create-user | edit
+ #:import_url, # XXX # create | create-user |
+ :issues_enabled, # create | create-user | edit
+ :lfs_enabled, # create | create-user | edit
+ :merge_requests_enabled, # create | create-user | edit
+ :name, # create | create-user | edit
+ #:namespace_id, # create | |
+ :only_allow_merge_if_build_succeeds, # create | create-user | edit
+ #:path, # create | | edit
+ :public, # create | create-user | edit
+ :public_builds, # create | create-user | edit
+ :request_access_enabled, # create | create-user | edit
+ :shared_runners_enabled, # create | create-user | edit
+ :snippets_enabled, # create | create-user | edit
+ :visibility_level, # create | create-user | edit
+ :wiki_enabled, # create | create-user | edit
+ :only_allow_merge_if_all_discussions_are_resolved # create | create-user | edit
+ ]
end
end
diff --git a/backends/gitlab-ee b/backends/gitlab-ee
index b69b1fc..5467e16 100755
--- a/backends/gitlab-ee
+++ b/backends/gitlab-ee
@@ -20,74 +20,80 @@ require 'uri'
require 'nokogiri'
class GitLabEE < GitLabCE
- def _mirrorURL
- unless @cache.has_key?(:mirror)
- req = Net::HTTP::Get.new(URI(_info["web_url"]+"/mirror"))
- req.add_field("PRIVATE-TOKEN", @api_key)
- con = _connection(req.uri)
- res = con.request(req)
- if res.code != "200"
- throw res
- end
- @cache[:mirror_res]=res
- doc = Nokogiri::HTML(res.body)
+ def project(project_id)
+ unless @projects.has_key?(project_id)
+ @projects[project_id] = Project.new(self, project_id)
+ end
+ return @projects[project_id]
+ end
+ class Project < GitLabCE::Project
+ def mirrorURL
+ unless @cache.has_key?(:mirror)
+ req = Net::HTTP::Get.new(URI(_info["web_url"]+"/mirror"))
+ req.add_field("PRIVATE-TOKEN", @api_key)
+ res = @gl.connection(req.uri).request(req)
+ if res.code != "200"
+ throw res
+ end
+ @cache[:mirror_res]=res
+ doc = Nokogiri::HTML(res.body)
- @cache[:mirror_cookie] = res["set-cookie"]
- @cache[:mirror_token] = doc.css('input[name="authenticity_token"]').first["value"]
- is_mirror = doc.css("#project_mirror").first["checked"]
- if !is_mirror
- @cache[:mirror] = nil
- else
- @cache[:mirror] = URI(doc.css("#project_import_url").first["value"])
+ @cache[:mirror_cookie] = res["set-cookie"]
+ @cache[:mirror_token] = doc.css('input[name="authenticity_token"]').first["value"]
+ is_mirror = doc.css("#project_mirror").first["checked"]
+ if !is_mirror
+ @cache[:mirror] = nil
+ else
+ @cache[:mirror] = URI(doc.css("#project_import_url").first["value"])
+ end
end
+ return @cache[:mirror]
end
- return @cache[:mirror]
- end
- def _mirrorURL=(url)
- _mirrorURL
+ def mirrorURL=(url)
+ self.mirrorURL()
- req = Net::HTTP::Patch.new(URI(_info["web_url"]+"/mirror"))
- req.add_field("PRIVATE-TOKEN", @api_key) # authenticate
- req.add_field("Cookie", @cache[:mirror_cookie]) # session id
- req.form_data = {
- "utf8" => "✓",
- "authenticity_token" => @cache[:mirror_token], # session state
- "project[mirror]" => (url.nil? ? "0" : "1"),
- "project[import_url]" => url.to_s,
- }
+ req = Net::HTTP::Patch.new(URI(self.info["web_url"]+"/mirror"))
+ req.add_field("PRIVATE-TOKEN", @gl.config['apikey']) # authenticate
+ req.add_field("Cookie", @cache[:mirror_cookie]) # session id
+ req.form_data = {
+ "utf8" => "✓",
+ "authenticity_token" => @cache[:mirror_token], # session state
+ "project[mirror]" => (url.nil? ? "0" : "1"),
+ "project[import_url]" => url.to_s,
+ }
- con = _connection(req.uri)
- res = con.request(req)
- if res.code != "302"
- throw res
- end
+ res = @gl.connection(req.uri).request(req)
+ if res.code != "302"
+ throw res
+ end
- @cache.delete(:mirror)
- @cache.delete(:mirror_token)
- @cache.delete(:mirror_cookie)
- return URI(url)
- end
+ @cache.delete(:mirror)
+ @cache.delete(:mirror_token)
+ @cache.delete(:mirror_cookie)
+ return URI(url)
+ end
- def cmd_get_meta
- map = super
- map["mirror"] = _mirrorURL.to_s
- return map
- end
+ def get_meta
+ map = super
+ map["mirror"] = self.mirrorURL.to_s
+ return map
+ end
- def cmd_set_meta(map)
- if map.has_key?("mirror")
- mirror = map["mirror"]
- map.delete("mirror")
- super(map)
- self._mirrorURL=url
- else
- super(map)
+ def set_meta(map)
+ if map.has_key?("mirror")
+ url = map["mirror"]
+ map.delete("mirror")
+ super(map)
+ self.mirrorURL = url
+ else
+ super(map)
+ end
end
- end
- def cmd_repo_mode
- return "active"
+ def repo_mode
+ return (self.mirrorURL.nil? ? "active" : "passive")
+ end
end
end
diff --git a/git-mirror b/git-mirror
index ed69ac3..d9e1932 100755
--- a/git-mirror
+++ b/git-mirror
@@ -90,8 +90,9 @@ upload() {
# push metadata
{
- printf '%q ' set-meta "${remote#*:}" "${params[@]}"
+ printf '%q ' set-meta "${remote#*:}"
git config --file "$local/config" --get-regexp '^git-mirror[.]' -z|sed -z 's/ /=/'|xargs -0r printf '%q '
+ printf '%q ' "${params[@]}"
} | account "${remote%%:*}"
# push repository
local repo_mode