pythongitlabpython-gitlab

python-gitlab - how to modify project settings?


I'm writing a python module to create projects in gitlab, but I can't figure out how to change the default project settings, like remove_source_branch_after_merge.

I've tried passing the argument to the projects.create() call, but it seems to be ignored.

project = gl.projects.create({'name': reponame, 'namespace_id': group_id, 'default_branch' : default_branch, 'remove_source_branch_after_merge' : False})

I managed to change the setting by manually POST'ing to /api/v4/projects/$ID?remove_source_branch_after_merge=false but I can't figure out how to do this in python-gitlab.

How can I create a project with customized settings, or modify a project settings after its creation in python-gitlab?

I'm using python-gitlab==1.7.0


Solution

  • To answer your question, modifying attributes after an object has been created can be done with save():

    import gitlab
    
    gl = gitlab.Gitlab("https://gitlab.example.com", private_token=token)
    project = gl.projects.create(
        {
            "name": reponame,
            "namespace_id": group_id,
            "default_branch": default_branch,
            "remove_source_branch_after_merge": False,
        }
    )
    
    # Enable remove after merge
    project.remove_source_branch_after_merge = True
    project.save()
    
    # Or disable again
    project.remove_source_branch_after_merge = False
    project.save()
    

    However, I think your initial create call should work, so maybe check for any typos. 1.7.0 is quite old, and I just checked this works on 3.2.0. You can also use gl.enable_debug() to get verbose output and check that the right parameters are sent to the API.