Archived
1
0
This repository has been archived on 2021-10-08. You can view files and clone it, but cannot push or open issues or pull requests.
ACU/hooks/submissions.pl
Mercier Pierre-Olivier 584fbf9895 Add APPING1 to exception
2013-11-08 21:05:05 +01:00

147 lines
3.9 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
use DateTime::Format::ISO8601;
use File::Basename;
use Net::IP;
use POSIX qw(strftime);
use Socket;
use ACU::API::Projects;
use ACU::API::Submission;
use ACU::LDAP;
use ACU::Log;
$ACU::Log::log_file = "/var/log/hooks/" . basename($0) . ".log";
use ACU::Process;
my ($ref, $oldsha, $newsha) = @ARGV;
my $promo;
my $id_project;
my $repo_login;
my @apping = qw(zinger_a zebard_w zanell_a yao_p vinois_a sraka_y soupam_j seck_a ngomsi_s morin_h milis_e menkar_m eusebe_r crief_a chhum_s boumra_n blemus_a bengan_l amasho_a);
# First, extract information, from config then guess from repository adress
if (my $tmp = `git config hooks.promo`) { chomp $tmp; $promo = $tmp; }
if (my $tmp = `git config hooks.idproject`) { chomp $tmp; $id_project = $tmp; }
if (my $tmp = `git config hooks.login`) { chomp $tmp; $repo_login = $tmp; }
$promo = $1 if (!$promo && $ENV{'GL_REPO'} =~ m/([0-9]{4}).*/);
$id_project = $1 if (!$id_project && $ENV{'GL_REPO'} =~ m/.*\/(.*)\//);
$repo_login = $1 if (!$repo_login && $ENV{'GL_REPO'} =~ m/.*\/.*\/(.*)/);
exit(0) if (!$promo || !$id_project || !$repo_login);
if ($ref =~ m<^refs/tags/(.+)$>)
{
my $tag = $1;
log DEBUG, "Pushed tag for repository $ENV{GL_REPO}: $tag with IP $ENV{'SSH_CLIENT'}";
# Get project informations
my $project;
eval {
$project = API::Projects::get($id_project, $promo);
};
if ($@ or !$project)
{
my $err = $@;
log TRACE, $err;
log ERROR, "Impossible d'envoyer de tags ; si le problème persiste, passez au laboratoire.";
exit 1;
}
log TRACE, $project;
# Extract lot of data
my @rendus = grep {
exists $_->{vcs} and $_->{vcs}{tag} eq $tag;
} @{ $project->{submissions} };
my $glts = DateTime::Format::ISO8601->parse_datetime(
do {
my $t = $ENV{'GL_TS'};
$t =~ tr/./T/;
$t
});
chomp (my $tokengiven = `git cat-file tag $newsha 2> /dev/null | sed -e '1,/^\$/d'`);
for my $rendu (@rendus)
{
my $open = DateTime::Format::ISO8601->parse_datetime($rendu->{period}{begin});
my $close = DateTime::Format::ISO8601->parse_datetime($rendu->{period}{end});
if ($id_project eq "myhttpd" && grep { $_ eq $repo_login } @apping)
{
$open = DateTime::Format::ISO8601->parse_datetime("2013-11-08T20:00:00");
$close = DateTime::Format::ISO8601->parse_datetime("2013-11-10T11:42:00");
}
# TODO: check exceptions by login/group
$open = DateTime::Format::ISO8601->parse_datetime("2013-10-16T16:00:00") if ($repo_login eq "ikouna_l");
say "Date courante : ", $glts->strftime("%d/%m/%Y %H:%M:%S");
if (DateTime->compare($glts, $open) == -1)
{
say "Date d'ouverture : ", $open->strftime("%d/%m/%Y %H:%M:%S");
log ERROR, "Tag rejeté : le rendu n'est pas encore ouvert.";
exit(4);
}
say "Date de fermeture : ", $close->strftime("%d/%m/%Y %H:%M:%S");
if (DateTime->compare($glts, $close) == 1)
{
log ERROR, "Tag rejeté : le rendu est clos.";
exit(5);
}
my $token = $rendu->{vcs}{token};
if ($token ne "" and $token ne $tokengiven and $newsha ne '0' x 40)
{
log ERROR, "Tag rejeté : mauvais token.";
exit(6);
}
}
if ($newsha eq '0' x 40) {
log USAGE, "Mais pour quelle raison voudriez-vous supprimer un tag ?!";
}
else
{
eval {
Process::Client::launch("send_git",
{
"year" => $promo,
"id" => $id_project,
"rendu" => $tag,
"login" => $repo_login,
# "path" => "ssh://git\@localhost/".$ENV{GL_REPO},
}, undef, 1);
};
if ($@) {
my $err = $@;
log DEBUG, "ERROR: ".$err;
}
# Send data to API
my $last_commit = `git log $newsha -1 --decorate --tags`;
eval {
API::Submission::add($promo, $id_project, $tag, $repo_login, $last_commit);
};
if ($@) {
my $err = $@;
log DEBUG, "ERROR: ".$err;
log DONE, "Tag '$tag' effectué avec succès !";
}
else {
log DONE, "Tag '$tag' effectué avec succès ! Vérifiez-le sur l'intranet.";
}
}
}
exit 0;