55use App \Event \GitHubEvent ;
66use App \GitHubEvents ;
77use App \Issues \GitHub \CachedLabelsApi ;
8+ use App \Repository \Repository ;
89use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
910
1011/**
1112 * Looks at new pull requests and auto-labels based on text.
1213 */
13- class AutoLabelPRFromContentSubscriber implements EventSubscriberInterface
14+ class AutoLabelFromContentSubscriber implements EventSubscriberInterface
1415{
1516 private $ labelsApi ;
1617
@@ -35,14 +36,15 @@ public function onPullRequest(GitHubEvent $event)
3536 if ('opened ' !== $ action = $ data ['action ' ]) {
3637 return ;
3738 }
39+ $ repository = $ event ->getRepository ();
3840
3941 $ prNumber = $ data ['pull_request ' ]['number ' ];
4042 $ prTitle = $ data ['pull_request ' ]['title ' ];
4143 $ prBody = $ data ['pull_request ' ]['body ' ];
4244 $ prLabels = [];
4345
4446 // the PR title usually contains one or more labels
45- foreach ($ this ->extractLabels ($ prTitle ) as $ label ) {
47+ foreach ($ this ->extractLabels ($ prTitle, $ repository ) as $ label ) {
4648 $ prLabels [] = $ label ;
4749 }
4850
@@ -60,26 +62,52 @@ public function onPullRequest(GitHubEvent $event)
6062 $ prLabels [] = 'Deprecation ' ;
6163 }
6264
63- $ this ->labelsApi ->addIssueLabels ($ prNumber , $ prLabels , $ event -> getRepository () );
65+ $ this ->labelsApi ->addIssueLabels ($ prNumber , $ prLabels , $ repository );
6466
6567 $ event ->setResponseData ([
6668 'pull_request ' => $ prNumber ,
6769 'pr_labels ' => $ prLabels ,
6870 ]);
6971 }
7072
71- private function extractLabels ($ prTitle )
73+ public function onIssue (GitHubEvent $ event )
74+ {
75+ $ data = $ event ->getData ();
76+ if ('opened ' !== $ action = $ data ['action ' ]) {
77+ return ;
78+ }
79+ $ repository = $ event ->getRepository ();
80+
81+ $ issueNumber = $ data ['issue ' ]['number ' ];
82+ $ prTitle = $ data ['issue ' ]['title ' ];
83+ $ labels = [];
84+
85+ // the issue title usually contains one or more labels
86+ foreach ($ this ->extractLabels ($ prTitle , $ repository ) as $ label ) {
87+ $ labels [] = $ label ;
88+ }
89+
90+ $ this ->labelsApi ->addIssueLabels ($ issueNumber , $ labels , $ repository );
91+
92+ $ event ->setResponseData ([
93+ 'issue ' => $ issueNumber ,
94+ 'issue_labels ' => $ labels ,
95+ ]);
96+ }
97+
98+ private function extractLabels ($ title , Repository $ repository )
7299 {
73100 $ labels = [];
74101
75102 // e.g. "[PropertyAccess] [RFC] [WIP] Allow custom methods on property accesses"
76- if (preg_match_all ('/\[(?P<labels>.+)\]/U ' , $ prTitle , $ matches )) {
103+ if (preg_match_all ('/\[(?P<labels>.+)\]/U ' , $ title , $ matches )) {
77104 // creates a key=>val array, but the key is lowercased
105+ $ allLabels = $ this ->labelsApi ->getAllLabelsForRepository ($ repository );
78106 $ validLabels = array_combine (
79107 array_map (function ($ s ) {
80108 return strtolower ($ s );
81- }, $ this -> getValidLabels () ),
82- $ this -> getValidLabels ()
109+ }, $ allLabels ),
110+ $ allLabels
83111 );
84112
85113 foreach ($ matches ['labels ' ] as $ label ) {
@@ -95,34 +123,6 @@ private function extractLabels($prTitle)
95123 return $ labels ;
96124 }
97125
98- /**
99- * TODO: get valid labels from the repository via GitHub API.
100- */
101- private function getValidLabels ()
102- {
103- $ realLabels = [
104- 'Asset ' , 'BC Break ' , 'BrowserKit ' , 'Bug ' , 'Cache ' , 'Config ' , 'Console ' ,
105- 'Contracts ' , 'Critical ' , 'CssSelector ' , 'Debug ' , 'DebugBundle ' , 'DependencyInjection ' ,
106- 'Deprecation ' , 'Doctrine ' , 'DoctrineBridge ' , 'DomCrawler ' , 'Dotenv ' ,
107- 'DX ' , 'Enhancement ' , 'ErrorHandler ' , 'EventDispatcher ' , 'ExpressionLanguage ' ,
108- 'Feature ' , 'Filesystem ' , 'Finder ' , 'Form ' , 'FrameworkBundle ' , 'Hack Day ' ,
109- 'HttpClient ' , 'HttpFoundation ' , 'HttpKernel ' , 'Inflector ' , 'Intl ' , 'Ldap ' ,
110- 'Locale ' , 'Lock ' , 'Mailer ' , 'Messenger ' , 'Mime ' , 'MonologBridge ' , 'Notifier ' ,
111- 'OptionsResolver ' , 'Performance ' , 'PhpUnitBridge ' , 'Process ' , 'PropertyAccess ' ,
112- 'PropertyInfo ' , 'ProxyManagerBridge ' , 'RFC ' , 'Routing ' , 'Security ' ,
113- 'SecurityBundle ' , 'Serializer ' , 'Stopwatch ' , 'String ' , 'Templating ' ,
114- 'Translator ' , 'TwigBridge ' , 'TwigBundle ' , 'Uid ' , 'Validator ' , 'VarDumper ' ,
115- 'VarExporter ' , 'WebLink ' , 'WebProfilerBundle ' , 'WebServerBundle ' , 'Workflow ' ,
116- 'Yaml ' ,
117- ];
118-
119- return array_merge (
120- $ realLabels ,
121- // also consider the "aliases" as valid, so they are used
122- array_keys (self ::$ labelAliases )
123- );
124- }
125-
126126 /**
127127 * It fixes common misspellings and aliases commonly used for label names
128128 * (e.g. DI -> DependencyInjection).
@@ -142,6 +142,7 @@ public static function getSubscribedEvents()
142142 {
143143 return [
144144 GitHubEvents::PULL_REQUEST => 'onPullRequest ' ,
145+ GitHubEvents::ISSUES => 'onIssue ' ,
145146 ];
146147 }
147148}
0 commit comments