Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Introduced Welcome feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed Nov 22, 2016
1 parent e21a33c commit 22adc04
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 50 deletions.
6 changes: 0 additions & 6 deletions app/Resources/views/markdown/welcome.md.twig
Original file line number Diff line number Diff line change
@@ -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 %}
11 changes: 1 addition & 10 deletions app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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%'
Expand Down
58 changes: 29 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions src/AppBundle/Commits/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
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;

class Repository implements RepositoryInterface
{
/**
* @var CommitApi
* @var CommitsApi
*/
private $commitsApi;

Expand All @@ -31,7 +32,7 @@ class Repository implements RepositoryInterface
private $repositoryName;

public function __construct(
CommitApi $commitsApi,
CommitsApi $commitsApi,
PullRequestApi $pullRequestApi,
$repositoryUsername,
$repositoryName
Expand All @@ -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']);
Expand Down
2 changes: 2 additions & 0 deletions src/AppBundle/Commits/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

interface RepositoryInterface
{
public function findAllByBranchAndUserLogin($branch, $userLogin);

public function findAllByPullRequest(PullRequest $pullRequest);
}
11 changes: 10 additions & 1 deletion src/AppBundle/EventSubscriber/PullRequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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',
Expand Down
15 changes: 14 additions & 1 deletion src/AppBundle/PullRequests/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
5 changes: 5 additions & 0 deletions src/AppBundle/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function getApi()
return $this->repositoryApi;
}

public function getCommits()
{
return $this->repositoryApi->commits();
}

public function getMembers()
{
return $this->repositoryApi
Expand Down

0 comments on commit 22adc04

Please sign in to comment.