diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 9a423dd4a..5825d2349 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -193,6 +193,10 @@ class GitlabHousekeepingError(GitlabOperationError): pass +class GitlabOwnershipError(GitlabOperationError): + pass + + def raise_error_from_response(response, error, expected_code=200): """Tries to parse gitlab error message from response and raises error. diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py index 19adb6296..f6dcb086b 100644 --- a/gitlab/v4/objects.py +++ b/gitlab/v4/objects.py @@ -2028,59 +2028,45 @@ class ProjectPipelineScheduleVariable(SaveMixin, ObjectDeleteMixin, RESTObject): _id_attr = 'key' -class ProjectPipelineScheduleVariableManager(CRUDMixin, RESTManager): - _path = '/projects/%(project_id)s/pipeline_schedules/%(pipeline_schedule_id)s/variables' +class ProjectPipelineScheduleVariableManager(CreateMixin, UpdateMixin, + DeleteMixin, RESTManager): + _path = ('/projects/%(project_id)s/pipeline_schedules/' + '%(pipeline_schedule_id)s/variables') _obj_cls = ProjectPipelineScheduleVariable _from_parent_attrs = {'project_id': 'project_id', 'pipeline_schedule_id' : 'id'} - _create_attrs = (('pipeline_schedule_id', 'key', 'value'), tuple()) _create_attrs = (('key', 'value'), tuple()) + _update_attrs = (('key', 'value'), tuple()) - def list(self): - array = [] - if 'variables' in self._parent._attrs: - for variable in self._parent._attrs['variables']: - schedule_variable = self._obj_cls(self, variable) - array.append(schedule_variable) - else: - obj = self._parent.manager.get(self._parent.id) - for variable in obj._attrs['variables']: - schedule_variable = self._obj_cls(self, variable) - array.append(schedule_variable) - return array +class ProjectPipelineSchedule(SaveMixin, ObjectDeleteMixin, RESTObject): + _managers = (('variables', 'ProjectPipelineScheduleVariableManager'),) + @cli.register_custom_action('ProjectPipelineSchedule') + @exc.on_http_error(exc.GitlabOwnershipError) + def take_ownership(self, **kwargs): + """Update the owner of a pipeline schedule. -class ProjectPipelineSchedule(RESTObject): - _managers = ( - ('variables', 'ProjectPipelineScheduleVariableManager'), - ) + Args: + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabOwnershipError: If the request failed + """ + path = '%s/%s/take_ownership' % (self.manager.path, self.get_id()) + server_data = self.manager.gitlab.http_post(path, **kwargs) + self._update_attrs(server_data) -class ProjectPipelineSchedulesManager(RetrieveMixin, CreateMixin, RESTManager): +class ProjectPipelineScheduleManager(CRUDMixin, RESTManager): _path = '/projects/%(project_id)s/pipeline_schedules' _obj_cls = ProjectPipelineSchedule _from_parent_attrs = {'project_id': 'id'} _create_attrs = (('description', 'ref', 'cron'), ('cron_timezone', 'active')) - - def create(self, data, **kwargs): - """Creates a new object. - - Args: - data (dict): Parameters to send to the server to create the - resource - **kwargs: Extra options to send to the server (e.g. sudo) - - Raises: - GitlabAuthenticationError: If authentication is not correct - GitlabCreateError: If the server cannot perform the request - - Returns: - RESTObject: A new instance of the managed object class build with - the data sent by the server - """ - return CreateMixin.create(self, data, path=self.path, **kwargs) + _update_attrs = (tuple(), + ('description', 'ref', 'cron', 'cron_timezone', 'active')) class ProjectSnippetNote(SaveMixin, ObjectDeleteMixin, RESTObject): @@ -2181,8 +2167,17 @@ class ProjectSnippetManager(CRUDMixin, RESTManager): class ProjectTrigger(SaveMixin, ObjectDeleteMixin, RESTObject): @cli.register_custom_action('ProjectTrigger') + @exc.on_http_error(exc.GitlabOwnershipError) def take_ownership(self, **kwargs): - """Update the owner of a trigger.""" + """Update the owner of a trigger. + + Args: + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabOwnershipError: If the request failed + """ path = '%s/%s/take_ownership' % (self.manager.path, self.get_id()) server_data = self.manager.gitlab.http_post(path, **kwargs) self._update_attrs(server_data) @@ -2398,7 +2393,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject): ('pagesdomains', 'ProjectPagesDomainManager'), ('pipelines', 'ProjectPipelineManager'), ('protectedbranches', 'ProjectProtectedBranchManager'), - ('pipeline_schedules', 'ProjectPipelineSchedulesManager'), + ('pipelineschedules', 'ProjectPipelineScheduleManager'), ('runners', 'ProjectRunnerManager'), ('services', 'ProjectServiceManager'), ('snippets', 'ProjectSnippetManager'),