Skip to content

Detect changes #1221

@hoerup

Description

@hoerup

When setting up a gitlab element via this library it would be nice to easily detect if there was any actual changes to the object in order to determine whether a save() is necessary

Currently i use this (maybe something similiar could be included?)


def is_modified(self): 
    for k in self.__dict__['_updated_attrs'].keys():

        if k == 'id':
            continue
        
        if k == 'password': #since we can't verify change in password we just assume that it is a change
            return True

        if k in self.__dict__['_attrs']:
            if self.__dict__['_updated_attrs'][k] !=  self.__dict__['_attrs'][k]:
                return True

        elif k in self.__dict__['_attrs']['properties']:
            if self.__dict__['_updated_attrs'][k] !=  self.__dict__['_attrs']['properties'][k]:
                return True
            
        else:
            raise Exception('Could not determine if \'%s\' was a changed property' % k)


    return False
    

setattr(gitlab.base.RESTObject, 'is_modified', is_modified)

usage:

approvals = project.approvals.get()

approvals.reset_approvals_on_push = True

if approvals.is_modified():
    approvals.save() #only send post if there's actually changes

Specifications

  • python-gitlab version: 2.5.0
  • API version you are using (v3/v4): v4

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