Last active
March 22, 2023 09:17
-
-
Save ym/5616781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* bitbucket user */ | |
$bb_user = 'aveline'; | |
$bb_pass = '**YOUR PASSWORD**'; | |
/* github username and password */ | |
$gh_conf = array( | |
'ym' => '**YOUR PASSWORD**', | |
'aveline' => '**YOUR PASSWORD**' | |
); | |
/* mapping */ | |
$bb_gh = array( | |
'aveline' => 'ym', | |
'aveline2' => 'aveline' | |
); | |
/* default github user */ | |
$bb_gh_default = 'ym'; | |
/* bitbucket repo name*/ | |
$repo = 'aveline/freengine4'; | |
/* github repo name */ | |
$gh_repo = 'ym/freengine4'; | |
$imports = array(); | |
$issue_max = 0; | |
$issue_min = 1024; | |
foreach(array('new', 'open','resolved','on hold','invalid','duplicate','wontfix') as $_status) { | |
echo "Fetched Issue(s) Which Status is {$_status} ... "; | |
$s = json_decode(file_get_contents("https://api.bitbucket.org/1.0/repositories/{$repo}/issues?count=99&status=". urlencode($_status), false, | |
stream_context_create(array( | |
'http' => array( | |
'header' => "Authorization: Basic " . base64_encode("$bb_user:$bb_pass") | |
))) | |
), true); | |
echo "Fetched {$s['count']} Issue(s)\n"; | |
foreach ($s['issues'] as $_issue) { | |
$imports[$_issue['local_id']] = $_issue; | |
} | |
} | |
echo "Fetched " . count($imports) . " Issues\n"; | |
echo "Rebuilding Index ...\n"; | |
foreach ($imports as $i=>$_issue) { | |
$i = (int) $i; | |
if($i > $issue_max) | |
$issue_max = $i; | |
if($i < $issue_min) | |
$issue_min = $i; | |
} | |
echo "Issue Range [{$issue_min}, {$issue_max}]\n"; | |
$issues = range($issue_min, $issue_max); | |
foreach ($issues as $i=>&$issue) { | |
if(isset($imports[$i])) { | |
echo "Issue #{$i}/#{$imports[$i]['local_id']} Found: {$imports[$i]['title']}, {$imports[$i]['reported_by']['username']}\n"; | |
$issue = $imports[$i]; | |
} else { | |
echo "Issue #{$i} Not Found, Filled with null data\n"; | |
$issue = false; | |
} | |
} | |
echo "Issue Index Rebuilt\n"; | |
foreach ($issues as $i=>$issue) { | |
if($i == 0) | |
continue; | |
$user = $imports[$i]['reported_by']['username']; | |
if(isset($bb_gh[$user])) { | |
$user = $bb_gh[$user]; | |
} else { | |
echo "No matched user {$user} in BB2GH array, using {$bb_gh_default} instead\n"; | |
$user = $bb_gh_default; | |
} | |
echo "Creating Issue #{$i} in Github using user {$user} ..."; | |
$ch = curl_init("https://api.github.com/repos/{$gh_repo}/issues"); | |
$data = array( | |
'title' => $issue['title'], | |
'body' => $issue['content'], | |
'labels' => array($issue['metadata']['kind']) | |
); | |
curl_setopt_array($ch, array( | |
CURLOPT_POST => true, | |
CURLOPT_USERPWD => "$user:{$gh_conf[$user]}", | |
CURLOPT_POSTFIELDS => json_encode($data), | |
CURLOPT_RETURNTRANSFER => true | |
)); | |
$result = curl_exec($ch); | |
if($result) { | |
echo "OK\n"; | |
} | |
} | |
unset($_status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment