Example #1
0
/**
 *	Unregister a project with the system
 *	@param $param[1] remote repository
 */
function api_post_project_delete($param = array())
{
    $repo = $param[1];
    // load
    $str = @file_get_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json');
    $json = @json_decode($str);
    if (!is_array($json)) {
        $json = array();
    }
    // search for repo
    $found = false;
    for ($i = 0; $i < count($json); $i++) {
        if (isset($json[$i]->repo) && $json[$i]->repo === $repo) {
            array_splice($json, $i, 1);
            $found = true;
            break;
        }
    }
    if (!$found) {
        router_error_404('Cannot find project ' . $repo);
    }
    // save
    $str = @json_encode($json);
    $old_umask = @umask(00);
    if (false === file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', $str)) {
        @umask($old_umask);
        router_error_500('Cannot save projects.json');
    }
    @umask($old_umask);
    return true;
}
Example #2
0
function github_post_push($param = array())
{
    $commit_msg_prefix = 'Regenerate output files';
    $payload = json_decode($param['payload'], true);
    // prevent error on "ping" notifications
    if (!isset($payload['head_commit']['message'])) {
        return 'Not a commit';
    }
    // prevent recursions
    if (substr($payload['head_commit']['message'], 0, strlen($commit_msg_prefix)) === $commit_msg_prefix) {
        return 'Not acting on my own changes';
    }
    // ref is like "refs/heads/master"
    $branch = @array_pop(explode('/', $payload['ref']));
    $tmp_key = get_repo($payload['repository']['clone_url'], $branch, true);
    if ($tmp_key === false) {
        router_error_500('Error getting branch ' . $branch . ' of ' . $payload['repository']['clone_url']);
    }
    $ret_val = make_run(tmp_dir($tmp_key), 'all', $out);
    // run "make clean" to remove temporary files (but not output files, that's the idea)
    make_run(tmp_dir($tmp_key), 'clean');
    $modified = repo_get_modified_files($tmp_key);
    if (empty($modified) && $ret_val === 0) {
        // nothing to commit, no error
        return 'No changes';
    }
    $ret = repo_stage_files($tmp_key, $modified);
    if ($ret === false) {
        router_error_500('Error staging files ' . implode(', ', $modified) . ' to ' . $tmp_key);
    }
    // setup commit message
    $msg = $commit_msg_prefix;
    // this needs to come first
    if ($ret_val !== 0) {
        $msg .= ' (error)' . "\n\n";
        $msg .= 'The process returned error code ' . $ret_val . '. ';
    } else {
        $msg .= "\n\n";
    }
    $msg .= 'The version of Pandoc used is ' . get_pandoc_version() . '.' . "\n\n";
    $msg .= 'Output:' . "\n";
    $msg .= implode("\n", $out);
    $ret = repo_commit($tmp_key, $msg);
    if ($ret === false) {
        router_error_500('Error committing ' . $tmp_key);
    }
    $ret = repo_push($tmp_key, $payload['repository']['ssh_url']);
    if ($ret === false) {
        router_error_500('Error pushing to ' . $payload['repository']['ssh_url']);
    }
    // count the number of collaborators
    $seen = array();
    foreach ($payload['commits'] as $commit) {
        if (!in_array($commit['author']['email'], $seen)) {
            $seen[] = $commit['author']['email'];
        }
    }
    if (1 < $seen) {
        // subtract the sausage machine
        $seen--;
    }
    // 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();
    }
    foreach ($projects as &$p) {
        if ($payload['repository']['full_name'] === $p['github_repo']) {
            $p['updated'] = time();
            $p['collaborators'] = count($seen);
        }
    }
    $old_umask = @umask(00);
    @file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', json_encode($projects));
    @umask($old_umask);
    return 'Success';
}