Skip to content

Commit

Permalink
Feature/add until ref commit (#1)
Browse files Browse the repository at this point in the history
* Added possibility to pass commit until which changelog should be generated

* Fixed requesting pull requests related to a commit
  • Loading branch information
norberttech authored Dec 29, 2020
1 parent 6ea7ef4 commit b7e962a
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "daily"
67 changes: 67 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: "Tests"

on:
pull_request:
push:
branches:
- "1.x"
schedule:
- cron: '* 8 * * *'

jobs:
tests:
name: "Tests"

runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
dependencies:
- "locked"
- "lowest"
- "highest"
php-version:
- "7.4"
- "8.0"
operating-system:
- "ubuntu-latest"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: pcov
tools: phive, composer:v2
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1

- name: "Cache dependencies"
uses: "actions/cache@v2"
with:
path: |
~/.composer/cache
tools
vendor
key: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"
restore-keys: "php-${{ matrix.php-version }}-${{ matrix.dependencies }}"

- name: "Install lowest dependencies"
if: ${{ matrix.dependencies == 'lowest' }}
run: "composer update --prefer-lowest --no-interaction --no-progress --no-suggest"

- name: "Install highest dependencies"
if: ${{ matrix.dependencies == 'highest' }}
run: "composer update --no-interaction --no-progress --no-suggest"

- name: "Install locked dependencies"
if: ${{ matrix.dependencies == 'locked' }}
run: "composer install --no-interaction --no-progress --no-suggest"

- name: "Install tools"
run: "phive install --trust-gpg-keys E82B2FB314E9906E,4AA394086372C20A --force-accept-unsigned"

- name: "Test"
run: "composer test"
49 changes: 25 additions & 24 deletions src/Aeon/Automation/Console/Command/ChangeLogGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use Aeon\Automation\ChangeLog;
use Aeon\Automation\Console\AeonStyle;
use Aeon\Automation\GitHub\Commit;
use Aeon\Automation\GitHub\Commits;
use Aeon\Automation\GitHub\PullRequest;
use Aeon\Automation\GitHub\Reference;
use Aeon\Automation\GitHub\Repository;
use Aeon\Automation\GitHub\Tags;
use Aeon\Calendar\Gregorian\DateTime;
use Github\Exception\RuntimeException;
use Github\HttpClient\Message\ResponseMediator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -38,6 +37,7 @@ protected function configure() : void
->addOption('branch', 'b', InputOption::VALUE_REQUIRED, 'Get the the branch used instead of tag-start option when it\'s not provided. If empty, default repository branch is taken.')
->addOption('tag-start', 'ts', InputOption::VALUE_REQUIRED, 'Optional tag from which changelog generation starts. When not provided branch is used instead.')
->addOption('tag-end', 'te', InputOption::VALUE_REQUIRED, 'Optional tag until which changelog is generated. When not provided, latest tag is taken')
->addOption('commit-end', 'ce', InputOption::VALUE_REQUIRED, 'Optional commit sha until which changelog is generated . When not provided, latest tag is taken')
->addOption('only-commits', 'oc', InputOption::VALUE_NONE, 'Use only commits to generate changelog')
->addOption('only-pull-requests', 'opr', InputOption::VALUE_NONE, 'Use only pull requests to generate changelog')
->addOption('changed-after', 'cb', InputOption::VALUE_REQUIRED, 'Ignore all changes after given date, relative date formats like "-1 day" are also supported')
Expand Down Expand Up @@ -67,17 +67,28 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
$io->note('Project: ' . $project->fullName());
$io->note('From Reference: ' . $fromReferenceName);
$io->note('Until Reference: ' . $untilReferenceName);
$io->note('Until Commit: ' . $input->getOption('commit-end'));

try {
$untilReference = $untilReferenceName !== ''
? Reference::commitFromString($this->github(), $project, $untilReferenceName)
$untilCommit = $untilReferenceName !== ''
? Reference::commitFromString($this->github(), $project, $untilReferenceName)->commit($this->github(), $project)
: null;
} catch (RuntimeException $e) {
$io->error('Reference "tags/' . $input->getOption('tag-end') . '" does not exists: ' . $e->getMessage());

return Command::FAILURE;
}

if ($input->getOption('commit-end')) {
try {
$untilCommit = Commit::fromSHA($this->github(), $project, $input->getOption('commit-end'));
} catch (RuntimeException $e) {
$io->error('Commit with SHA ' . $input->getOption('commit-end') . '" does not exists: ' . $e->getMessage());

return Command::FAILURE;
}
}

try {
Reference::commitFromString($this->github(), $project, $branchName);
} catch (RuntimeException $e) {
Expand All @@ -87,20 +98,20 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
}

try {
$fromReference = Reference::commitFromString($this->github(), $project, $fromReferenceName);
$fromCommit = Reference::commitFromString($this->github(), $project, $fromReferenceName)->commit($this->github(), $project);
} catch (RuntimeException $e) {
$io->error("Reference \"{$fromReferenceName}\" does not exists: " . $e->getMessage());

return Command::FAILURE;
}

$io->note("Fetching all commits between \"{$fromReferenceName}\" and \"{$untilReferenceName}\"");
$commits = Commits::allFrom($this->github(), $project, $fromReference, $untilReference);
$commits = Commits::allFrom($this->github(), $project, $fromCommit, $untilCommit);
$io->note('Total commits: ' . $commits->count());

$io->progressStart($commits->count());

$changeLog = new ChangeLog($release, $fromReference->commit($this->github(), $project)->date()->day());
$changeLog = new ChangeLog($release, $fromCommit->date()->day());

$onlyCommits = $input->getOption('only-commits');
$onlyPullRequests = $input->getOption('only-pull-requests');
Expand All @@ -124,29 +135,19 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
}

