Skip to content

Commit

Permalink
Add a list of updates (#8)
Browse files Browse the repository at this point in the history
* Add a list of updates

* Add the missing class

* Add test and remove some unused code

* Code style
  • Loading branch information
eiriksm authored Aug 29, 2020
1 parent 6792f25 commit 89c1d70
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/UpdateListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace eiriksm\ViolinistMessages;

class UpdateListItem
{
private $packageName;
private $oldVersion;
private $newVersion;
private $isNew = false;

public function __construct($packageName, $newVersion, $oldVersion = null)
{
$this->packageName = $packageName;
$this->newVersion = $newVersion;
$this->oldVersion = $oldVersion;
if (!$this->oldVersion) {
// Just assume it is new then?
$this->setIsNew(true);
}
}

/**
* @return bool
*/
public function isNew(): bool
{
return $this->isNew;
}

/**
* @param bool $isNew
*/
public function setIsNew(bool $isNew)
{
$this->isNew = $isNew;
}

/**
* @return mixed
*/
public function getPackageName()
{
return $this->packageName;
}

/**
* @return mixed
*/
public function getOldVersion()
{
return $this->oldVersion;
}

/**
* @return mixed
*/
public function getNewVersion()
{
return $this->newVersion;
}
}
18 changes: 18 additions & 0 deletions src/ViolinistMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,30 @@ public function getPullRequestBody(ViolinistUpdate $msg)
{
$twig = $this->twig->load('pull-request-body.twig');
return $twig->render([
'updated_list' => $this->getUpdatedList($msg->getUpdatedList()),
'title' => $this->getPullRequestTitle($msg),
'changelog' => $msg->getChangelog(),
'custom_message' => $msg->getCustomMessage(),
]);
}

/**
* @param UpdateListItem[] $list
*/
protected function getUpdatedList(array $list)
{
// Create some nice looking markdown for this.
$lines = [];
foreach ($list as $item) {
if ($item->isNew()) {
$lines[] = sprintf('- %s: %s (new package, previously not installed)', $item->getPackageName(), $item->getNewVersion());
} else {
$lines[] = sprintf('- %s: %s (updated from %s)', $item->getPackageName(), $item->getNewVersion(), $item->getOldVersion());
}
}
return implode("\n", $lines);
}

/**
* @param \eiriksm\ViolinistMessages\ViolinistUpdate $msg
*
Expand Down
21 changes: 21 additions & 0 deletions src/ViolinistUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ class ViolinistUpdate
*/
private $customMessage = null;

/**
* @var UpdateListItem[]
*/
private $updatedList = [];

/**
* @return UpdateListItem[]
*/
public function getUpdatedList()
{
return $this->updatedList;
}

/**
* @param UpdateListItem[]
*/
public function setUpdatedList(array $updatedList)
{
$this->updatedList = $updatedList;
}

/**
* @return null|string
*/
Expand Down
15 changes: 15 additions & 0 deletions templates/pull-request-body.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
If you have a high test coverage index, and your tests for this pull request are passing, it should be both safe and recommended to merge this update.

{% if updated_list %}
### Updated packages

Some times an update also needs new or updated dependencies to be installed. Even if this branch is for updating one dependency, it might contain other installs or updates. All of the updates in this branch can be found here.

<details>
<summary>List of updated packages</summary>

{{ updated_list }}

</details>
{% endif %}

{% if changelog %}
### Changelog

Here is a list of changes between the version you use, and the version this pull request updates to:

{{ changelog }}
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Violinist\ViolinistMessages\Tests\Unit;

use eiriksm\ViolinistMessages\UpdateListItem;
use eiriksm\ViolinistMessages\ViolinistMessages;
use eiriksm\ViolinistMessages\ViolinistUpdate;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -65,6 +66,36 @@ public function testLegacyBody()
$this->assertFalse(strpos($body, 'This is an automated pull request') === false);
}

public function testUpdateList()
{
$message = new ViolinistMessages();
$update_items = [
new UpdateListItem('first/updated', '2.0.1', '2.0.2'),
new UpdateListItem('other/new', '2.0.0'),
];
$update = new ViolinistUpdate();
$update->setUpdatedList($update_items);
$body = $message->getPullRequestBody($update);
$this->assertEquals('If you have a high test coverage index, and your tests for this pull request are passing, it should be both safe and recommended to merge this update.
### Updated packages
Some times an update also needs new or updated dependencies to be installed. Even if this branch is for updating one dependency, it might contain other installs or updates. All of the updates in this branch can be found here.
<details>
<summary>List of updated packages</summary>
- first/updated: 2.0.1 (updated from 2.0.2)
- other/new: 2.0.0 (new package, previously not installed)
</details>
***
This is an automated pull request from [Violinist](https://violinist.io/): Continuously and automatically monitor and update your composer dependencies. Have ideas on how to improve this message? All violinist messages are open-source, and [can be improved here](https://github.com/violinist-dev/violinist-messages).
', $body);
}

protected function getLegacyItem()
{
return [
Expand Down

0 comments on commit 89c1d70

Please sign in to comment.