Skip to content

Commit 45c7e77

Browse files
committed
Issue #16: Allow defining a target branch with a different name
1 parent de24b5f commit 45c7e77

5 files changed

Lines changed: 75 additions & 14 deletions

File tree

.drupal-artifact-builder.yml.dist

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
# repository
12
# Repository URL (git SSH / git HTTP URL).
23
repository: git@github.com:example/example-artifact.git
4+
5+
# include
36
# Extra files or folders to include into the artifact.
47
include: []
8+
9+
# author
510
# It will be the author used in git commits.
6-
author: John Doe <passionate.developer@example.com>
11+
# author: John Doe <passionate.developer@example.com>
12+
13+
# branch
14+
# Allow a source branch to be pushed to a specific target branch.
15+
# Use it only if you need push branches with a different branch name than the source.
16+
# branches_map:
17+
# develop:develop-build

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,39 @@ to have an starting point:
2121
cp vendor/metadrop/drupal-artifact-builder/.drupal-artifact-builder.yml.dist .drupal-artifact-builder.yml
2222
```
2323

24-
Configuration properties:
24+
#### Configuration properties
2525

2626
- **repository**: Repository URL (git SSH / git HTTP URL).
27-
- **author**: It will be the author used in git commits.
27+
28+
Example:
29+
```yaml
30+
repository: git@github.com:example/example-artifact.git
31+
```
32+
2833
- **include**: Extra files or folders to include into the artifact.
2934
35+
Example:
36+
```yaml
37+
include: []
38+
```
39+
40+
- **author**: It will be the author used in git commits.
41+
42+
Example:
43+
```yaml
44+
author: John Doe <passionate.developer@example.com>
45+
```
46+
47+
- **branches_map**: Key value map to git push source artifact branches to different artifact branches.
48+
49+
Example:
50+
```yaml
51+
branches_map:
52+
develop:develop-build
53+
```
54+
55+
This example will make push the artifacts coming from develop source branch to the develop-build artifact branch.
56+
3057
## Usage
3158
3259
Builds the artifact and push the changes to git:

src/Config/Config.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Config implements ConfigInterface {
1111

1212
/**
13-
* Branch where the artifact will be pushed.
13+
* Branch where the artifact is created from.
1414
*
1515
* @var string
1616
*/
@@ -25,11 +25,14 @@ class Config implements ConfigInterface {
2525
* Extra files or folders included into the artifact.
2626
* @param string|null $author
2727
* Artifact commit author.
28+
* @param array|null $branches_map
29+
* Map between source and target branches.
2830
*/
2931
public function __construct(
3032
protected ?string $repository = NULL,
3133
protected array $include = [],
3234
protected ?string $author = NULL,
35+
protected array $branches_map = [],
3336
) {
3437
}
3538

@@ -80,7 +83,7 @@ public function setBranch(string $branch) : void {
8083
* @var string
8184
*/
8285
public function getBranch() : string {
83-
return $this->branch;
86+
return $this->branches_map[$this->branch] ?? $this->branch;
8487
}
8588

8689
/**
@@ -110,15 +113,25 @@ public static function createFromConfigurationFile(string $config_file) {
110113
'author',
111114
];
112115

113-
if (isset($configuration['repository']) && !is_string($configuration['repository'])) {
114-
throw new \InvalidArgumentException(sprintf('"repository" configuration key must be a string, %s given', gettype($configuration['repository'])));
116+
foreach ($string_fields as $string_field) {
117+
if (isset($configuration[$string_field]) && !is_string($configuration[$string_field])) {
118+
throw new \InvalidArgumentException(sprintf('"%s" configuration key must be a string, %s given', $string_field, gettype($configuration['repository'])));
119+
}
115120
}
116121

117-
if (isset($configuration['include']) && !is_array($configuration['include'])) {
118-
throw new \InvalidArgumentException('"include" config key must be a string, %s given!');
122+
$array_fields = [
123+
'include',
124+
'branches_map',
125+
];
126+
127+
foreach ($array_fields as $array_field) {
128+
if (isset($configuration[$array_field]) && !is_array($configuration[$array_field])) {
129+
throw new \InvalidArgumentException('"%s" config key must be a string, %s given!', $array_field, gettype($configuration[$array_field]));
130+
}
119131
}
120132

121-
return new self($configuration['repository'] ?? NULL, $configuration['include'] ?? []);
133+
134+
return new self($configuration['repository'] ?? NULL, $configuration['include'] ?? [], $configuration['author'] ?? NULL, $configuration['branches_map'] ?? []);
122135
}
123136

124137
}

src/Config/ConfigInterface.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
interface ConfigInterface
66
{
77
/**
8-
* Branch where the artifact will be created.
8+
* Branch where artifact is pushed.
99
*
10-
* Its value will be set with the current branch.
10+
* Default value: current branch.
1111
*
1212
* @var string
1313
*/
1414
public function getBranch() : string;
1515

16+
/**
17+
* Sets the branch where artifact is pushed.
18+
*
19+
* @param string $branch
20+
* Branch name.
21+
*/
22+
public function setBranch(string $branch) : void;
23+
1624
/**
1725
* Gets the repository where the commits will be pushed.
1826
*

src/DrupalArtifactBuilderGit.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ protected function initialize(InputInterface $input, OutputInterface $output) {
3636
if ($input->hasOption('repository') && !empty($input->getOption('repository'))) {
3737
$this->config->setRepository($input->getOption('repository'));
3838
}
39-
$this->getConfiguration()->setBranch($this->getBranch($input));
40-
$this->log(sprintf('Selected %s branch', $this->getConfiguration()->getBranch()));
39+
$selected_branch = $this->getBranch($input);
40+
$this->getConfiguration()->setBranch($selected_branch);
41+
$this->log(sprintf('Source branch: %s', $selected_branch));
42+
$this->log(sprintf('Target branch: %s', $this->getConfiguration()->getBranch()));
4143
$this->assertArtifactExists();
4244

4345
if ($input->hasOption('author') && !empty($input->getOption('author'))) {

0 commit comments

Comments
 (0)