Skip to content

Commit

Permalink
Merge pull request #237 from vtsykun/fix/pull-review
Browse files Browse the repository at this point in the history
Check changed files before adding composer lock review
  • Loading branch information
vtsykun authored Mar 2, 2024
2 parents e3e1830 + 8f99e69 commit f801733
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Entity/OAuthIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OAuthIntegration

public function __construct()
{
$this->createdAt = new \DateTime();
$this->createdAt = new \DateTimeImmutable();
$this->serializedFields = [];
}

Expand Down
14 changes: 14 additions & 0 deletions src/Integrations/Base/AppIntegrationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ protected function pushEventSynchronize(App $app, array $payload): ?array
return $job;
}

/**
* @param array{name: string, id: string, source_id: string, source_branch: string,
* target_id: string, target_branch: string, default_branch: string, iid: int|string} $payload
* {@inheritdoc}
*/
protected function hasComposerLockChanges(array $token, array $payload): ?bool
{
return null;
}

/**
* @param array{name: string, id: string, source_id: string, source_branch: string,
* target_id: string, target_branch: string, default_branch: string, iid: int|string} $payload
Expand Down Expand Up @@ -283,6 +293,10 @@ protected function pullRequestReview(App $app, array $payload, callable $request
}

$token = $this->refreshToken($app);
if (false === $this->hasComposerLockChanges($token, $payload)) {
return [];
}

$newLock = $this->getRemoteContent($token, $payload['source_id'], 'composer.lock', $payload['source_branch']);
$prevLock = $this->getRemoteContent($token, $payload['target_id'], 'composer.lock', $payload['target_branch']);
if (null === ($diff = ComposerDiffReview::generateDiff($prevLock, $newLock))) {
Expand Down
26 changes: 24 additions & 2 deletions src/Integrations/Github/GitHubIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function organizations(App $app, bool $withCache = true): array
return $response->toArray();
});

$orgs = array_map(function ($org) {
$orgs = array_map(static function ($org) {
$org['identifier'] = $org['organization']['login'];
$org['name'] = $org['organization']['login'];
$org['logo'] = $org['organization']['avatar_url'];
Expand Down Expand Up @@ -295,7 +295,10 @@ public function receiveHooks(App $accessToken, Request $request = null, ?array $
}

$repoId = $payload['repository']['id'];
$kind = $request->headers->get('x-github-event', 'push');
$kind = $request->headers->get('x-github-event');
if (null === $kind) {
$kind = ($payload['pull_request'] ?? null) ? 'pull_request' : 'push';
}

if (in_array($kind, ['push', 'tag_push'])) {
return $this->processPushEvent($accessToken, $payload, $repoId, $repoName);
Expand Down Expand Up @@ -357,6 +360,25 @@ protected function processPushEvent(App $app, array $payload, $id, $repoName): ?
return $this->pushEventSynchronize($app, $data + $payload);
}

/**
* {@inheritdoc}
*/
protected function hasComposerLockChanges(array $token, array $payload): ?bool
{
try {
$name = $this->paginatorQueryName ?? 'per_page';
$content = $this->makeApiRequest($token, 'GET', "/repos/{$payload['name']}/pulls/{$payload['iid']}/files", ['query' => [$name => 100]]);
$files = array_column($content, 'filename');
} catch (\Throwable $e) {
return null;
}
if (empty($files) || count($files) === 100) {
return null;
}

return in_array('composer.lock', $files, true);
}

protected function getRemoteContent(array $token, string|int $projectId, string $file, ?string $ref = null, bool $asJson = true): null|string|array
{
$url = str_replace(['{project_id}', '{file}', '{ref}'], [(string)$projectId, $file, $ref], $this->remoteContentUrl);
Expand Down

0 comments on commit f801733

Please sign in to comment.