Skip to content

Commit

Permalink
Merge pull request #18 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Improved work of dependabot maker
  • Loading branch information
Andrey Helldar authored Feb 9, 2022
2 parents 50fbc9b + 0f8eff6 commit e224fad
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ updates:
directory: /
schedule:
interval: daily
time: 00:00
93 changes: 74 additions & 19 deletions dependabot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,86 @@

declare(strict_types=1);

$target_path = './.github/dependabot.yml';
class Dependabot
{
protected array $update = [
'package-ecosystem' => 'github-actions',

$content = file_exists($target_path) ? yaml_parse_file($target_path) : [];
'directory' => '/',

$updates = $content['updates'] ?? [];
'schedule' => [
'interval' => 'daily',
],
];

foreach ($updates as $update) {
if ($update['package-ecosystem'] === 'github-actions') {
exit(0);
protected array $content = [];

protected int $version = 2;

protected string $name = 'github-actions';

public function __construct(
protected string $path
) {
$this->content = $this->parse();
}

public function handle(): void
{
$updates = $this->each($this->getUpdates());
$version = $this->getVersion();

$this->store($version, $updates);
}

protected function each(array $updates): array
{
$found = false;

foreach ($updates as &$update) {
if ($update['package-ecosystem'] === $this->name) {
$update = $this->update;
$found = true;
break;
}
}

return $found ? $updates : $this->put($updates);
}
}

$updates[] = [
'package-ecosystem' => 'github-actions',
protected function put(array $updates): array
{
$updates[] = $this->update;

'directory' => '/',
return $updates;
}

protected function store(int $version, array $updates): void
{
yaml_emit_file($this->path, compact('version', 'updates'));
}

'schedule' => [
'interval' => 'daily',
'time' => '00:00',
'timezone' => 'Etc/GMT',
],
];
protected function getUpdates(): array
{
return $this->content['updates'] ?? [];
}

protected function getVersion(): int
{
return $this->version;
}

protected function parse(): array
{
return $this->exists() ? yaml_parse_file($this->path) : [];
}

protected function exists(): bool
{
return file_exists($this->path);
}
}

$content['version'] = 2;
$content['updates'] = $updates;
$dependabot = new Dependabot('./.github/dependabot.yml');

yaml_emit_file($target_path, $content);
$dependabot->handle();

0 comments on commit e224fad

Please sign in to comment.