def print_jira_preflight(tce): start, duration = z.get(['start', 'duration'], tce.entry) print bcolors.header('Adding Jira worklog:'), \ 'start(%s)' % bcolors.okblue(start.split('T')[-1].split('+')[0]), \ 'duration(%s)' % bcolors.okblue(timedelta(seconds=duration)), print bcolors.okblue(tce.ticket), tce.comment, print '...',
def print_jira_preflight(tce): start, duration = z.get(["start", "duration"], tce.entry) print bcolors.header("Adding Jira worklog:"), "start(%s)" % bcolors.okblue( start.split("T")[-1].split("+")[0] ), "duration(%s)" % bcolors.okblue(timedelta(seconds=duration)), print bcolors.okblue(tce.ticket), tce.comment, print "...",
def check_jira(self): """ Make sure the user has entered correct jira credentials """ print '\nChecking your Jira credentials...' try: # Jira automatically does a credentials check when you instantiate the class JIRA(self.jira_url, basic_auth=(self.jira_username, self.jira_password)) except JIRAError: raise RuntimeError(bcolors.fail('Invalid Jira credentials.')) print bcolors.okblue('Success!')
def check_toggl(self): """ Make sure the user has entered a correct toggl token """ print '\nChecking your Toggl credentials...' toggl = PyToggl(self.toggl_api_token) try: toggl.query('/me') except HTTPError: raise RuntimeError(bcolors.fail('Invalid Toggl API token.')) print bcolors.okblue('Success!')
def clear_all_tags(dt): print 'Clearing tags for day', dt.isoformat() entries = toggl.query('/time_entries', params={ 'start_date': get_start_for_date(dt), 'end_date': get_end_for_date(dt) }) # entries = z.filter(lambda e: JIGGLD_TAG in z.get('tags', e, []), entries) ids = list(z.pluck('id', entries)) tags = list(z.pluck('tags', entries, default=[])) print tags, bcolors.okblue('-->'), if not ids or not any(tags): print bcolors.okblue('No tags to clear') else: resp = toggl.update_tags(ids, [JIGGLD_TAG, 'jiggggld'], REMOVE_TAG) print list(z.pluck('tags', resp['data'], default=[]))
def print_valid_entries(entries): print bcolors.header(bcolors.underline("\nLogging to Jira\n")) # entries = map(get_val, entries) groups = z.groupby(z.get("description", default=""), entries) cleaned = map(split_description, groups.iteritems()) for (issue, comment), es in cleaned: print (bcolors.bold("%-8s") + " %s") % (sum_as_timedelta(es), bcolors.okblue(issue) + " " + comment)
def print_valid_entries(entries): print bcolors.header(bcolors.underline('\nLogging to Jira\n')) # entries = map(get_val, entries) groups = z.groupby(z.get('description', default=''), entries) cleaned = map(split_description, groups.iteritems()) for (issue, comment), es in cleaned: print(bcolors.bold('%-8s') + ' %s') % ( sum_as_timedelta(es), bcolors.okblue(issue) + ' ' + comment)
def for_day(dt): print_welcome_for_date(dt) entries = toggl.query('/time_entries', params={'start_date': get_start_for_date(dt), 'end_date': get_end_for_date(dt)}) entries = z.map(replace_cologne, entries) valid_entries, invalid_entries = split_entries(entries) print_invalid_entries(invalid_entries) valid_entries = list(z.map(get_val, valid_entries)) if len(valid_entries) == 0: print bcolors.okblue('No time entries to log for today') # if not raw_input('\nContinue? (Y/n)') in ('Y', 'y', ''): # exit() return print_valid_entries(valid_entries) print_total_for_day(valid_entries) if raw_input('\nLog time? (Y/n) ') not in ('Y', 'y', ''): print bcolors.fail('Will not log time. Exiting') exit() print logged_entries = [] # the tickets on Toggl that need to be marked as jiggld tces = z.groupby(lambda tce: tce.ticket, z.map(to_tce, valid_entries)) try: for ticked_id, tces in tces.iteritems(): for tce in tces: print_jira_preflight(tce) with ensure_open_ticket(ticked_id): # continue try: worklog = add_worklog(tce) except JIRAError as e: if 'non-editable workflow state' in str(e): print bcolors.fail('CANNOT EDIT!') exit() print bcolors.fail(str(e)) continue # print tce.entry['id'] logged_entries.append(tce.entry) print 'WORKLOG ID', worklog.id record_worklog(tce, worklog.id) print_jira_postflight(tce, None) # if raw_input('Good?') != '': # print bcolors.fail('Exiting') # exit() except Exception as e: print bcolors.fail(str(e)) raise finally: if logged_entries: print 'Marking %s Toggl entries as tagged' % (len(logged_entries)) resp = toggl.update_tags(list(z.pluck('id', logged_entries)), [JIGGLD_TAG]) else: print 'No Toggl entries to tag as jiggld'