Skip to content

Commit 84c0605

Browse files
committed
[FEATURE] ✨ Select branch column in batch mode
Allow to select a branch column from the CSV file provided in the batch mode, e.g. `branch-column=test-stage`.
1 parent 44437a0 commit 84c0605

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ to use the batch processing mode instead.
261261
This mode will trigger the `patch` or `merge` command for a list of
262262
repositories. The list is expected as CSV file named `repositories.csv`.
263263

264-
*repositories.csv - Example file content*
264+
*repositories.csv - Example file content, with repository & branch to use*
265265
```csv
266-
repository-url,source-branch
266+
repository-url,main-branch
267267
https://git.example.com/projecta,main
268268
https://git.example.com/projectb,main
269269
https://git.example.com/projectc,development
@@ -292,6 +292,34 @@ in all repositories of the first column:
292292
./vendor/bin/patchbot batch merge --source=feature-add-phpcs-rules
293293
```
294294

295+
**Different branch names**
296+
297+
When the branch names used in the `patch` and `merge` subcommand differ,
298+
or when you need to merge the feature branch into several stage branches
299+
you may provide a file with all branches and pass the name of the designated
300+
branch column as option `branch-column`.
301+
302+
*repositories.csv - Example file content with many branch columns*
303+
```csv
304+
repository-url,main,development,integration-stage,test-stage
305+
https://git.example.com/projecta,main,development,integration,testing
306+
https://git.example.com/projectb,main,dev,stage/integration,stage/test
307+
https://git.example.com/projectc,live,development,stage/integration,stage/test
308+
```
309+
310+
Apply the patch `rename-changelog` to the feature branch
311+
`feature-rename-changelog`, which is based on branch name given in column
312+
`development`:
313+
```bash
314+
./vendor/bin/patchbot batch patch --branch-column=development patch-name=rename-changelog branch-name=feature-rename-changelog
315+
```
316+
Now merge the feature branch into the branch name given in column `test-stage`
317+
and then into the of given in column `integration-stage`:
318+
```bash
319+
./vendor/bin/patchbot batch merge --branch-column=test-stage source=feature-rename-changelog
320+
./vendor/bin/patchbot batch merge --branch-column=integration-stage source=feature-rename-changelog
321+
```
322+
295323
## License
296324

297325
GNU General Public License version 2 or later

src/RoboFile.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public function create(array $options = [
184184
*
185185
* @param string $batchCommand Name of command to run in batch mode (patch or merge, default: patch)
186186
* @param array $options
187+
* @option $repository-column Name of the repository column (fallback: first column in CSV)
188+
* @option $branch-column Name of the branch column (fallback: second column in CSV)
187189
* @option $working-directory Working directory to check out repositories
188190
* @option $patch-source-directory Source directory for all collected patches
189191
* @option $patch-name Name of the directory where the patch code resides
@@ -194,6 +196,8 @@ public function create(array $options = [
194196
* @throws TaskException
195197
*/
196198
public function batch(string $batchCommand, array $options = [
199+
'repository-column' => null,
200+
'branch-column' => null,
197201
'working-directory|d' => null,
198202
'patch-source-directory|s' => null,
199203
'patch-name|p' => 'template',
@@ -209,23 +213,24 @@ public function batch(string $batchCommand, array $options = [
209213
return 1;
210214
}
211215

216+
// Parse CSV to array, with keys from header row
212217
$csvFileContent = file_get_contents('repositories.csv');
213218
$repositories = str_getcsv($csvFileContent, PHP_EOL);
214-
array_walk($repositories, static function (&$k) use ($repositories) {
215-
$k = str_getcsv($k);
219+
$header = str_getcsv(array_shift($repositories)); // get & remove header
220+
array_walk($repositories, static function (&$k) use ($repositories, $header) {
221+
$k = array_combine($header, str_getcsv($k));
216222
});
217-
array_shift($repositories); // remove column header
218223

219224
if ($batchCommand === 'patch') {
220225
foreach ($repositories as $repository) {
221226
/** @noinspection DisconnectedForeachInstructionInspection */
222227
chdir($workingDirectory); // reset working directory
223228
$this->patch([
224-
'repository-url' => $repository[0],
229+
'repository-url' => $repository[$options['repository-column']] ?? array_values($repository)[0],
225230
'working-directory' => $options['working-directory'],
226231
'patch-source-directory' => $options['patch-source-directory'],
227232
'patch-name' => $options['patch-name'],
228-
'source-branch' => $repository[1],
233+
'source-branch' => $repository[$options['branch-column']] ?? array_values($repository)[1],
229234
'branch-name' => $options['branch-name'],
230235
'halt-before-commit' => $options['halt-before-commit'],
231236
]);
@@ -236,10 +241,10 @@ public function batch(string $batchCommand, array $options = [
236241
/** @noinspection DisconnectedForeachInstructionInspection */
237242
chdir($workingDirectory); // reset working directory
238243
$this->merge([
239-
'repository-url' => $repository[0],
244+
'repository-url' => $repository[$options['repository-column']] ?? array_values($repository)[0],
240245
'working-directory' => $options['working-directory'],
241246
'source' => $options['source'],
242-
'target' => $repository[1],
247+
'target' => $repository[$options['branch-column']] ?? array_values($repository)[1],
243248
]);
244249
}
245250
}

0 commit comments

Comments
 (0)