예제 #1
0
/**
 * Upload a local file to a GitHub repository
 * @param string $ps_user GitHub username
 * @param string $ps_token access token. Global account password can be used here but it's recommended to create a personal access token instead.
 * @param string $ps_owner The repository owner
 * @param string $ps_repo repository name
 * @param string $ps_git_path path for the file destination inside the repository, e.g. "/exports/from_collectiveaccess/export.xml."
 * @param string $ps_local_filepath file to upload as absolute local path. Note that the file must be loaded in memory to be committed to GitHub.
 * @param string $ps_branch branch to commit to. defaults to 'master'
 * @param bool $pb_update_on_conflict Determines what happens if file already exists in GitHub repository.
 * 		true means the file is updated in place for. false means we abort. default is true
 * @param string $ps_commit_msg commit message
 * @return bool success state
 */
function caUploadFileToGitHub($ps_user, $ps_token, $ps_owner, $ps_repo, $ps_git_path, $ps_local_filepath, $ps_branch = 'master', $pb_update_on_conflict = true, $ps_commit_msg = null)
{
    // check mandatory params
    if (!$ps_user || !$ps_token || !$ps_owner || !$ps_repo || !$ps_git_path || !$ps_local_filepath) {
        caLogEvent('DEBG', "Invalid parameters for GitHub file upload. Check your configuration!", 'caUploadFileToGitHub');
        return false;
    }
    if (!$ps_commit_msg) {
        $ps_commit_msg = 'Commit created by CollectiveAccess on ' . date('c');
    }
    $o_client = new \Github\Client();
    $o_client->authenticate($ps_user, $ps_token);
    $vs_content = @file_get_contents($ps_local_filepath);
    try {
        $o_client->repositories()->contents()->create($ps_owner, $ps_repo, $ps_git_path, $vs_content, $ps_commit_msg, $ps_branch);
    } catch (Github\Exception\RuntimeException $e) {
        switch ($e->getCode()) {
            case 401:
                caLogEvent('DEBG', "Could not authenticate with GitHub. Error message was: " . $e->getMessage() . " - Code was: " . $e->getCode(), 'caUploadFileToGitHub');
                break;
            case 422:
                if ($pb_update_on_conflict) {
                    try {
                        $va_content = $o_client->repositories()->contents()->show($ps_owner, $ps_repo, $ps_git_path);
                        if (isset($va_content['sha'])) {
                            $o_client->repositories()->contents()->update($ps_owner, $ps_repo, $ps_git_path, $vs_content, $ps_commit_msg, $va_content['sha'], $ps_branch);
                        }
                        return true;
                        // overwrite was successful if there was no exception in above statement
                    } catch (Github\Exception\RuntimeException $ex) {
                        caLogEvent('DEBG', "Could not update exiting file in GitHub. Error message was: " . $ex->getMessage() . " - Code was: " . $ex->getCode(), 'caUploadFileToGitHub');
                        break;
                    }
                } else {
                    caLogEvent('DEBG', "Could not upload file to GitHub. It looks like a file already exists at {$ps_git_path}.", 'caUploadFileToGitHub');
                }
                break;
            default:
                caLogEvent('DEBG', "Could not upload file to GitHub. A generic error occurred. Error message was: " . $e->getMessage() . " - Code was: " . $e->getCode(), 'caUploadFileToGitHub');
                break;
        }
        return false;
    } catch (Github\Exception\ValidationFailedException $e) {
        caLogEvent('DEBG', "Could not upload file to GitHub. The parameter validation failed. Error message was: " . $e->getMessage() . " - Code was: " . $e->getCode(), 'caUploadFileToGitHub');
        return false;
    } catch (Exception $e) {
        caLogEvent('DEBG', "Could not upload file to GitHub. A generic error occurred. Error message was: " . $e->getMessage() . " - Code was: " . $e->getCode(), 'caUploadFileToGitHub');
        return false;
    }
    return true;
}