From 22adc046b18c2fc250aa1b13985de300dc10ec2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Andrieu?= Date: Fri, 4 Nov 2016 10:20:15 +0100 Subject: [PATCH] Introduced Welcome feature --- app/Resources/views/markdown/welcome.md.twig | 6 -- app/config/services.yml | 11 +--- composer.lock | 58 +++++++++---------- src/AppBundle/Commits/Repository.php | 35 ++++++++++- src/AppBundle/Commits/RepositoryInterface.php | 2 + .../EventSubscriber/PullRequestSubscriber.php | 11 +++- src/AppBundle/PullRequests/Listener.php | 15 ++++- src/AppBundle/Repositories/Repository.php | 5 ++ 8 files changed, 93 insertions(+), 50 deletions(-) diff --git a/app/Resources/views/markdown/welcome.md.twig b/app/Resources/views/markdown/welcome.md.twig index d4c66c9..446e31e 100644 --- a/app/Resources/views/markdown/welcome.md.twig +++ b/app/Resources/views/markdown/welcome.md.twig @@ -1,9 +1,3 @@ -{% if nb.contrib === 0 %} Hello {{ username }}! Thank you for this first pull request on the PrestaShop project, and welcome in this Open Source community! -{% elseif nb.contrib === 1%} -Hey {{ username }}! - -This is your second PR on the PrestaShop project: thank you for being part of our Open Source community! -{% endif %} \ No newline at end of file diff --git a/app/config/services.yml b/app/config/services.yml index 1abd53b..a8696b5 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -49,15 +49,6 @@ services: class: Github\Api\Issue\Labels factory: ['@app.github.issues_api', labels] - app.github.gitdata_api: - class: Github\Api\GitData - factory: ['@app.github.client', api] - arguments: [gitData] - - app.github.commits_api: - class: Github\Api\GitData\Commits - factory: ['@app.github.gitdata_api', commits] - # Application GitHub API app.github.cached_labels_api: @@ -144,7 +135,7 @@ services: app.commit.repository: class: AppBundle\Commits\Repository arguments: - - '@app.github.commits_api' + - '@app.repository_repository' - '@app.github.pullrequests_api' - '%repository_username%' - '%repository_name%' diff --git a/composer.lock b/composer.lock index 4aac999..af37295 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a3f2fd8672bf69940049389e3dbb15eb", - "content-hash": "8d2150132390dd215f90db45a3f55b01", + "hash": "546ea3ce313a890555fa28b708f5f457", + "content-hash": "b839a1a6a6be768c5b448eb69a2a8868", "packages": [ { "name": "doctrine/annotations", @@ -81,12 +81,12 @@ "source": { "type": "git", "url": "https://github.com/doctrine/cache.git", - "reference": "6c1b03f643fc4c388318dc3d754555c41a1cf732" + "reference": "0e7ac7811c67d1c4fe93e0fe44d11c90e09d6b20" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/cache/zipball/6c1b03f643fc4c388318dc3d754555c41a1cf732", - "reference": "6c1b03f643fc4c388318dc3d754555c41a1cf732", + "url": "https://api.github.com/repos/doctrine/cache/zipball/0e7ac7811c67d1c4fe93e0fe44d11c90e09d6b20", + "reference": "0e7ac7811c67d1c4fe93e0fe44d11c90e09d6b20", "shasum": "" }, "require": { @@ -96,7 +96,7 @@ "doctrine/common": ">2.2,<2.4" }, "require-dev": { - "phpunit/phpunit": "~5.0", + "phpunit/phpunit": "^5.6.2", "predis/predis": "~1.0" }, "type": "library", @@ -142,7 +142,7 @@ "cache", "caching" ], - "time": "2016-10-26 23:08:41" + "time": "2016-10-29 11:43:38" }, { "name": "doctrine/collections", @@ -1039,16 +1039,16 @@ }, { "name": "lp-digital/github-event-parser", - "version": "v0.7.7", + "version": "v0.7.8", "source": { "type": "git", "url": "https://github.com/Lp-digital/github-event-parser.git", - "reference": "bf0a198b24ad3546d41c79d9716be2517b5e11fc" + "reference": "bd000b3ec47b7796a62fade09298d2677f69aeba" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Lp-digital/github-event-parser/zipball/bf0a198b24ad3546d41c79d9716be2517b5e11fc", - "reference": "bf0a198b24ad3546d41c79d9716be2517b5e11fc", + "url": "https://api.github.com/repos/Lp-digital/github-event-parser/zipball/bd000b3ec47b7796a62fade09298d2677f69aeba", + "reference": "bd000b3ec47b7796a62fade09298d2677f69aeba", "shasum": "" }, "require": { @@ -1086,7 +1086,7 @@ "github", "json parser" ], - "time": "2016-10-06 14:39:23" + "time": "2016-11-03 14:53:41" }, { "name": "monolog/monolog", @@ -1313,12 +1313,12 @@ "source": { "type": "git", "url": "https://github.com/sensiolabs/SensioDistributionBundle.git", - "reference": "7bc47dcfdbde6d567e1a834577d1c04ddb970281" + "reference": "e64947de9ebc37732a62f5115164484a9bee7fa6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sensiolabs/SensioDistributionBundle/zipball/7bc47dcfdbde6d567e1a834577d1c04ddb970281", - "reference": "7bc47dcfdbde6d567e1a834577d1c04ddb970281", + "url": "https://api.github.com/repos/sensiolabs/SensioDistributionBundle/zipball/e64947de9ebc37732a62f5115164484a9bee7fa6", + "reference": "e64947de9ebc37732a62f5115164484a9bee7fa6", "shasum": "" }, "require": { @@ -1357,7 +1357,7 @@ "configuration", "distribution" ], - "time": "2016-10-08 18:50:33" + "time": "2016-10-30 23:18:01" }, { "name": "sensio/framework-extra-bundle", @@ -1983,12 +1983,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/symfony.git", - "reference": "46a8edea8d473e82ab8ac9a82294df047ba5f962" + "reference": "31d5fffc3c74c1d1c6ccffd954065300fb79c3a9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/symfony/zipball/46a8edea8d473e82ab8ac9a82294df047ba5f962", - "reference": "46a8edea8d473e82ab8ac9a82294df047ba5f962", + "url": "https://api.github.com/repos/symfony/symfony/zipball/31d5fffc3c74c1d1c6ccffd954065300fb79c3a9", + "reference": "31d5fffc3c74c1d1c6ccffd954065300fb79c3a9", "shasum": "" }, "require": { @@ -2117,7 +2117,7 @@ "keywords": [ "framework" ], - "time": "2016-10-28 14:46:54" + "time": "2016-11-03 08:25:26" }, { "name": "twig/twig", @@ -2239,16 +2239,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.5.4", + "version": "1.5.5", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "ea74994a3dc7f8d2f65a06009348f2d63c81e61f" + "reference": "399c1f9781e222f6eb6cc238796f5200d1b7f108" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/ea74994a3dc7f8d2f65a06009348f2d63c81e61f", - "reference": "ea74994a3dc7f8d2f65a06009348f2d63c81e61f", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/399c1f9781e222f6eb6cc238796f5200d1b7f108", + "reference": "399c1f9781e222f6eb6cc238796f5200d1b7f108", "shasum": "" }, "require": { @@ -2277,7 +2277,7 @@ "object", "object graph" ], - "time": "2016-09-16 13:37:59" + "time": "2016-10-31 17:19:45" }, { "name": "phpdocumentor/reflection-common", @@ -2494,12 +2494,12 @@ "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "8ca687f6b13f38f0593fe26a84de00ef4fd82c51" + "reference": "6cba06ff75a1a63a71033e1a01b89056f3af1e8d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/8ca687f6b13f38f0593fe26a84de00ef4fd82c51", - "reference": "8ca687f6b13f38f0593fe26a84de00ef4fd82c51", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6cba06ff75a1a63a71033e1a01b89056f3af1e8d", + "reference": "6cba06ff75a1a63a71033e1a01b89056f3af1e8d", "shasum": "" }, "require": { @@ -2549,7 +2549,7 @@ "testing", "xunit" ], - "time": "2016-10-03 07:41:08" + "time": "2016-11-01 05:06:24" }, { "name": "phpunit/php-file-iterator", diff --git a/src/AppBundle/Commits/Repository.php b/src/AppBundle/Commits/Repository.php index f08d5d6..d59e047 100644 --- a/src/AppBundle/Commits/Repository.php +++ b/src/AppBundle/Commits/Repository.php @@ -3,7 +3,8 @@ namespace AppBundle\Commits; use Github\Exception\RuntimeException; -use Github\Api\GitData\Commits as CommitApi; +use AppBundle\Repositories\Repository as CommitsApi; + use Github\Api\PullRequest as PullRequestApi; use Lpdigital\Github\Entity\Commit; use Lpdigital\Github\Entity\PullRequest; @@ -11,7 +12,7 @@ class Repository implements RepositoryInterface { /** - * @var CommitApi + * @var CommitsApi */ private $commitsApi; @@ -31,7 +32,7 @@ class Repository implements RepositoryInterface private $repositoryName; public function __construct( - CommitApi $commitsApi, + CommitsApi $commitsApi, PullRequestApi $pullRequestApi, $repositoryUsername, $repositoryName @@ -54,6 +55,34 @@ public function findAllByPullRequest(PullRequest $pullRequest) $responseApi = []; } + return $this->buildCommits($responseApi); + } + + public function findAllByBranchAndUserLogin($branch, $userLogin) + { + $responseApi = $this->commitsApi + ->getCommits() + ->all( + $this->repositoryUsername, + $this->repositoryName, + ['sha' => $branch] + ); + + $commits = $this->buildCommits($responseApi); + $userCommits = []; + + foreach ($commits as $commit) { + $authorName = $commit->getAuthor()->getName(); + if ($authorName === $userLogin) { + $userCommits[] = $commit; + } + } + + return $userCommits; + } + + private function buildCommits($responseApi) + { $commits = []; foreach ($responseApi as $commitApi) { $commits[] = Commit::createFromData($commitApi['commit']); diff --git a/src/AppBundle/Commits/RepositoryInterface.php b/src/AppBundle/Commits/RepositoryInterface.php index 7d0253f..2379e13 100644 --- a/src/AppBundle/Commits/RepositoryInterface.php +++ b/src/AppBundle/Commits/RepositoryInterface.php @@ -6,5 +6,7 @@ interface RepositoryInterface { + public function findAllByBranchAndUserLogin($branch, $userLogin); + public function findAllByPullRequest(PullRequest $pullRequest); } diff --git a/src/AppBundle/EventSubscriber/PullRequestSubscriber.php b/src/AppBundle/EventSubscriber/PullRequestSubscriber.php index 409dcb2..b7c36cc 100644 --- a/src/AppBundle/EventSubscriber/PullRequestSubscriber.php +++ b/src/AppBundle/EventSubscriber/PullRequestSubscriber.php @@ -103,7 +103,7 @@ public function checkCommits(GitHubEvent $githubEvent) public function checkForNewTranslations(GitHubEvent $githubEvent) { $event = $githubEvent->getEvent(); - $pullRequest = $githubEvent->getEvent()->pullRequest; + $pullRequest = $event->pullRequest; $diff = Diff::create(file_get_contents($pullRequest->getDiffUrl())); if ($found = $diff->additions()->contains(self::TRANS_PATTERN)->match()) { @@ -125,6 +125,15 @@ public function checkForNewTranslations(GitHubEvent $githubEvent) public function welcomePeople(GitHubEvent $githubEvent) { + $pullRequest = $githubEvent->getEvent()->pullRequest; + $sender = $githubEvent->getEvent()->sender; + $branch = $pullRequest->getBase()['ref']; + + $this->container + ->get('app.pullrequest_listener') + ->welcomePeople($pullRequest, $sender, $branch) + ; + $githubEvent->addStatus([ 'event' => 'pr_opened', 'action' => 'user welcomed', diff --git a/src/AppBundle/PullRequests/Listener.php b/src/AppBundle/PullRequests/Listener.php index bbed40c..5169f3e 100644 --- a/src/AppBundle/PullRequests/Listener.php +++ b/src/AppBundle/PullRequests/Listener.php @@ -121,12 +121,25 @@ public function removeCommitValidationComment(PullRequest $pullRequest) return false; } + public function welcomePeople(PullRequest $pullRequest, User $sender, $branch) + { + $userCommits = $this->commitRepository->findAllByBranchAndUserLogin($branch, $sender); + + if (0 === count($userCommits)) { + $this->commentApi->sendWithTemplate( + $pullRequest, + 'markdown/welcome.md.twig', + ['username' => $pullRequest->getUser()->getLogin()] + ); + } + } + /** * Wrap the validation of commits. * * @return array error messages if any. */ - public function getErrorsFromCommits(PullRequest $pullRequest) + private function getErrorsFromCommits(PullRequest $pullRequest) { $commits = $this->commitRepository->findAllByPullRequest($pullRequest); $commitsErrors = []; diff --git a/src/AppBundle/Repositories/Repository.php b/src/AppBundle/Repositories/Repository.php index 7ee87d7..3b50f93 100644 --- a/src/AppBundle/Repositories/Repository.php +++ b/src/AppBundle/Repositories/Repository.php @@ -69,6 +69,11 @@ public function getApi() return $this->repositoryApi; } + public function getCommits() + { + return $this->repositoryApi->commits(); + } + public function getMembers() { return $this->repositoryApi