Skip to content

Commit 4274d48

Browse files
committed
Add create command that creates a new issue or merge_request when syncing
1 parent 69ebc10 commit 4274d48

File tree

11 files changed

+83
-10
lines changed

11 files changed

+83
-10
lines changed

readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,14 @@ gtt start --type=merge_request "kriskbx/example-project" 15
182182
gtt start --type=merge_request 15
183183
```
184184

185-
If you configured a project in your config you can omit the project.
185+
**Start local time monitoring and create a new issue or merge request with the given title:**
186+
187+
```shell
188+
gtt create "kriskbx/example-project" "New Issue"
189+
gtt create "New Issue"
190+
gtt create --type=merge_request "kriskbx/example-project" "New Issue"
191+
gtt create --type=merge_request "New Issue"
192+
```
186193

187194
**Show the current time monitoring status:**
188195

src/gtt-cancel.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ let tasks = new Tasks(config);
1717

1818
tasks.cancel()
1919
.then(frames => {
20-
frames.forEach(frame => console.log(`Cancel project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green}`))
20+
frames.forEach(frame => {
21+
if(!frame.resource.new)
22+
return console.log(`Cancel project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green}`)
23+
24+
console.log(`Cancel project ${frame.project.magenta} for new ${frame.resource.type} "${(frame.resource.id).blue}", started ${moment(frame.start).fromNow().green}`)
25+
})
2126
})
2227
.catch(error => Cli.error(error));

src/gtt-create.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const colors = require('colors');
2+
const moment = require('moment');
3+
const program = require('commander');
4+
5+
const Config = require('./include/file-config');
6+
const Cli = require('./include/cli');
7+
const Tasks = require('./include/tasks');
8+
9+
program
10+
.arguments('[project] [title]')
11+
.option('-t, --type <type>', 'specify resource type: issue, merge_request')
12+
.option('--verbose', 'show verbose output')
13+
.parse(process.argv);
14+
15+
Cli.verbose = program.verbose;
16+
17+
let config = new Config(process.cwd()),
18+
tasks = new Tasks(config),
19+
type = program.type ? program.type : 'issue',
20+
title = program.args.length === 1 ? program.args[0] : program.args[1],
21+
project = program.args.length === 2 ? program.args[0] : null;
22+
23+
if (program.args.length < 2 && !config.get('project'))
24+
Cli.error('No project set');
25+
26+
if (!title)
27+
Cli.error('Wrong or missing title');
28+
29+
tasks.start(project, type, title)
30+
.then(frame => console.log(`Starting project ${config.get('project').magenta} and create ${type} "${title.blue}" at ${moment().format('HH:mm').green}`))
31+
.catch(error => Cli.error(error));

src/gtt-log.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ tasks.log()
3232
frames[date]
3333
.sort((a, b) => moment(a.start).isBefore(moment(b.start)) ? -1 : 1)
3434
.forEach(frame => {
35-
console.log(` ${frame.id} ${moment(frame.start).format('HH:mm').green} to ${moment(frame.stop).format('HH:mm').green}\t${toHumanReadable(frame.duration)}\t\t${frame.project.magenta}\t\t${(frame.resource.type + ' #' + frame.resource.id).blue}`)
35+
let issue = frame.resource.new ? `new ${frame.resource.type + ' "' + frame.resource.id.blue}"` : `${(frame.resource.type + ' #' + frame.resource.id).blue}`;
36+
console.log(` ${frame.id} ${moment(frame.start).format('HH:mm').green} to ${moment(frame.stop).format('HH:mm').green}\t${toHumanReadable(frame.duration)}\t\t${frame.project.magenta}\t\t${issue}`)
3637
});
3738
});
3839
}

src/gtt-stop.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ let config = new Config(__dirname),
1717

1818
tasks.stop()
1919
.then(frames => {
20-
frames.forEach(frame => console.log(`Stopping project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`));
20+
frames.forEach(frame => {
21+
if(!frame.resource.new)
22+
return console.log(`Stopping project ${frame.project.magenta} ${frame.resource.type.blue} ${('#' + frame.resource.id).blue}, started ${moment(frame.start).fromNow().green} (id: ${frame.id})`)
23+
24+
console.log(`Stopping project ${frame.project.magenta} for new ${frame.resource.type} "${(frame.resource.id).blue}", started ${moment(frame.start).fromNow().green} (id: ${frame.id})`)
25+
});
2126
})
2227
.catch(error => Cli.error(error));

src/gtt-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let config = new Config(process.cwd())
2121
tasks.syncInit()
2222
.then(() => tasks.sync.frames.length === 0 ? process.exit(0) : null)
2323
.then(() => {
24-
Cli.bar(`${Cli.fetch} Fetching issues & merge requests...`, tasks.sync.frames.length);
24+
Cli.bar(`${Cli.fetch} Fetching or creating issues & merge requests...`, tasks.sync.frames.length);
2525
return tasks.syncResolve(Cli.advance);
2626
})
2727
.then(() => {

src/gtt.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const program = require('commander');
66
program
77
.version(version)
88
.command('start [project] [id]', 'start monitoring time for the given project and resource id')
9+
.command('create [project] [title]', 'start monitoring time for the given project and create a new issue or merge request with the given title')
910
.command('status', 'shows if time monitoring is running')
1011
.command('stop', 'stop monitoring time')
1112
.command('cancel', 'cancel and discard active monitoring time')

src/include/tasks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class tasks {
5555

5656
this.sync.resources[type][id] = new classes[type](this.config, {});
5757
this.sync.resources[type][id]
58-
.make(project, id)
58+
.make(project, id, frame.resource.new)
5959
.then(() => {
6060
if (callback) callback();
6161
done();
@@ -115,6 +115,11 @@ class tasks {
115115
resource.createTime(Math.ceil(time))
116116
.then(() => resource.getNotes())
117117
.then(() => {
118+
if(frame.resource.new) {
119+
delete frame.resource.new;
120+
frame.resource.id = resource.data.iid;
121+
}
122+
118123
frame.notes.push({
119124
id: resource.notes[0].id,
120125
time: Math.ceil(time)

src/models/frame.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class frame {
1515
this.config = config;
1616
this.project = config.get('project');
1717
this.resource = {id, type};
18+
19+
if(typeof id === 'string' || id instanceof String)
20+
this.resource.new = true;
21+
1822
this.id = frame.generateId();
1923
this.start = false;
2024
this.stop = false;

src/models/issue.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ class issue extends hasTimes {
1212
this.data = data;
1313
}
1414

15-
make(project, id) {
16-
let promise = this.get(`projects/${encodeURIComponent(project)}/issues/${id}`);
15+
make(project, id, create = false) {
16+
let promise;
17+
18+
if (create) {
19+
promise = this.post(`projects/${encodeURIComponent(project)}/issues`, {title: id});
20+
} else {
21+
promise = this.get(`projects/${encodeURIComponent(project)}/issues/${id}`);
22+
}
23+
1724
promise.then(issue => {
1825
this.data = issue.body;
1926
return promise;

0 commit comments

Comments
 (0)