-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempo.ts
executable file
·221 lines (183 loc) · 7.62 KB
/
tempo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#! /usr/bin/env deno run --allow-run --allow-env --allow-net
import {
DAY_IN_MS,
SEC_IN_MS,
WEEK_IN_MS,
format,
} from './common/date.ts';
import {
bold,
cyan,
dim,
italic,
magenta,
yellow,
} from './common/colors.ts';
import {
abort,
prompt,
} from './common/io.ts';
import { parse } from './common/args.ts';
import {
Issue as JiraIssue,
IssuePermissions as JiraIssuePermissions,
Worklog as TempoWorklog,
addWorklog,
getIssue,
getPermissions,
getWorklogs,
} from './apis/jira.ts';
import {
Record as TimewRecord,
iterateRecords,
} from './apis/timew.ts';
type TimewRecordCompleted = TimewRecord & { end: NonNullable<TimewRecord['end']> }
const RX_JIRA_TICKET = /^(?:CSD|INT)-\d+$/;
const USAGE = [
`${bold('Usage')}: tempo.ts <...options>`,
'Uploads timewarrior records to your jira tempo timesheet.',
'',
'Options:',
` ${cyan(bold('--from'))} Date to begin uploading records from.`,
` ${cyan(bold('--until'))} Date to upload records until.`,
` ${cyan(`${bold('--help')} -h`)} Prints this help message.`,
].join('\n');
const isCompleted = (record: TimewRecord): record is TimewRecordCompleted => record.end != null;
const linebreak = ( ) => console.log('');
// Parse command line arguments
const args = parse(Deno.args);
if (args['help'] || args['h']) (console.log(USAGE), Deno.exit(0));
// Get range to be submitted
const { until, from } = args;
if (until != null && typeof until !== 'string') abort(`Invalid value for ${bold('--until')} argument, expected a date`);
if (from != null && typeof from !== 'string') abort(`Invalid value from ${bold('--from')} argument, expected a date`);
const dateUntil = until != null ? new Date(until) : (function getCurrentDate( ) {
console.log(yellow('No end date provided.'));
const now = new Date( );
console.log(dim('Defaulting to current time.'));
return now;
})( );
if (until != null && Number.isNaN(dateUntil)) abort(`Invalid value for ${bold('--until')} argument, expected a date`);
linebreak( );
const dateBegin = from != null ? new Date(from) : await (async function getLastSubmissionDate( ) {
console.log(yellow('No start date provided.'));
console.log(dim('Defaulting to date of last submission.'));
const intervals = {
day: DAY_IN_MS,
week: WEEK_IN_MS,
month: 31 * DAY_IN_MS,
trimester: 3 * 31 * DAY_IN_MS,
};
const unchecked = (Object.keys(intervals) as Array<keyof typeof intervals>)
.sort((a, b) => intervals[b] - intervals[a]);
let lastSubmittedWorklog: TempoWorklog | null = null;
let dateTo = dateUntil;
while (lastSubmittedWorklog == null && unchecked.length) {
const interval = unchecked.pop( ) as keyof typeof intervals;
const dateFrom = new Date(dateUntil.getTime( ) - intervals[interval]);
console.log(dim(`Looking for worklogs submitted since ${bold(format(dateFrom))}`));
const worklogs = await getWorklogs(dateFrom, dateTo);
dateTo = dateFrom;
if (!worklogs.length) continue;
lastSubmittedWorklog = worklogs
.sort((a: TempoWorklog, b: TempoWorklog) => a.started.getTime( ) - b.started.getTime( ))
.pop( ) as TempoWorklog;
}
if (lastSubmittedWorklog == null) {
const reason = `Unable to find any worklogs submitted before ${bold(dateUntil)}`;
const solution = `please provide a ${bold('--from')} date`;
abort(`${reason}, ${solution}`);
}
const lastSubmittedIssue = lastSubmittedWorklog.issue.key;
const lastSubmittedStart = lastSubmittedWorklog.started;
const lastSubmittedDuration = lastSubmittedWorklog.timeSpentSeconds * SEC_IN_MS;
const lastSubmittedFinished = new Date(lastSubmittedStart.getTime( ) + lastSubmittedDuration);
console.log(dim(`Last submission: ${bold(lastSubmittedIssue)} (${format(lastSubmittedStart)} -> ${format(lastSubmittedFinished)})`));
return lastSubmittedFinished;
})( );
if (from != null && Number.isNaN(dateBegin)) abort(`Invalid value for ${bold('--from')} argument, expected a date`);
linebreak( );
/** Checks if the given record is in the time range to be submitted to Tempo. */
const isInRange = (record: TimewRecordCompleted) => record.start >= dateBegin && record.end <= dateUntil;
// Get any existing records in that range, so we avoid conflicts
console.log(`Checking for existing submissions in range: ${magenta(format(dateBegin))} -> ${magenta(format(dateUntil))}`);
const existing = await getWorklogs(dateBegin, dateUntil);
console.log(`Existing submissions found: ${bold(cyan(existing.length))}`);
for (const worklog of existing) {
const worklogDuration = worklog.timeSpentSeconds * SEC_IN_MS;
const worklogFinished = new Date(worklog.started.getTime( ) + worklogDuration);
console.log(dim(`${worklog.issue.key}: ${italic(format(worklog.started))} -> ${italic(format(worklogFinished))}`));
}
linebreak( );
/** Checks if the given record conflicts with an existing worklog. */
const findConflict = (record: TimewRecordCompleted) => existing.find(function isConflictingWith(worklog) {
if (worklog.started > record.end) return false;
const worklogDuration = worklog.timeSpentSeconds * SEC_IN_MS;
const worklogFinished = worklog.started.getTime( ) + worklogDuration;
return record.start.getTime( ) <= worklogFinished;
});
// Work through and submit timewarrior records
console.log(`Submitting range: ${magenta(format(dateBegin))} -> ${magenta(format(dateUntil))}.`);
let numRecordsIgnored = 0;
function resetIgnoredCount( ) {
if (numRecordsIgnored > 0) console.log(dim(`Ignored ${bold(numRecordsIgnored)} timewarrior record(s).`));
numRecordsIgnored = 0;
}
const issues = new Map<string, JiraIssue>( );
const permissions = new Map<string, JiraIssuePermissions>( );
for await (const record of iterateRecords( )) {
// ignore currently ongoing record
if (!isCompleted(record)) {
numRecordsIgnored += 1;
continue;
}
// ignore record outside wanted range
if (!isInRange(record)) {
numRecordsIgnored += 1;
continue;
}
linebreak( );
const recordIdent = bold(`@${record.id}`);
const recordRange = `${magenta(format(record.start))} -> ${magenta(format(record.end))}`;
const recordTags = record.tags.map((tag) => italic(cyan(tag))).join(', ');
// ignore record conflicting with existing worklog
const conflict = findConflict(record);
if (conflict != null) {
console.log(`${recordIdent} - ${recordRange}: ${recordTags}`);
const finished = new Date(conflict.started.getTime( ) + conflict.timeSpentSeconds * SEC_IN_MS);
const existing = `${bold(conflict.issue.key)} (${italic(format(conflict.started))} -> ${italic(format(finished))})`;
console.log(`Ignoring - conflicts with existing worklog: ${existing}`);
numRecordsIgnored += 1;
continue;
}
resetIgnoredCount( );
console.log(`${recordIdent} - ${recordRange}: ${recordTags}`);
const ticket = record.tags.find((tag) => RX_JIRA_TICKET.test(tag)) ?? await (async function promptForTicket( ) {
const ticket = await prompt([
`No ticket id found within the tags for record ${recordIdent}.`,
`Tags: ${recordTags}`,
'Please provide a valid ticket ID: ',
].join('\n'));
return ticket.toUpperCase( );
})( );
if (ticket == null || !RX_JIRA_TICKET.test(ticket)) {
console.log(yellow('No ticket matched to record.'));
continue;
}
console.log(`Checking permissions for issue: ${bold(ticket)}`);
// TODO: handle issue not found
const issue = issues.get(ticket) ?? await getIssue(ticket);
issues.set(ticket, issue);
const permission = permissions.get(ticket) ?? await getPermissions(ticket);
permissions.set(ticket, permission);
const { WORK_ON_ISSUES } = permission;
if (!WORK_ON_ISSUES.havePermission) throw new Error(`No work permissions for issue ${issue.key}`);
console.log(`Submitting under issue: ${bold(issue.key)} (${italic(issue.summary)})`);
await addWorklog({
from: record.start,
to: record.end,
issue: issue,
});
}
linebreak( );
resetIgnoredCount( );