Skip to content

Commit e4622cd

Browse files
authored
refactor: #40 set project name once
1 parent 44e44bb commit e4622cd

22 files changed

+383
-262
lines changed

.github/workflows/ci.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ jobs:
3838
- name: Acceptance Test
3939
run: ./vendor/bin/phpunit tests/Acceptance
4040
env:
41-
CODING_TOKEN: ${{ secrets.CODING_TOKEN }}
41+
CODING_PERSONAL_TOKEN: ${{ secrets.CODING_PERSONAL_TOKEN }}
4242
CODING_PASSWORD: ${{ secrets.CODING_PASSWORD }}
4343
CODING_USERNAME: ${{ secrets.CODING_USERNAME }}
4444
CODING_TEAM_DOMAIN: ${{ secrets.CODING_TEAM_DOMAIN }}
45+
CODING_PROJECT_TOKEN: ${{ secrets.CODING_PROJECT_TOKEN }}
4546
CODING_PROJECT_NAME: ${{ secrets.CODING_PROJECT_NAME }}
4647
CODING_DEPOT_ID: ${{ secrets.CODING_DEPOT_ID }}
4748

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ composer require coding/sdk
2020

2121
require 'vendor/autoload.php';
2222

23+
use Coding\Client;
2324
use Coding\Iteration;
2425

