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

Commit

Permalink
Fixed non deletion of Commit validation comment
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed Nov 10, 2016
1 parent c0ffe1b commit e11575b
Show file tree
Hide file tree
Showing 11 changed files with 704 additions and 167 deletions.
15 changes: 11 additions & 4 deletions app/config/services_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ parameters:
mailer_password: null

services:
fake_comment_api:
class: Tests\AppBundle\Comments\FakeCommentApi
arguments:
- '@app.github.comments_api'
- '%repository_username%'
- '%repository_name%'
- '@twig'
app.status_api:
class: Tests\AppBundle\Issues\NullStatusApi
arguments: ['@app.github.cached_labels_api', '%repository_username%', '%repository_name%']
# Your integrations tests shouldn't post comments to github
app.pullrequest_listener:
class: Tests\AppBundle\PullRequests\FakeListener
class: AppBundle\PullRequests\Listener
arguments:
- '@app.comment_api'
- '@validator'
- '@twig'
- '@fake_comment_api'
- '@app.commit.repository'
- '@validator'
- '@app.pull_request.repository'
15 changes: 10 additions & 5 deletions src/AppBundle/Commits/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AppBundle\Commits;

use Github\Exception\RuntimeException;
use Github\Api\GitData\Commits as CommitApi;
use Github\Api\PullRequest as PullRequestApi;
use Lpdigital\Github\Entity\Commit;
Expand Down Expand Up @@ -43,11 +44,15 @@ public function __construct(

public function findAllByPullRequest(PullRequest $pullRequest)
{
$responseApi = $this->pullRequestApi->commits(
$this->repositoryUsername,
$this->repositoryName,
$pullRequest->getNumber()
);
try {
$responseApi = $this->pullRequestApi->commits(
$this->repositoryUsername,
$this->repositoryName,
$pullRequest->getNumber()
);
} catch (RuntimeException $e) {
$responseApi = [];
}

$commits = [];
foreach ($responseApi as $commitApi) {
Expand Down
31 changes: 16 additions & 15 deletions src/AppBundle/EventSubscriber/PullRequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,20 @@ public function removePullRequestValidationComment(GithubEvent $githubEvent)
return;
}

$this->container
$success = $this->container
->get('app.pullrequest_listener')
->removePullRequestValidationComment($pullRequest)
;

$githubEvent->addStatus([
'event' => 'pr_edited',
'action' => 'preston validation comment removed',
])
;
if ($success) {
$githubEvent->addStatus([
'event' => 'pr_edited',
'action' => 'preston validation comment removed',
])
;
}
}

/**
* @todo: create functional test in WebhookController
*/
public function removeCommitValidationComment(GithubEvent $githubEvent)
{
$pullRequest = $githubEvent->getEvent()->pullRequest;
Expand All @@ -167,15 +166,17 @@ public function removeCommitValidationComment(GithubEvent $githubEvent)
return;
}

$this->container
$success = $this->container
->get('app.pullrequest_listener')
->removeCommitValidationComment($pullRequest)
;

$githubEvent->addStatus([
'event' => 'pr_edited',
'action' => 'preston validation commit comment removed',
])
;
if ($success) {
$githubEvent->addStatus([
'event' => 'pr_edited',
'action' => 'preston validation commit comment removed',
])
;
}
}
}
10 changes: 9 additions & 1 deletion src/AppBundle/PullRequests/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,26 @@ public function removePullRequestValidationComment(PullRequest $pullRequest)
self::TABLE_ERROR,
self::PRESTONBOT_NAME
);

return true;
}

return false;
}

public function removeCommitValidationComment(PullRequest $pullRequest)
{
if (0 === $this->getErrorsFromCommits($pullRequest)) {
if (0 === count($this->getErrorsFromCommits($pullRequest))) {
$this->repository->removeCommentsIfExists(
$pullRequest,
self::COMMIT_ERROR,
self::PRESTONBOT_NAME
);

return true;
}

return false;
}

/**
Expand Down
21 changes: 13 additions & 8 deletions src/AppBundle/PullRequests/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AppBundle\PullRequests;

use Github\Exception\RuntimeException;
use Github\Api\Issue\Comments as KnpCommentsApi;
use AppBundle\Search\Repository as SearchRepository;
use Lpdigital\Github\Entity\Comment;
Expand All @@ -12,7 +13,7 @@
* As GitHub consider pull requests as specific issues
* don't be surprised too much by the produced repository.
*/
class Repository
class Repository implements RepositoryInterface
{
private $searchRepository;
private $knpCommentsApi;
Expand Down Expand Up @@ -64,13 +65,17 @@ public function findAllWithLabel($label, $base = 'develop')

public function getComments(PullRequest $pullRequest)
{
$commentsApi = $this->knpCommentsApi
->all(
$this->repositoryUsername,
$this->repositoryName,
$pullRequest->getNumber()
)
;
try {
$commentsApi = $this->knpCommentsApi
->all(
$this->repositoryUsername,
$this->repositoryName,
$pullRequest->getNumber()
)
;
} catch (RuntimeException $e) {
$commentsApi = [];
}

$comments = [];
foreach ($commentsApi as $comment) {
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/PullRequests/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ public function getCommentsByExpressionFrom(
* @param PullRequest the pull request
* @param $pattern expression to filter comments in CommentApi
*/
public function removeCommentsIfExists(PullRequest $pullRequest, $pattern);
public function removeCommentsIfExists(PullRequest $pullRequest, $pattern, $userLogin);
}
59 changes: 59 additions & 0 deletions tests/AppBundle/Comments/FakeCommentApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace tests\AppBundle\Comments;

use Github\Api\Issue\Comments as KnpCommentApi;
use AppBundle\Comments\CommentApiInterface;
use Lpdigital\Github\Entity\PullRequest;
use Twig_Environment;

/**
* Responsible of comments publication on repository.
*/
class FakeCommentApi implements CommentApiInterface
{
/**
* @var KnpCommentApi
*/
private $knpCommentApi;

/**
* @var string
*/
private $repositoryUsername;

/**
* @var string
*/
private $repositoryName;

/**
* @var Twig_Environment
*/
private $twig;

public function __construct(KnpCommentApi $knpCommentApi, $repositoryUsername, $repositoryName, Twig_Environment $twig)
{
$this->knpCommentApi = $knpCommentApi;
$this->repositoryUsername = $repositoryUsername;
$this->repositoryName = $repositoryName;
$this->twig = $twig;
}

public function send(PullRequest $pullRequest, $comment)
{
return true;
}

public function sendWithTemplate(PullRequest $pullRequest, $templateName, $params)
{
$comment = $this->twig->render($templateName, $params);

return $comment;
}

public function remove($commentId)
{
return true;
}
}
10 changes: 10 additions & 0 deletions tests/AppBundle/Controller/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public function getTests()
'wrong_repository.pull_request.json',
[],
];
$tests['Pull request synchronize'] = [
'pull_request',
'pull_request.synchronize.json',
[
[
'event' => 'pr_edited',
'action' => 'preston validation commit comment removed',
],
],
];

return $tests;
}
Expand Down
133 changes: 0 additions & 133 deletions tests/AppBundle/PullRequests/FakeListener.php

This file was deleted.

Loading

0 comments on commit e11575b

Please sign in to comment.