PHP Script To Migrate From GitHub To Codeberg

I have been using the bash script to migrate from GitHub to Codeberg, but it was failing due to special characters being put into the URL to codeberg. As a quick fix, I converted the script to PHP.

<?php

$codebergToken = "CODEBERG-TOKENN";
$githubToken = "GITHUB-TOKEN";

$codebergUser = "my_codeberg_user";
$githubUser = "my_github_user";

$repos = [
	'repo1',
	'repo2',
];

foreach ($repos as $repo) {
    $data = [
        "clone_addr" => "https://github.com/$githubUser/$repo.git",
        "repo_name" => $repo,
        "repo_owner" => $codebergUser,
        "service" => "github",
        "auth_token" => $githubToken,
        "mirror" => false,
        "issues" => true,
        "labels" => true,
        "releases" => true,
    ];

    $data = json_encode($data);

    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, "https://codeberg.org/api/v1/repos/migrate");
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);

    $headers = [];
    $headers[] = "Authorization: token $codebergToken";
    $headers[] = "Content-Type: application/json";

    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($curl_handle);

    if (curl_errno($curl_handle)) {
        echo 'Error:' . curl_error($curl_handle);
    }

    curl_close($curl_handle);

    echo $result;

    echo "Done. Check https://codeberg.org/$codebergUser/$repo";

    // Be nice to codeberg and delay the next update by 5 seconds.
    sleep(5);
}

Just update your details and run it using PHP like this.

php ./codeberg.php

This will allow you to migrate multiple repos at once.

Add new comment

The content of this field is kept private and will not be shown publicly.