Creating student projects hierarchy for a course

Hello,
I am trying to write a script that automatically setup a hierarchy of groups and projects in order to provide each student a private repository where they will be able to push/pull/branch their code and make me able to easily grab their code to evaluate it.
Right now, my script is creating a group with the course name (and the year). Then, I create a private subgroup with the student name_surname where I enroll the student as Developer. Finally, I create a project with the student as Developer (again).
It seems that the access rights are almost correct. The students can only see their own project and have no access to the work of others.

But, I still have some problems to initialize the project with a default branch ‘main’ through my script. Moreover, I think that by default created branches seems to be protected which is just not desired here.

When a student tries to initialize the default branch I get:

remote: GitLab:
remote: A default branch (e.g. main) does not yet exist for course-2021/name-surname/project-repo
remote: Ask a project Owner or Maintainer to create a default branch: 

I tried to create the project with the following code:

project = \
  gl.projects.create(
        { 'name': PROJECT_NAME,
          'namespace_id': subgroup.id }
    )
project.default_branch = 'main'
project.save()

But, I always get the error: Could not change HEAD: branch 'main' does not exist

So, how can I create a project and initialize a default main branch in order that the students can land safely in the repository ?

Welcome to the GitLab forum @perror !

Are you sure the token you’re using to run the script is an “owner” or “maintainer” of the group you’re putting it inside / the project that gets created? Also what library are you using to access the GitLab API?

I’m thinking one of those things may help figure out what’s going on here.

I am using an API Token and I am the owner of all the projects I create. And, I am using gitlab-python 2.10.

In fact, I am using a shorter code now which rule out the problem of saving it:

  gl.projects.create(
        { 'name': PROJECT_NAME,
          'namespace_id': subgroup.id 
          'default_branch': 'main'}
    )

But, my problem is really about how to create a first branch originating from an initial commit with the Python API.

Hi Perror. Thanks for the clarification.

Is it possible you need to manually create the branch first and then you’re able to say which the default is?

 gl.projects.create(
        { 'name': PROJECT_NAME,
          'namespace_id': subgroup.id 
          'branch': 'main'
          'default_branch': 'main'}
    )

This is kind of based on the docs here

Additionally, it looks like you might be able to allow developers to push to protected branches or use the API to unprotect a branch.

I found this in our docs here. Maybe you just need initialize_with_readme to be true first.

image

Damn, I missed this point… This explain a everything. The commit with the README.md is the source of the new branch.

Thanks a lot!

You got it! Thanks for coming and using our forum.

If you haven’t yet, please consider taking our GitLab DevOps in Education survey for 2021. We appreciate any insight you have on DevOps in education and research. Have a great day!