Allow setup of notify metadata fields
[girocco.git] / Girocco / Project.pm
blob4358371f8714eaa3f418e2a2e5b20adaeb264ecd
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::ProjPerm;
11 use Girocco::Config;
12 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
15 our $metadata_fields = {
16 homepage => ['Homepage URL', 'hp', 'text'],
17 shortdesc => ['Short description', 'desc', 'text'],
18 README => ['README (HTML, lt 8kb)', 'README', 'textarea'],
19 notifymail => ['Commit notifications - mail to', 'notifymail', 'text'],
20 notifyjson => ['Commit notifications - POST JSON at', 'notifyjson', 'text'],
21 notifycia => ['Commit notifications - CIA project name', 'notifycia', 'text'],
24 sub _mkdir_forkees {
25 my $self = shift;
26 my @pelems = split('/', $self->{name});
27 pop @pelems; # do not create dir for the project itself
28 my $path = $self->{base_path};
29 foreach my $pelem (@pelems) {
30 $path .= "/$pelem";
31 (-d "$path") or mkdir $path or die "mkdir $path: $!";
32 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
36 our %propmap = (
37 url => ':baseurl',
38 email => ':owner',
39 desc => 'description',
40 README => 'README.html',
41 hp => ':homepage',
42 notifymail => '%hooks.mailinglist',
43 notifyjson => '%hooks.jsonurl',
44 notifycia => '%hooks.cianame',
47 sub _property_path {
48 my $self = shift;
49 my ($name) = @_;
50 $self->{path}.'/'.$name;
53 sub _property_fget {
54 my $self = shift;
55 my ($name) = @_;
56 my $pname = $propmap{$name};
57 $pname or die "unknown property: $name";
58 if ($pname =~ s/^://) {
59 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
60 chomp $val;
61 return $val;
62 } elsif ($pname =~ s/^%//) {
63 my $val = `git --git-dir="$self->{path}" config "$pname"`;
64 chomp $val;
65 return $val;
68 open P, $self->_property_path($pname) or return undef;
69 my @value = <P>;
70 close P;
71 my $value = join('', @value); chomp $value;
72 $value;
75 sub _property_fput {
76 my $self = shift;
77 my ($name, $value) = @_;
78 my $pname = $propmap{$name};
79 $pname or die "unknown property: $name";
80 $value ||= '';
81 if ($pname =~ s/^://) {
82 system('git', '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
83 } elsif ($pname =~ s/^%//) {
84 system('git', '--git-dir='.$self->{path}, 'config', $pname, $value);
87 my $P = lock_file($self->_property_path($pname));
88 $value ne '' and print $P "$value\n";
89 close $P;
90 unlock_file($self->_property_path($pname));
93 sub _properties_load {
94 my $self = shift;
95 foreach my $prop (keys %propmap) {
96 $self->{$prop} = $self->_property_fget($prop);
100 sub _properties_save {
101 my $self = shift;
102 foreach my $prop (keys %propmap) {
103 $self->_property_fput($prop, $self->{$prop});
107 sub _nofetch_path {
108 my $self = shift;
109 $self->_property_path('.nofetch');
112 sub _nofetch {
113 my $self = shift;
114 my ($nofetch) = @_;
115 my $np = $self->_nofetch_path;
116 if ($nofetch) {
117 open X, '>'.$np or die "nofetch failed: $!";
118 close X;
119 } else {
120 unlink $np or die "yesfetch failed: $!";
124 sub _clonelog_path {
125 my $self = shift;
126 $self->_property_path('.clonelog');
129 sub _clonefail_path {
130 my $self = shift;
131 $self->_property_path('.clone_failed');
134 sub _clonep_path {
135 my $self = shift;
136 $self->_property_path('.clone_in_progress');
139 sub _clonep {
140 my $self = shift;
141 my ($nofetch) = @_;
142 my $np = $self->_clonep_path;
143 if ($nofetch) {
144 open X, '>'.$np or die "clonep failed: $!";
145 close X;
146 } else {
147 unlink $np or die "clonef failed: $!";
150 sub _alternates_setup {
151 my $self = shift;
152 return unless $self->{name} =~ m#/#;
153 my $forkee_name = get_forkee_name($self->{name});
154 my $forkee_path = get_forkee_path($self->{name});
155 return unless -d $forkee_path;
156 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
157 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
158 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
160 # We set up both alternates and http_alternates since we cannot use
161 # relative path in alternates - that doesn't work recursively.
163 my $filename = $self->{path}.'/objects/info/alternates';
164 open X, '>'.$filename or die "alternates failed: $!";
165 print X "$forkee_path/objects\n";
166 close X;
167 chmod 0664, $filename or warn "cannot chmod $filename: $!";
169 if ($Girocco::Config::httppullurl) {
170 $filename = $self->{path}.'/objects/info/http-alternates';
171 open X, '>'.$filename or die "http-alternates failed: $!";
172 my $upfork = $forkee_name;
173 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
174 close X;
175 chmod 0664, $filename or warn "cannot chmod $filename: $!";
178 # The symlink is problematic since git remote prune will traverse it.
179 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
182 sub _ctags_setup {
183 my $self = shift;
184 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
187 sub _group_add {
188 my $self = shift;
189 my ($xtra) = @_;
190 $xtra .= join(',', @{$self->{users}});
191 filedb_atomic_append(jailed_file('/etc/group'),
192 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
195 sub _group_update {
196 my $self = shift;
197 my $xtra = join(',', @{$self->{users}});
198 filedb_atomic_edit(jailed_file('/etc/group'),
199 sub {
200 $_ = $_[0];
201 chomp;
202 if ($self->{name} eq (split /:/)[0]) {
203 # preserve readonly flag
204 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
205 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
206 } else {
207 return "$_\n";
213 sub _group_remove {
214 my $self = shift;
215 filedb_atomic_edit(jailed_file('/etc/group'),
216 sub {
217 $self->{name} ne (split /:/)[0] and return $_;
222 sub _hook_path {
223 my $self = shift;
224 my ($name) = @_;
225 $self->{path}.'/hooks/'.$name;
228 sub _hook_install {
229 my $self = shift;
230 my ($name) = @_;
231 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
232 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
233 while (<SRC>) { print DST $_; }
234 close DST;
235 close SRC;
236 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
239 sub _hooks_install {
240 my $self = shift;
241 foreach my $hook ('post-receive', 'update') {
242 $self->_hook_install($hook);
246 # private constructor, do not use
247 sub _new {
248 my $class = shift;
249 my ($name, $base_path, $path) = @_;
250 valid_name($name) or die "refusing to create project with invalid name ($name)!";
251 $path ||= "$base_path/$name.git";
252 my $proj = { name => $name, base_path => $base_path, path => $path };
254 bless $proj, $class;
257 # public constructor #0
258 # creates a virtual project not connected to disk image
259 # you can conjure() it later to disk
260 sub ghost {
261 my $class = shift;
262 my ($name, $mirror) = @_;
263 my $self = $class->_new($name, $Girocco::Config::reporoot);
264 $self->{users} = [];
265 $self->{mirror} = $mirror;
266 $self;
269 # public constructor #1
270 sub load {
271 my $class = shift;
272 my ($name) = @_;
274 open F, jailed_file("/etc/group") or die "project load failed: $!";
275 while (<F>) {
276 chomp;
277 @_ = split /:+/;
278 next unless (shift eq $name);
280 my $self = $class->_new($name, $Girocco::Config::reporoot);
281 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
283 my $ulist;
284 ($self->{crypt}, $self->{gid}, $ulist) = @_;
285 $ulist ||= '';
286 $self->{users} = [split /,/, $ulist];
287 $self->{orig_users} = [@{$self->{users}}];
288 $self->{mirror} = ! -e $self->_nofetch_path;
289 $self->{clone_in_progress} = -e $self->_clonep_path;
290 $self->{clone_logged} = -e $self->_clonelog_path;
291 $self->{clone_failed} = -e $self->_clonefail_path;
292 $self->{ccrypt} = $self->{crypt};
294 $self->_properties_load;
295 return $self;
297 close F;
298 undef;
301 # $proj may not be in sane state if this returns false!
302 sub cgi_fill {
303 my $self = shift;
304 my ($gcgi) = @_;
305 my $cgi = $gcgi->cgi;
307 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
308 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
309 if ($pwd or not $self->{crypt}) {
310 $self->{crypt} = scrypt($pwd);
313 if ($pwd2 and $pwd ne $pwd2) {
314 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
317 $self->{cpwd} = $cgi->param('cpwd');
319 if ($Girocco::Config::project_owners eq 'email') {
320 $self->{email} = $gcgi->wparam('email');
321 valid_email($self->{email})
322 or $gcgi->err("Your email sure looks weird...?");
325 $self->{url} = $gcgi->wparam('url');
326 if ($self->{url}) {
327 valid_repo_url($self->{url})
328 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
331 $self->{desc} = $gcgi->wparam('desc');
332 length($self->{desc}) <= 1024
333 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
335 $self->{README} = $gcgi->wparam('README');
336 length($self->{README}) <= 8192
337 or $gcgi->err("README length &gt; 8kb!");
339 $self->{hp} = $gcgi->wparam('hp');
340 if ($self->{hp}) {
341 valid_web_url($self->{hp})
342 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
345 # FIXME: Permit only existing users
346 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
348 not $gcgi->err_check;
351 sub form_defaults {
352 my $self = shift;
354 name => $self->{name},
355 email => $self->{email},
356 url => $self->{url},
357 desc => html_esc($self->{desc}),
358 README => html_esc($self->{README}),
359 hp => $self->{hp},
360 users => $self->{users},
364 sub authenticate {
365 my $self = shift;
366 my ($gcgi) = @_;
368 $self->{ccrypt} or die "Can't authenticate against a project with no password";
369 $self->{cpwd} ||= '';
370 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
371 $gcgi->err("Your admin password does not match!");
372 return 0;
374 return 1;
377 sub _setup {
378 my $self = shift;
379 my ($pushers) = @_;
381 $self->_mkdir_forkees;
383 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
384 if ($Girocco::Config::owning_group) {
385 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
386 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
387 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
388 } else {
389 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
391 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
392 or die "git init $self->{path} failed: $?";
393 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
394 or die "disabling receive.denyNonFastforwards failed: $?";
395 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
396 or die "disabling gc.auto failed: $?";
398 # /info must have right permissions even before the fixup job,
399 # and git init didn't do it for some reason.
400 if ($Girocco::Config::owning_group) {
401 chmod(02775, $self->{path}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
402 } else {
403 chmod(02777, $self->{path}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
406 $self->_properties_save;
407 $self->_alternates_setup;
408 $self->_ctags_setup;
409 $self->_group_add($pushers);
410 $self->_hooks_install;
411 $self->perm_initialize;
412 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
415 sub premirror {
416 my $self = shift;
418 $self->_setup(':');
419 $self->_clonep(1);
422 sub conjure {
423 my $self = shift;
425 $self->_setup;
426 $self->_nofetch(1);
429 sub clone {
430 my $self = shift;
432 unlink ($self->_clonefail_path()); # Ignore EEXIST error
433 unlink ($self->_clonelog_path()); # Ignore EEXIST error
435 use IO::Socket;
436 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
437 $sock->print("clone ".$self->{name}."\n");
438 # Just ignore reply, we are going to succeed anyway and the I/O
439 # would apparently get quite hairy.
440 $sock->flush();
441 sleep 2; # *cough*
442 $sock->close();
445 sub update {
446 my $self = shift;
448 $self->_properties_save;
449 $self->_group_update;
451 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
452 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
453 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
454 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
459 sub update_password {
460 my $self = shift;
461 my ($pwd) = @_;
463 $self->{crypt} = scrypt($pwd);
464 $self->_group_update;
467 # You can explicitly do this just on a ghost() repository too.
468 sub delete {
469 my $self = shift;
471 if (-d $self->{path}) {
472 system('rm', '-rf', $self->{path}) == 0
473 or die "rm -rf $self->{path} failed: $?";
475 $self->_group_remove;
478 sub has_forks {
479 my $self = shift;
481 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
485 ### static methods
487 sub get_forkee_name {
488 $_ = $_[0];
489 (m#^(.*)/.*?$#)[0]; #
491 sub get_forkee_path {
492 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
493 -d $forkee ? $forkee : '';
496 sub valid_name {
497 $_ = $_[0];
498 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
499 and (not m#\./#)
500 and (not m#/$#)
501 and m#^[a-zA-Z0-9+./_-]+$#;
504 sub does_exist {
505 my ($name) = @_;
506 valid_name($name) or die "tried to query for project with invalid name $name!";
507 (-d $Girocco::Config::reporoot."/$name.git");