Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erkenes committed Jul 13, 2022
0 parents commit 3f98a56
Show file tree
Hide file tree
Showing 17 changed files with 911 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.yml]
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @erkenes
59 changes: 59 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build Docker-Image

on:
push:
tags: [ '*.*.*' ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build-php:
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata - php
id: meta-php
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/php

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image - php
id: build-and-push-php
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
file: ./DockerfileBuild
push: true
tags: ${{ steps.meta-php.outputs.tags }}
labels: ${{ steps.meta-php.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
Slicer/git-subsplit-temporary
137 changes: 137 additions & 0 deletions Classes/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
declare(strict_types=1);

namespace ErkEnes\MonorepoSplit;

class Config
{
public function __construct(
private readonly string $repositoryProtocol,
private readonly string $repositoryHost,
private readonly string $repositoryOrganization,
private readonly string $repositoryName,
private readonly string $accessToken,
private readonly string $allowedRefsPattern,
private readonly string $defaultBranch,
private readonly string $targetBranch,
private readonly string $packageDirectory,
private readonly string $remoteRepository,
private readonly ?string $remoteRepositoryAccessToken = null
)
{
}

/**
* @return string
*/
public function getRepositoryProtocol(): string
{
return $this->repositoryProtocol;
}

/**
* @return string
*/
public function getRepositoryHost(): string
{
return $this->repositoryHost;
}

/**
* @return string
*/
public function getRepositoryOrganization(): string
{
return $this->repositoryOrganization;
}

/**
* @return string
*/
public function getRepositoryName(): string
{
return $this->repositoryName;
}

/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}

/**
* @param bool $withAccessToken
* @return string
*/
public function getRepositoryUrl(bool $withAccessToken = false): string
{
$repositoryUrl = $this->repositoryHost . '/' . $this->repositoryOrganization . '/' . $this->repositoryName . '.git';

return $withAccessToken ? $this->repositoryProtocol . $this->accessToken . '@' . $repositoryUrl : $this->repositoryProtocol . $repositoryUrl;
}

/**
* @return string
*/
public function getAllowedRefsPattern(): string
{
return $this->allowedRefsPattern;
}

public function getSplits(): array
{
return [
$this->getPackageDirectory() . ':' . $this->getRemoteRepository()
];
}

/**
* @return string
*/
public function getDefaultBranch(): string
{
return $this->defaultBranch;
}

/**
* @param bool $fullPath
* @return string
*/
public function getTargetBranch(bool $fullPath = false): string
{
return $fullPath ? 'refs/heads/' . $this->targetBranch : $this->targetBranch;
}

/**
* @return string
*/
public function getPackageDirectory(): string
{
return $this->packageDirectory;
}

/**
* @return string
*/
public function getRemoteRepository(): string
{
return $this->remoteRepositoryAccessToken
? $this->insertStringBetween($this->remoteRepository, 'https://', $this->remoteRepositoryAccessToken . '@')
: $this->remoteRepository;
}

/**
* @return string|null
*/
public function getRemoteRepositoryAccessToken(): ?string
{
return $this->remoteRepositoryAccessToken;
}

private function insertStringBetween ($string, $keyword, $body): string
{
return substr_replace($string, $body, strpos($string, $keyword) + strlen($keyword), 0);
}
}
45 changes: 45 additions & 0 deletions Classes/ConfigFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace ErkEnes\MonorepoSplit;

use ErkEnes\MonorepoSplit\Exception\ConfigurationException;

class ConfigFactory
{
private const GITHUB = 'GITHUB';
private const DEFAULT_BRANCH = 'main';

private PublicAccessTokenResolver $publicAccessTokenResolver;

public function __construct()
{
$this->publicAccessTokenResolver = new PublicAccessTokenResolver();
}

public function create(array $env): Config
{
$accessToken = $this->publicAccessTokenResolver->resolve($env);

return $this->createFormEnv($env, $accessToken, self::GITHUB);
}

protected function createFormEnv(array $env, string $accessToken, string $ciPlatform): Config
{
$envPrefix = $ciPlatform === self::GITHUB ? 'INPUT_' : '';

return new Config(
repositoryProtocol: $env[$envPrefix . 'REPOSITORY_PROTOCOL'] ?? throw new ConfigurationException('Repository Protocol is missing'),
repositoryHost: $env[$envPrefix . 'REPOSITORY_HOST'] ?? throw new ConfigurationException('Repository Host is missing'),
repositoryOrganization: $env[$envPrefix . 'REPOSITORY_ORGANIZATION'] ?? throw new ConfigurationException('Repository Organization is missing'),
repositoryName: $env[$envPrefix . 'REPOSITORY_NAME'] ?? throw new ConfigurationException('Repository Name is missing'),
accessToken: $accessToken ?? throw new ConfigurationException('Access Token is missing'),
allowedRefsPattern: $env[$envPrefix . 'ALLOWED_REFS_PATTERN'] ?? throw new ConfigurationException('Allowed Refs Pattern is missing'),
defaultBranch: $env[$envPrefix . 'DEFAULT_BRANCH'] ?? self::DEFAULT_BRANCH,
targetBranch: $env[$envPrefix . 'TARGET_BRANCH'] ?? throw new ConfigurationException('Target Branch is missing'),
packageDirectory: $env[$envPrefix . 'PACKAGE_DIRECTORY'] ?? throw new ConfigurationException('Package Directory is missing'),
remoteRepository: $env[$envPrefix . 'REMOTE_REPOSITORY'] ?? throw new ConfigurationException('Remote Repository is missing'),
remoteRepositoryAccessToken: $env[$envPrefix . 'REMOTE_REPOSITORY_ACCESS_TOKEN'] ?? null,
);
}
}
10 changes: 10 additions & 0 deletions Classes/Exception/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace ErkEnes\MonorepoSplit\Exception;

use InvalidArgumentException;

final class ConfigurationException extends InvalidArgumentException
{
}
35 changes: 35 additions & 0 deletions Classes/PublicAccessTokenResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace ErkEnes\MonorepoSplit;

use ErkEnes\MonorepoSplit\Exception\ConfigurationException;

final class PublicAccessTokenResolver
{
private const GITHUB_TOKEN = 'GITHUB_TOKEN';

/**
* @var string[]
*/
private const POSSIBLE_TOKEN_ENVS = [
self::GITHUB_TOKEN
];

/**
* @param array<string, mixed> $env
*/
public function resolve(array $env): string
{
if (isset($env[self::GITHUB_TOKEN])) {
return $env[self::GITHUB_TOKEN];
}

$message = sprintf(
'Public access token is missing, add it via: "%s"',
implode('", "', self::POSSIBLE_TOKEN_ENVS)
);

throw new ConfigurationException($message);
}
}
Loading

0 comments on commit 3f98a56

Please sign in to comment.