if ($onlyPullRequests) {
$pullRequestsData = ResponseMediator::getContent(
$this->github()->getHttpClient()->get(
'/repos/' . \rawurlencode($project->organization()) . '/' . \rawurlencode($project->name()) . '/commits/' . \rawurlencode($commit->id()) . '/pulls',
['Accept' => 'application/vnd.github.groot-preview+json']
)
);

if (!\count($pullRequestsData)) {
$pullRequests = $commit->pullRequests($this->github(), $project);

if (!$pullRequests->count()) {
continue;
}

$source = new PullRequest($pullRequestsData[0]);
$source = $pullRequests->first();
}

if (!$onlyPullRequests && !$onlyCommits) {
$pullRequestsData = ResponseMediator::getContent(
$this->github()->getHttpClient()->get(
'/repos/' . \rawurlencode($project->organization()) . '/' . \rawurlencode($project->name()) . '/commits/' . \rawurlencode($commit->id()) . '/pulls',
['Accept' => 'application/vnd.github.groot-preview+json']
)
);

$source = \count($pullRequestsData) ? new PullRequest($pullRequestsData[0]) : $commit;
$pullRequests = $commit->pullRequests($this->github(), $project);

$source = $pullRequests->count() ? $pullRequests->first() : $commit;
}

$changeLog->add($source->changes());
Expand Down
25 changes: 25 additions & 0 deletions src/Aeon/Automation/GitHub/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use Aeon\Automation\Changes;
use Aeon\Automation\ChangesSource;
use Aeon\Automation\Project;
use Aeon\Calendar\Gregorian\DateTime;
use Github\Client;
use Github\HttpClient\Message\ResponseMediator;

final class Commit implements ChangesSource
{
Expand All @@ -17,7 +20,17 @@ public function __construct(array $data)
$this->data = $data;
}

public static function fromSHA(Client $client, Project $project, string $sha) : self
{
return new self($client->repo()->commits()->show($project->organization(), $project->name(), $sha));
}

public function id() : string
{
return $this->sha();
}

public function sha() : string
{
return $this->data['sha'];
}
Expand Down Expand Up @@ -96,4 +109,16 @@ public function equals(ChangesSource $source) : bool
{
return $source->id() === $this->id();
}

public function pullRequests(Client $client, Project $project) : PullRequests
{
$pullRequestsData = ResponseMediator::getContent(
$client->getHttpClient()->get(
'/repos/' . \rawurlencode($project->organization()) . '/' . \rawurlencode($project->name()) . '/commits/' . \rawurlencode($this->sha()) . '/pulls',
['Accept' => 'application/vnd.github.groot-preview+json']
)
);

return new PullRequests(...\array_map(fn (array $pullRequestData) : PullRequest => new PullRequest($pullRequestData), $pullRequestsData));
}
}
6 changes: 3 additions & 3 deletions src/Aeon/Automation/GitHub/Commits.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function __construct(Commit ...$commits)
$this->commits = $commits;
}

public static function allFrom(Client $client, Project $project, Reference $fromReference, ?Reference $untilReference = null) : self
public static function allFrom(Client $client, Project $project, Commit $fromCommit, ?Commit $untilCommit = null) : self
{
$commitsPaginator = new ResultPager($client);
$commitsData = $commitsPaginator->fetch($client->api('repo')->commits(), 'all', [$project->organization(), $project->name(), ['sha' => $fromReference->sha()]]);
$commitsData = $commitsPaginator->fetch($client->repo()->commits(), 'all', [$project->organization(), $project->name(), ['sha' => $fromCommit]]);

$foundAll = false;

Expand All @@ -34,7 +34,7 @@ public static function allFrom(Client $client, Project $project, Reference $from
foreach ($commitsData as $commitData) {
$commit = new Commit($commitData);

if ($untilReference !== null && $commit->id() === $untilReference->sha()) {
if ($untilCommit !== null && $commit->sha() === $untilCommit->sha()) {
$foundAll = true;

break;
Expand Down
9 changes: 9 additions & 0 deletions src/Aeon/Automation/GitHub/PullRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public function count() : int
return \count($this->pullRequests);
}

public function first() : ?PullRequest
{
if (!$this->count()) {
return null;
}

return \current($this->pullRequests);
}

/**
* @return PullRequest[]
*/
Expand Down
Loading

0 comments on commit b7e962a

Please sign in to comment.