Description
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