Skip to content

Conversation

@nickbroon
Copy link
Contributor

Passing start_branch as kwargs results in it being passed as query argument to the API:

send: b'PUT /api/v4/projects/12345678/repository/files/readme.txt?start_branch=main

send: b'{"file_path": "readme.txt", "branch": "new_branch", "content":
"Modified contents", "commit_message": "File was modified for this new
branch"}'

which results in error being returned:

{"message":"You can only create or edit files when you are on a branch"}

It should instead be sent a body attribute, which succeeds in creating the branch during the save.

To be sent as body attribute it must be specified as concrete function argument and class attribute instead of just using kwargs

Closes: #3318

Changes

Documentation and testing

Please consider whether this PR needs documentation and tests. This is not required, but highly appreciated:

@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.75%. Comparing base (4a8d82b) to head (ad2802a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3329   +/-   ##
=======================================
  Coverage   95.75%   95.75%           
=======================================
  Files          98       98           
  Lines        6051     6053    +2     
=======================================
+ Hits         5794     5796    +2     
  Misses        257      257           
Flag Coverage Δ
api_func_v4 83.71% <100.00%> (+0.05%) ⬆️
cli_func_v4 78.67% <50.00%> (-0.01%) ⬇️
unit 90.17% <50.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
gitlab/v4/objects/files.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Passing `start_branch` as kwargs results in it being passed as query
argument to the API:

```
send: b'PUT /api/v4/projects/12345678/repository/files/readme.txt?start_branch=main

send: b'{"file_path": "readme.txt", "branch": "new_branch", "content":
"Modified contents", "commit_message": "File was modified for this new
branch"}'
```

which results in error being returned:

```
{"message":"You can only create or edit files when you are on a branch"}
```

It should instead be sent a body attribute, which succeeds in creating
the branch during the save.

To be sent as body attribute it must be specified as concrete function
argument and class attribute instead of just using kwargs

Closes: python-gitlab#3318
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the start_branch parameter in ProjectFile.save() was being incorrectly passed as a query parameter instead of a body attribute, causing API errors when attempting to create a new branch during file save operations.

Changes:

  • Added start_branch as a class attribute on ProjectFile with default value None
  • Added start_branch as an explicit parameter to the save() method signature
  • Updated the save() method to assign start_branch to the instance before calling the parent save() method
Comments suppressed due to low confidence (1)

gitlab/v4/objects/files.py:24

  • The class 'ProjectFile' does not override 'eq', but adds the new attribute branch.
    The class 'ProjectFile' does not override 'eq', but adds the new attribute commit_message.
    The class 'ProjectFile' does not override 'eq', but adds the new attribute start_branch.
    The class 'ProjectFile' does not override 'eq', but adds the new attribute file_path.
class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

self,
branch: str,
commit_message: str,
start_branch: str | None = None,
Copy link

Copilot AI Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The start_branch parameter should be included in the functional test coverage. The existing functional test in tests/functional/api/test_repository.py already tests the save() method on line 26, but it doesn't test the new start_branch parameter. Consider adding a test case that creates a file and saves it to a new branch using start_branch to ensure this fix works correctly.

Copilot uses AI. Check for mistakes.
Copy link
Member

@JohnVillalovos JohnVillalovos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @nickbroon

LGTM

@JohnVillalovos JohnVillalovos merged commit 1001d93 into python-gitlab:main Jan 20, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

project.files.save() fails due to start_branch being a URL query parameter and not a body attribute

2 participants