Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ jobs:
- name: Acceptance Test
run: ./vendor/bin/phpunit tests/Acceptance
env:
CODING_TOKEN: ${{ secrets.CODING_TOKEN }}
CODING_PERSONAL_TOKEN: ${{ secrets.CODING_PERSONAL_TOKEN }}
CODING_PASSWORD: ${{ secrets.CODING_PASSWORD }}
CODING_USERNAME: ${{ secrets.CODING_USERNAME }}
CODING_TEAM_DOMAIN: ${{ secrets.CODING_TEAM_DOMAIN }}
CODING_PROJECT_TOKEN: ${{ secrets.CODING_PROJECT_TOKEN }}
CODING_PROJECT_NAME: ${{ secrets.CODING_PROJECT_NAME }}
CODING_DEPOT_ID: ${{ secrets.CODING_DEPOT_ID }}

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ composer require coding/sdk

require 'vendor/autoload.php';

use Coding\Client;
use Coding\Iteration;

$iteration = new Iteration('c127894e5a851cef22dc317f882dfb9ca6054321');
$client = new Client();
$projectName = 'project-foo';
$client->setProjectName($projectName);
$client->setProjectToken('c127894e5a851cef22dc317f882dfb9ca6054321');

$iteration = new Iteration($client);
$result = $iteration->create([
'ProjectName' => $projectName,
'Name' => 'Sprint 1',
]);
echo "https://my-team.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n";
$teamDomain = 'my-team';
echo "https://${teamDomain}.coding.net/p/{$projectName}/iterations/${result['Code']}/issues\n";
```

## Resources
Expand Down
11 changes: 3 additions & 8 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@

abstract class Base
{
protected Core $core;
protected Client $client;

public function __construct(string $token = '', Core $core = null)
public function __construct(Client $client = null)
{
$this->core = $core ?? new Core($token);
}

public function setToken(string $token)
{
$this->core->setToken($token);
$this->client = $client ?? new Client();
}

protected function validate(array $data, array $rules)
Expand Down
97 changes: 97 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Coding;

use Coding\Exceptions\ApiError;
use Coding\Exceptions\ValidationException;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use InvalidArgumentException;

class Client
{
private const API_URL = 'https://e.coding.net/open-api';

protected array $config;
protected string $token;
public bool $usePersonalToken = false;
protected ?ClientInterface $http = null;

public function __construct(array $config = [])
{
$this->config = array_merge([
'api_url' => self::API_URL,
'personal_token' => '',
'project_name' => '',
'project_token' => '',
], $config);
}

public function setHttpClient(ClientInterface $http): void
{
$this->http = $http;
}

public function getHttpClient(): ClientInterface
{
if (null === $this->http) {
$this->http = new GuzzleClient();
}

return $this->http;
}

public function request(string $action, array $data)
{
$params = ['Action' => $action];
$response = $this->getHttpClient()->request('POST', $this->config['api_url'], [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'token ' . $this->getToken(),
'Content-Type' => 'application/json'
],
'json' => array_merge($params, $data),
]);
$result = json_decode($response->getBody(), true);
if (isset($result['Response']['Error']['Message'])) {
throw new ApiError($result['Response']['Error']['Message']);
}
return $result['Response'];
}

public function requestProjectApi(string $action, array $data = [])
{
if (empty($this->config['project_name'])) {
throw new ValidationException('Need set project name first.');
}
$data['ProjectName'] = $this->config['project_name'];
return $this->request($action, $data);
}

public function setPersonalToken(string $token)
{
$this->config['personal_token'] = $token;
}

public function setProjectName(string $projectName)
{
$this->config['project_name'] = $projectName;
}

public function setProjectToken(string $token)
{
$this->config['project_token'] = $token;
}

private function getToken()
{
if ($this->usePersonalToken) {
if (empty($this->config['personal_token'])) {
throw new InvalidArgumentException('Need set project token first.');
}
return $this->config['personal_token'];
}
return !empty($this->config['project_token']) ?
$this->config['project_token'] : $this->config['personal_token'];
}
}
42 changes: 0 additions & 42 deletions src/Core.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/GitBranch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function index(array $data)
'KeyWord' => 'string', // 搜索关键字
]);
$data['DepotId'] = intval($data['DepotId']);
$response = $this->core->request('DescribeGitBranches', $data);
$response = $this->client->requestProjectApi('DescribeGitBranches', $data);
return $response['Branches'];
}
}
12 changes: 4 additions & 8 deletions src/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Issue extends Base
public function create(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'Name' => 'string|required',
'Priority' => [
'string',
Expand Down Expand Up @@ -58,34 +57,31 @@ public function create(array $data)
// 自定义属性值列表 Array of IssueCustomFieldForm
'CustomFieldValues' => 'nullable|array',
]);
$response = $this->core->request('CreateIssue', $data);
$response = $this->client->requestProjectApi('CreateIssue', $data);
return $response['Issue'];
}

public function delete(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IssueCode' => 'integer|required',
]);
$this->core->request('DeleteIssue', $data);
$this->client->requestProjectApi('DeleteIssue', $data);
return true;
}

public function get(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IssueCode' => 'integer|required',
]);
$response = $this->core->request('DescribeIssue', $data);
$response = $this->client->requestProjectApi('DescribeIssue', $data);
return $response['Issue'];
}

public function update(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IssueCode' => 'integer',
'ParentCode' => 'nullable|integer',
'Name' => 'nullable|string',
Expand Down Expand Up @@ -118,7 +114,7 @@ public function update(array $data)
// 自定义属性值列表 Array of IssueCustomFieldForm
'CustomFieldValues' => 'nullable|array',
]);
$response = $this->core->request('ModifyIssue', $data);
$response = $this->client->requestProjectApi('ModifyIssue', $data);
return $response['Issue'];
}
}
12 changes: 4 additions & 8 deletions src/Iteration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,45 @@ class Iteration extends Base
public function create(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'Name' => 'string|required',
'Goal' => 'nullable|string',
'Assignee' => 'nullable|integer',
'StartAt' => 'nullable|date_format:Y-m-d',
'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt',
]);
$response = $this->core->request('CreateIteration', $data);
$response = $this->client->requestProjectApi('CreateIteration', $data);
return $response['Iteration'];
}

public function get(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IterationCode' => 'integer|required',
]);
$response = $this->core->request('DescribeIteration', $data);
$response = $this->client->requestProjectApi('DescribeIteration', $data);
return $response['Iteration'];
}

public function update(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IterationCode' => 'integer|required',
'Name' => 'nullable|string',
'Goal' => 'nullable|string',
'Assignee' => 'nullable|integer',
'StartAt' => 'nullable|date_format:Y-m-d',
'EndAt' => 'nullable|date_format:Y-m-d|after:StartAt',
]);
$response = $this->core->request('ModifyIteration', $data);
$response = $this->client->requestProjectApi('ModifyIteration', $data);
return $response['Iteration'];
}

public function delete(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IterationCode' => 'integer|required',
]);
$this->core->request('DeleteIteration', $data);
$this->client->requestProjectApi('DeleteIteration', $data);
return true;
}
}
10 changes: 3 additions & 7 deletions src/ProjectSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@

class ProjectSetting extends Base
{
public function getIssueTypes(array $data)
public function getIssueTypes()
{
$this->validate($data, [
'ProjectName' => 'string|required',
]);
$response = $this->core->request('DescribeProjectIssueTypeList', $data);
$response = $this->client->requestProjectApi('DescribeProjectIssueTypeList');
return $response['IssueTypes'];
}

public function getIssueStatuses(array $data)
{
$this->validate($data, [
'ProjectName' => 'string|required',
'IssueType' => [
'required_without:IssueTypeId',
Rule::in(Issue::TYPE),
],
'IssueTypeId' => 'nullable|integer',
]);
$response = $this->core->request('DescribeProjectIssueStatusList', $data);
$response = $this->client->requestProjectApi('DescribeProjectIssueStatusList', $data);
return $response['ProjectIssueStatusList'];
}
}
10 changes: 4 additions & 6 deletions tests/Acceptance/ArtifactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Tests\Acceptance;

use Coding\Core;
use Coding\Issue;
use GuzzleHttp\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7;

class ArtifactTest extends TestCase
Expand All @@ -16,22 +14,22 @@ public function testUploadAndDownload()
$package = 'status.txt';
$version = date('Ymd.Hi.s', time());
file_put_contents($package, $version);
$client = new Client();
$http = new GuzzleClient();
$body = Psr7\Utils::tryFopen($package, 'r');
$url = "https://${teamDomain}-generic.pkg.coding.net/${projectName}/generic/${package}?version=${version}";
$auth = [
getenv('CODING_USERNAME'),
getenv('CODING_PASSWORD'),
];
$response = $client->request('PUT', $url, [
$response = $http->request('PUT', $url, [
'auth' => $auth,
'body' => $body,
]);
$this->assertEquals(200, $response->getStatusCode());

// Download
$tmpfname = tempnam(sys_get_temp_dir(), $package);
$client->request('GET', $url, [
$http->request('GET', $url, [
'auth' => $auth,
'sink' => $tmpfname,
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Acceptance/GitBranchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testIndex()
$data = [
'DepotId' => getenv('CODING_DEPOT_ID'),
];
$branch = new GitBranch($this->token);
$branch = new GitBranch($this->client);
$result = $branch->index($data);
$names = [];
foreach ($result as $branch) {
Expand Down
Loading