Skip to content

Commit

Permalink
Check that issue_id exists when a 500 internal server error is receiv…
Browse files Browse the repository at this point in the history
…ed by Redmine
  • Loading branch information
brunetton committed Jan 20, 2015
1 parent 8ee7037 commit 0866dc9
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions redminetimesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
from docopt import docopt # http://docopt.org/
import moment # https://pypi.python.org/pypi/moment
from redmine import Redmine # https://pypi.python.org/pypi/python-redmine
from redmine.exceptions import AuthError, ResourceNoFieldsProvidedError
from redmine.exceptions import (
AuthError,
ResourceNoFieldsProvidedError,
ResourceNotFoundError,
ServerError
)



Expand Down Expand Up @@ -139,18 +144,40 @@ def fetchFromDatabase(db_filename, date):

def syncToRedmine(time_entries, date, redmine):
'''Push all given time_entries to Redmine'''
def issue_exists(id, redmine):
assert id
issue_exists = True
try:
redmine.issue.get(id)
except ResourceNotFoundError:
issue_exists = False
return issue_exists

print_("-> Sending entries")
try:
for time_entry_infos in time_entries:
# Send this activity to Redmine
time_entry = redmine.time_entry.create(
spent_on=date.date, # converts Moment date to Datetime
issue_id=time_entry_infos['issue_id'],
hours=time_entry_infos['duration'],
activity_id=time_entry_infos['activity_id'],
comments=time_entry_infos['comment']
)
print_('.')
try:
issue_id=time_entry_infos['issue_id']
# Send this activity to Redmine
time_entry = redmine.time_entry.create(
spent_on=date.date, # converts Moment date to Datetime
issue_id=issue_id,
hours=time_entry_infos['duration'],
activity_id=time_entry_infos['activity_id'],
comments=time_entry_infos['comment']
)
except ServerError:
# Error 500
# Check that issue id exists (may be a cause of 500 error on some Redmine versions)
print
if not issue_exists(issue_id, redmine):
print "** ERROR ** Internal server error received from Redmine. "\
"This is probably because issue id {} doesn't exists ?".format(issue_id)
else:
print "** ERROR ** Internal server error received from Redmine !"
print "\nLast time entry was:\n{}".format(pformat(time_entry_infos))
sys.exit(-1)
except ConnectionError as e:
print "Connection Error: {}".format(e.message)
print "\n"
Expand Down

0 comments on commit 0866dc9

Please sign in to comment.