From 1d2634f0ee90a95b05a5ba52a36a05f2505400af Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 12 Mar 2011 22:55:38 +0100 Subject: [ticket/9806] Script for merging pull requests PHPBB3-9806 --- git-tools/merge.php | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100755 git-tools/merge.php (limited to 'git-tools') diff --git a/git-tools/merge.php b/git-tools/merge.php new file mode 100755 index 0000000000..cbd84b896f --- /dev/null +++ b/git-tools/merge.php @@ -0,0 +1,175 @@ +#!/usr/bin/env php +getMessage(); + exit($e->getCode()); +} + +function work($pull_id, $remote) +{ + // Get some basic data + $pull = get_pull('phpbb', 'phpbb3', $pull_id); + + if (!$pull_id) + { + show_usage(); + } + + if ($pull['state'] != 'open') + { + throw new RuntimeException(sprintf("Error: pull request is closed\n", + $target_branch), 5); + } + + $pull_user = $pull['head'][0]; + $pull_branch = $pull['head'][1]; + $target_branch = $pull['base'][1]; + + switch ($target_branch) + { + case 'develop-olympus': + run("git checkout develop-olympus"); + run("git pull $remote develop-olympus"); + + add_remote($pull_user, 'phpbb3'); + run("git fetch $pull_user"); + run("git merge --no-ff $pull_user/$pull_branch"); + run("phpunit"); + + run("git checkout develop"); + run("git pull $remote develop"); + run("git merge --no-ff develop-olympus"); + run("phpunit"); + break; + + case 'develop': + run("git checkout develop"); + run("git pull $remote develop"); + + add_remote($pull_user, 'phpbb3'); + run("git fetch $pull_user"); + run("git merge --no-ff $pull_user/$pull_branch"); + run("phpunit"); + break; + + default: + throw new RuntimeException(sprintf("Error: pull request target branch '%s' is not a main branch\n", + $target_branch), 5); + break; + } +} + +function add_remote($username, $repository, $pushable = false) +{ + $url = get_repository_url($username, $repository, false); + run("git remote add $username $url", true); + + if ($pushable) + { + $ssh_url = get_repository_url($username, $repository, true); + run("git remote set-url --push $username $ssh_url"); + } +} + +function get_repository_url($username, $repository, $ssh = false) +{ + $url_base = ($ssh) ? 'git@github.com:' : 'git://github.com/'; + + return $url_base . $username . '/' . $repository . '.git'; +} + +function api_request($query) +{ + $contents = file_get_contents("http://github.com/api/v2/json/$query"); + + if ($contents === false) + { + throw new RuntimeException("Error: failed to retrieve pull request data\n", 4); + } + + return json_decode($contents); +} + +function get_pull($username, $repository, $pull_id) +{ + $request = api_request("pulls/$username/$repository/$pull_id"); + + $pull = $request->pull; + + $pull_data = array( + 'base' => array($pull->base->user->login, $pull->base->ref), + 'head' => array($pull->head->user->login, $pull->head->ref), + 'state' => $pull->state, + ); + + return $pull_data; +} + +function get_arg($array, $index, $default) +{ + return isset($array[$index]) ? $array[$index] : $default; +} + +function run($cmd, $ignore_fail = false) +{ + global $dry_run; + + if (!empty($dry_run)) + { + echo "$cmd\n"; + } + else + { + passthru(escapeshellcmd($cmd), $status); + + if ($status != 0 && !$ignore_fail) + { + throw new RuntimeException(sprintf("Error: command '%s' failed with status %s'\n", + $cmd, $status), 6); + } + } +} -- cgit v1.2.1