25-
$iteration = new Iteration('c127894e5a851cef22dc317f882dfb9ca6054321');
26+
$client = new Client();
2627
$projectName = 'project-foo';
28+
$client->setProjectName($projectName);
29+
$client->setProjectToken('c127894e5a851cef22dc317f882dfb9ca6054321');
30+
31+
$iteration = new Iteration($client);
2732
$result = $iteration->create([
28-
'ProjectName' => $projectName,
2933
'Name' => 'Sprint 1',
3034
]);
31-
echo "https://my-team.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n";
35+
$teamDomain = 'my-team';
36+
echo "https://${teamDomain}.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n";
3237
```
3338

3439
## Resources

src/Base.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77

88
abstract class Base
99
{
10-
protected Core $core;
10+
protected Client $client;
1111

12-
public function __construct(string $token = '', Core $core = null)
12+
public function __construct(Client $client = null)
1313
{
14-
$this->core = $core ?? new Core($token);
15-
}
16-
17-
public function setToken(string $token)
18-
{
19-
$this->core->setToken($token);
14+
$this->client = $client ?? new Client();
2015
}
2116

2217
protected function validate(array $data, array $rules)

src/Client.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Coding;
4+
5+
use Coding\Exceptions\ApiError;
6+
use Coding\Exceptions\ValidationException;
7+
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\ClientInterface;
9+
use InvalidArgumentException;
10+
11+
class Client
12+
{
13+
private const API_URL = 'https://e.coding.net/open-api';
14+
15+
protected array $config;
16+
protected string $token;
17+
public bool $usePersonalToken = false;
18+
protected ?ClientInterface $http = null;
19+
20+
public function __construct(array $config = [])
21+
{
22+
$this->config = array_merge([
23+
'api_url' => self::API_URL,
24+
'personal_token' => '',
25+
'project_name' => '',
26+
'project_token' => '',
27+
], $config);
28+
}
29+
30+
public function setHttpClient(ClientInterface $http): void
31+
{
32+
$this->http = $http;
33+
}
34+
35+
public function getHttpClient(): ClientInterface
36+
{
37+
if (null === $this->http) {
38+
$this->http = new GuzzleClient();
39+
}
40+
41+
return $this->http;
42+
}
43+
44+
public function request(string $action, array $data)
45+
{
46+
$params = ['Action' => $action];
47+
$response = $this->getHttpClient()->request('POST', $this->config['api_url'], [
48+
'headers' => [
49+
'Accept' => 'application/json',
50+
'Authorization' => 'token ' . $this->getToken(),
51+
'Content-Type' => 'application/json'
52+
],
53+
'json' => array_merge($params, $data),
54+
]);
55+
$result = json_decode($response->getBody(), true);
56+
if (isset($result['Response']['Error']['Message'])) {
57+
throw new ApiError($result['Response']['Error']['Message']);
58+
}
59+
return $result['Response'];
60+
}
61+
62+
public function requestProjectApi(string $action, array $data = [])
63+
{
64+
if (empty($this->config['project_name'])) {
65+
throw new ValidationException('Need set project name first.');
66+
}
67+
$data['ProjectName'] = $this->config['project_name'];
68+
return $this->request($action, $data);
69+
}
70+
71+
public function setPersonalToken(string $token)
72+
{
73+
$this->config['personal_token'] = $token;
74+
}
75+
76+
public function setProjectName(string $projectName)
77+
{
78+
$this->config['project_name'] = $projectName;
79+
}
80+
81+
public function setProjectToken(string $token)
82+
{
83+
$this->config['project_token'] = $token;
84+
}
85+
86+
private function getToken()
87+
{
88+
if ($this->usePersonalToken) {
89+
if (empty($this->config['personal_token'])) {
90+
throw new InvalidArgumentException('Need set project token first.');
91+
}
92+
return $this->config['personal_token'];
93+
}
94+
return !empty($this->config['project_token']) ?
95+
$this->config['project_token'] : $this->config['personal_token'];
96+
}
97+
}

src/Core.php

-42
This file was deleted.

src/GitBranch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function index(array $data)
1515
'KeyWord' => 'string', // 搜索关键字
1616
]);
1717
$data['DepotId'] = intval($data['DepotId']);
18-
$response = $this->core->request('DescribeGitBranches', $data);
18+
$response = $this->client->requestProjectApi('DescribeGitBranches', $data);
1919
return $response['Branches'];
2020
}
2121
}

src/Issue.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Issue extends Base
1717
public function create(array $data)
1818
{
1919
$this->validate($data, [
20-
'ProjectName' => 'string|required',
2120
'Name' => 'string|required',
2221
'Priority' => [
2322
'string',
@@ -58,34 +57,31 @@ public function create(array $data)
5857
// 自定义属性值列表 Array of IssueCustomFieldForm
5958
'CustomFieldValues' => 'nullable|array',
6059
]);
61-
$response = $this->core->request('CreateIssue', $data);
60+
$response = $this->client->requestProjectApi('CreateIssue', $data);
6261
return $response['Issue'];
6362
}
6463

6564
public function delete(array $data)
6665
{
6766
$this->validate($data, [
68-
'ProjectName' => 'string|required',
6967
'IssueCode' => 'integer|required',
7068
]);
71-
$this->core->request('DeleteIssue', $data);
69+
$this->client->requestProjectApi('DeleteIssue', $data);
7270
return true;
7371
}
7472

7573
public function get(array $data)
7674
{
7775
$this->validate($data, [
78-
'ProjectName' => 'string|required',
7976
'IssueCode' => 'integer|required',
8077
]);
81-
$response = $this->core->request('DescribeIssue', $data);
78+
$response = $this->client->requestProjectApi('DescribeIssue', $data);
8279
return $response['Issue'];
8380
}
8481

8582
public function update(array $data)
8683
{
8784
$this->validate($data, [
88-
'ProjectName' => 'string|required',
8985
'IssueCode' => 'integer',
9086
'ParentCode' => 'nullable|integer',
9187
'Name' => 'nullable|string',
@@ -118,7 +114,7 @@ public function update(array $data)
118114
// 自定义属性值列表 Array of IssueCustomFieldForm
119115
'CustomFieldValues' => 'nullable|array',
120116
]);
121-
$response = $this->core->request('ModifyIssue', $data);
117+
$response = $this->client->requestProjectApi('ModifyIssue', $data);
122118
return $response['Issue'];
123119
}
124120
}

src/Iteration.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,45 @@ class Iteration extends Base
77
public function create(array $data)
88
{
99
$this->validate($data, [
10-
'ProjectName' => 'string|required',
1110
'Name' => 'string|required',
1211
'Goal' => 'nullable|string',
1312
'Assignee' => 'nullable|integer',
1413
'StartAt' => 'nullable|date_format:Y-m-d',
1514
'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt',
1615
]);
17-
$response = $this->core->request('CreateIteration', $data);
16+
$response = $this->client->requestProjectApi('CreateIteration', $data);
1817
return $response['Iteration'];
1918
}
2019

2120
public function get(array $data)
2221
{
2322
$this->validate($data, [
24-
'ProjectName' => 'string|required',
2523
'IterationCode' => 'integer|required',
2624
]);
27-
$response = $this->core->request('DescribeIteration', $data);
25+
$response = $this->client->requestProjectApi('DescribeIteration', $data);
2826
return $response['Iteration'];
2927
}
3028

3129
public function update(array $data)
3230
{
3331
$this->validate($data, [
34-
'ProjectName' => 'string|required',
3532
'IterationCode' => 'integer|required',
3633
'Name' => 'nullable|string',
3734
'Goal' => 'nullable|string',
3835
'Assignee' => 'nullable|integer',
3936
'StartAt' => 'nullable|date_format:Y-m-d',
4037
'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt',
4138
]);
42-
$response = $this->core->request('ModifyIteration', $data);
39+
$response = $this->client->requestProjectApi('ModifyIteration', $data);
4340
return $response['Iteration'];
4441
}
4542

4643
public function delete(array $data)
4744
{
4845
$this->validate($data, [
49-
'ProjectName' => 'string|required',
5046
'IterationCode' => 'integer|required',
5147
]);
52-
$this->core->request('DeleteIteration', $data);
48+
$this->client->requestProjectApi('DeleteIteration', $data);
5349
return true;
5450
}
5551
}

src/ProjectSetting.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@
66

77
class ProjectSetting extends Base
88
{
9-
public function getIssueTypes(array $data)
9+
public function getIssueTypes()
1010
{
11-
$this->validate($data, [
12-
'ProjectName' => 'string|required',
13-
]);
14-
$response = $this->core->request('DescribeProjectIssueTypeList', $data);
11+
$response = $this->client->requestProjectApi('DescribeProjectIssueTypeList');
1512
return $response['IssueTypes'];
1613
}
1714

1815
public function getIssueStatuses(array $data)
1916
{
2017
$this->validate($data, [
21-
'ProjectName' => 'string|required',
2218
'IssueType' => [
2319
'required_without:IssueTypeId',
2420
Rule::in(Issue::TYPE),
2521
],
2622
'IssueTypeId' => 'nullable|integer',
2723
]);
28-
$response = $this->core->request('DescribeProjectIssueStatusList', $data);
24+
$response = $this->client->requestProjectApi('DescribeProjectIssueStatusList', $data);
2925
return $response['ProjectIssueStatusList'];
3026
}
3127
}

tests/Acceptance/ArtifactTest.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Tests\Acceptance;
44

5-
use Coding\Core;
6-
use Coding\Issue;
7-
use GuzzleHttp\Client;
5+
use GuzzleHttp\Client as GuzzleClient;
86
use GuzzleHttp\Psr7;
97

108
class ArtifactTest extends TestCase
@@ -16,22 +14,22 @@ public function testUploadAndDownload()
1614
$package = 'status.txt';
1715
$version = date('Ymd.Hi.s', time());
1816
file_put_contents($package, $version);
19-
$client = new Client();
17+
$http = new GuzzleClient();
2018
$body = Psr7\Utils::tryFopen($package, 'r');
2119
$url = "https://${teamDomain}-generic.pkg.coding.net/${projectName}/generic/${package}?version=${version}";
2220
$auth = [
2321
getenv('CODING_USERNAME'),
2422
getenv('CODING_PASSWORD'),
2523
];
26-
$response = $client->request('PUT', $url, [
24+
$response = $http->request('PUT', $url, [
2725
'auth' => $auth,
2826
'body' => $body,
2927
]);
3028
$this->assertEquals(200, $response->getStatusCode());
3129

3230
// Download
3331
$tmpfname = tempnam(sys_get_temp_dir(), $package);
34-
$client->request('GET', $url, [
32+
$http->request('GET', $url, [
3533
'auth' => $auth,
3634
'sink' => $tmpfname,
3735
]);

tests/Acceptance/GitBranchTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testIndex()
1212
$data = [
1313
'DepotId' => getenv('CODING_DEPOT_ID'),
1414
];
15-
$branch = new GitBranch($this->token);
15+
$branch = new GitBranch($this->client);
1616
$result = $branch->index($data);
1717
$names = [];
1818
foreach ($result as $branch) {

0 commit comments

Comments
 (0)