Skip to content

Commit 3207135

Browse files
Nyholmweaverryan
andauthored
Added welcome message to first time user (#95)
* Added welcome message to first time user * CS fixes * Regsiter github api * Added a suggestion * Added link * CS * Updated message * Use the same message on symfony-docs * remove listener for symfony-docs * Apply suggestions from code review Co-authored-by: Ryan Weaver <weaverryan+github@gmail.com> * minor * Added hint about bugfixes will be merged up Co-authored-by: Ryan Weaver <weaverryan+github@gmail.com>
1 parent a89fc24 commit 3207135

9 files changed

+627
-0
lines changed

config/services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ parameters:
88
- 'App\Subscriber\BugLabelNewIssueSubscriber'
99
- 'App\Subscriber\AutoLabelFromContentSubscriber'
1010
- 'App\Subscriber\MilestoneNewPRSubscriber'
11+
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
1112
secret: '%env(SYMFONY_SECRET)%'
1213

1314
symfony/symfony-docs:
@@ -31,6 +32,7 @@ parameters:
3132
- 'App\Subscriber\BugLabelNewIssueSubscriber'
3233
- 'App\Subscriber\AutoLabelFromContentSubscriber'
3334
- 'App\Subscriber\MilestoneNewPRSubscriber'
35+
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
3436

3537
services:
3638
_defaults:
@@ -59,7 +61,11 @@ services:
5961
Github\Api\Issue\Milestones:
6062
factory: ['@Github\Api\Issue', milestones]
6163

64+
Github\Api\Issue\Comments:
65+
factory: ['@Github\Api\Issue', comments]
66+
6267
App\Issues\StatusApi: '@App\Issues\GitHub\GitHubStatusApi'
68+
App\Issues\CommentsApiInterface: '@App\Issues\GitHub\GithubCommentsApi'
6369

6470
App\Repository\Provider\InMemoryRepositoryProvider:
6571
arguments: [ '%repositories%' ]

config/services_test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ services:
55

66
App\Issues\GitHub\CachedLabelsApi:
77
class: App\Tests\Service\Issues\Github\FakedCachedLabelApi
8+
9+
App\Issues\StatusApi: '@App\Issues\NullStatusApi'
10+
App\Issues\CommentsApiInterface: '@App\Issues\NullCommentsApi'

src/Issues/CommentsApiInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Issues;
4+
5+
use App\Repository\Repository;
6+
7+
interface CommentsApiInterface
8+
{
9+
public function commentOnIssue(Repository $repository, $issueNumber, $commentBody);
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Issues\GitHub;
4+
5+
use App\Issues\CommentsApiInterface;
6+
use App\Repository\Repository;
7+
use Github\Api\Issue\Comments;
8+
9+
class GithubCommentsApi implements CommentsApiInterface
10+
{
11+
private $issueCommentApi;
12+
13+
public function __construct(Comments $issueCommentApi)
14+
{
15+
$this->issueCommentApi = $issueCommentApi;
16+
}
17+
18+
/**
19+
* This will comment on both Issues and Pull Requests.
20+
*/
21+
public function commentOnIssue(Repository $repository, $issueNumber, $commentBody)
22+
{
23+
$this->issueCommentApi->create(
24+
$repository->getVendor(),
25+
$repository->getName(),
26+
$issueNumber,
27+
['body' => $commentBody]
28+
);
29+
}
30+
}

src/Issues/NullCommentsApi.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Issues;
4+
5+
use App\Repository\Repository;
6+
7+
class NullCommentsApi implements CommentsApiInterface
8+
{
9+
public function commentOnIssue(Repository $repository, $issueNumber, $commentBody)
10+
{
11+
}
12+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace App\Subscriber;
4+
5+
use App\Event\GitHubEvent;
6+
use App\GitHubEvents;
7+
use App\Issues\CommentsApiInterface;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
/**
11+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
12+
*/
13+
class WelcomeFirstTimeContributorSubscriber implements EventSubscriberInterface
14+
{
15+
private $commentsApi;
16+
17+
public function __construct(CommentsApiInterface $commentsApi)
18+
{
19+
$this->commentsApi = $commentsApi;
20+
}
21+
22+
public function onPullRequest(GitHubEvent $event)
23+
{
24+
$data = $event->getData();
25+
if ('opened' !== $data['action']) {
26+
return;
27+
}
28+
29+
if ('NONE' !== ($data['pull_request']['author_association'] ?? '')) {
30+
return;
31+
}
32+
33+
$repository = $event->getRepository();
34+
$pullRequestNumber = $data['pull_request']['number'];
35+
$defaultBranch = $data['repository']['default_branch'];
36+
$this->commentsApi->commentOnIssue($repository, $pullRequestNumber, <<<TXT
37+
Hey!
38+
39+
I see that this is your first PR. That is great! Welcome!
40+
41+
Symfony has a [contribution guide](https://symfony.com/doc/current/contributing/index.html) which I suggest you to read.
42+
43+
In short:
44+
- Always add tests
45+
- Keep backward compatibility (see https://symfony.com/bc).
46+
- Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
47+
- Features and deprecations must be submitted against the $defaultBranch branch.
48+
49+
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.
50+
51+
When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
52+
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.
53+
54+
I am going to sit back now and wait for the reviews.
55+
56+
Cheers!
57+
58+
Carsonbot
59+
TXT
60+
);
61+
62+
$event->setResponseData([
63+
'pull_request' => $pullRequestNumber,
64+
'new_contributor' => true,
65+
]);
66+
}
67+
68+
public static function getSubscribedEvents()
69+
{
70+
return [
71+
GitHubEvents::PULL_REQUEST => 'onPullRequest',
72+
];
73+
}
74+
}

tests/Controller/WebhookControllerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function getTests()
7373
'issues.labeled.feature.json',
7474
['issue' => 2, 'status_change' => null],
7575
],
76+
'Welcome first users' => [
77+
'pull_request',
78+
'pull_request.new_contributor.json',
79+
['pull_request' => 4, 'status_change' => 'needs_review', 'pr_labels' => [], 'new_contributor' => true],
80+
],
7681
];
7782
}
7883
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Request URL: https://nyholm.tech:
2+
Request method: POST
3+
Accept: */*
4+
content-type: application/x-www-form-urlencoded
5+
User-Agent: GitHub-Hookshot/888f7f5
6+
X-GitHub-Delivery: 47beb300-1d19-11eb-818f-ace469801400
7+
X-GitHub-Event: pull_request
8+
X-GitHub-Hook-ID: 259877073
9+
X-GitHub-Hook-Installation-Target-ID: 309178226
10+
X-GitHub-Hook-Installation-Target-Type: repository

0 commit comments

Comments
 (0)