-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGitlabCommenter.php
More file actions
145 lines (111 loc) · 5.21 KB
/
Copy pathGitlabCommenter.php
File metadata and controls
145 lines (111 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Danger\Platform\Gitlab;
use Danger\Config;
use Danger\Renderer\HTMLRenderer;
use Gitlab\Client;
use Gitlab\ResultPager;
class GitlabCommenter
{
public const NOTE_ANCHOR = '#note_';
public function __construct(private Client $client)
{
}
public function postNote(string $projectIdentifier, int $prId, string $body, Config $config, string $baseUrl): string
{
$noteIds = $this->getRelevantNoteIds($projectIdentifier, $prId);
if ($config->getUpdateCommentMode() === Config::UPDATE_COMMENT_MODE_REPLACE) {
foreach ($noteIds as $relevantNoteId) {
$this->client->mergeRequests()->removeNote($projectIdentifier, $prId, $relevantNoteId);
}
/** @var array{'id': string} $note */
$note = $this->client->mergeRequests()->addNote($projectIdentifier, $prId, $body);
return $baseUrl . self::NOTE_ANCHOR . $note['id'];
}
if (\count($noteIds) === 0) {
/** @var array{'id': string} $note */
$note = $this->client->mergeRequests()->addNote($projectIdentifier, $prId, $body);
return $baseUrl . self::NOTE_ANCHOR . $note['id'];
}
$noteId = array_pop($noteIds);
$this->client->mergeRequests()->updateNote($projectIdentifier, $prId, $noteId, $body);
foreach ($noteIds as $relevantNoteId) {
$this->client->mergeRequests()->removeNote($projectIdentifier, $prId, $relevantNoteId);
}
return $baseUrl . self::NOTE_ANCHOR . $noteId;
}
public function postThread(string $projectIdentifier, int $prId, string $body, Config $config, string $baseUrl): string
{
$threadIds = $this->getRelevantThreadIds($projectIdentifier, $prId);
if ($config->getUpdateCommentMode() === Config::UPDATE_COMMENT_MODE_REPLACE) {
foreach ($threadIds as $threadId) {
$this->client->mergeRequests()->removeDiscussionNote($projectIdentifier, $prId, $threadId['threadId'], $threadId['noteId']);
}
/** @var array{'notes': array{'id': string}[]} $thread */
$thread = $this->client->mergeRequests()->addDiscussion($projectIdentifier, $prId, ['body' => $body]);
return $baseUrl . self::NOTE_ANCHOR . $thread['notes'][0]['id'];
}
if ($threadIds !== []) {
$foundThread = $threadIds[0];
$this->client->mergeRequests()->updateDiscussionNote($projectIdentifier, $prId, $foundThread['threadId'], $foundThread['noteId'], ['body' => $body]);
if ($foundThread['noteBody'] !== $body) {
$this->client->mergeRequests()->updateDiscussionNote($projectIdentifier, $prId, $foundThread['threadId'], $foundThread['noteId'], ['resolved' => false]);
}
return $baseUrl . self::NOTE_ANCHOR . $foundThread['noteId'];
}
/** @var array{'notes': array{'id': string}[]} $thread */
$thread = $this->client->mergeRequests()->addDiscussion($projectIdentifier, $prId, ['body' => $body]);
return $baseUrl . self::NOTE_ANCHOR . $thread['notes'][0]['id'];
}
public function removeNote(string $projectIdentifier, int $prId): void
{
foreach ($this->getRelevantNoteIds($projectIdentifier, $prId) as $relevantNoteId) {
$this->client->mergeRequests()->removeNote($projectIdentifier, $prId, $relevantNoteId);
}
}
public function removeThread(string $projectIdentifier, int $prId): void
{
foreach ($this->getRelevantThreadIds($projectIdentifier, $prId) as $threadId) {
$this->client->mergeRequests()->removeDiscussionNote($projectIdentifier, $prId, $threadId['threadId'], $threadId['noteId']);
}
}
/**
* @return int[]
*/
public function getRelevantNoteIds(string $projectIdentifier, int $prId): array
{
$pager = new ResultPager($this->client, 100);
/** @var list<array{id: int, system: bool, body: string}> $notes */
$notes = $pager->fetchAll($this->client->mergeRequests(), 'showNotes', [$projectIdentifier, $prId]);
$ids = [];
foreach ($notes as $note) {
if ($note['system']) {
continue;
}
if (str_contains($note['body'], HTMLRenderer::MARKER)) {
$ids[] = $note['id'];
}
}
return $ids;
}
/**
* @return array{'threadId': string, 'noteId': int, 'noteBody': string}[]
*/
private function getRelevantThreadIds(string $projectIdentifier, int $prId): array
{
$pager = new ResultPager($this->client, 100);
/** @var list<array{id: string, notes: list<array{id: int, body: string}>}> $threads */
$threads = $pager->fetchAll($this->client->mergeRequests(), 'showDiscussions', [$projectIdentifier, $prId]);
$ids = [];
foreach ($threads as $thread) {
if (str_contains($thread['notes'][0]['body'], HTMLRenderer::MARKER)) {
$ids[] = [
'threadId' => $thread['id'],
'noteId' => $thread['notes'][0]['id'],
'noteBody' => $thread['notes'][0]['body'],
];
}
}
return $ids;
}
}