Skip to content

ProjectFileManager.update doesn't work as expected #340

Closed

Description

@mgoodhand

Perhaps I'm using it wrong, but gitlab.v4.objects.ProjectFileManager.update always gives me an error about missing attributes:

  File "/Users/mrg/git/gitlabcp/env/lib/python2.7/site-packages/gitlab/mixins.py", line 216, in update
    self._check_missing_update_attrs(new_data)
  File "/Users/mrg/git/gitlabcp/env/lib/python2.7/site-packages/gitlab/mixins.py", line 183, in _check_missing_update_attrs
    raise AttributeError("Missing attributes: %s" % ", ".join(missing))
AttributeError: Missing attributes: file_path, branch, content, commit_message

I'm invoking it with the same parameters I used for create:

           response = p.files.update({
                    'file_path': escaped_path,
                    'branch': args.branch,
                    'content': file_content,
                    'encoding': 'base64',
                    'commit_message': args.commit_message
                    })

I see that create and get are explicitly defined on ProjectFileManager, whereas update falls back to the mixin.

If I use the following method instead (hacked version of create with put instead of post), it seems to work:

def update(self, data, **kwargs):
    """Updates an existing object
    Args:
        data (dict): parameters to send to the server to create the
                        resource
        **kwargs: Extra options to send to the Gitlab server (e.g. sudo)
    Returns:
        RESTObject: a new instance of the managed object class built with
            the data sent by the server
    Raises:
        GitlabAuthenticationError: If authentication is not correct
        GitlabCreateError: If the server cannot perform the request
    """

    self._check_missing_create_attrs(data)
    file_path = data.pop('file_path')
    path = '%s/%s' % (self.path, file_path)
    server_data = self.gitlab.http_put(path, post_data=data, **kwargs)
    return self._obj_cls(self, server_data)

Activity

  1. gpocentek commented on Oct 12, 2017

    @gpocentek
    Contributor

    You're absolutely right, the update method should be redefined.

  2. gpocentek commented on Oct 21, 2017

    @gpocentek
    Contributor

    @mgoodhand to keep things consistent the update prototype will be update(self, file_path, new_data={}, **kwargs). file_path is not considered new data, it is the ID of the object.

  3. mgoodhand commented on Oct 23, 2017

    @mgoodhand
    Author

    Thanks. Looks good.

  4. locked as resolved and limited conversation to collaborators on Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions