Skip to content

Commit 3ac365b

Browse files
authored
Run a search to verify that this is the first PR of the user (#124)
1 parent c9b68e4 commit 3ac365b

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

config/services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ services:
6969
factory: ['@Github\Client', api]
7070
arguments: [pullRequest]
7171

72+
Github\Api\Repo:
73+
factory: ['@Github\Client', api]
74+
arguments: [repo]
75+
7276
Github\Api\Search:
7377
factory: ['@Github\Client', api]
7478
arguments: [search]
@@ -87,6 +91,7 @@ services:
8791
App\Api\Issue\IssueApi: '@App\Api\Issue\GithubIssueApi'
8892
App\Api\Label\LabelApi: '@App\Api\Label\GithubLabelApi'
8993
App\Api\Milestone\MilestoneApi: '@App\Api\Milestone\GithubMilestoneApi'
94+
App\Api\PullRequest\PullRequestApi: '@App\Api\PullRequest\GithubPullRequestApi'
9095
App\Api\Status\StatusApi: '@App\Api\Status\GitHubStatusApi'
9196

9297
App\Service\RepositoryProvider:

config/services_test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ services:
66
App\Api\Issue\IssueApi: '@App\Api\Issue\NullIssueApi'
77
App\Api\Label\LabelApi: '@App\Api\Label\StaticLabelApi'
88
App\Api\Milestone\MilestoneApi: '@App\Api\Milestone\StaticMilestoneApi'
9+
App\Api\PullRequest\PullRequestApi: '@App\Api\PullRequest\GithubPullRequestApi'
910
App\Api\Status\StatusApi: '@App\Api\Status\NullStatusApi'

src/Api/PullRequest/GithubPullRequestApi.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Model\Repository;
66
use Github\Api\PullRequest;
77
use Github\Api\Repo;
8+
use Github\Api\Search;
89

910
/**
1011
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -15,14 +16,16 @@ class GithubPullRequestApi implements PullRequestApi
1516
* @var Repo
1617
*/
1718
private $github;
18-
private $botUsername;
1919
private $pullRequest;
20+
private $search;
21+
private $botUsername;
2022

21-
public function __construct(Repo $github, PullRequest $pullRequest, string $botUsername)
23+
public function __construct(Repo $github, PullRequest $pullRequest, Search $search, string $botUsername)
2224
{
2325
$this->github = $github;
24-
$this->botUsername = $botUsername;
2526
$this->pullRequest = $pullRequest;
27+
$this->search = $search;
28+
$this->botUsername = $botUsername;
2629
}
2730

2831
public function show(Repository $repository, $number): array
@@ -41,4 +44,11 @@ public function findReviewer(Repository $repository, $number, string $type)
4144
'type' => $type,
4245
]);
4346
}
47+
48+
public function getAuthorCount(Repository $repository, string $author): int
49+
{
50+
$result = $this->search->issues(sprintf('is:pr repo:%s author:%s', $repository->getFullName(), $author));
51+
52+
return $result['total_count'];
53+
}
4454
}

src/Api/PullRequest/NullPullRequestApi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ public function show(Repository $repository, $number): array
1919
public function findReviewer(Repository $repository, $number, string $type)
2020
{
2121
}
22+
23+
public function getAuthorCount(Repository $repository, string $author): int
24+
{
25+
return 1;
26+
}
2227
}

src/Api/PullRequest/PullRequestApi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ interface PullRequestApi
1717
public function show(Repository $repository, $number): array;
1818

1919
public function findReviewer(Repository $repository, $number, string $type);
20+
21+
public function getAuthorCount(Repository $repository, string $author): int;
2022
}

src/Subscriber/WelcomeFirstTimeContributorSubscriber.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Subscriber;
44

55
use App\Api\Issue\IssueApi;
6+
use App\Api\PullRequest\PullRequestApi;
67
use App\Event\GitHubEvent;
78
use App\GitHubEvents;
89
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -13,10 +14,12 @@
1314
class WelcomeFirstTimeContributorSubscriber implements EventSubscriberInterface
1415
{
1516
private $commentsApi;
17+
private $pullRequestApi;
1618

17-
public function __construct(IssueApi $commentsApi)
19+
public function __construct(IssueApi $commentsApi, PullRequestApi $pullRequestApi)
1820
{
1921
$this->commentsApi = $commentsApi;
22+
$this->pullRequestApi = $pullRequestApi;
2023
}
2124

2225
public function onPullRequest(GitHubEvent $event)
@@ -32,6 +35,11 @@ public function onPullRequest(GitHubEvent $event)
3235
}
3336

3437
$repository = $event->getRepository();
38+
if ($this->pullRequestApi->getAuthorCount($repository, $data['sender']['login']) > 1) {
39+
// This users has made pull requests before
40+
return;
41+
}
42+
3543
$pullRequestNumber = $data['pull_request']['number'];
3644
$defaultBranch = $data['repository']['default_branch'];
3745
$this->commentsApi->commentOnIssue($repository, $pullRequestNumber, <<<TXT

0 commit comments

Comments
 (0)