Skip to content

Commit fa2db9a

Browse files
Add project fork parameters (#895)
1 parent 7656acf commit fa2db9a

3 files changed

Lines changed: 103 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* Add support for project integrations endpoints
1515
* Add support for `Projects::updateDeployKey`
1616
* Add support for project push rules
17+
* Add support for additional parameters in `Projects::fork`
1718
* Add support for group hook endpoints
1819
* Add support for `job_inputs` and `job_variables_attributes` in `Jobs::play`
1920
* Add support for `inputs` in `Projects::createPipeline`

src/Api/Projects.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -945,19 +945,52 @@ public function forks(int|string $project_id, array $parameters = []): mixed
945945
/**
946946
* @param array $parameters {
947947
*
948-
* @var string $namespace The ID or path of the namespace that the project will be forked to
949-
* @var string $path The path of the forked project (optional)
950-
* @var string $name The name of the forked project (optional)
948+
* @var string $branches Branches to fork (empty for all branches)
949+
* @var string $description The description assigned to the resultant project after forking
950+
* @var bool $mr_default_target_self For forked projects, target merge requests to this project
951+
* @var string $name The name assigned to the resultant project after forking
952+
* @var int $namespace_id The ID of the namespace that the project is forked to
953+
* @var string $namespace_path The path of the namespace that the project is forked to
954+
* @var int|string $namespace deprecated; use namespace_id or namespace_path instead
955+
* @var string $path The path assigned to the resultant project after forking
956+
* @var string $visibility The visibility level assigned to the resultant project after forking
951957
* }
958+
*
959+
* @throws UndefinedOptionsException If an option name is undefined
960+
* @throws InvalidOptionsException If an option doesn't fulfill the specified validation rules
952961
*/
953962
public function fork(int|string $project_id, array $parameters = []): mixed
954963
{
955964
$resolver = new OptionsResolver();
956-
$resolver->setDefined(['namespace', 'path', 'name']);
957-
958-
$resolved = $resolver->resolve($parameters);
965+
$resolver->setDefined('branches')
966+
->setAllowedTypes('branches', 'string')
967+
;
968+
$resolver->setDefined('description')
969+
->setAllowedTypes('description', 'string')
970+
;
971+
$resolver->setDefined('mr_default_target_self')
972+
->setAllowedTypes('mr_default_target_self', 'bool')
973+
;
974+
$resolver->setDefined('name')
975+
->setAllowedTypes('name', 'string')
976+
;
977+
$resolver->setDefined('namespace_id')
978+
->setAllowedTypes('namespace_id', 'int')
979+
;
980+
$resolver->setDefined('namespace_path')
981+
->setAllowedTypes('namespace_path', 'string')
982+
;
983+
$resolver->setDefined('namespace')
984+
->setAllowedTypes('namespace', ['int', 'string'])
985+
;
986+
$resolver->setDefined('path')
987+
->setAllowedTypes('path', 'string')
988+
;
989+
$resolver->setDefined('visibility')
990+
->setAllowedValues('visibility', ['public', 'internal', 'private'])
991+
;
959992

960-
return $this->post($this->getProjectPath($project_id, 'fork'), $resolved);
993+
return $this->post($this->getProjectPath($project_id, 'fork'), $resolver->resolve($parameters));
961994
}
962995

963996
public function createForkRelation(int|string $project_id, int|string $forked_project_id): mixed

tests/Api/ProjectsTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,42 @@ public function shouldForkWithNamespace(): void
19361936
]));
19371937
}
19381938

1939+
#[Test]
1940+
public function shouldForkWithIntegerNamespace(): void
1941+
{
1942+
$expectedArray = [
1943+
'namespace' => 123,
1944+
];
1945+
1946+
$api = $this->getApiMock();
1947+
$api->expects($this->once())
1948+
->method('post')
1949+
->with('projects/1/fork', $expectedArray)
1950+
->willReturn($expectedArray);
1951+
1952+
$this->assertEquals($expectedArray, $api->fork(1, [
1953+
'namespace' => 123,
1954+
]));
1955+
}
1956+
1957+
#[Test]
1958+
public function shouldForkWithNamespaceId(): void
1959+
{
1960+
$expectedArray = [
1961+
'namespace_id' => 7,
1962+
];
1963+
1964+
$api = $this->getApiMock();
1965+
$api->expects($this->once())
1966+
->method('post')
1967+
->with('projects/1/fork', $expectedArray)
1968+
->willReturn($expectedArray);
1969+
1970+
$this->assertEquals($expectedArray, $api->fork(1, [
1971+
'namespace_id' => 7,
1972+
]));
1973+
}
1974+
19391975
#[Test]
19401976
public function shouldForkWithNamespaceAndPath(): void
19411977
{
@@ -1978,6 +2014,32 @@ public function shouldForkWithNamespaceAndPathAndName(): void
19782014
]));
19792015
}
19802016

2017+
#[Test]
2018+
public function shouldForkWithNamespacePathAndAdditionalParameters(): void
2019+
{
2020+
$expectedArray = [
2021+
'namespace_path' => 'group/subgroup',
2022+
'branches' => 'main',
2023+
'description' => 'Fork description',
2024+
'mr_default_target_self' => true,
2025+
'visibility' => 'private',
2026+
];
2027+
2028+
$api = $this->getApiMock();
2029+
$api->expects($this->once())
2030+
->method('post')
2031+
->with('projects/1/fork', $expectedArray)
2032+
->willReturn($expectedArray);
2033+
2034+
$this->assertEquals($expectedArray, $api->fork(1, [
2035+
'namespace_path' => 'group/subgroup',
2036+
'branches' => 'main',
2037+
'description' => 'Fork description',
2038+
'mr_default_target_self' => true,
2039+
'visibility' => 'private',
2040+
]));
2041+
}
2042+
19812043
#[Test]
19822044
public function shouldCreateForkRelation(): void
19832045
{

0 commit comments

Comments
 (0)