Skip to content

Commit

Permalink
D9 module updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Sep 9, 2020
1 parent 2bc13e8 commit bdc2e72
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
title: "Drupal 8 Module Updates policy"
class: \Drutiny\algm\Audit\D8ModuleUpdateStatus
name: algm:D8ModuleUpdates
title: "Drupal 9 Module Updates policy"
class: \Drutiny\algm\Audit\D9ModuleUpdateStatus
name: algm:D9ModuleUpdates
tags:
- Drupal 8
- Drupal 9
- Module Updates
- Security
description: |
Throughout the lifetime of your site, the Drupal project and its community
Expand All @@ -14,9 +15,7 @@ description: |
schedule for your site, regression testing changes is of equal importance.
success: No updates were found.
failure: >-
[ {{#updates}}
{"name": "{{ name }}", "version": "{{ version }}", "latest": "{{ latest }}", "latest_status": "{{ latest_status }}", "description": "{{ description }}"}{{^last_item}},{{/last_item}}
{{/updates}} ]
{{ updates }}
warning: |
There are modules with available updates. Please consider upgrading as it
reduces the chance of introducing regressions when more urgent security updates
Expand Down
63 changes: 0 additions & 63 deletions src/Audit/D8ModuleUpdateStatus.php

This file was deleted.

51 changes: 51 additions & 0 deletions src/Audit/D9ModuleUpdateStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drutiny\algm\Audit;

use Drutiny\Audit;
use Drutiny\Sandbox\Sandbox;
use Drutiny\Annotation\Token;
use Drutiny\algm\Utils\MarkdownTableGenerator;

/**
* Look for contrib modules with available updates.
* @Token(
* name = "updates",
* type = "array",
* description = "Description of module updates available."
* )
*/
class D9ModuleUpdateStatus extends Audit {

/**
* @inheritdoc
*/
public function audit(Sandbox $sandbox) {
$output = $sandbox->exec('COMPOSER_MEMORY_LIMIT=-1 composer show "drupal/*" -o --no-cache --format=json 2> /dev/null && echo \'\'');

$modules = json_decode($output, TRUE);
if ($modules === null) {
// If null, then the json cannot be decoded
return AUDIT::ERROR;
}

// Pass check if there are no updates.
if (empty($modules["installed"])) {
return Audit::SUCCESS;
}

$num_modules = count($modules["installed"]);
$columns = ['Module', 'Version', 'Status', 'Latest'];
$rows = [];
foreach ($modules["installed"] as $key => $module) {
$rows[] = [ $module["name"], $module["version"], $module["latest-status"], $module["latest"] ];
}

$md_table = new MarkdownTableGenerator($columns, $rows);
$rendered_table_markdown = $md_table->render();

$sandbox->setParameter('updates', $rendered_table_markdown);

return Audit::FAIL;
}
}
96 changes: 96 additions & 0 deletions src/Utils/MarkdownTableGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Drutiny\algm\Utils;

/**
* Generate markdown table as output from php array
*/
class MarkdownTableGenerator {
private $columns = [];
private $length = [];
private $data = [];

const MAX_STRING_LENGTH = 75;

/**
* Generate columns and populate rows with given data
*/
public function __construct($columns = null, $rows = []) {
if ($columns) {
$this->columns = $columns;
}
elseif ($rows) {
foreach ($rows[0] as $key => $value) {
$this->columns[$key] = $key;
}
}

foreach ($this->columns as $key => $header) {
$this->length[$key] = strlen($header);
}

foreach ($rows as &$row) {
foreach ($this->columns as $key => $value) {
if (!isset($row[$key])) {
$row[$key] = '-';
}
elseif (strlen($row[$key]) > self::MAX_STRING_LENGTH) {
$this->length[$key] = self::MAX_STRING_LENGTH;
// Add an ellipsis if string reaches max.
$row[$key] = substr($row[$key], 0, self::MAX_STRING_LENGTH - 3).'...';
}
elseif (strlen($row[$key]) > $this->length[$key]) {
$this->length[$key] = strlen($row[$key]);
}
}
}

$this->data = $rows;
}

private function renderColumnRow() {
$res = '|';

foreach ($this->length as $key => $l) {
$res .= ' ' . str_repeat('-', $l) . ' ' . '|';
}

return $res."\r\n";
}

private function renderLineRow() {
$res = ' ';

$sum = 0;
foreach ($this->length as $key => $l) {
$sum += $l+3;
}

$res .= str_repeat('-', $sum);

return $res."\r\n";
}

private function renderRow($row) {
$res = '|';
foreach ($this->length as $key => $l) {
$res .= ' '.$row[$key].($l > strlen($row[$key]) ? str_repeat(' ', $l - strlen($row[$key])) : '').' |';
}

return $res."\r\n";
}

public function render() {
$res = $this->renderLineRow();
$res .= $this->renderRow($this->columns);
$res .= $this->renderColumnRow();

foreach ($this->data as $row) {
$res .= $this->renderRow($row);
}

$res .= $this->renderLineRow();

return $res;
}
}

0 comments on commit bdc2e72

Please sign in to comment.