Skip to content

Commit 59793d5

Browse files
[10.2] Add $variable_type to addVariable and updateVariable (#596)
1 parent 28ee882 commit 59793d5

File tree

2 files changed

+124
-12
lines changed

2 files changed

+124
-12
lines changed

src/Api/Projects.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -848,15 +848,19 @@ public function variable($project_id, string $key)
848848
}
849849

850850
/**
851-
* @param int|string $project_id
852-
* @param string $key
853-
* @param string $value
854-
* @param bool|null $protected
855-
* @param string|null $environment_scope
851+
* @param int|string $project_id
852+
* @param string $key
853+
* @param string $value
854+
* @param bool|null $protected
855+
* @param string|null $environment_scope
856+
* @param array<string,mixed> $parameters {
857+
*
858+
* @var string $variable_type env_var (default) or file
859+
* }
856860
*
857861
* @return mixed
858862
*/
859-
public function addVariable($project_id, string $key, string $value, ?bool $protected = null, ?string $environment_scope = null)
863+
public function addVariable($project_id, string $key, string $value, ?bool $protected = null, ?string $environment_scope = null, array $parameters = [])
860864
{
861865
$payload = [
862866
'key' => $key,
@@ -871,19 +875,25 @@ public function addVariable($project_id, string $key, string $value, ?bool $prot
871875
$payload['environment_scope'] = $environment_scope;
872876
}
873877

878+
$payload = \array_merge($parameters, $payload);
879+
874880
return $this->post($this->getProjectPath($project_id, 'variables'), $payload);
875881
}
876882

877883
/**
878-
* @param int|string $project_id
879-
* @param string $key
880-
* @param string $value
881-
* @param bool|null $protected
882-
* @param string|null $environment_scope
884+
* @param int|string $project_id
885+
* @param string $key
886+
* @param string $value
887+
* @param bool|null $protected
888+
* @param string|null $environment_scope
889+
* @param array<string,mixed> $parameters {
890+
*
891+
* @var string $variable_type env_var (default) or file
892+
*}
883893
*
884894
* @return mixed
885895
*/
886-
public function updateVariable($project_id, string $key, string $value, ?bool $protected = null, ?string $environment_scope = null)
896+
public function updateVariable($project_id, string $key, string $value, ?bool $protected = null, ?string $environment_scope = null, array $parameters = [])
887897
{
888898
$payload = [
889899
'value' => $value,
@@ -897,6 +907,8 @@ public function updateVariable($project_id, string $key, string $value, ?bool $p
897907
$payload['environment_scope'] = $environment_scope;
898908
}
899909

910+
$payload = \array_merge($parameters, $payload);
911+
900912
return $this->put($this->getProjectPath($project_id, 'variables/'.self::encodePath($key)), $payload);
901913
}
902914

tests/Api/ProjectsTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,53 @@ public function shouldAddVariableWithProtectionAndEnvironment(): void
16091609
);
16101610
}
16111611

1612+
/**
1613+
* @test
1614+
*/
1615+
public function shouldAddVariableWithEnvironmentAndVariableType(): void
1616+
{
1617+
$expectedArray = [
1618+
'key' => 'DEPLOY_SERVER',
1619+
'value' => 'stage.example.com',
1620+
'environment_scope' => 'staging',
1621+
'variable_type' => 'file',
1622+
];
1623+
1624+
$api = $this->getApiMock();
1625+
$api->expects($this->once())
1626+
->method('post')
1627+
->with('projects/1/variables', $expectedArray)
1628+
->will($this->returnValue($expectedArray));
1629+
1630+
$this->assertEquals(
1631+
$expectedArray,
1632+
$api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging', ['variable_type' => 'file'])
1633+
);
1634+
}
1635+
1636+
/**
1637+
* @test
1638+
*/
1639+
public function shouldAddVariableWithEnvironmentFromParameterList(): void
1640+
{
1641+
$expectedArray = [
1642+
'key' => 'DEPLOY_SERVER',
1643+
'value' => 'stage.example.com',
1644+
'environment_scope' => 'staging',
1645+
];
1646+
1647+
$api = $this->getApiMock();
1648+
$api->expects($this->once())
1649+
->method('post')
1650+
->with('projects/1/variables', $expectedArray)
1651+
->will($this->returnValue($expectedArray));
1652+
1653+
$this->assertEquals(
1654+
$expectedArray,
1655+
$api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging', ['environment_scope' => 'production'])
1656+
);
1657+
}
1658+
16121659
/**
16131660
* @test
16141661
*/
@@ -1704,6 +1751,59 @@ public function shouldUpdateVariableWithProtectedAndEnvironment(): void
17041751
);
17051752
}
17061753

1754+
/**
1755+
* @test
1756+
*/
1757+
public function shouldUpdateVariableWithEnvironmentAndVariableType(): void
1758+
{
1759+
$expectedArray = [
1760+
'key' => 'DEPLOY_SERVER',
1761+
'value' => 'stage.example.com',
1762+
'environment_scope' => 'staging',
1763+
'variable_type' => 'file',
1764+
];
1765+
1766+
$api = $this->getApiMock();
1767+
$api->expects($this->once())
1768+
->method('put')
1769+
->with(
1770+
'projects/1/variables/DEPLOY_SERVER',
1771+
['value' => 'stage.example.com', 'environment_scope' => 'staging', 'variable_type' => 'file']
1772+
)
1773+
->will($this->returnValue($expectedArray));
1774+
1775+
$this->assertEquals(
1776+
$expectedArray,
1777+
$api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging', ['variable_type' => 'file'])
1778+
);
1779+
}
1780+
1781+
/**
1782+
* @test
1783+
*/
1784+
public function shouldUpdateVariableWithEnvironmentFromParameterList(): void
1785+
{
1786+
$expectedArray = [
1787+
'key' => 'DEPLOY_SERVER',
1788+
'value' => 'stage.example.com',
1789+
'environment_scope' => 'staging',
1790+
];
1791+
1792+
$api = $this->getApiMock();
1793+
$api->expects($this->once())
1794+
->method('put')
1795+
->with(
1796+
'projects/1/variables/DEPLOY_SERVER',
1797+
['value' => 'stage.example.com', 'environment_scope' => 'staging']
1798+
)
1799+
->will($this->returnValue($expectedArray));
1800+
1801+
$this->assertEquals(
1802+
$expectedArray,
1803+
$api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging', ['environment_scope' => 'production'])
1804+
);
1805+
}
1806+
17071807
/**
17081808
* @test
17091809
*/

0 commit comments

Comments
 (0)