From 24aae489c51512f98564d8a44b08051560b7c08e Mon Sep 17 00:00:00 2001 From: Nicolas Vigier Date: Mon, 17 Jun 2013 14:31:27 +0000 Subject: Add 'nextid' and 'publish' commands --- NEWS | 2 ++ config_default | 5 +++++ lib/MGA/Advisories.pm | 23 +++++++++++++++++++++++ mgaadv | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/NEWS b/NEWS index b45ef4f..2807504 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ +- add 'nextid' and 'publish' commands to assign an ID to an advisory file + Version 0.6 - Fix hash which was not dereferenced diff --git a/config_default b/config_default index 0b9448b..76cecff 100644 --- a/config_default +++ b/config_default @@ -10,3 +10,8 @@ output_format: out_dir: /var/lib/mga-advisories/out site_url: http://advisories.mageia.org advisories_repo_url: svn+ssh://svn.mageia.org/svn/advisories +advisory_types: + bugfix: + prefix: MGAA + security: + prefix: MGASA diff --git a/lib/MGA/Advisories.pm b/lib/MGA/Advisories.pm index 848bf24..9601712 100644 --- a/lib/MGA/Advisories.pm +++ b/lib/MGA/Advisories.pm @@ -67,6 +67,29 @@ sub get_advisories_from_dir { return \%advisories; } +sub next_id { + my $prefix = shift; + my $year = DateTime->now->year; + my $newid = (0, sort map { m/^$prefix-$year-(\d+)$/ ? int $1 : () } @_)[-1] + 1; + return sprintf("%s-%s-%.4d", $prefix, $year, $newid); +} + +sub assign_id { + my ($bugnum) = @_; + my $advfile = "$config->{advisories_dir}/$bugnum.adv"; + my $adv = LoadFile($advfile); + if ($adv->{ID}) { + print STDERR "$bugnum already has an ID assigned: $adv->{ID}\n"; + return; + } + $adv->{ID} = next_id($config->{advisory_types}{$adv->{type}}{prefix}, + keys %{get_advisories_from_dir()}); + open(my $fh, '>>', $advfile) or die "Error opening $advfile"; + print $fh "ID: $adv->{ID}\n"; + close $fh; + print "Assigned ID $adv->{ID} to advisory $bugnum\n"; +} + sub advdb_dumpfile { $config->{advdb_dumpfile} || $ENV{HOME} . '/.mga-advisories/advisories.yaml'; } diff --git a/mgaadv b/mgaadv index 05308a3..3e6bdc1 100755 --- a/mgaadv +++ b/mgaadv @@ -43,6 +43,24 @@ $0 new [type] [bugnum] Create a new advisory file. [type] should be security or bugfix and [bugnum] is the bugzilla bug number. +END + }, + nextid => { + run => \&nextid, + descr => 'Print next available ID', + usage => < { + run => \&publish, + descr => 'Assign an ID to an advisory file', + usage => < { @@ -111,6 +129,23 @@ sub newadv { } } +sub nextid { + usageexit('usage', $_[0]) unless @_ == 2; + my $type = $_[1]; + if (!$MGA::Advisories::config->{advisory_types}{$type}) { + print STDERR "Unknown type $type\n"; + exit 1; + } + print MGA::Advisories::next_id( + $MGA::Advisories::config->{advisory_types}{$type}{prefix}, + keys %{MGA::Advisories::get_advisories()}), "\n"; +} + +sub publish { + usageexit('usage', $_[0]) unless @_ == 2; + MGA::Advisories::assign_id($_[1]); +} + sub listadv { usageexit('usage', $_[0]) unless @_ == 1; my %advdb; -- cgit v1.2.1