umonkey

devtest rebuilder

Sep 24th, 2020
878
3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env php72
  2. <?php
  3.  
  4. declare(strict_types=1);
  5.  
  6. define('MY_REMOTE', 'dev1');
  7.  
  8.  
  9. create_branch('dev1_test', [
  10.     'dev1/Ef4Fau3q',
  11.     'dev1/YKuOxdJb',
  12.     'dev1/T2tZE02X',
  13. ]);
  14.  
  15.  
  16. function create_branch(string $branchName, array $tickets): void
  17. {
  18.     run_command(['git', 'fetch', 'live', 'master']);
  19.  
  20.     try {
  21.         run_command(['git', 'branch', '-D', $branchName]);
  22.     } catch (RuntimeException $e) {
  23.         // pass
  24.     }
  25.  
  26.     run_command(['git', 'checkout', '-f', '-b', $branchName, 'live/master']);
  27.  
  28.     foreach ($tickets as $ticket) {
  29.         list($remote, $branch) = explode('/', $ticket);
  30.  
  31.         try {
  32.             if ($remote === MY_REMOTE) {
  33.                 run_command(['git', 'merge', $branch, '--no-commit', '--no-ff']);
  34.             } else {
  35.                 run_command(['git', 'fetch', $remote, $branch]);
  36.                 run_command(['git', 'merge', $remote . '/' . $branch, '--no-commit', '--no-ff']);
  37.             }
  38.  
  39.             run_command(['git', 'commit', '-am', "merge {$branch} from {$remote}", '--no-verify']);
  40.         } catch (ProcessError $e) {
  41.             fprintf(STDERR, "Error merging %s\n", $ticket);
  42.             run_command(['git', 'reset', '--hard']);
  43.             run_command(['git', 'clean', '-fd']);
  44.             notify_trello($ticket, $e->output);
  45.         }
  46.     }
  47. }
  48.  
  49.  
  50. function run_command(array $args): void
  51. {
  52.     $args = array_map('escapeshellarg', $args);
  53.     $command = implode(' ', $args);
  54.  
  55.     printf("> %s\n", $command);
  56.     exec($command . ' 2>&1', $output, $exitCode);
  57.  
  58.     if ($exitCode !== 0) {
  59.         throw new ProcessError('command failed', $exitCode, implode("\n", $output));
  60.     }
  61. }
  62.  
  63.  
  64. function notify_trello(string $ticket, string $output): void
  65. {
  66.     fprintf(STDERR, "ERROR: merge failed for %s.\n", $ticket);
  67.     // trello_comment($ticket, "Merge failed, please rebase.\n\n" . $output);
  68.     // trello_move_to_list($ticket, 'Needs Rebase');
  69. }
  70.  
  71.  
  72. class ProcessError extends RuntimeException
  73. {
  74.     public $code;
  75.  
  76.     public $output;
  77.  
  78.     public function __construct(string $message, int $code, string $output)
  79.     {
  80.         parent::__construct($message);
  81.  
  82.         $this->code = $code;
  83.         $this->output = $output;
  84.     }
  85. }
  86.  
  87. // vim: set ts=4 sts=4 sw=4 et ai ci:
  88.  
RAW Paste Data