how to create a project using gitlab-rails console?

401 Views Asked by At

I try to create a gitlab project using gitlab-rails console but i did not find how to do that.

I found how to create a user :

u = User.new(username: 'name', email: '[email protected]', name: 'Name name', password: 'password1234', password_confirmation: 'password1234', admin: true)
u.skip_confirmation!
u.save! 

But does someone know how to create project ? Or best where can i find a documentation about gitlab-rails objects ? Something like a javadoc would be helpfull ...

Sincerly

2

There are 2 best solutions below

0
granier On BEST ANSWER

I did not find how to create a project using gitlab-rails console, but I found a solution:

  • First create a user and set an API token to this user
  • Second create the project using Gitlab API

ruby script:

u = User.new(username: 'aName', email: '[email protected]', name: 'aName lastName ', password: 'password', password_confirmation: 'passworx', admin: true)                                          
u.skip_confirmation!
u.save!
token = u.personal_access_tokens.create(scopes: ['api','admin_mode'], name: 'install_token', expires_at: 365.days.from_now)
token.set_token('abcd1234')
token.save!

API request to create the project:

curl -k --request POST --header 'PRIVATE-TOKEN: abcd1234' --header 'Content-Type: application/json' --data  '{"name\": "aName", "description": "example","namespace": "name", "initialize_with_readme": "true"}' --url 'https://www.example.com/api/v4/projects/'
1
user20664576 On

I was facing the same problem when stumbling on this post. It took me a while since there is no documentation on this, but I found how to do it. Open a ruby console on the server:

gitlab-rails console

Enter the Ruby code shown below, replace 'group-name' with the desired group name and 'root' with the desired user name.

group = Group.create(name: 'group-name', path: 'group-name')
user = User.find_by(username: 'root')
group.add_owner(user)
group.save!

EDIT: The project can be created using the ruby command shown below. Note that although this creates a project, it is not linked to a repository, something I haven't managed to figure out.

project = Project.create(
    name: 'test-project',
    path: 'test-project',
    creator_id: user.id,
    namespace_id: group.id,
    visibility_level: 20, # public, default is private
)