#!/usr/bin/lua5.3 local http_request = require("http.request") local cjson = require("cjson") local socket = require('socket') local redmine_api_key = "" local gitlab_api_key = "" local redmine_uri = "https://bugs.alpinelinux.org" local gitlab_uri = "http://192.168.2.163" local allowed_chars = "[%a%d_-%.]" local function tfind(t,i) for _,v in pairs(t) do if v == i then return true end end return false end local function string_allowed(str, allowed_chars) local r = "" str:gsub(".", function(c) r = c:find(allowed_chars) and r..c or r.."-" end) return r end local function add_headers(request, headers) local private = { "x-redmine-api-key", "private-token" } for k,v in pairs(headers) do request.headers:upsert(k, v, tfind(private, k)) end end local function get_http_body(uri, headers, method, data) local request = assert(http_request.new_from_uri(uri)) local status_ok = "200" add_headers(request, headers) if data then request.headers:upsert(":method", method) request:set_body(data) end if method == "POST" then status_ok = "201" end local headers, stream = request:go() local body = assert(stream:get_body_as_string()) local status = headers:get(":status") if status ~= status_ok then print(("Status: %s\nMessage: %s"):format(status, cjson.decode(body).message)) end return body end -- add user to gitlab -- user: table with user data local function gitlab_add_user(data) local uri = ("%s/api/v4/users"):format(gitlab_uri) local headers = { ["private-token"] = gitlab_api_key, ["content-type"] = "application/json" } return get_http_body(uri, headers, "POST", cjson.encode(data)) end local function gitlab_update_user(id, data) local uri = ("%s/api/v4/users/%s"):format(gitlab_uri, id) local headers = { ["private-token"] = gitlab_api_key, ["content-type"] = "application/json" } return get_http_body(uri, headers, "PUT", cjson.encode(data)) end -- get list of redmine users -- offset of the user list local function redmine_get_users(offset, limit) local uri = ("%s/users.json?offset=%s&limit=%s"):format(redmine_uri, offset, limit) local headers = { ["x-redmine-api-key"] = redmine_api_key } return get_http_body(uri, headers) end local function redmine_get_user(uid) local uri = ("%s/users/%s.json"):format(redmine_uri, uid) local headers = { ["x-redmine-api-key"] = redmine_api_key } return get_http_body(uri, headers) end local function genpass(length) local charset = {} for c = 48, 57 do table.insert(charset, string.char(c)) end for c = 65, 90 do table.insert(charset, string.char(c)) end for c = 97, 122 do table.insert(charset, string.char(c)) end if not length or length <= 0 then return '' end math.randomseed(socket.gettime() * 1000000) return genpass(length - 1) .. charset[math.random(1, #charset)] end local function add_users_legacy() local limit = 25 local offset = 0 local total = 0 while total >= offset do local resp = redmine_get_users(offset, limit) local json = cjson.decode(resp) for _,v in ipairs(json.users) do if v.last_login_on == cjson.null or v.mail:match(".*@mail%.ru") then print(("Skipping user: %s %s (%s) with email: %s"):format( v.firstname, v.lastname, v.login, v.mail)) else print(("Adding user: %s %s (%s) with email: %s"):format( v.firstname, v.lastname, v.login, v.mail)) gitlab_add_user({ username = string_allowed(v.login, allowed_chars), name = ("%s %s"):format(v.firstname, v.lastname), email = v.mail, skip_confirmation = true, reset_password = false, password = "Mit5EmhGup1q" }) end end total = json.total_count offset = offset + limit end end local function add_users() for uid in io.lines(arg[1]) do local json = redmine_get_user(uid) local user = cjson.decode(json) local pass = genpass(16) user = user.user if user.mail == cjson.null then print(("Skipping user: %s %s (%s) with email: %s"):format( user.firstname, user.lastname, user.login, user.mail)) else print(("Adding user: %s %s (%s) with email: %s and password: %s"):format( user.firstname, user.lastname, user.login, user.mail, pass)) gitlab_add_user({ username = string_allowed(user.login, allowed_chars), name = ("%s %s"):format(user.firstname, user.lastname), email = user.mail, skip_confirmation = true, reset_password = false, password = pass, admin = true }) end end end add_users()