3 * scmhook commitEmail Plugin Class
4 * Copyright 2012, Denise Patzker <denise.patzker@tu-dresden.de>
5 * Copyright 2012, Franck Villaume - TrivialDev
7 * This class provides hook to activate/deactivate Mercurials e-mail
8 * notification per repository.
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require_once $gfplugins.'scmhook/common/scmhook.class.php';
29 class commitEmail extends scmhook {
30 function __construct() {
31 $this->name = "Commit Email";
32 $this->description = _('Every commit pushed sends a notification e-mail to the users of the commit-mailinglist.
33 The hook is triggered after \'serve push pull bundle\' on the projects repository.');
34 $this->classname = "commitEmail";
35 $this->label = "scmhg";
36 $this->hooktype = "serve-push-pull-bundle";
37 $this->unixname = "commitemail";
39 $this->command = 'exit 0';
43 * This function activates e-mail notification for pushed commits.
44 * This is done by adding the needed entries to the projects hgrc file.
46 function enable($project) {
51 $project_name = $project->getUnixName();
52 $root = forge_get_config('repos_path', 'scmhg') . '/' . $project_name;
53 $unix_group = forge_get_config('apache_group');
54 $unix_user = forge_get_config('apache_user');
55 $main_repo = $root . '/.hg';
56 if (is_dir("$main_repo")) {
57 $mail = $project_name.'-commits@'.forge_get_config('web_host');
60 /*strip of repository path for subject line*/
62 $strip = count(explode($delim, $root))-1;
64 if (is_file($main_repo.'/hgrc')) {
65 /*set the needed entries within hgrc*/
67 $hgrc_val = parse_ini_file("$main_repo/hgrc", true);
68 if (!isset( $hgrc_val['extensions'])) {
69 /*makes notify extension usable*/
70 $hgrc_val['extensions']['hgext.notify'] = '';
72 if (!isset( $hgrc_val['hooks'])) {
73 /*activates the notify hook*/
74 $hgrc_val['hooks']['changegroup.notify'] = 'python:hgext.notify.hook';
76 if (!isset( $hgrc_val['email'])) {
77 /*set email parameter*/
78 $hgrc_val['email']['from'] = $mail;
79 $sendmail = forge_get_config('sendmail_path');
80 $hgrc_val['email']['method'] = $sendmail;
82 if (!isset( $hgrc_val['notify'])) {
83 /*define when notify does something*/
84 $hgrc_val['notify']['sources'] = 'serve push pull bundle';
85 /*test = true will not deliver the mail, instead you will get command line output*/
86 $hgrc_val['notify']['test'] = 'false';
88 /*configure subscribers*/
89 if (!isset( $hgrc_val['reposubs'])) {
90 $hgrc_val['reposubs']['**'] = $mail;
92 $hgrc_val['notify']['template'] = '"\ndetails: {webroot}/rev/{node|short}\nchangeset: {rev}:{node|short}\nuser: {author}\ndate:
93 {date|date}\ndescription:\n{desc}\n"' ;
94 $hgrc_val['notify']['maxdiff'] = '300';
95 $hgrc_val['notify']['strip'] = $strip;
97 /*parse_ini_file() has problems with boolean and special characters*/
98 $hgrc_val['notify']['test'] = 'false';
99 $hgrc_val['notify']['template'] = '"\ndetails: {webroot}/rev/{node|short}\nchangeset: {rev}:{node|short}\nuser: {author}\ndate:
100 {date|date}\ndescription:\n{desc}\n"';
102 /* write configuration back to file*/
103 foreach ($hgrc_val as $section => $sub) {
104 $hgrc .= '['.$section."]\n";
105 foreach ($sub as $prop => $value) {
106 $hgrc .= "$prop = $value\n";
107 if ($value == end($sub)) {
113 /*create new hgrc with default values*/
115 $hgrc .= "baseurl = /hg";
116 $hgrc .= "\ndescription = ".$project_name;
117 $hgrc .= "\nstyle = paper";
118 $hgrc .= "\nallow_read = *";
119 $hgrc .= "\nallow_push = *\n\n";
121 $hgrc .= "[extensions]\n" ;
122 $hgrc .= "hgext.notify =\n\n";
124 $hgrc .= "[hooks]\n" ;
125 $hgrc .= "changegroup.notify = python:hgext.notify.hook\n\n";
127 $hgrc .= "[email]\n";
128 $hgrc .= "from = $mail\n";
129 $hgrc .= "method = $sendmail\n\n";
131 $hgrc .= "[notify]\n" ;
132 $hgrc .= "sources = serve push pull bundle\n";
133 $hgrc .= "test = false\n";
134 $hgrc .= 'template = "\ndetails: {webroot}/rev/{node|short}/\nchangeset: {rev}:{node|short}\nuser: {author}\ndate:
135 {date|date}\ndescription:\n{desc}\n"';
136 $hgrc .= "\nmaxdiff = 300\n";
137 $hgrc .= "strip = $strip\n\n";
138 $hgrc .= "[reposubs]\n";
139 $hgrc .= "** = $mail";
141 $f = fopen ("$main_repo/hgrc.new", 'w');
144 rename ($main_repo.'/hgrc.new', $main_repo.'/hgrc');
145 system( "chown $unix_user:$unix_group $main_repo/hgrc" );
146 system( "chmod 660 $path/hgrc" );
152 * This function deactivates e-mail notification.
153 * This is done by removing the needed entries from the projects hgrc file.
155 * @param $project object containing project data
157 function disable($project) {
162 $unix_group = forge_get_config('apache_group');
163 $unix_user = forge_get_config('apache_user');
165 $project_name = $project->getUnixName();
166 $root = forge_get_config('repos_path', 'scmhg') . '/' . $project_name;
167 $main_repo = $root . '/.hg';
168 if (is_file($main_repo.'/hgrc')) {
169 /*unset extension and hook to unable notification emails*/
170 $hgrc_val = parse_ini_file("$main_repo/hgrc", true);
171 if ( isset( $hgrc_val['extensions'] )) {
172 unset($hgrc_val['extensions']);
174 if ( isset( $hgrc_val['hooks'] )) {
175 unset($hgrc_val['hooks']);
177 if ( isset( $hgrc_val['notify']['test'] )) {
178 $hgrc_val['notify']['test'] = "false";
180 if ( isset( $hgrc_val['notify']['template'] )) {
181 $hgrc_val['notify']['template'] = '"\ndetails: {webroot}/rev/{node|short}\nchangeset: {rev}:{node|short}\nuser: {author}\ndate:
182 {date|date}\ndescription:\n{desc}\n"' ;
186 foreach ($hgrc_val as $section => $sub) {
187 $hgrc .= '['.$section."]\n";
188 foreach ($sub as $prop => $value) {
189 $hgrc .= "$prop = $value\n";
190 if ($value == end($sub)) {
196 $f = fopen ("$main_repo/hgrc.new", 'w');
199 rename($main_repo.'/hgrc.new', $main_repo.'/hgrc');
200 system( "chown $unix_user:$unix_group $main_repo/hgrc" );
201 system( "chmod 660 $main_repo/hgrc" );