/** * Get information about a temporary (working) repository * @param $param[1] temporary repository */ function api_get_temp($param = array()) { $temp = $param[1]; if (!@is_dir(tmp_dir($temp))) { router_error_404('Cannot get ' . $temp); } return array('temp' => $temp, 'repo' => repo_get_url($temp), 'branch' => 'master', 'commit' => repo_get_current_commit(tmp_dir($temp)), 'files' => repo_get_all_files(tmp_dir($temp)), 'modified' => repo_get_modified_files($temp)); }
/** * Create a repository on GitHub and push a local (temporary) repository to it */ function github_post_repo($param = array()) { if (empty($param['github_access_token'])) { router_error_400('Required parameter github_access_token missing or empty'); } if (empty($param['github_repo_name'])) { router_error_400('Required parameter github_repo_name missing or empty'); } if (empty($param['temp'])) { router_error_400('Required parameter temp missing or empty'); } $github_repo = github_create_repo($param['github_access_token'], $param['github_repo_name']); if ($github_repo === false) { router_error_500('Error creating GitHub repository ' . $param['github_repo_name'] . '. Make sure there is no existing repository with the same name.'); } $ret = github_add_collaborator($param['github_access_token'], $github_repo, config('github_push_as')); if ($ret === false) { router_error_500('Error adding ' . config('github_push_as') . ' as a collaborator to ' . $github_repo); } $ret = github_add_webhook($param['github_access_token'], $github_repo); if ($ret === false) { router_error_500('Error adding webhook to ' . $github_repo); } $modified = repo_get_modified_files($param['temp']); $ret = repo_stage_files($param['temp'], $modified); if ($ret === false) { router_error_500('Error staging files ' . implode(', ', $modified) . ' to ' . $param['temp']); } $ret = repo_commit($param['temp'], 'Initial commit'); if ($ret === false) { router_error_500('Error committing ' . $param['temp']); } $ret = repo_push($param['temp'], 'ssh://git@github.com/' . $github_repo . '.git'); if ($ret === false) { router_error_500('Error pushing to ' . $github_repo . '. Make sure all checks in setup.php pass.'); } // add to projects.json // XXX (later): create helper functions, make atomic $s = @file_get_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json'); $projects = @json_decode($s, true); if (!@is_array($projects)) { $projects = array(); } $projects[] = array('created' => time(), 'updated' => time(), 'github_repo' => $github_repo, 'parent' => repo_get_url($param['temp'])); $old_umask = @umask(00); @file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', json_encode($projects)); @umask($old_umask); return $github_repo; }