Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

project creation ignores default_branch and default-branch contradicts documentation #3181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
schollii opened this issue Apr 26, 2025 · 2 comments

Comments

@schollii
Copy link

schollii commented Apr 26, 2025

Description of the problem, including code/CLI snippet

I've spent a couple hours on this, but basically when creating a project from the python package or from CLI, the default_branch (in SDK) and --default-branch settings are ignored,

    group_id = gl.groups.list(search=namespace)[0].id
    project = gl.projects.create(
        {
            'name': project_name,
            'path': repo_name,
            'namespace_id': group_id,
            'default_branch': 'master',
        }
    )

The same thing happens from CLI:

gitlab project create --name myproj --path myrepo --namespace-id GROUP_ID --default-branch master

Expected Behavior

After creation as described, the project object should show default_branch == "master" and the branch should show as protected.

Regardless of being created from CLI or SDK.

Actual Behavior

The CLI and SDK both show that the default branch is "main". The console says that a default branch cannot be set on an empty repo.

If this is not possible because of how the gitlab package is a thin wrapper over gitlab REST API, the readme in this repo should at least mention such discrepencies between the docs (auto generated from the REST API, shown in the CLI --help output) and actual behavior.

Workaround (possibly a hack)

Call these 2 functions, in order:

def gitlab_project_repo_setup_content(project):
    try:
        project.files.get('README.md', ref='main')
    except GitlabGetError:
        print('Creating file in repo')
        project.files.create(
            {
                'file_path': 'README.md',
                'branch': 'main',
                'content': '# Placeholder file',
                'commit_message': 'Add README to initialize repository',
            }
        )

def gitlab_project_set_default_branch(project):
    """
    Workaround for gitlab default branch can only be changed after there is content added:
    if you set default_branch in the project.create(), it gets ignored, ie project.default_branch will
    still be 'main'. Same goes for command line. It appears that there HAS to be at least one
    file in the reop for the default branch to be changed.
    """
    # verify that that readme exists, this will raise exception if not:
    project.files.get('README.md', ref='main')

    project.branches.create({'branch': 'master', 'ref': 'main'})
    project.default_branch = 'master'
    project.save()

    project.protectedbranches.create({'name': 'master'})
    project.protectedbranches.delete('main')
    project.branches.delete('main')

After running these 2, the git repo will have only master branch, and gitlab console will show this and that it is default and protected,

Specifications

  • python-gitlab version: 5.6.0
  • Gitlab server version (or gitlab.com): gitlab.com
@JohnVillalovos
Copy link
Member

Maybe try with initialize_with_readme?

@schollii
Copy link
Author

schollii commented Apr 26, 2025

@JohnVillalovos thanks that's interesting. Led me to this: #2869 (comment), and in particular

in a group with fully protected default branches

I'm thinking that could be the cause here, since the gitlab account is part of an organization where there could be such rules in place, is there a way that I can verify whether that's the case?

@python-gitlab python-gitlab locked and limited conversation to collaborators Apr 26, 2025
@JohnVillalovos JohnVillalovos converted this issue into discussion #3182 Apr 26, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants