[% USE date -%]
[% PERL -%]
use JSON;
[% END -%]
[% MACRO jsonvar(var) PERL -%]
# JSON-encode the given variable, including adding quotes
print JSON->new->utf8(0)->encode($stash->get($stash->get('var')));
[% END %]
[%- SET adv = advdb.advisories.$advisory -%]
{
  "schema_version": "1.7.0",
  "id": [% jsonvar('advisory') %],
  "published": "[% date.format(adv.status.published, format => '%Y-%m-%dT%H:%M:%SZ', gmt => 1) %]",
  "modified": "[% date.format(adv.status.modified, format => '%Y-%m-%dT%H:%M:%SZ', gmt => 1) %]",
  "summary": [% jsonvar('adv.subject') %],
  "details": [% jsonvar('adv.description') %],
[% IF adv.CVE && adv.CVE.list.size != 0 -%]
  "upstream": [
[% FOREACH cve IN adv.CVE -%]
    [% jsonvar('cve') -%]
[% "," IF ! loop.last %]
[% END -%]
  ],
[% END -%]
  "references": [
    {
      "type": "ADVISORY",
      "url": "[% config.site_url %]/[% basename.ID(advisory) %].html"
    }
[%- "," IF adv.references && adv.references.list.size != 0 %]
[% FOREACH ref IN adv.references -%]
    {
      "type": [% IF ref.search('CVE-20\d\d-\d+') || ref.search('security.(advisory|advisories|notices)') || ref.search('/(advisory|advisories)') %]"ADVISORY"[% ELSE %][% IF ref.search('/(bugs|issues|bugzilla|hackerone\.com/)\b') %]"REPORT"[% ELSE %]"WEB"[% END %][% END %],
      "url": [% jsonvar('ref') %]
    }
[%- "," IF ! loop.last %]
[% END -%]
  ],
  "affected": [
[% USE srciter = iterator(adv.src.keys.sort) -%]
[% FOREACH rel IN srciter -%]
[% USE mediaiter = iterator(adv.src.$rel.keys.sort) -%]
[% FOREACH media IN mediaiter -%]
[% FOREACH srpm IN adv.src.$rel.$media -%]
    {
      "package": {
        "ecosystem": "Mageia:[% rel %]",
[% USE pkg = String(srpm) -%]
[% FOREACH part IN pkg.split('-[\w\.+~^]+-[\w\.]+\.mga\d+') -%]
[% SET pkgname = part -%]
[% LAST -%]
[% END -%]
        "name": [% jsonvar('pkgname') %],
[% CALL pkg.shift(pkgname) -%]
[% CALL pkg.shift('-') -%]
[% SET pkgver = pkg.text -%]
[% USE purl = String('pkg:rpm/mageia/') -%]
[% CALL purl.push(pkgname) -%]
[% CALL purl.push('?arch=source&distro=mageia-') -%]
[% CALL purl.push(rel) -%]
[% SET purltext = purl.text -%]
        "purl": [% jsonvar('purltext') %]
      },
      "ranges": [
        {
          "type": "ECOSYSTEM",
          "events": [
            {
              "introduced": "0"
            },
            {
              "fixed": [% jsonvar('pkgver') %]
            }
          ]
        }
      ],
      "ecosystem_specific": {
        "section": [% jsonvar('media') %]
      }
    }
[%- "," IF ! loop.last || ! mediaiter.last || ! srciter.last %]
[% END %]
[%- END -%]
[%- END -%]
  ],
  "credits": [
    {
      "name": "Mageia",
      "type": "COORDINATOR",
      "contact": [
        "https://wiki.mageia.org/en/Packages_Security_Team"
      ]
    }
  ]
}
