22
33namespace App \Main \Plugin ;
44
5- use PhpToken ;
65use SavinMikhail \CommentsDensity \AnalyzeComments \Analyzer \DTO \Output \CommentDTO ;
76use SavinMikhail \CommentsDensity \AnalyzeComments \Analyzer \DTO \Output \Report ;
87use SavinMikhail \CommentsDensity \AnalyzeComments \Comments \FixMeComment ;
98use SavinMikhail \CommentsDensity \AnalyzeComments \Comments \TodoComment ;
109use SavinMikhail \CommentsDensity \AnalyzeComments \Config \DTO \Config ;
10+ use SavinMikhail \CommentsDensity \AnalyzeComments \File \FileEditor ;
1111use SavinMikhail \CommentsDensity \Plugin \PluginInterface ;
1212use Symfony \Component \HttpClient \HttpClient ;
1313use Symfony \Contracts \HttpClient \HttpClientInterface ;
1414
1515final readonly class OpenIssue implements PluginInterface
1616{
17- private const YOUTRACK_URL = 'https://yt ' ;
18- private const AUTHORIZATION_TOKEN = '' ;
17+ private const YOUTRACK_URL = 'https://yt.kr.digital ' ;
18+ private const AUTHORIZATION_TOKEN = 'perm:bXNhdmlu.NjctMjg=.cAwxwx1bebsCApr3yoyU1OJYaHiPQh ' ;
1919 private const PROJECT_ID = '59-178 ' ;
2020 private const STAGE = 'Второй этап ' ;
2121 private const BRANCH_NAME = 'develop ' ;
22- private const GITLAB_PROJECT_URL = 'https://gitlab/ backend ' ;
22+ private const GITLAB_PROJECT_URL = 'https://gitlab.kr.digital/dit/sphere/sphere- backend ' ;
2323
2424 public function handle (Report $ report , Config $ config ): void
2525 {
2626 $ httpClient = HttpClient::create ();
27-
2827 foreach ($ report ->comments as $ comment ) {
2928 if (!in_array ($ comment ->commentType , [TodoComment::NAME , FixMeComment::NAME ], true )) {
3029 continue ;
@@ -37,7 +36,14 @@ public function handle(Report $report, Config $config): void
3736 $ draftId = $ this ->createDraft ($ httpClient , $ comment );
3837 $ issueId = $ this ->createIssueFromDraft ($ httpClient , $ draftId );
3938 $ issueUrl = self ::YOUTRACK_URL . '/issue/ ' . $ issueId ;
40- $ this ->updateCommentInFile ($ comment , $ issueUrl );
39+ $ newComment = new CommentDTO (
40+ commentType: $ comment ->commentType ,
41+ commentTypeColor: $ comment ->commentTypeColor ,
42+ file: $ comment ->file ,
43+ line: $ comment ->line ,
44+ content: rtrim ($ comment ->content ) . " ( $ issueUrl) " ,
45+ );
46+ (new FileEditor ())->updateCommentInFile ($ newComment );
4147 }
4248 }
4349
@@ -93,40 +99,11 @@ private function createIssueFromDraft(HttpClientInterface $httpClient, string $d
9399 ]
94100 );
95101 $ response = $ response ->toArray ();
96- return $ response ['idReadable ' ]; // looks like backend -287
102+ return $ response ['idReadable ' ]; // looks like mossphere -287
97103 }
98104
99105 private function hasIssueUrl (string $ commentContent ): bool
100106 {
101107 return (bool )preg_match ('/https:\/\/yt\.kr\.digital\/issue\/\w+-\d+/ ' , $ commentContent );
102108 }
103-
104- private function updateCommentInFile (CommentDTO $ comment , string $ issueUrl ): void
105- {
106- $ fileContent = file_get_contents ($ comment ->file );
107- $ tokens = \PhpToken::tokenize ($ fileContent );
108- $ changed = false ;
109- foreach ($ tokens as $ token ) {
110- if ($ token ->line === $ comment ->line && $ token ->is ([T_COMMENT , T_DOC_COMMENT ]) ) {
111- $ token ->text = rtrim ($ token ->text ) . " ( $ issueUrl) " ;
112- $ changed = true ;
113- }
114- }
115- if (!$ changed ) {
116- return ;
117- }
118- $ this ->save ($ comment ->file , $ tokens );
119- }
120-
121- /**
122- * @param PhpToken[] $tokens
123- */
124- private function save (string $ file , array $ tokens ): void
125- {
126- $ content = implode ('' , array_map (static fn (\PhpToken $ token ) => $ token ->text , $ tokens ));
127- $ res = file_put_contents ($ file , $ content );
128- if ($ res === false ) {
129- throw new \Exception ('failed to write to file: ' . $ file );
130- }
131- }
132109}
0 commit comments