Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/newrelic deploy #14

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Laravel Package

on: [push, pull_request]
jobs:
laravel-tests:

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: PHP Setup
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, dom, fileinfo, curl
coverage: xdebug

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

- name: PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --stop-on-violation --using-cache=no

- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ To access the sha
```php
PlacetoPay\AppVersion\VersionFile::readSha()
```
### Note
The Newrelic deploy will read the CHANGELOG.md as long as it meets the following standard [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
2 changes: 1 addition & 1 deletion src/Console/Commands/CreateVersionFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(Factory $validator): int

VersionFile::generate([
'sha' => $options['sha'] ?? exec('git rev-parse HEAD'),
'time' => $options['time'] ?? date('c'),
'time' => $options['time'] ?? date('c'),
'branch' => $options['branch'] ?? exec('git symbolic-ref -q --short HEAD'),
'version' => $options['tag'] ?? exec('git describe --tags'),
]);
Expand Down
54 changes: 54 additions & 0 deletions src/Helpers/Changelog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace PlacetoPay\AppVersion\Helpers;

/**
* The Changelog format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
*/
class Changelog
{
public const H2_REGEX = "/^##\s\[v?(\d+\.){2}\d+.*]/m";

public const DEFAULT_MESSAGE = 'Not Available';

public const FILENAME_REGEX = "/(?i)changelog(?-i)\.md/";

public static function read(): string
{
$path = self::findFilePath();

if ($path) {
return self::getLastVersion(file_get_contents($path));
}

return self::DEFAULT_MESSAGE;
}

private static function findFilePath(): string
{
$basePath = base_path();
$files = scandir($basePath);

$matches = preg_grep(self::FILENAME_REGEX, $files);
if ($matches) {
return $basePath . DIRECTORY_SEPARATOR . array_pop($matches);
}

return '';
}

/**
* Returns the content from the first H2 to the beginning of the second H2 that meets the H2_REGEX.
*/
private static function getLastVersion(string $content): string
{
preg_match_all(self::H2_REGEX, $content, $matches, PREG_OFFSET_CAPTURE);
if (count($matches) === 2 && count($matches[0]) > 1) {
$firstH2Position = $matches[0][0][1];
$secondH2Position = $matches[0][1][1];
return substr($content, $firstH2Position, $secondH2Position - $firstH2Position);
}

return self::DEFAULT_MESSAGE;
}
}
3 changes: 2 additions & 1 deletion src/NewRelic/NewRelicApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PlacetoPay\AppVersion\NewRelic;

use PlacetoPay\AppVersion\Exceptions\UnsupportedException;
use PlacetoPay\AppVersion\Helpers\Changelog;
use PlacetoPay\AppVersion\Helpers\HttpClient;

class NewRelicApi
Expand Down Expand Up @@ -46,7 +47,7 @@ public function createDeploy(string $version, string $environment)
return $this->client->post($this->constructUrl(), [
'deployment' => [
'revision' => $version,
'changelog' => 'Not available right now',
'changelog' => Changelog::read(),
'description' => 'Commit on ' . $environment,
'user' => 'Not available right now',
],
Expand Down
105 changes: 101 additions & 4 deletions tests/Commands/CreateDeployCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@ class CreateDeployCommandTest extends TestCase
{
use InteractsWithFakeClient;

protected function setUp(): void
{
parent::setUp();
$this->expectedChangelog = '## [0.0.2 (2023-09-25)](https://bitbucket.org/)

### Updated
- Change number 1.
- Change number 2. [TK-002](https://app.clickup.com/)

';
$this->expectedDefaultMessage = 'Not Available';
}

/** @test */
public function can_create_a_release_for_sentry()
public function can_create_a_release_for_sentry(): void
{
$this->setSentryEnvironmentSetUp();

Expand All @@ -25,7 +38,7 @@ public function can_create_a_release_for_sentry()
}

/** @test */
public function can_create_a_release_for_newrelic()
public function can_create_a_deploy_for_newrelic_without_changelog(): void
{
$this->setNewRelicEnvironmentSetUp();

Expand All @@ -36,11 +49,95 @@ public function can_create_a_release_for_newrelic()

$this->fakeClient->assertLastRequestHas('deployment', [
'revision' => 'asdfg2',
'changelog' => 'Not available right now',
'changelog' => $this->expectedDefaultMessage,
'description' => 'Commit on testing',
'user' => 'Not available right now',
]);

$this->assertEquals(
$this->fakeClient->lastRequest()['headers'][0],
'X-Api-Key: ' . config('app-version.newrelic.api_key')
);
}

/** @test */
public function can_create_a_newrelic_deploy_with_wrong_changelog_name(): void
{
$changelogName = base_path('changelog.md');
copy('tests/Mocks/CHANGELOG.md', $changelogName);

$this->setNewRelicEnvironmentSetUp();
$this->bindNewRelicFakeClient();
$this->fakeClient->push('success_deploy');

$this->artisan('app-version:create-deploy')->assertExitCode(0);

$this->fakeClient->assertLastRequestHas('deployment', [
'revision' => 'asdfg2',
'changelog' => $this->expectedChangelog,
'description' => 'Commit on testing',
'user' => 'Not available right now',
]);

$this->assertEquals($this->fakeClient->lastRequest()['headers'][0], 'X-Api-Key: ' . config('app-version.newrelic.api_key'));
$this->assertEquals(
$this->fakeClient->lastRequest()['headers'][0],
'X-Api-Key: ' . config('app-version.newrelic.api_key')
);

unlink($changelogName);
}

/** @test */
public function can_create_a_newrelic_deploy_with_wrong_format(): void
{
$changelogName = base_path('CHANGELOG.md');
copy('tests/Mocks/WRONG-CHANGELOG.md', $changelogName);

$this->setNewRelicEnvironmentSetUp();
$this->bindNewRelicFakeClient();
$this->fakeClient->push('success_deploy');

$this->artisan('app-version:create-deploy')->assertExitCode(0);

$this->fakeClient->assertLastRequestHas('deployment', [
'revision' => 'asdfg2',
'changelog' => $this->expectedDefaultMessage,
'description' => 'Commit on testing',
'user' => 'Not available right now',
]);

$this->assertEquals(
$this->fakeClient->lastRequest()['headers'][0],
'X-Api-Key: ' . config('app-version.newrelic.api_key')
);

unlink($changelogName);
}

/** @test */
public function can_create_a_newrelic_deploy_with_last_changelog(): void
{
$changelogName = base_path('CHANGELOG.md');
copy('tests/Mocks/CHANGELOG.md', $changelogName);

$this->setNewRelicEnvironmentSetUp();
$this->bindNewRelicFakeClient();
$this->fakeClient->push('success_deploy');

$this->artisan('app-version:create-deploy')->assertExitCode(0);

$this->fakeClient->assertLastRequestHas('deployment', [
'revision' => 'asdfg2',
'changelog' => $this->expectedChangelog,
'description' => 'Commit on testing',
'user' => 'Not available right now',
]);

$this->assertEquals(
$this->fakeClient->lastRequest()['headers'][0],
'X-Api-Key: ' . config('app-version.newrelic.api_key')
);

unlink($changelogName);
}
}
19 changes: 19 additions & 0 deletions tests/Mocks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

## [0.0.2 (2023-09-25)](https://bitbucket.org/)

### Updated
- Change number 1.
- Change number 2. [TK-002](https://app.clickup.com/)

## [0.0.1 (2023-09-10)](https://bitbucket.org/)

### Fixed

- Fix api parameters
24 changes: 24 additions & 0 deletions tests/Mocks/WRONG-CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

## [0.0.3 (2023-10-02)](https://bitbucket.org/)

### Added
- Feature number 1.

### [0.0.2 (2023-09-25)](https://bitbucket.org/)

### Updated
- Change number 1.
- Change number 2. [TK-002](https://app.clickup.com/)

## 0.0.1(https://bitbucket.org/)

### Fixed

- Fix api parameters