From 6208964ad32a7f36e03d8d14d969642255524566 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 15 Dec 2016 18:23:32 -0500 Subject: fix --- git-mirror-gitlab-ce | 56 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 11 deletions(-) (limited to 'git-mirror-gitlab-ce') diff --git a/git-mirror-gitlab-ce b/git-mirror-gitlab-ce index 1c628e8..ed45aec 100755 --- a/git-mirror-gitlab-ce +++ b/git-mirror-gitlab-ce @@ -4,6 +4,7 @@ # TODO: add 'owner' to get/set_meta load 'git-mirror-backend.rb' +load 'libremessages.rb' require 'net/http' require 'uri' require 'cgi' @@ -17,6 +18,9 @@ class GitLabCE < GitMirrorBackend def obj return @obj end + def to_s + return @obj.to_s + end end def initialize() @@ -82,24 +86,30 @@ class GitLabCE < GitMirrorBackend end def get_meta - return self.info.select{|k,v| @vars.include?(k.to_sym)} + return self.info.select{|k,v| @gl.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)} + illegal = map.select{|k,v| not @gl.vars.include?(k.to_sym)} if illegal.count > 0 raise Error.new(illegal) end - if info == nil + if self.info().nil? # create - req = Net::HTTP::Put.new(@gl.config['apiurl'] + "projects") + libremessages('msg2', 'Creating repo %s', @project_id) + namespace, path = @project_id.split('/', 2) + namespace_id = namespace_path2id(namespace) + + req = Net::HTTP::Post.new(@gl.config['apiurl'] + "projects") req.add_field("PRIVATE-TOKEN", @gl.config['apikey']) req.add_field("Content-Type", "application/json") - map["path"] = @project_id + map["name"] ||= path + map["namespace_id"] = namespace_id + map["path"] = path if not mirror.nil? map["import_url"] = mirror end @@ -108,10 +118,11 @@ class GitLabCE < GitMirrorBackend if res.code != "201" raise Error.new(res) end - info = JSON::parse(res.body) + self.info = JSON::parse(res.body) else # update - req = Net::HTTP::Put.new(@gl.config['apiurl'] + "projects/" + CGI::escape(info["id"].to_s)) + libremessages('msg2', 'Updating repo %s metadata', @project_id) + req = Net::HTTP::Put.new(@gl.config['apiurl'] + "projects/" + CGI::escape(self.info["id"].to_s)) req.add_field("PRIVATE-TOKEN", @gl.config['apikey']) req.add_field("Content-Type", "application/json") req.body = JSON::dump(map) @@ -119,7 +130,26 @@ class GitLabCE < GitMirrorBackend if res.code != "200" raise Error.new(res) end - info = JSON::parse(res.body) + self.info = JSON::parse(res.body) + end + return nil + end + + def namespace_path2id(path, pageno=1) + req = Net::HTTP::Get.new(@gl.config['apiurl'] + "namespaces?page=#{pageno}&search=#{CGI::escape(path)}") + req.add_field("PRIVATE-TOKEN", @gl.config['apikey']) + res = @gl.connection(req.uri).request(req) + if res.code != "200" + raise Error.new(res) + end + page = JSON::parse(res.body) + page.each do |namespace| + if namespace["path"] == path + return namespace["id"].to_i + end + end + if pageno < res['X-Total-Pages'].to_i + return namespace_path2id(path, pageno+1) end return nil end @@ -159,9 +189,13 @@ class GitLabCE < GitMirrorBackend def cmd_set_meta(project_id, *pairs) map = {} pairs.each do |pair| - key, val = arg.split('=', 2) + key, val = pair.split('=', 2) key = key.gsub('-', '_') - map[key] = val + if val.nil? + map.delete(key) + else + map[key] = val + end end self.project(project_id).set_meta(map) return nil @@ -176,7 +210,7 @@ class GitLabCE < GitMirrorBackend end def cmd_repo_mode(project_id) - return self.project(projecT_id).repo_mode() + return self.project(project_id).repo_mode() end def vars -- cgit v1.2.3