-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitcamp
executable file
·513 lines (397 loc) · 12.5 KB
/
gitcamp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/php
<?php
if ( ! @include 'Zend/Loader.php' ) {
echo "Zend Framework could not be found. Please try installing with:\n\n";
echo " sudo pear channel-discover zend.googlecode.com/svn\n";
echo " sudo pear install zend/zend\n\n";
exit;
}
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__)));
date_default_timezone_set('PST8PDT');
$GitCamp = new GitCamp();
class GitCamp {
public $opts;
public $args;
public $method;
// Loaded from git & Basecamp
public $subdomain;
public $apitoken;
public $personid;
public $project;
public $script_name;
public $basecamp;
function __construct() {
// Load Zend Classes
foreach( array('Zend_Console_Getopt', 'Zend_Http_Client', 'Zend_Cache') as $class ){
try {
@Zend_Loader::loadClass($class);
}catch ( Zend_Exception $e) {
exit("Unable to load Zend class $class.\n");
}
}
if ( !@ include_once ($file = dirname(__FILE__).'/basecamp.php') ) {
exit("Unable to load file $file\n");
}
// CLI arguments
$this->opts = new Zend_Console_Getopt('abp:');
if ( empty($this->args) ) { $this->args = $this->opts->getRemainingArgs(); }
$this->method = ( empty($this->args[0]) ) ? 'help' : strtolower( $this->args[0] ); // Default to help method
$this->script_name = basename($_SERVER["SCRIPT_FILENAME"]);
$this->run();
}
// General routing
private function run() {
// Aliases
switch ( $this->method ) {
case 'help':
$this->help(); // Don't run init for help
break;
case 'clear':
case 'cache':
$this->init();
$this->flush();
break;
case 't':
case 'todos':
$this->init();
$this->todo();
exit;
break;
default:
// Call class method by subcommand
$method = $this->method;
if (method_exists($this, $method) ) {
$refl = new ReflectionMethod($this, $method);
if ( $refl->isPublic() ) {
$this->init();
$this->$method();
}else {
$this->help();
}
}else {
$this->help();
}
break;
}
exit;
}
// Default output
public function help() {
include 'help.txt';
exit;
}
// Setup
public function init() {
// Are we in a git repo?
unset($return); exec('git status >/dev/null 2>&1', $return, $exit);
$in_git_repo = ($exit === 0) ? true : false;
if ( !$in_git_repo && $this->method == 'init') {
exit("Please run $this->script_name init inside a git repository.\n");
}
// Alias
unset($return);
exec('git config --get alias.camp', $return);
if ( empty( $return ) ) {
exec( "git config --global alias.camp '!gitcamp $1'" );
$this->init();
}
// Subdomain
unset($return);
exec('git config --get basecamp.subdomain', $return);
$this->subdomain = $return[0];
if (empty( $this->subdomain) ) {
$this->subdomain = $this->input("# Enter your Basecamp subdomain: ");
exec( 'git config --global basecamp.subdomain ' . $this->subdomain );
$this->init();
}
// API Token
unset($return);
exec('git config --get basecamp.apitoken', $return);
$this->apitoken = $return[0];
if (empty( $this->apitoken ) ) {
$this->apitoken = $this->input("# Enter your basecamp API Token: ");
exec( 'git config --global basecamp.apitoken ' . $this->apitoken );
$this->init();
}
// Connect to Basecamp
$this->basecamp = new Basecamp( $this->subdomain, $this->apitoken );
// Person ID
unset($return);
exec('git config --get basecamp.personid', $return);
$this->personid = $return[0];
if ( empty($this->personid) ) {
$me = $this->basecamp->current_person();
$this->personid = $me->id;
exec( 'git config --global basecamp.personid ' . $this->personid );
$this->init();
}
// Project Name & ID
unset($return);
exec('git config --get basecamp.projectid', $return);
$this->project->id = $return[0];
if ( $in_git_repo && empty( $this->project->id ) ) {
echo "Loading projects...\n";
// Show only active projects
$projects = $this->basecamp->projects()->xpath('//project[status="active"]');
echo "\n";
foreach ( $projects as $key => $project ) {
echo "[$key] {$project->name}\n";
}
echo "\n";
$key = $this->input('Which project is this git repo for?');
$this->project->id = $projects[$key]->id;
exec( 'git config basecamp.projectid ' . $this->project->id ); // Not global
$this->init();
}else {
foreach ( $this->basecamp->projects() as $project ) {
if ( $project->id == $this->project->id ) {
$this->project->name = $project->name;
}
}
}
if ( $this->method == 'init' ) { // Called explicitly
$this->hooks(); // Setup hooks
echo "Gitcamp setup complete. Hooks will run on 'git commit'.\n";
echo "To change settings, run gitcamp config\n\n";
exit; // If init was the subcommand, we don't need to continue
}
}
// Change existing settings
public function config() {
echo "Change what setting?\n";
echo " [s]ubdomain $this->subdomain \n";
echo " [p]roject {$this->project->name} \n";
echo " [a]pi key $this->apitoken \n\n";
echo " [c]ache flush\n";
echo " [q]uit\n";
$this->input();
}
// Copy gitcamp hooks into .git/hooks in current repository
public function hooks() {
exec( 'if [ -d ./.git ]; then echo 1; else echo 0; fi', $is_git_root );
if ( $is_git_root[0] == '0' ) {
echo "Please run $this->script_name init from the root of your git directory.\n";
exit;
}else {
exec('cp -f '.dirname(__FILE__).'/hooks/prepare-commit-msg .git/hooks/prepare-commit-msg; chmod 755 .git/hooks/prepare-commit-msg;');
// echo "Prepare commit message hook added.\n";
exec('cp -f '.dirname(__FILE__).'/hooks/commit-msg .git/hooks/commit-msg; chmod 755 .git/hooks/commit-msg;');
// echo "Post-commit hook added.\n";
}
}
// Force delete application cache
public function flush() {
$this->basecamp->cache->clean();
exit("Cache cleared\n");
}
// List all todo lists and items for current project
public function todo() {
if ( !empty( $this->project->id ) ) {
// In a Git Repo assigned to a specific project
$this->todo_output($this->project->id);
}else {
// No project, list them all
// Show only active projects
$projects = $this->basecamp->projects()->xpath('//project[status="active"]');
echo "\n";
foreach ( $projects as $key => $project ) {
ob_start();
$this->todo_output( $project->id );
$tasks = ob_get_contents();
ob_end_clean();
if ( !empty($tasks) ) {
echo "\n\n$project->name\n"
. str_repeat( '-', strlen($project->name) ) . "\n"
. str_replace("\n#", "\n #", $tasks);
}
}
echo "\n";
}
}
// List all todo lists and items for any project
private function todo_output( $project_id ) {
$lists = $this->basecamp->lists( $project_id, false); // complete = false
if ( is_a($lists, 'SimpleXMLElement') ) {
$queries = array(
"\n## ASSIGNED TO ME ##\n" => '//todo-item[completed="false" and responsible-party-id="'.$this->personid.'"]',
"\n## ASSIGNED TO ANYONE ##\n" => '//todo-item[completed="false" and not(responsible-party-id) ]',
);
// Two sections of lists base on assignment
foreach ( $queries as $title => $query ) {
ob_start();
foreach ( $lists as $list) {
$list_items = $this->basecamp->list_items( $list->id )->xpath( $query );
if (empty($list_items)){ continue; }
echo "\n# $list->name\n";
foreach ( $list_items as $item ) {
$content = str_replace( "\n", ' ', $item->content );
echo "# $content #{$item->id}t\n";
}
$this->time_tracking_enable($list);
}
$tasks = ob_get_contents();
ob_end_clean();
if ( !empty($tasks) ) {
echo $title;
echo $tasks;
}
}
}
}
// Scan the last Git commit message for todo IDs and time entries
// Send updates to Basecamp
public function complete() {
exec( 'pwd', $pwd );
$commit_file = $pwd[0].'/'.$this->args[2];
if ( false === ( $commit_message = file_get_contents( $commit_file ) ) ) { // Get message from file
exec( 'git log -n 1 --format=format:"%B"', $commit_message ); // Get Message from log
}else {
$commit_message = explode( "\n", $commit_message);
}
// Check for commit message source
$source_regex = '/# source:([a-z]*)/i';
preg_match($source_regex, implode( "\n", $commit_message), $match);
$source = $match[1];
if ( in_array($source, array(
'message',
'merge',
'squash',
'amend',
), true) ) {
// Not a normal commit.
// Remove source info from commit
$commit_message = preg_replace($source_regex, '', implode( "\n", $commit_message));
$fh = fopen($commit_file, 'w') or die('Cannot write to commit file: '.$commit_file);
fwrite($fh, $commit_message);
fclose($fh);
exit("Commit from $source: Tasks and time entries are not proccessed.\n\n");
}
// Find solo time entries
foreach ( $commit_message as $line ) {
if ($line[0] == '#') {continue;} // Comment
preg_match('/
^ # beginning of line
[\s]* # optional white space
t # t seperator
[\s]* # optional white space
([.0-9:]*) # time entry (1)
[\s]* # optional white space
([a-z \d !-~ \s]*) # description (2)
$ # end of line
/ix',$line, $match);
$task = array( 'time' => $match[1], 'description' => $match[2], );
// Time Tracking
if ( !empty( $task['time'] ) ) {
$this->basecamp->create_time_entry( $this->project->id, array(
'person-id' => $this->personid,
'date' => date('Y-m-d'),
'hours' => $task['time'],
'description' => $task['description'],
));
}
}
// Find todo entries
foreach ( $commit_message as $line ) {
if ($line[0] == '#') {continue;} // Comment
preg_match('/
[#]([0-9]{8}) # todo item ID (1)
[\s]* # optional white space
t # t seperator
[\s]* # optional white space
([.0-9:]*) # time entry (2)
[\s]* # optional white space
([a-z \d !-~ \s]*)$ # description (3)
/ix',$line, $match);
if (!empty($match[1])) {
$tasks[] = array(
'id' => $match[1],
'time' => $match[2],
'description' => $match[3],
'line' => $line,
);
}
}
if (empty($tasks)) {
exit("No tasks found in last commit message.\n\n");
}
echo "# Marking tasks complete... \n";
foreach ($tasks as $task) {
echo '# '.$task['line'].'... ';
// Time Tracking
if ( !empty( $task['time'] ) ) {
$this->basecamp->create_time_entry_for_item( $task['id'], array(
'person-id' => $this->personid,
'date' => date('Y-m-d'),
'hours' => $task['time'],
'description' => $task['description'],
));
}
// Todo
if ( $this->basecamp->complete_item( $task['id'] ) ) {
echo "Done. \n";
}else {
echo "Failed. \n";
}
}
$this->basecamp->cache->clean();
exit;
}
// Turn on time tracking for a given Basecamp todo list
private function time_tracking_enable($list) {
if ( $list->tracked == 'true' ) {
return;
}
$this->basecamp->update_list( $list->id, array('tracked' => 'true') );
}
// Respond to input
private function respond( $input ) {
$input = strtolower( $input[0] );
switch ( $input ) {
case 'p':
case 'projects':
exec('git config --unset basecamp.projectid');
unset( $this->project->name, $this->project->id );
break;
case 'c':
case 'cache':
$this->basecamp->cache->clean();
break;
case 's':
case 'subdomain':
exec( 'git config --global --unset basecamp.subdomain' );
unset( $this->subdomain );
break;
case 'a':
case 'api':
exec( 'git config --global --unset basecamp.apitoken' );
unset( $this->apitoken );
break;
case 'q':
case 'quit':
case 'x':
case 'exit':
exit;
break;
default:
// If input not listed, don't continue to run() below.
return false;
break;
}
$this->run();
}
// Request terminal input with an optional prompt
private function input($prompt='') {
echo "$prompt \n";
$input = trim(fgets(STDIN));
$this->respond( $input );
return $input;
}
// Clear Terminal window
private function clear($out = TRUE) {
$clearscreen = chr(27)."[H".chr(27)."[2J";
if ($out) print $clearscreen;
else return $clearscreen;
}
}