/
ugit.js
executable file
·139 lines (126 loc) · 4.55 KB
/
ugit.js
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
#!/usr/bin/env node
/*
* Copyright 2015, All Rights Reserved.
*
* Code licensed under the MIT License:
* https://github.com/grevolution/ugit/blob/master/LICENSE.md
*
* @author Shan Ul Haq <g@grevolution.me>
*/
'use strict';
var inquirer = require("inquirer");
var exec = require("child_process").exec;
var json = '';
function findAndCommit(keyword) {
if(!process.env.UGIT_UNFUDDLE_DOMAIN || !process.env.UGIT_UNFUDDLE_USERNAME
|| !process.env.UGIT_UNFUDDLE_PASSWORD) {
console.log("environment variables are not defined")
return
}
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb').concat(" ").concat("\""+keyword+"\""), printTicketsToSelect);
}
function printTicketsToSelect(err, stdout, stderr){
if(err || stderr) {
console.log(stderr)
return
}
json = JSON.parse(stdout)
var summaries = []
for(var i in json) {
if(json[i].title.length > 70) {
summaries.push(json[i].title.substring(0,70)+"...")
} else {
summaries.push(json[i].title)
}
}
inquirer.prompt([
{
type: "list",
name: "ticket",
message: "Ticket:",
choices: summaries,
},
{
type: "input",
name: "message",
message: "Your message?",
default: function() {return "Implementation Done"}
}
], function( answers ) {
var commit_message = answers.ticket.concat(" - ").concat(answers.message)
exec('git commit -m \"'.concat(commit_message).concat("\""), showAll)
if(process.env.UGIT_POWERFULL_COMMIT == "true") {
//if the message contains the word `fixed` in it. mark the ticket as resolved
//if the ticket contains the word `spent` in it. see the next
for(var i in json) {
var t = json[i]
if(t.title.valueOf().indexOf(answers.ticket.valueOf()) > -1 ) {
checkFixedAndTime(t, answers.message)
}
}
}
});
}
var cMessage = ""
function checkFixedAndTime(obj, msg) {
var resolved = false;
if(msg.toLowerCase().indexOf("fixed") > -1) {
//the string contains the word fixed.
var ticketId = getTicketId(obj)
var projectId = obj.project_id
exec('git rev-parse --verify HEAD', function(err, stdout, stderr){
cMessage = stdout
if(cMessage && cMessage.trim().length > 0) {
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb -u ').concat(projectId+" ").concat(ticketId+" ").concat("1"+" ").concat(cMessage), showError);
} else {
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb -u ').concat(projectId+" ").concat(ticketId+" ").concat("1"+" "), showError);
}
});
resolved = true
}
var spIndex = msg.toLowerCase().indexOf("spen")
if(spIndex > -1){
var str1 = msg.substring(spIndex + "spen".length + 1)
var timeEntryMessage = msg.substring(0, spIndex + 1);
var hIndex = str1.toLowerCase().indexOf("h");
if(hIndex > -1){
var time1 = str1.substring(0, hIndex);
time1 = time1.trim();
var timeSpent = parseFloat(time1)
if(timeSpent > 0){
exec('git rev-parse --verify HEAD', function(err, stdout, stderr){
cMessage = stdout
if(cMessage.trim().length <= 0){
cMessage = "";
}
//enter time entry
var ticketId = getTicketId(obj)
var projectId = obj.project_id
if(!resolved){
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb -u ').concat(projectId+" ").concat(ticketId+" ").concat("0"), showError);
}
if(timeEntryMessage.trim().length > 0) {
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb -a ').concat(projectId+" ").concat(ticketId+" ").concat(""+timeSpent).concat(" \""+ timeEntryMessage+ " " + cMessage +"\""), showError);
} else {
exec('ruby '.concat(process.env.NODE_PATH).concat('/ugit/unfuddler/upload.rb -a ').concat(projectId+" ").concat(ticketId+" ").concat(""+timeSpent).concat(" " + cMessage), showError);
}
});
}
}
}
}
function getTicketId(obj){
var location = obj.location
var arr = location.split("/")
return arr[arr.length -1]
}
function showAll(err, stdout, stderr) {
if(stdout) console.log(stdout)
if(stderr) console.log(stderr)
}
function showError(err, stdout, stderr) {
if(err) console.log(err)
if(stdout) console.log(stdout)
if(stderr) console.log(stderr)
}
findAndCommit(process.argv[2])