From 0d16dfd9c35c20c1b7b9dd007a46d17f9b098829 Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Wed, 15 Feb 2017 16:05:44 +0100 Subject: [PATCH 1/7] Added gitlabs time tracking features - get/set/remove estimated time per issue - get/set/remove time spent per issu --- gitlab/exceptions.py | 4 +++ gitlab/objects.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 1d1f477b9..11bbe26cb 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -143,6 +143,10 @@ class GitlabTodoError(GitlabOperationError): pass +class GitlabTimeTrackingError(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/objects.py b/gitlab/objects.py index 41931abf6..15ffda5b7 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1525,6 +1525,65 @@ def todo(self, **kwargs): r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTodoError, [201, 304]) + def time_stats(self, **kwargs): + """Get time stats for the issue. + + Raises: + GitlabConnectionError: If the server cannot be reached. + """ + url = ('/projects/%(project_id)s/issues/%(issue_id)s/time_stats' % + {'project_id': self.project_id, 'issue_id': self.id}) + r = self.gitlab._raw_get(url, **kwargs) + raise_error_from_response(r, GitlabGetError) + return r.json() + + def time_estimate(self, **kwargs): + """Set an estimated time of work for the issue. + + Raises: + GitlabConnectionError: If the server cannot be reached. + """ + url = ('/projects/%(project_id)s/issues/%(issue_id)s/time_estimate' % + {'project_id': self.project_id, 'issue_id': self.id}) + r = self.gitlab._raw_post(url, **kwargs) + raise_error_from_response(r, GitlabTimeTrackingError, 201) + return r.json() + + def reset_time_estimate(self, **kwargs): + """Resets estimated time for the issue to 0 seconds. + + Raises: + GitlabConnectionError: If the server cannot be reached. + """ + url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' % + {'project_id': self.project_id, 'issue_id': self.id}) + r = self.gitlab._raw_post(url, **kwargs) + raise_error_from_response(r, GitlabTimeTrackingError, 200) + return r.json() + + def add_spent_time(self, **kwargs): + """Set an estimated time of work for the issue. + + Raises: + GitlabConnectionError: If the server cannot be reached. + """ + url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' % + {'project_id': self.project_id, 'issue_id': self.id}) + r = self.gitlab._raw_post(url, **kwargs) + raise_error_from_response(r, GitlabTimeTrackingError, 200) + return r.json() + + def reset_spent_time(self, **kwargs): + """Set an estimated time of work for the issue. + + Raises: + GitlabConnectionError: If the server cannot be reached. + """ + url = ('/projects/%(project_id)s/issues/%(issue_id)s/add_spent_time' % + {'project_id': self.project_id, 'issue_id': self.id}) + r = self.gitlab._raw_post(url, **kwargs) + raise_error_from_response(r, GitlabTimeTrackingError, 200) + return r.json() class ProjectIssueManager(BaseManager): obj_cls = ProjectIssue From 1db24511e381d22b77acc9539f5b71eb7050e9a7 Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Thu, 16 Feb 2017 16:14:26 +0100 Subject: [PATCH 2/7] Added documentation for time tracking functions --- docs/gl_objects/issues.py | 20 ++++++++++++++++++++ docs/gl_objects/issues.rst | 31 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/docs/gl_objects/issues.py b/docs/gl_objects/issues.py index ad48dc80e..df13c20da 100644 --- a/docs/gl_objects/issues.py +++ b/docs/gl_objects/issues.py @@ -77,3 +77,23 @@ # project issue todo issue.todo() # end project issue todo + +# project issue time tracking stats +issue.time_stats() +# end project issue time tracking stats + +# project issue set time estimate +issue.set_time_estimate({'duration': '3h30m'}) +# end project issue set time estimate + +# project issue reset time estimate +issue.reset_time_estimate() +# end project issue reset time estimate + +# project issue set time spent +issue.add_time_spent({'duration': '3h30m'}) +# end project issue set time spent + +# project issue reset time spent +issue.reset_time_spent() +# end project issue reset time spent diff --git a/docs/gl_objects/issues.rst b/docs/gl_objects/issues.rst index d4cbf003d..27724b8b3 100644 --- a/docs/gl_objects/issues.rst +++ b/docs/gl_objects/issues.rst @@ -104,3 +104,34 @@ Make an issue as todo: .. literalinclude:: issues.py :start-after: # project issue todo :end-before: # end project issue todo + +Get time tracking stats: + +.. literalinclude:: issues.py + :start-after: # project issue time tracking stats + :end-before: # end project time tracking stats + +Set a time estimate for an issue: + +.. literalinclude:: issues.py + :start-after: # project issue set time estimate + :end-before: # end project set time estimate + +Reset a time estimate for an issue: + +.. literalinclude:: issues.py + :start-after: # project issue reset time estimate + :end-before: # end project reset time estimate + +Add spent time for an issue: + +.. literalinclude:: issues.py + :start-after: # project issue set time spent + :end-before: # end project set time spent + +Reset spent time for an issue: + +.. literalinclude:: issues.py + :start-after: # project issue reset time spent + :end-before: # end project reset time spent + From 5c1f074398bf5c6365f7e24d7e6c437d98eebb36 Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Tue, 28 Feb 2017 08:11:29 +0100 Subject: [PATCH 3/7] adjusted to pep8 guide lines --- gitlab/objects.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index 15ffda5b7..4cd95eed1 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1555,7 +1555,8 @@ def reset_time_estimate(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' % + url = ( + '/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' % {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) @@ -1567,7 +1568,8 @@ def add_spent_time(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' % + url = ( + '/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' % {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) @@ -1585,6 +1587,7 @@ def reset_spent_time(self, **kwargs): raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() + class ProjectIssueManager(BaseManager): obj_cls = ProjectIssue From 5a9119a0cb0318961d287fc010cbbb67262eb922 Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Tue, 28 Feb 2017 08:28:39 +0100 Subject: [PATCH 4/7] adjusted to pep8 guide lines --- gitlab/objects.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index 4cd95eed1..975c2e972 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1555,9 +1555,8 @@ def reset_time_estimate(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ( - '/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' % - {'project_id': self.project_id, 'issue_id': self.id}) + url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' + % {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1568,9 +1567,8 @@ def add_spent_time(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ( - '/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' % - {'project_id': self.project_id, 'issue_id': self.id}) + url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' + % {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() From 7a35a61bd863707af470797422f7745a09097529 Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Tue, 28 Feb 2017 08:39:17 +0100 Subject: [PATCH 5/7] adjusted to pep8 guide lines --- gitlab/objects.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index 975c2e972..ff87fac6c 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1555,8 +1555,9 @@ def reset_time_estimate(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_time_estimate' - % {'project_id': self.project_id, 'issue_id': self.id}) + url = ('/projects/%(project_id)s/issues/%(issue_id)s/' + 'reset_time_estimate' % + {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1567,8 +1568,9 @@ def add_spent_time(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ('/projects/%(project_id)s/issues/%(issue_id)s/reset_spent_time' - % {'project_id': self.project_id, 'issue_id': self.id}) + url = ('/projects/%(project_id)s/issues/%(issue_id)s/' + 'reset_spent_time' % + {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1579,7 +1581,8 @@ def reset_spent_time(self, **kwargs): Raises: GitlabConnectionError: If the server cannot be reached. """ - url = ('/projects/%(project_id)s/issues/%(issue_id)s/add_spent_time' % + url = ('/projects/%(project_id)s/issues/%(issue_id)s/' + 'add_spent_time' % {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) From a56881e00d3e3d55b2bbe19617f5bd201e611c4d Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Tue, 28 Feb 2017 08:43:57 +0100 Subject: [PATCH 6/7] adjusted to pep8 guide lines --- gitlab/objects.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index ff87fac6c..95f1d1f45 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1557,7 +1557,8 @@ def reset_time_estimate(self, **kwargs): """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' 'reset_time_estimate' % - {'project_id': self.project_id, 'issue_id': self.id}) + {'project_id': self.project_id, 'issue_id': self.id} + ) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1570,7 +1571,8 @@ def add_spent_time(self, **kwargs): """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' 'reset_spent_time' % - {'project_id': self.project_id, 'issue_id': self.id}) + {'project_id': self.project_id, 'issue_id': self.id} + ) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1583,7 +1585,8 @@ def reset_spent_time(self, **kwargs): """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' 'add_spent_time' % - {'project_id': self.project_id, 'issue_id': self.id}) + {'project_id': self.project_id, 'issue_id': self.id} + ) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() From bfc92aba4eddd6a7d1a99a1af4e7001f768f1bfc Mon Sep 17 00:00:00 2001 From: Sascha Mahmood Date: Tue, 28 Feb 2017 09:14:29 +0100 Subject: [PATCH 7/7] adjusted to pep8 guide lines --- gitlab/objects.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/gitlab/objects.py b/gitlab/objects.py index 95f1d1f45..78a0c287b 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -1556,9 +1556,8 @@ def reset_time_estimate(self, **kwargs): GitlabConnectionError: If the server cannot be reached. """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' - 'reset_time_estimate' % - {'project_id': self.project_id, 'issue_id': self.id} - ) + 'reset_time_estimate' % + {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1570,9 +1569,8 @@ def add_spent_time(self, **kwargs): GitlabConnectionError: If the server cannot be reached. """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' - 'reset_spent_time' % - {'project_id': self.project_id, 'issue_id': self.id} - ) + 'reset_spent_time' % + {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json() @@ -1584,9 +1582,8 @@ def reset_spent_time(self, **kwargs): GitlabConnectionError: If the server cannot be reached. """ url = ('/projects/%(project_id)s/issues/%(issue_id)s/' - 'add_spent_time' % - {'project_id': self.project_id, 'issue_id': self.id} - ) + 'add_spent_time' % + {'project_id': self.project_id, 'issue_id': self.id}) r = self.gitlab._raw_post(url, **kwargs) raise_error_from_response(r, GitlabTimeTrackingError, 200) return r.json()