Skip to content

Instantly share code, notes, and snippets.

@mcrider
Forked from mattbenic/index.php
Created September 5, 2012 21:09
Show Gist options
  • Save mcrider/3644801 to your computer and use it in GitHub Desktop.
Save mcrider/3644801 to your computer and use it in GitHub Desktop.
PHP script to bulk import issues to a GitHub repo from a .json file
<?php
/* PHP script to bulk import issues in json format from a file to a GitHub repository
*
* The json in the uploaded file should contain an array of issues at the top level.
* Fields in the json mapped to the issue title and body (nothing else is supported)
* are specified in the submission form.
*
* Depends on the php-github-api from here: https://github.com/ornicar/php-github-api
*/
session_start();
require_once '/php-github-api/lib/Github/Autoloader.php';
//Helper function to get a value from post or session
function valueInPostOrSession($key, $default = NULL)
{
if ($_POST[$key]) return $_POST[$key];
if ($_SESSION[$key]) return $_SESSION[$key];
return $default;
}
// Pull some values from POST/SESSION early so the form is populated if at all possible
$username = valueInPostOrSession('username');
$password = valueInPostOrSession('password');
$repoowner = valueInPostOrSession('repoowner');
$reponame = valueInPostOrSession('reponame');
$issuestitlefield = valueInPostOrSession('issuestitlefield', 'title');
$issuesbodyfield = valueInPostOrSession('issuesbodyfield', 'body');
// If no action don't do any of this stuff
$action = $_POST['action'];
if ($action)
{
Github_Autoloader::register();
$github = new Github_Client();
// Authenticate with GitHub
$result = NULL;
if ($username && $password)
{
$github->authenticate($username, $password, Github_Client::AUTH_HTTP_PASSWORD);
$authenticated = 1;
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
}
// Perform whatever action was requested (just import for now)
if ($authenticated)
{
switch ($action)
{
case "importissues":
{
// Grab the issues file and json decode it
$issuesfilename = $_FILES['issuesfilename']['name'];
$issuesjson = file_get_contents($_FILES['issuesfilename']['tmp_name']);
$issues = json_decode($issuesjson);
$json_err = json_last_error();
if ($json_err || !$issues || 0==count($issues))
{
$result = "Failed with json_last_error=".$json_err;
}
else
{
// Run through issues, posting them to github (and adding to output)
$result = "Success, ".count($issues)." issues found:<br>".
"<table>".
"<tr>".
"<th/><th>title</th><th>body</th>".
"</tr>";
$count = 0;
foreach ($issues as $issue)
{
$issueArray = (array) $issue;
$result .= "<tr>".
"<td>$count</td>".
"<td>".$issueArray[$issuestitlefield]."</td>".
"<td>".$issueArray[$issuesbodyfield]."</td></tr>";
$github->getIssueApi()->open($repoowner, $reponame, $issueArray[$issuestitlefield], $issueArray[$issuesbodyfield]);
++$count;
}
$result .= "</table>";
$_SESSION['repoowner'] = $repoowner;
$_SESSION['reponame'] = $reponame;
}
}
break;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
if ($result)
{
?>
<div id="result">
<i><?php echo($result);?></i></br>
</div>
<?php
}
?>
<div id="importissues">
<form enctype="multipart/form-data" action="index.php?XDEBUG_SESSION_START=netbeans-xdebug" method="post">
<b>Github Login</b><br/>
Username: <input name="username" value="<?echo($username)?>"/><br/>
Password: <input type="password" name="password" value="<?echo($password)?>"/><br/>
<br>
<b>Import Issues</b><br/>
Repository owner (username): <input name="repoowner" value="<?echo($repoowner)?>"/><br/>
Repository name: <input name="reponame" value="<?echo($reponame)?>"/><br/>
<!-- Issues (json): <input name="issuesjson" value="<?/*echo($issuesjson)*/?>"/><br/> -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Issues file (json): <input name="issuesfilename" type="file" value="<?echo($issuestitlefield)?>"/><br/>
Title field in json: <input name="issuestitlefield" value="<?echo($issuestitlefield)?>"/><br/>
Body field in json: <input name="issuesbodyfield" value="<?echo($issuesbodyfield)?>"/><br/>
<input type="hidden" name="action" value="importissues"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment