Skip to content

Commit b90495d

Browse files
committed
feat: #22 update wiki title
1 parent 9a7679a commit b90495d

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

app/Coding.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,23 @@ public function getWiki(string $token, string $projectName, int $id, int $versio
186186
$result = json_decode($response->getBody(), true);
187187
return $result['Response']['Data'];
188188
}
189+
190+
public function updateWikiTitle(string $token, string $projectName, int $id, string $title)
191+
{
192+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
193+
'headers' => [
194+
'Accept' => 'application/json',
195+
'Authorization' => "token ${token}",
196+
'Content-Type' => 'application/json'
197+
],
198+
'json' => [
199+
'Action' => 'ModifyWikiTitle',
200+
'ProjectName' => $projectName,
201+
'Iid' => $id,
202+
'Title' => $title,
203+
],
204+
]);
205+
$result = json_decode($response->getBody(), true);
206+
return $result['Response']['Data']['Title'] == $title;
207+
}
189208
}

app/Commands/WikiImportCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ private function handleConfluenceHtml(): int
183183
private function uploadConfluencePages(string $dataPath, array $tree, array $titles, int $parentId = 0): void
184184
{
185185
foreach ($tree as $page => $subPages) {
186-
$this->info('标题:' . $titles[$page]);
186+
$title = $titles[$page];
187+
$this->info('标题:' . $title);
187188
$markdown = $this->confluence->htmlFile2Markdown($dataPath . $page);
188189
$mdFilename = substr($page, 0, -5) . '.md';
189190
$zipFilePath = $this->coding->createMarkdownZip($markdown, $dataPath, $mdFilename);
@@ -210,6 +211,7 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
210211
}
211212
if ($jobStatus['Status'] == 'success') {
212213
$wikiId = intval($jobStatus['Iids'][0]);
214+
$this->coding->updateWikiTitle($this->codingToken, $this->codingProjectUri, $wikiId, $title);
213215
}
214216
break;
215217
}

tests/Feature/WikiImportCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public function testHandleConfluenceHtmlSuccess()
166166
file_get_contents($this->dataDir . 'coding/' . 'DescribeImportJobStatusResponse.json'),
167167
true
168168
)['Response']['Data']);
169+
$mock->shouldReceive('updateWikiTitle')->times(4)->andReturn(true);
169170

170171
$this->artisan('wiki:import')
171172
->expectsQuestion('数据来源?', 'Confluence')

tests/Unit/CodingTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,38 @@ public function testGetWiki()
241241
$result = $coding->getWiki($codingToken, $codingProjectUri, $id, $version);
242242
$this->assertEquals(json_decode($responseBody, true)['Response']['Data'], $result);
243243
}
244+
245+
public function testUpdateWikiTitle()
246+
{
247+
$responseBody = file_get_contents($this->dataDir . 'coding/ModifyWikiTitleResponse.json');
248+
$codingToken = $this->faker->md5;
249+
$codingProjectUri = $this->faker->slug;
250+
$id = $this->faker->randomNumber();
251+
$title = 'new title';
252+
253+
$clientMock = $this->getMockBuilder(Client::class)->getMock();
254+
$clientMock->expects($this->once())
255+
->method('request')
256+
->with(
257+
'POST',
258+
'https://e.coding.net/open-api',
259+
[
260+
'headers' => [
261+
'Accept' => 'application/json',
262+
'Authorization' => "token ${codingToken}",
263+
'Content-Type' => 'application/json'
264+
],
265+
'json' => [
266+
'Action' => 'ModifyWikiTitle',
267+
'ProjectName' => $codingProjectUri,
268+
'Iid' => $id,
269+
'Title' => $title,
270+
],
271+
]
272+
)
273+
->willReturn(new Response(200, [], $responseBody));
274+
$coding = new Coding($clientMock);
275+
$result = $coding->updateWikiTitle($codingToken, $codingProjectUri, $id, $title);
276+
$this->assertTrue($result);
277+
}
244278
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Response" : {
3+
"Data" : {
4+
"HistoriesCount" : 2,
5+
"HistoryId" : 2756351,
6+
"Id" : 1362209,
7+
"Iid" : 148,
8+
"LastVersion" : 2,
9+
"Path" : "148",
10+
"Title" : "new title",
11+
"UpdatedAt" : 1626075508079,
12+
"VisibleRange" : "INHERIT"
13+
},
14+
"RequestId" : "0509b996-3b9f-8d7f-8c2f-03613327bb1c"
15+
}
16+
}

0 commit comments

Comments
 (0)