Skip to content
Draft
8 changes: 4 additions & 4 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ services:

App\Services\GitHub\GitHubService:
arguments:
$gitHubApiToken: '%app.githubApiToken%'
$apiToken: '%app.githubApiToken%'
$dataFolder: '%app.dataDir%'
$gitHubUrl: '%app.githubUrl%'
$url: '%app.githubUrl%'

App\Services\GitLab\GitLabService:
arguments:
$gitLabApiToken: '%app.gitlabApiToken%'
$apiToken: '%app.gitlabApiToken%'
$dataFolder: '%app.dataDir%'
$gitLabUrl: '%app.gitlabUrl%'
$url: '%app.gitlabUrl%'

App\Repository\TranslationUpdateEntityRepository:
arguments:
Expand Down
23 changes: 23 additions & 0 deletions src/Entity/TranslationUpdateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class TranslationUpdateEntity {
*/
protected ?string $language = null;

/**
* Subject message.
* @see getSubject()
*/
private string $subject = 'Translation update';


public function setAuthor(string $author): void {
$this->author = $author;
}
Expand Down Expand Up @@ -112,6 +119,22 @@ public function getLanguage(): ?string {
return $this->language;
}

/**
* Returns a standard subject for translation updates.
*
* This can be used e.g. as commit message, pull request title or email subject.
* If defined, the language code is added as a suffix.
*
* @return string
*/
public function getSubject(): string {
$subject = $this->subject;
if ($this->language) {
$subject .= ' (' . $this->language . ')';
}
return $subject;
}

public function setErrorMsg(string $errorMsg): void
{
$this->errorMsg = $errorMsg;
Expand Down
115 changes: 115 additions & 0 deletions src/Services/GitHostingProviderService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Services;

use App\Services\GitHub\GitHubService;
use App\Services\GitLab\GitLabService;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use Github\Client as GithubClient;
use Gitlab\Client as GitlabClient;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;

/**
* Parent Class for Git Hosting Provider Services.
*
* @see GitHubService, GitLabService
*/
abstract class GitHostingProviderService
{

/**
* PCRE pattern to parse repository and username from URL.
* Must be defined by child class.
* @see getUsernameAndRepositoryFromURL()
*/
protected const REGEX_REPO_USER = '';

/**
* Hosting Provider identifier.
* Must be defined by child class.
*/
protected string $provider;

/**
* Hosting provider-specific Exception to be thrown.
*/
protected string $exceptionType;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? It is only used for the exception on getting the user and repo name. What about renaming the exception to 'GitHostingProviderServiceException' or ´GitHostingServiceException' or .. ?

Note that GitHubServiceException and GitLabServiceException are mentioned quite a bit through the code. Feels as duplication as well. Also the error reporter distincts them. But sent almost the same email. In the email the entire git url is mentioned, so in general it is clear if github/gitlab is meant. But GitLab and GitHub are mentioned in text as well, this must be generalized or the name must be set via the exception or something like that. We have to check that the logging is unique enough to prevent potential confusion if repositories are added to the translation tool with same usernames and reponames for github and gitlab.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed?

Maybe not. Bear in mind that I am not very familiar with the code base.

What about renaming the exception to 'GitHostingProviderServiceException' or ´GitHostingServiceException'

Sounds like a good idea - I thought about it also but I was not sure about the implications and I did not want to make too far-reaching changes.


/**
* Git Repository client API.
* Actual type depends on child class.
* @var GithubClient|GitlabClient
*/
protected $client;

/**
* Repository URL
*/
protected string $url;

public function __construct(string $apiToken, string $dataFolder, string $url, bool $autoStartup = true)
{
$this->url = $url;
}

protected function getCachePool(string $dataFolder): FilesystemCachePool
{
// folders are relative to folder set here
$filesystemAdapter = new Local($dataFolder);
$filesystem = new Filesystem($filesystemAdapter);

$pool = new FilesystemCachePool($filesystem);
$pool->setFolder('cache/' . strtolower($this->provider));

return $pool;
}

/**
* Create fork in our Hosting Provider account
*
* @param string $url URL to create the fork from
* @return string Git URL of the fork
*/
abstract public function createFork(string $url): string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not all thrown exceptions are mentioned. So far I tried to mention all throws, to make it easier to remember how and where they are caught.


/**
* Delete fork from our Hosting Provider account
*
* @param string $remoteUrl Git url of the forked repository
*/
abstract public function deleteFork(string $remoteUrl): void;

/**
* @param string $patchBranch name of branch with language update
* @param string $destinationBranch name of branch at remote
* @param string $url git url original upstream repository
* @param string $patchUrl remote url
* @param string $title Title for the pull request
* @param string $body Text to be inserted as description for the pull request
*/
abstract public function createPullRequest(string $patchBranch, string $destinationBranch, string $url, string $patchUrl, string $title, string $body): void;

/**
* Get information about the open pull requests i.e. url and count
*
* @param string $urlUpstream original git clone url
* @param string $languageCode
* @return array{count: int, listURL: string, title: string}
*/
abstract public function getOpenPRListInfo(string $urlUpstream, string $languageCode): array;

/**
* @param string $url git clone url
* @return array with user's account name, repository name
*/
protected function getUsernameAndRepositoryFromURL(string $url): array
{
$result = preg_replace($this::REGEX_REPO_USER, '$2', $url, 1, $counter);
if ($counter === 0) {
throw new $this->exceptionType('Invalid ' . $this->provider . ' clone URL: ' . $url);
}
return explode('/', $result);
}

}
40 changes: 40 additions & 0 deletions src/Services/GitHostingProviderStatusService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Services;


/**
* Parent Class for Git Hosting Status Services
*/
abstract class GitHostingProviderStatusService
{
/**
* URL to Hosting Provider's status site.
*/
protected const STATUS_URL = '';

protected ?bool $status = null;

/**
* Check if Hosting Provider is functional.
*
* @return bool true if status is good, otherwise false
*/
public function isFunctional(): bool
{
if ($this->status === null) {
$json = file_get_contents($this::STATUS_URL);
$this->status = $this->checkResponse($json);
}
return $this->status;
}

/**
* Returns true if response status of API Requests is good, otherwise false
*
* @param string|false $content
* @return bool
*/
abstract protected function checkResponse($content): bool;

}
84 changes: 26 additions & 58 deletions src/Services/GitHub/GitHubService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,45 @@

namespace App\Services\GitHub;

use Cache\Adapter\Filesystem\FilesystemCachePool;
use App\Services\GitHostingProviderService;
use Exception;
use Github\AuthMethod;
use Github\Client;
use Github\Exception\MissingArgumentException;
use Github\Exception\RuntimeException;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Symfony\Component\HttpClient\HttplugClient;


class GitHubService
class GitHubService extends GitHostingProviderService
{
const REGEX_REPO_USER = '#^(https://github.com/|git@.*?github.com:|git://github.com/)(.*)\.git$#';

private Client $client;
private string $gitHubUrl;
protected string $provider = 'GitHub';
protected string $exceptionType = GitHubServiceException::class;

public function __construct(string $gitHubApiToken, string $dataFolder, string $gitHubUrl, bool $autoStartup = true)
/**
* @var Client
*/
protected $client;


public function __construct(string $apiToken, string $dataFolder, string $url, bool $autoStartup = true)
{
$this->gitHubUrl = $gitHubUrl;
parent::__construct($apiToken, $dataFolder, $url, $autoStartup);
if (!$autoStartup) {
return;
}

$filesystemAdapter = new Local($dataFolder); // folders are relative to folder set here
$filesystem = new Filesystem($filesystemAdapter);

$pool = new FilesystemCachePool($filesystem);
$pool->setFolder('cache/github');

$this->client = Client::createWithHttpClient(
new HttplugClient()
);

$this->client->addCache($pool);
$this->client->authenticate($gitHubApiToken, null, AuthMethod::ACCESS_TOKEN);
$this->client->addCache($this->getCachePool($dataFolder));
$this->client->authenticate($apiToken, null, AuthMethod::ACCESS_TOKEN);
}

/**
* Create fork in our GitHub account
*
* @param string $url GitHub URL to create the fork from
* @return string Git URL of the fork
* @inheritDoc
*
* @throws GitHubForkException
* @throws GitHubServiceException
Expand All @@ -61,9 +57,7 @@ public function createFork(string $url): string
}

/**
* Delete fork from our GitHub account
*
* @param string $remoteUrl git url of the forked repository
* @inheritDoc
*
* @throws GitHubServiceException
*/
Expand All @@ -78,17 +72,13 @@ public function deleteFork(string $remoteUrl): void
}

/**
* @param string $patchBranch name of branch with language update
* @param string $destinationBranch name of branch at remote
* @param string $languageCode
* @param string $url git url original upstream repository
* @param string $patchUrl remote url
* @inheritDoc
*
* @throws GitHubCreatePullRequestException
* @throws GitHubServiceException
* @throws MissingArgumentException
*/
public function createPullRequest(string $patchBranch, string $destinationBranch, string $languageCode, string $url, string $patchUrl): void
public function createPullRequest(string $patchBranch, string $destinationBranch, string $url, string $patchUrl, string $title, string $body): void
{
[$user, $repository] = $this->getUsernameAndRepositoryFromURL($url);
[$repoName, ] = $this->getUsernameAndRepositoryFromURL($patchUrl);
Expand All @@ -97,27 +87,23 @@ public function createPullRequest(string $patchBranch, string $destinationBranch
$this->client->api('pull_request')->create($user, $repository, [
'base' => $destinationBranch,
'head' => $repoName . ':' . $patchBranch,
'title' => 'Translation update (' . $languageCode . ')',
'body' => 'This pull request contains some translation updates.'
'title' => $title,
'body' => $body,
]);
} catch (RuntimeException $e) {
throw new GitHubCreatePullRequestException($e->getMessage() . " $user/$repository", 0, $e);
}
}

/**
* Get information about the open pull requests i.e. url and count
*
* @param string $url original git clone url
* @param string $languageCode
* @return array{count: int, listURL: string, title: string}
* @inheritDoc
*
* @throws GitHubServiceException
* @throws Exception only if in 'test' environment
*/
public function getOpenPRListInfo(string $url, string $languageCode): array
public function getOpenPRListInfo(string $urlUpstream, string $languageCode): array
{
[$user, $repository] = $this->getUsernameAndRepositoryFromURL($url);
[$user, $repository] = $this->getUsernameAndRepositoryFromURL($urlUpstream);

$info = [
'listURL' => '',
Expand All @@ -144,33 +130,15 @@ public function getOpenPRListInfo(string $url, string $languageCode): array
return $info;
}

/**
* @param string $url git clone url
* @return array with user's account name, repository name
*
* @throws GitHubServiceException
*/
private function getUsernameAndRepositoryFromURL(string $url): array
{
$result = preg_replace(
'#^(https://github.com/|git@.*?github.com:|git://github.com/)(.*)\.git$#',
'$2', $url, 1, $counter
);
if ($counter === 0) {
throw new GitHubServiceException('Invalid GitHub clone URL: ' . $url);
}
return explode('/', $result);
}

/**
* @param string $url git clone url
* @return string modified git clone url
*/
private function gitHubUrlHack(string $url): string
{
if ($this->gitHubUrl === 'github.com') {
if ($this->url === 'github.com') {
return $url;
}
return str_replace('github.com', $this->gitHubUrl, $url);
return str_replace('github.com', $this->url, $url);
}
}
Loading