MTrackWeb/Hook/git.php
[web.mtrack] / MTrackWeb / Hook / git.php
1 <?php  
2
3 /****
4  *
5  *  usage:
6  *
7  *  #repo/hooks/post-receive
8  *  /usr/bin/php {PATH TO APPLICATION}/index.php Hook/git post  >> /tmp/githooklog
9  *
10  *
11  *
12  */
13
14
15
16 /* For licensing and copyright terms, see the file named LICENSE */
17 // called as:
18
19 // git-commit-hook what [mtrackconfig] > /tmp/gitlog
20 // the cwd is the repo path
21 // a list of "oldrev newrev refname" lines is presented to us on stdin
22
23  
24 require_once 'MTrackWeb.php';
25
26 class MTrackWeb_Hook_git extends MTrackWeb
27 {
28     
29     static $cli_desc = "Commit hook for git - see source for usage";
30     
31     function getAuth()
32     {
33         if (!HTML_FlexyFramework::get()->cli) {
34             die("NOT CLI!");
35         }
36         
37     }
38     
39     
40     
41      
42     function get($action)
43     {
44         try {
45             $repo = DB_DataObject::factory('mtrack_repos');
46             $repo->repopath = getcwd();
47             if (!$repo->find(true)) {
48                 $this->addEvent("HOOKERROR", false, "invalid repo:". getcwd() );
49                 die("INVALID REPO" . getcwd() );
50             }
51             echo "running hook";
52             
53             require_once 'MTrack/SCM/Git/CommitHookBridge.php';
54             
55             $bridge = new MTrack_SCM_Git_CommitHookBridge($repo);
56             
57             // for POST commit, we can use bridge->commits[0] to determine who..
58             $revs = $repo->history('.',1,'rev', $bridge->commits[0]);
59             
60             //print_R($revs);
61             
62             $who = $revs[0]->changebyToEmail();
63             
64             $this->authUser = DB_DataObject::factory('Person');
65             $this->authUser->get('email', $who); 
66             
67             $cfg = HTML_FlexyFramework::get()->MTrackWeb;
68             //print_R($cfg);
69             
70             
71             require_once 'MTrack/CommitChecker.php';
72             
73             
74             $checker = new MTrack_CommitChecker( array(
75                     'repo' => $repo,
76                     'checks' => $cfg['checks'],
77                     'authUser' => $this->authUser,
78             ));
79              
80              
81             switch ($action) {
82                 
83                 case 'pre':
84                     $checker->preCommit($bridge);
85                     break;
86                     
87                 default:
88                     $checker->postCommit($bridge);
89                     
90                     // at this point we have
91                     // checker->no_ticket (contains commits without tickets
92                     
93                     foreach($checker->no_ticket as $change_event) {
94                         $cg = DB_DataObject::factory('mtrack_change');
95                         $cg->createFromCommit($change_event, $checker);
96                         
97                     }
98                     
99                     print_r($checker);
100                   
101             }
102             //_log("SUCCESS");
103             exit(0);
104             
105             
106             
107         } catch (Exception $e) {
108              
109           fwrite(STDERR, "\n" . $e->getMessage() .
110             "\n\n" .
111             $e->getTraceAsString() .
112             "\n\n ** Commit failed [$action]\n");
113         
114           exit(1);
115         }
116         
117     }
118     
119 }
120