Skip to content

Instantly share code, notes, and snippets.

@prasann
Created March 10, 2017 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasann/ec4c80c3e7e84cbc6e0e7cdbd4d70b54 to your computer and use it in GitHub Desktop.
Save prasann/ec4c80c3e7e84cbc6e0e7cdbd4d70b54 to your computer and use it in GitHub Desktop.
require 'uri'
require 'net/https'
require 'json'
GITLAB_URL = 'https://gitlab-your-url.com'
USERS_API = '/api/v3/users'
PRIVATE_TOKEN='XXXXXXXXX' #Private token from Git-lab profile.
DEFAULT_PASSWORD = 'some_default_password'
def users
[{name: 'Name', email: 'username@gmail.com'}]
end
def post_request(user)
uri = URI.parse("#{GITLAB_URL}#{USERS_API}")
http = Net::HTTP.new(uri.host, uri.port)
headers = {'private-token' => PRIVATE_TOKEN, 'Content-type' => 'application/json'}
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = user.to_json
response = http.request(request)
response.code == '201' ? "Successfully created #{user[:name]} \u{1f60e}" : 'Failed :('
end
def create_users
users.each do |user|
json_body = {
email: user[:email],
password: DEFAULT_PASSWORD,
name: user[:name],
can_create_group: false,
username: user[:email].split('@').first
}
puts post_request(json_body)
end
end
create_users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment