Skip to content

Commit

Permalink
Merge pull request #60 from wikimedia/git-commands
Browse files Browse the repository at this point in the history
Cache Git commands for 10 minutes
  • Loading branch information
theresnotime committed Jul 14, 2023
2 parents 076dd54 + 8d8feee commit c349b9b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 65 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: [ '7.4', '8.0', '8.1' ]
php: [ '7.4', '8.0', '8.1', '8.2' ]

runs-on: ubuntu-latest

Expand All @@ -34,5 +34,4 @@ jobs:
- name: Test
run: |
composer install
./vendor/bin/phpcs -s
SYMFONY_DEPRECATIONS_HELPER=disabled ./vendor/bin/simple-phpunit
composer test
1 change: 1 addition & 0 deletions Resources/config/services.yaml
Expand Up @@ -45,6 +45,7 @@ services:
Wikimedia\ToolforgeBundle\Twig\Extension:
arguments:
- '@Krinkle\Intuition\Intuition'
- "@cache.app"
- "@request_stack"
- "%toolforge.intuition.domain%"
tags: [ "twig.extension" ]
4 changes: 3 additions & 1 deletion Tests/Twig/ExtensionTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Wikimedia\ToolforgeBundle\Tests\Twig;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
Expand All @@ -28,7 +29,8 @@ public function setUp(): void
$domain = 'toolforge';
$rootDir = dirname(__DIR__, 2);
$intuition = Intuition::serviceFactory($requestStack, $rootDir, $domain);
$this->extension = new Extension($intuition, $requestStack, $domain);
$cache = new NullAdapter();
$this->extension = new Extension($intuition, $cache, $requestStack, $domain);
}

public function testBasics(): void
Expand Down
49 changes: 34 additions & 15 deletions Twig/Extension.php
Expand Up @@ -4,10 +4,13 @@

namespace Wikimedia\ToolforgeBundle\Twig;

use DateInterval;
use NumberFormatter;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Process\Process;
use Symfony\Contracts\Cache\CacheInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand All @@ -19,6 +22,9 @@ class Extension extends AbstractExtension
/** @var Intuition */
protected $intuition;

/** @var CacheInterface */
private $cache;

/** @var Session */
protected $session;

Expand All @@ -30,10 +36,12 @@ class Extension extends AbstractExtension

public function __construct(
Intuition $intuition,
CacheInterface $cache,
RequestStack $requestStack,
string $domain
) {
$this->intuition = $intuition;
$this->cache = $cache;
if ($requestStack->getCurrentRequest() && $requestStack->getCurrentRequest()->hasSession()) {
$this->session = $requestStack->getCurrentRequest()->getSession();
}
Expand Down Expand Up @@ -211,12 +219,15 @@ public function isRtl($lang = false): bool
*/
public function gitTag(): string
{
$process = new Process(['git', 'describe', '--tags', '--always']);
$process->run();
if (!$process->isSuccessful()) {
return $this->gitHashShort();
}
return trim($process->getOutput());
return $this->cache->get('toolforgebundle-git-tag', function (CacheItemInterface $cacheItem) {
$cacheItem->expiresAfter(new DateInterval('PT10M'));
$process = new Process(['git', 'describe', '--tags', '--always']);
$process->run();
if (!$process->isSuccessful()) {
return $this->gitHashShort();
}
return trim($process->getOutput());
});
}

/**
Expand All @@ -225,9 +236,7 @@ public function gitTag(): string
*/
public function gitBranch(): string
{
$process = new Process(['git', 'rev-parse', '--symbolic-full-name', '--abbrev-ref', 'HEAD']);
$process->run();
return trim($process->getOutput());
return $this->gitCommand('git-branch', ['git', 'rev-parse', '--symbolic-full-name', '--abbrev-ref', 'HEAD']);
}

/**
Expand All @@ -236,9 +245,7 @@ public function gitBranch(): string
*/
public function gitHash(): string
{
$process = new Process(['git', 'rev-parse', 'HEAD']);
$process->run();
return trim($process->getOutput());
return $this->gitCommand('git-hash', ['git', 'rev-parse', 'HEAD']);
}

/**
Expand All @@ -247,9 +254,21 @@ public function gitHash(): string
*/
public function gitHashShort(): string
{
$process = new Process(['git', 'rev-parse', '--short', 'HEAD']);
$process->run();
return trim($process->getOutput());
return $this->gitCommand('git-hash-short', ['git', 'rev-parse', '--short', 'HEAD']);
}

/**
* @param string[] $command Command parts.
*/
private function gitCommand(string $cacheKey, array $command): string
{
$key = 'toolforge-bundle-'.$cacheKey;
return $this->cache->get($key, function (CacheItemInterface $cacheItem) use ($command) {
$cacheItem->expiresAfter(new DateInterval('PT10M'));
$process = new Process($command);
$process->run();
return trim($process->getOutput());
});
}

/**
Expand Down
101 changes: 55 additions & 46 deletions composer.json
@@ -1,48 +1,57 @@
{
"name": "wikimedia/toolforge-bundle",
"description": "Symfony 4 bundle providing useful Toolforge features.",
"type": "symfony-bundle",
"license": "GPL-3.0-or-later",
"homepage": "https://github.com/wikimedia/toolforgebundle",
"support": {
"issues": "https://github.com/wikimedia/toolforgebundle/issues",
"source": "https://github.com/wikimedia/toolforgebundle",
"forum": "https://discourse-mediawiki.wmflabs.org/"
},
"authors": [
{
"name": "Sam Wilson",
"email": "sam@samwilson.id.au"
}
],
"autoload": {
"psr-4": {
"Wikimedia\\ToolforgeBundle\\": "./"
}
},
"require": {
"ext-intl": "*",
"krinkle/intuition": "^2.3",
"mediawiki/oauthclient": "^1.0",
"symfony/config": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"symfony/routing": "^4.4|^5.0",
"symfony/twig-bridge": "^4.4|^5.0",
"twig/twig": "^2.4||^3.0",
"symfony/http-client": "^4.4|^5.0",
"symfony/cache": "^4.4|^5.0",
"doctrine/doctrine-bundle": "^2.2",
"symfony/dependency-injection": "^4.4|^5.1",
"symfony/console": "^4.4|^5.2"
},
"require-dev": {
"slevomat/coding-standard": "^8.0",
"symfony/phpunit-bridge": "^4.4"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
"name": "wikimedia/toolforge-bundle",
"description": "Symfony 4 & 5 bundle providing useful Toolforge features.",
"type": "symfony-bundle",
"license": "GPL-3.0-or-later",
"homepage": "https://github.com/wikimedia/toolforgebundle",
"support": {
"issues": "https://phabricator.wikimedia.org/tag/toolforgebundle/",
"source": "https://github.com/wikimedia/toolforgebundle"
},
"authors": [
{
"name": "Sam Wilson",
"email": "sam@samwilson.id.au"
}
],
"autoload": {
"psr-4": {
"Wikimedia\\ToolforgeBundle\\": "./"
}
},
"require": {
"ext-intl": "*",
"krinkle/intuition": "^2.3",
"mediawiki/oauthclient": "^1.0",
"symfony/config": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"symfony/routing": "^4.4|^5.0",
"symfony/twig-bridge": "^4.4|^5.0",
"twig/twig": "^2.4||^3.0",
"symfony/http-client": "^4.4|^5.0",
"symfony/cache": "^4.4|^5.0",
"doctrine/doctrine-bundle": "^2.2",
"symfony/dependency-injection": "^4.4|^5.1",
"symfony/console": "^4.4|^5.2"
},
"require-dev": {
"slevomat/coding-standard": "^8.0",
"symfony/phpunit-bridge": "^4.4"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
},
"scripts": {
"test": [
"composer validate",
"phpcs -s .",
"simple-phpunit"
],
"fix": [
"phpcbf"
]
}
}

0 comments on commit c349b9b

Please sign in to comment.