Skip to content

Commit 5236920

Browse files
committed
Add remove_time_spent command
1 parent 8b2b354 commit 5236920

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

gtt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const version = '1.1.1';
3+
const version = '1.1.2';
44
const program = require('commander');
55

66
program

include/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const defaults = {
1818
recordColumns: ['user', 'date', 'type', 'iid', 'time'],
1919
userColumns: false,
2020
dateFormat: 'DD.MM.YYYY HH:mm:ss',
21-
timeFormat: '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]',
21+
timeFormat: Time.defaultTimeFormat,
2222
output: 'table',
2323
excludeByLabels: false,
2424
includeByLabels: false,

models/hasTimes.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Time = require('./time');
66

77
const regex = /added (.*) of time spent/i;
88
const subRegex = /subtracted (.*) of time spent/i;
9+
const removeRegex = /Removed time spent/i;
910

1011
/**
1112
* base model for models that have times
@@ -53,8 +54,14 @@ class hasTimes extends Base {
5354
let timeSpent = 0;
5455
let timeUsers = {};
5556

57+
// sort by created at
58+
this.notes.sort((a, b) => {
59+
if (a.created_at === b.created_at) return 0;
60+
return moment(a.created_at).isBefore(b.created_at) ? -1 : 1;
61+
});
62+
5663
let promise = this.parallel(this.notes, (note, done) => {
57-
let created = moment(note.created_at), match, subMatch;
64+
let created = moment(note.created_at), match, subMatch, removeMatch;
5865

5966
if (
6067
// filter out user notes
@@ -64,11 +71,13 @@ class hasTimes extends Base {
6471
// filter out times that are not in the given time frame
6572
!(created.isSameOrAfter(this.config.get('from')) && created.isSameOrBefore(this.config.get('to'))) ||
6673
// filter out notes that are no time things
67-
!(match = regex.exec(note.body)) && !(subMatch = subRegex.exec(note.body))
74+
!(match = regex.exec(note.body)) && !(subMatch = subRegex.exec(note.body)) && !(removeMatch = removeRegex.exec(note.body))
6875
) return done();
6976

7077
if (!timeUsers[note.author.username]) timeUsers[note.author.username] = 0;
71-
let time = new Time(match ? match[1] : `-${subMatch[1]}`, note, this, this.config);
78+
79+
let timeString = match ? match[1] : (subMatch ? `-${subMatch[1]}` : `-${Time.toHumanReadable(timeSpent, this.config.get('hoursPerDay'))}`);
80+
let time = new Time(timeString, note, this, this.config);
7281

7382
timeSpent += time.seconds;
7483
timeUsers[note.author.username] += time.seconds;

models/time.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const _ = require('underscore');
22
const moment = require('moment');
33

4+
const defaultTimeFormat = '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]';
45
const mappings = ['complete', 'sign', 'days', 'hours', 'minutes', 'seconds'];
56
const regex = /^(?:([-])\s*)?(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s\s*)?$/;
67
const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/ig;
@@ -31,6 +32,10 @@ class time {
3132
/*
3233
* properties
3334
*/
35+
static get defaultTimeFormat() {
36+
return defaultTimeFormat;
37+
}
38+
3439
get user() {
3540
return this.data.author.username;
3641
}
@@ -51,7 +56,6 @@ class time {
5156
return time.toHumanReadable(this.seconds, this._hoursPerDay, this._timeFormat);
5257
}
5358

54-
5559
get _timeFormat() {
5660
return this.config && this.config.get('timeFormat') ? this.config.get('timeFormat') : '';
5761
}
@@ -85,7 +89,7 @@ class time {
8589
* @param format
8690
* @returns {string}
8791
*/
88-
static toHumanReadable(input, hoursPerDay = 8, format = '') {
92+
static toHumanReadable(input, hoursPerDay = 8, format = time.defaultTimeFormat) {
8993
let sign = parseInt(input) < 0 ? '-' : '', output = format, match;
9094
input = Math.abs(input);
9195

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gitlab-time-tracker",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "A CLI that makes working with GitLabs time tracking feature more enjoyable",
55
"main": "gtt.js",
66
"scripts": {},

0 commit comments

Comments
 (0)