-
Notifications
You must be signed in to change notification settings - Fork 4
Reduce code duplication #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dregad
wants to merge
11
commits into
dokuwiki:master
Choose a base branch
from
dregad:refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
11407d0
Add language code to commit message
dregad 92ea221
New abstract class GitHostingProviderService
dregad 43c26de
Rename parameter to match parent method signature
dregad d30b9e1
New abstract class GitHostingProviderStatusService
dregad 08ca3a4
Squelch PHPStorm warning
dregad 1a7f6cf
New abstract class GitHostingProviderBehavior
dregad 0cda159
Change createPullRequest() method signature
dregad 9e83c87
Define pull request body text in class property
dregad a584eb6
New method TranslationUpdateEntity::getSubject()
dregad 47ebb53
Update argument names in the service configuration as well
Klap-in 9ce944b
remote name can be generalized, so no need anymore for GitlabBehavior…
Klap-in File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| /** | ||
| * 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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not. Bear in mind that I am not very familiar with the code base.
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.