3 * scmhook commitEmail Plugin Class
4 * Copyright 2012, Denise Patzker <denise.patzker@tu-dresden.de>
5 * Copyright 2012,2018, 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 HgCommitEmail extends scmhook {
33 function __construct() {
34 $this->group = $GLOBALS['group'];
35 $this->name = "Commit Email";
36 $this->description = _('Commit message log is pushed to commit mailing-list of the project (which you need to create)')
38 . _('The hook is triggered after "serve push pull bundle" on the projects repository.');
39 $this->classname = "commitEmail";
40 $this->label = "scmhg";
41 $this->hooktype = "serve-push-pull-bundle";
42 $this->unixname = "commitemail";
44 $this->command = 'exit 0';
47 function getDisabledMessage() {
48 return $this->disabledMessage;
51 function getParams() {
54 'description' => _('Send commit e-mail notification to'),
56 'default' => $this->group->getUnixName().'-commits@'.forge_get_config('lists_host'),
62 * This function activates e-mail notification for pushed commits.
63 * This is done by adding the needed entries to the projects hgrc file.
65 function enable($project, $scmdir_root) {
70 $project_name = $project->getUnixName();
71 $unix_group = forge_get_config('apache_group');
72 $unix_user = forge_get_config('apache_user');
73 $sendmail = forge_get_config('sendmail_path');
74 $main_repo = $scmdir_root.'/.hg';
75 if (is_dir($main_repo)) {
76 $mail = $project_name.'-commits@'.forge_get_config('lists_host');
77 $table = 'plugin_scmhook_scmhg_'.strtolower($this->classname);
78 if (db_check_table_exists($table)) {
79 $res = db_query_params('SELECT * FROM '.$table.' WHERE group_id = $1 and repository_name = $2', array($project->getID(), basename($scmdir_root)));
80 $values = db_fetch_array($res);
81 foreach ($this->getParams() as $pname => $pconf) {
82 $mail = ($values[$pname] != null) ? $values[$pname] : $pconf['default'];
86 /*strip of repository path for subject line*/
88 $strip = count(explode($delim, $scmdir_root))-1;
91 $read = ""; /*pull,clone*/
94 $users = $project->getMembers();
95 $pname = $project->getUnixName();
96 foreach ($users as $user) {
97 if (forge_check_perm_for_user($user, 'scm', $project->getID(), 'write')) {
104 $push .= $user->getUnixName();
105 $read .= $user->getUnixName();
108 } elseif (forge_check_perm_for_user($user, 'scm', $project->getID(), 'read')) {
112 $read .= $user->getUnixName();
117 if ($project->enableAnonSCM()) {
121 /*create new hgrc with default values*/
123 if (is_file($main_repo.'/hgrc')) {
124 $hgrc_val = parse_ini_file($main_repo.'/hgrc', true);
125 if (isset($hgrc_val['web'])) {
126 $hgrc_val['web']['allow_read'] = $read;
127 $hgrc_val['web']['allow_push'] = $push;
129 foreach ($hgrc_val as $section => $sub) {
130 if (in_array($section, array('web'))) {
131 $hgrc .= '['.$section."]\n";
132 foreach ($sub as $prop => $value) {
133 $hgrc .= "$prop = $value\n";
134 if ($value == end($sub)) {
142 $hgrc .= "baseurl = /hg/".$project->getUnixName().'/'.basename($scmdir_root);
143 $hgrc .= "\ndescription = ".$project->getUnixName().'/'.basename($scmdir_root);
144 $hgrc .= "\nstyle = paper";
145 $hgrc .= "\nallow_push = ".$push;
146 $hgrc .= "\nallow_read = ".$read;
147 if (!forge_get_config('use_ssl', 'scmhg')) {
148 $hgrc .= "\n".'push_ssl = 0';
153 $hgrc .= "[extensions]\n" ;
154 $hgrc .= "hgext.notify =\n\n";
156 $hgrc .= "[hooks]\n" ;
157 $hgrc .= "changegroup.notify = python:hgext.notify.hook\n\n";
159 $hgrc .= "[email]\n";
160 $hgrc .= "from = $mail\n";
161 $hgrc .= "method = $sendmail\n\n";
163 $hgrc .= "[notify]\n" ;
164 $hgrc .= "sources = serve push pull bundle\n";
165 $hgrc .= "test = false\n";
166 $hgrc .= 'template = "\ndetails: {webroot}/rev/{node|short}/\nchangeset: {rev}:{node|short}\nuser: {author}\ndate: {date|date}\ndescription:\n{desc}\n"';
167 $hgrc .= "\nmaxdiff = 300\n";
168 $hgrc .= "strip = $strip\n\n";
170 $hgrc .= "[reposubs]\n";
171 $hgrc .= "** = $mail\n";
173 $f = fopen ("$main_repo/hgrc.new", 'w');
176 rename($main_repo.'/hgrc.new', $main_repo.'/hgrc');
177 system("chown $unix_user:$unix_group $main_repo/hgrc");
178 system("chmod 660 $main_repo/hgrc");
184 * This function deactivates e-mail notification.
185 * This is done by removing the needed entries from the projects hgrc file.
187 * @param $project object containing project data
190 function disable($project, $scmdir_root) {
195 $unix_group = forge_get_config('apache_group');
196 $unix_user = forge_get_config('apache_user');
198 $project_name = $project->getUnixName();
199 $main_repo = $scmdir_root.'/.hg';
200 if (is_dir($main_repo)) {
202 $read = ""; /*pull,clone*/
205 $users = $project->getMembers();
206 $pname = $project->getUnixName();
207 foreach ($users as $user) {
208 if (forge_check_perm_for_user($user, 'scm', $project->getID(), 'write')) {
215 $push .= $user->getUnixName();
216 $read .= $user->getUnixName();
219 } elseif (forge_check_perm_for_user($user, 'scm', $project->getID(), 'read')) {
223 $read .= $user->getUnixName();
228 if ($project->enableAnonSCM()) {
231 /*create new hgrc with default values*/
233 if (is_file($main_repo.'/hgrc')) {
234 $hgrc_val = parse_ini_file($main_repo.'/hgrc', true);
235 if (isset($hgrc_val['web'])) {
236 $hgrc_val['web']['allow_read'] = $read;
237 $hgrc_val['web']['allow_push'] = $push;
239 foreach ($hgrc_val as $section => $sub) {
240 if (in_array($section, array('web'))) {
241 $hgrc .= '['.$section."]\n";
242 foreach ($sub as $prop => $value) {
243 $hgrc .= "$prop = $value\n";
244 if ($value == end($sub)) {
252 $hgrc .= "baseurl = /hg/".$project->getUnixName().'/'.basename($scmdir_root);
253 $hgrc .= "\ndescription = ".$project->getUnixName().'/'.basename($scmdir_root);
254 $hgrc .= "\nstyle = paper";
255 $hgrc .= "\nallow_push = ".$push;
256 $hgrc .= "\nallow_read = ".$read;
257 if (!forge_get_config('use_ssl', 'scmhg')) {
258 $hgrc .= "\n".'push_ssl = 0';
261 $f = fopen ("$main_repo/hgrc.new", 'w');
264 rename($main_repo.'/hgrc.new', $main_repo.'/hgrc');
265 system("chown $unix_user:$unix_group $main_repo/hgrc");
266 system("chmod 660 $main_repo/hgrc");