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/subjects.pl
2013-09-14 18:35:08 +02:00

195 lines
3.6 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
use Digest::SHA qw(sha1_base64);
use File::Basename;
use ACU::API::Projects;
use ACU::LDAP;
use ACU::Log;
$ACU::Log::log_file = "/var/log/hooks/" . basename($0) . ".log";
use ACU::Process;
# First, check if the repository is in the subjects/ directory
exit 0 if ($ENV{GL_REPO} !~ /^subjects\//);
my ($ref, $oldsha, $newsha) = @ARGV;
log WARN, "This is a subject!";
my %known_tags = (
"defense" => \&tag_defense,
"grades" => \&tag_grades,
"project" => \&tag_project,
"subject" => \&tag_document,
"ref" => \&tag_ref,
"tests" => \&tag_tests,
);
if ($ref =~ m<^refs/tags(/.+)$>)
{
my $tag = $1;
my @args;
while ($tag =~ m<[,/]([^,]+)>g) {
push @args, $1;
}
my $create = ($newsha ne '0' x 40);
if (exists $known_tags{$args[0]}) {
exit $known_tags{$args[0]}($create, @args);
}
}
exit 0;
sub check_xml
{
my $content = shift;
my $dtd = shift;
my $fh;
if ($dtd) {
open $fh, "|xmllint --noout --dtdvalid $dtd -";
}
else {
open $fh, "|xmllint --noout -";
}
print $fh ${ $content };
close $fh;
return $?;
}
sub repository_name
{
my $repo = $ENV{GL_REPO};
$repo =~ s#^subjects/(.*)#$1#;
return $repo;
}
sub tag_defense
{
}
sub tag_document
{
}
sub tag_grades
{
}
sub tag_project
{
my $creation = shift;
# From here, we have:
# 1: "project"
# 2: $id
# 3: $year
my $project_id = repository_name();
if ($_[2]) {
# Check on ID/flavour_id
if ($_[2] =~ /^\d+$/) {
log ERROR, "project:* tag can't take version. Tag format: project:id:year";
}
$project_id .= "-" . $_[2];
}
$project_id = lc $project_id;
$project_id =~ s/[^a-z0-9-_]/_/g;
my $year;
if ($_[3]) {
# Check on year
if ($_[3] !~ /^\d+$/) {
log ERROR, "project:*:* second argument is the year. Tag format: project:id:year";
}
$year = $_[3];
}
else {
$year = LDAP::get_year;
}
if ($creation)
{
my $newref = $ARGV[2];
log INFO, "Création/mise à jour du projet...";
my $content = qx(git show $newref:project.xml);
# Check file exists
if ($?) {
log ERROR, "Créez un fichier project.xml à la racine du dépôt.";
}
# Check DTD validity
if (check_xml(\$content, "http://acu.epita.fr/dtd/project.dtd")) {
log ERROR, "Corrigez les erreurs du fichier project.xml avant de lancer la création du projet.";
}
# TODO: check user permissions
# Generate token for VCS submission
my $dom = XML::LibXML->load_xml(string => (\$content));
my $mod = 0;
for $vcs ($dom->documentElement()->getElementsByTagName("vcs"))
{
if (! $vcs->hasAttribute("token"))
{
my $token;
do {
$token = sha1_base64(rand);
$token =~ s/[^a-zA-Z0-9]//g;
} while (length $token >= 12);
$vcs->setAttribute("token", $token);
$mod = 1;
}
}
if ($mod) {
$content = $dom->toString();
}
# Send data to intradata
log INFO, "Attente d'un processus de publication...";
if (my $err = Process::Client::launch("intradata_get", { action => "update", type => "project", id => $project_id, "year" => $year }, { "butler.xml" => $content }))
{
if (${ $err } ne "Ok") {
log ERROR, "Erreur durant le processus de publication : " . ${ $err };
}
}
# Call API
log ERROR, $_ if(API::Projects::add($project_id, $year));
# FIXME: Remove next line after 2016 piscine: ça ne devrait pas être fait à ce moment là
log ERROR, $_ if(API::Projects::gen_groups($project_id, $year));
}
else
{
log USAGE, "Suppression du projet !";
}
}
sub tag_ref
{
}
sub tag_tests
{
}