Skip to content

Commit e9e1499

Browse files
committed
update examples
1 parent a72ce59 commit e9e1499

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

docs/examples/OpenIssue.php

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22

33
namespace App\Main\Plugin;
44

5-
use PhpToken;
65
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
76
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\Report;
87
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\FixMeComment;
98
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\TodoComment;
109
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
10+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileEditor;
1111
use SavinMikhail\CommentsDensity\Plugin\PluginInterface;
1212
use Symfony\Component\HttpClient\HttpClient;
1313
use Symfony\Contracts\HttpClient\HttpClientInterface;
1414

1515
final 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
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Main\Plugin;
4+
5+
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
6+
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\Report;
7+
use SavinMikhail\CommentsDensity\AnalyzeComments\Comments\RegularComment;
8+
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
9+
use SavinMikhail\CommentsDensity\AnalyzeComments\File\FileEditor;
10+
use SavinMikhail\CommentsDensity\Plugin\PluginInterface;
11+
12+
final readonly class RemoveRelaxComment implements PluginInterface
13+
{
14+
public function handle(Report $report, Config $config): void
15+
{
16+
foreach ($report->comments as $comment) {
17+
if ($comment->commentType !== RegularComment::NAME) {
18+
continue;
19+
}
20+
21+
if (!str_contains($comment->content, '// relax')) {
22+
continue;
23+
}
24+
25+
$newComment = new CommentDTO(
26+
commentType: $comment->commentType,
27+
commentTypeColor: $comment->commentTypeColor,
28+
file: $comment->file,
29+
line: $comment->line,
30+
content: '',
31+
);
32+
(new FileEditor())->updateCommentInFile($newComment);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)