Skip to content

Commit 1df7d70

Browse files
committed
カスタムフィールド
1 parent 05d6356 commit 1df7d70

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Takuya\BacklogApiClient\Backup\Copy\Traits;
4+
5+
use function Takuya\Utils\array_select;
6+
7+
trait CopyProjectCustomField {
8+
public function copyProjectCustomField ( int $src_project_id, array $type_ids, int $dst_project_id ) {
9+
$list = $this->src_cli->getCustomFieldList( $src_project_id );
10+
$map = [];
11+
foreach ( $list as $item ) {
12+
$data = $this->formatCustomField( $item, $type_ids );
13+
$ret = $this->dst_cli->addCustomField($dst_project_id,$data);
14+
$map[$item->id]=$ret->id;
15+
}
16+
return $map;
17+
}
18+
19+
protected function formatCustomField ( object $customField, array $type_ids ) {
20+
$keys = [
21+
'typeId',
22+
'name',
23+
'description',
24+
'required',
25+
'min',
26+
'max',
27+
'initialValue',
28+
'unit',
29+
'initialValueType',
30+
'initialDate',
31+
'initialShift',
32+
'items',
33+
'allowInput',
34+
'allowAddItem',
35+
'applicableIssueTypes',
36+
];
37+
$data = array_select( $keys, json_decode( json_encode( $customField ), JSON_OBJECT_AS_ARRAY ) );
38+
$data = array_filter($data,fn($e)=>!empty($e));
39+
$data = array_combine(array_keys($data),array_values($data));
40+
41+
if ( !empty( $data['applicableIssueTypes'] ) ) {
42+
foreach ( $data['applicableIssueTypes'] as $idx => $old_key ) {
43+
$new_key = $type_ids[$old_key];
44+
$data['applicableIssueTypes'][$idx] = $new_key;
45+
}
46+
}
47+
if (!empty($data['items'])){
48+
$values = array_column($data['items'],'name');
49+
$data['items'] = $values;
50+
}
51+
//
52+
return $data;
53+
}
54+
55+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace tests\Feature\API;
4+
5+
6+
7+
use tests\Feature\TestCase\TestCaseTemporaryProject;
8+
9+
class CustomFieldListTest extends TestCaseTemporaryProject {
10+
protected $sample_cf=null;
11+
protected $sample_issue_type_id;
12+
protected $sample_priority_id;
13+
14+
protected function setUp (): void {
15+
parent::setUp();
16+
$this->create_custom_field();
17+
$this->issue_type_id();
18+
$this->priority_id();
19+
}
20+
21+
protected function tearDown (): void {
22+
$this->delete_custom_field();
23+
parent::tearDown();
24+
}
25+
protected function delete_custom_field(){
26+
if($this->sample_cf){
27+
$api = $this->api_client();
28+
$api->deleteCustomField($this->project_id,$this->sample_cf->id);
29+
}
30+
}
31+
protected function create_custom_field(){
32+
$api = $this->api_client();
33+
$ret = $api->addCustomField($this->project_id,[
34+
'typeId'=>6,
35+
'name'=>'sample-for-api',
36+
'description'=>'APIテスト用',
37+
'required'=>false,
38+
'items'=>['項目A','項目B','項目C'],
39+
'allowInput'=>true,
40+
'allowAddItem'=>true
41+
]);
42+
$this->sample_cf=$ret;
43+
return $ret;
44+
}
45+
protected function issue_type_id(){
46+
$api = $this->api_client();
47+
$ret =$api->getIssueTypeList($this->project_id);
48+
$this->sample_issue_type_id = $ret[0]->id;
49+
}
50+
protected function priority_id(){
51+
$api = $this->api_client();
52+
$ret =$api->getPriorityList();
53+
$this->sample_priority_id = $ret[0]->id;
54+
}
55+
56+
57+
public function test_create_issue_with_custom_field_of_multiple_list(){
58+
$api = $this->api_client();
59+
$cf_id=$this->sample_cf->id;
60+
$data = [
61+
'projectId' =>$this->project_id,
62+
'summary'=>'追加テスト',
63+
'issueTypeId'=>$this->sample_issue_type_id,
64+
'priorityId'=>$this->sample_priority_id,
65+
] ;
66+
$data['customField_'.$cf_id] = [1,2];
67+
$ret = $api->addIssue($data);
68+
$api->deleteIssue($ret->id);
69+
$posted_cf = array_values(array_filter($ret->customFields,fn($e)=>$e->id==$cf_id));
70+
$this->assertNotEmpty($posted_cf);
71+
$posted_cf= $posted_cf[0]->value;
72+
$this->assertEquals(2,sizeof($posted_cf) );
73+
74+
75+
76+
}
77+
78+
}

0 commit comments

Comments
 (0)