The same thing happens from CLI:
\ngitlab project create --name myproj --path myrepo --namespace-id GROUP_ID --default-branch master\nAfter 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.
\nThe 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.
\nIf 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.
\nCall these 2 functions, in order:
\ndef gitlab_project_repo_setup_content(project):\n try:\n project.files.get('README.md', ref='main')\n except GitlabGetError:\n print('Creating file in repo')\n project.files.create(\n {\n 'file_path': 'README.md',\n 'branch': 'main',\n 'content': '# Placeholder file',\n 'commit_message': 'Add README to initialize repository',\n }\n )\n\ndef gitlab_project_set_default_branch(project):\n \"\"\"\n Workaround for gitlab default branch can only be changed after there is content added:\n if you set default_branch in the project.create(), it gets ignored, ie project.default_branch will\n still be 'main'. Same goes for command line. It appears that there HAS to be at least one\n file in the reop for the default branch to be changed.\n \"\"\"\n # verify that that readme exists, this will raise exception if not:\n project.files.get('README.md', ref='main')\n\n project.branches.create({'branch': 'master', 'ref': 'main'})\n project.default_branch = 'master'\n project.save()\n\n project.protectedbranches.create({'name': 'master'})\n project.protectedbranches.delete('main')\n project.branches.delete('main')\nAfter running these 2, the git repo will have only master branch, and gitlab console will show this and that it is default and protected,
\nMaybe try with initialize_with_readme?
-
Description of the problem, including code/CLI snippetI've spent a couple hours on this, but basically when creating a project from the python package or from CLI, the The same thing happens from CLI: Expected BehaviorAfter creation as described, the project object should show Regardless of being created from CLI or SDK. Actual BehaviorThe 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: 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
|
Beta Was this translation helpful? Give feedback.
-
|
Maybe try with |
Beta Was this translation helpful? Give feedback.
-
|
@JohnVillalovos thanks that's interesting. Led me to this: #2869 (comment), and in particular
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? |
Beta Was this translation helpful? Give feedback.
-
|
So adding However I still think it would be useful to make this more obvious. I searched the web and all the hits I find, nothing indicates that Also I guess this should be converted back to an issue? |
Beta Was this translation helpful? Give feedback.
Maybe try with
initialize_with_readme?