Skip to content

Commit e4a398c

Browse files
committed
feat: #8 wiki import confluence
1 parent a38306a commit e4a398c

13 files changed

+1128
-308
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
env:
2626
XDEBUG_MODE: coverage
2727
with:
28-
args: ./vendor/bin/pest --coverage-clover coverage.xml
28+
args: ./vendor/bin/phpunit --coverage-clover coverage.xml --coverage-filter src/ tests/
2929

3030
- name: codecov
3131
uses: codecov/codecov-action@v1

app/Coding.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use GuzzleHttp\Client;
6+
7+
class Coding
8+
{
9+
private Client $client;
10+
11+
public function __construct(Client $client)
12+
{
13+
$this->client = $client;
14+
}
15+
16+
public function createWiki($token, $projectName, $data)
17+
{
18+
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
19+
'headers' => [
20+
'Accept' => 'application/json',
21+
'Authorization' => "token ${token}",
22+
'Content-Type' => 'application/json'
23+
],
24+
'json' => array_merge([
25+
'Action' => 'CreateWiki',
26+
'ProjectName' => $projectName,
27+
], $data),
28+
]);
29+
return json_decode($response->getBody(), true);
30+
}
31+
}

app/Commands/WikiImportCommand.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use App\Coding;
6+
use Confluence\Content;
7+
use Illuminate\Support\Str;
8+
use LaravelFans\Confluence\Facades\Confluence;
9+
use LaravelZero\Framework\Commands\Command;
10+
11+
class WikiImportCommand extends Command
12+
{
13+
/**
14+
* The signature of the command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'wiki:import
19+
{--coding_import_provider= : 数据来源,如 Confluence、MediaWiki}
20+
{--confluence_base_uri= : Confluence API URL,如 http://localhost:8090/confluence/rest/api/}
21+
{--confluence_username=}
22+
{--confluence_password=}
23+
{--coding_token= : CODING 令牌}
24+
{--coding_team_domain= : CODING 团队域名,如 xxx.coding.net 即填写 xxx}
25+
{--coding_project_uri= : CODING 项目标识,如 xxx.coding.net/p/yyy 即填写 yyy}
26+
';
27+
28+
/**
29+
* The description of the command.
30+
*
31+
* @var string
32+
*/
33+
protected $description = 'import wiki from confluence and so on';
34+
35+
/**
36+
* Execute the console command.
37+
*
38+
* @return mixed
39+
*/
40+
public function handle(Coding $coding)
41+
{
42+
if ($this->option('coding_import_provider')) {
43+
$provider = $this->option('coding_import_provider');
44+
} else {
45+
$provider = config('coding.import.provider') ?? $this->choice(
46+
'数据来源?',
47+
['Confluence', 'MediaWiki'],
48+
0
49+
);
50+
}
51+
if ($provider != 'Confluence') {
52+
$this->info('TODO');
53+
return;
54+
}
55+
56+
if ($this->option('confluence_base_uri')) {
57+
$baseUri = $this->option('confluence_base_uri');
58+
} else {
59+
$baseUri = config('confluence.base_uri') ?? $this->ask(
60+
$provider . ' API 链接:',
61+
'http://9.134.190.26:8090/rest/api/'
62+
);
63+
}
64+
config(['confluence.base_uri' => $baseUri]);
65+
66+
if ($this->option('confluence_username')) {
67+
$username = $this->option('confluence_username');
68+
} else {
69+
$username = config('confluence.username') ?? $this->ask($provider . ' 账号:', 'admin');
70+
}
71+
if ($this->option('confluence_password')) {
72+
$password = $this->option('confluence_password');
73+
} else {
74+
$password = config('confluence.password') ?? $this->ask($provider . ' 密码:', 'Demo.2021');
75+
}
76+
config(['confluence.auth' => [$username, $password]]);
77+
78+
if ($this->option('coding_token')) {
79+
$codingToken = $this->option('coding_token');
80+
} else {
81+
$codingToken = config('coding.token') ?? $this->ask(
82+
'CODING Token:',
83+
'793fe7845effd2596f2da38562336523b58585fb'
84+
);
85+
}
86+
87+
if ($this->option('coding_team_domain')) {
88+
$codingTeamDomain = $this->option('coding_team_domain');
89+
} else {
90+
$codingTeamDomain = config('coding.team_domain') ?? $this->ask('CODING 团队域名:', 'codes-farm');
91+
}
92+
$codingTeamDomain = str_replace('.coding.net', '', $codingTeamDomain);
93+
94+
if ($this->option('coding_project_uri')) {
95+
$codingProjectUri = $this->option('coding_project_uri');
96+
} else {
97+
$codingProjectUri = config('coding.project_uri') ?? $this->ask('CODING 项目标识:', 'laravel-demo');
98+
}
99+
100+
$data = Confluence::resource(Content::class)->index();
101+
$this->info("已获得 ${data['size']} 条数据");
102+
if ($data['size'] == 0) {
103+
return;
104+
}
105+
$this->info("开始导入 CODING:");
106+
foreach ($data['results'] as $result) {
107+
$content = Confluence::resource(Content::class)->show($result['id'], ['expand' => 'body.storage']);
108+
$result = $coding->createWiki($codingToken, $codingProjectUri, [
109+
'Title' => $content['title'],
110+
'Content' => $content['body']['storage']['value'],
111+
'ParentIid' => 0,
112+
]);
113+
$path = $result['Response']['Data']['Path'];
114+
$this->info("https://${codingTeamDomain}.coding.net/p/${codingProjectUri}/wiki/${path}");
115+
}
116+
}
117+
}

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.3|^8.0",
19+
"php": "^8.0",
20+
"ext-json": "*",
21+
"laravel-fans/confluence": "^0.1.1",
2022
"laravel-zero/framework": "^8.8"
2123
},
2224
"require-dev": {
25+
"fakerphp/faker": "^1.14",
2326
"mockery/mockery": "^1.4.3",
24-
"pestphp/pest": "^1.3"
27+
"phpunit/phpunit": "^9.5"
2528
},
2629
"autoload": {
2730
"psr-4": {
@@ -38,7 +41,7 @@
3841
"sort-packages": true,
3942
"optimize-autoloader": true
4043
},
41-
"minimum-stability": "dev",
44+
"minimum-stability": "stable",
4245
"prefer-stable": true,
4346
"bin": ["coding"]
4447
}

0 commit comments

Comments
 (0)