Skip to content

Commit

Permalink
Added support for label migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jul 13, 2012
1 parent 0f9c8d6 commit f7c4338
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion trac2github.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
// Do not convert milestones at this run
$skip_milestones = false;

// Do not convert labels at this run
$skip_labels = false;

// Do not convert tickets
$skip_tickets = false;
$ticket_offset = 0; // Start at this offset if limit > 0
Expand All @@ -49,6 +52,7 @@

// Uncomment to refresh cache
// @unlink($save_milestones);
// @unlink($save_labels);
// @unlink($save_tickets);

// DO NOT EDIT BELOW
Expand Down Expand Up @@ -92,6 +96,53 @@
file_put_contents($save_milestones, serialize($milestones));
}

$labels = array();
$labels['T'] = array();
$labels['C'] = array();
$labels['P'] = array();
$labels['R'] = array();
if (file_exists($save_labels)) {
$labels = unserialize(file_get_contents($save_labels));
}

if (!$skip_labels) {
// Export all "labels"
$res = $trac_db->query("SELECT DISTINCT 'T' label_type, type name, 'cccccc' color
FROM ticket WHERE IFNULL(type, '') <> ''
UNION
SELECT DISTINCT 'C' label_type, component name, '0000aa' color
FROM ticket WHERE IFNULL(component, '') <> ''
UNION
SELECT DISTINCT 'P' label_type, priority name, case when lower(priority) = 'urgent' then 'ff0000'
when lower(priority) = 'high' then 'ff6666'
when lower(priority) = 'medium' then 'ffaaaa'
when lower(priority) = 'low' then 'ffdddd'
else 'aa8888' end color
FROM ticket WHERE IFNULL(priority, '') <> ''
UNION
SELECT DISTINCT 'R' label_type, resolution name, '55ff55' color
FROM ticket WHERE IFNULL(resolution, '') <> ''");

foreach ($res->fetchAll() as $row) {
$resp = github_add_label(array(
'name' => $row['label_type'] . ': ' . $row['name'],
'color' => $row['color']
));

if (isset($resp['url'])) {
// OK
$labels[$row['label_type']][crc32($row['name'])] = $resp['name'];
echo "Label {$row['name']} converted to {$resp['name']}\n";
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert label {$row['name']}: $error\n";
}
}
// Serialize to restore in future
file_put_contents($save_labels, serialize($labels));
}

// Try get previously fetched tickets
$tickets = array();
if (file_exists($save_tickets)) {
Expand All @@ -109,13 +160,27 @@
if (empty($row['owner']) || !isset($users_list[$row['owner']])) {
$row['owner'] = $username;
}
$ticketLabels = array();
if (!empty($labels['T'][crc32($row['type'])])) {
$ticketLabels[] = $labels['T'][crc32($row['type'])];
}
if (!empty($labels['C'][crc32($row['component'])])) {
$ticketLabels[] = $labels['C'][crc32($row['component'])];
}
if (!empty($labels['P'][crc32($row['priority'])])) {
$ticketLabels[] = $labels['P'][crc32($row['priority'])];
}
if (!empty($labels['R'][crc32($row['resolution'])])) {
$ticketLabels[] = $labels['R'][crc32($row['resolution'])];
}

// There is a strange issue with summaries containing percent signs...
$resp = github_add_issue(array(
'title' => preg_replace("/%/", '[pct]', $row['summary']),
'body' => empty($row['description']) ? 'None' : translate_markup($row['description']),
'assignee' => isset($users_list[$row['owner']]) ? $users_list[$row['owner']] : $row['owner'],
'milestone' => $milestones[crc32($row['milestone'])]
'milestone' => $milestones[crc32($row['milestone'])],
'labels' => $ticketLabels
));
if (isset($resp['number'])) {
// OK
Expand All @@ -128,6 +193,7 @@
'body' => empty($row['description']) ? 'None' : translate_markup($row['description']),
'assignee' => isset($users_list[$row['owner']]) ? $users_list[$row['owner']] : $row['owner'],
'milestone' => $milestones[crc32($row['milestone'])],
'labels' => $ticketLabels,
'state' => 'closed'
));
if (isset($resp['number'])) {
Expand Down Expand Up @@ -192,6 +258,12 @@ function github_add_milestone($data) {
return json_decode(github_post("/repos/$project/$repo/milestones", json_encode($data)), true);
}

function github_add_label($data) {
global $project, $repo, $verbose;
if ($verbose) print_r($data);
return json_decode(github_post("/repos/$project/$repo/labels", json_encode($data)), true);
}

function github_add_issue($data) {
global $project, $repo, $verbose;
if ($verbose) print_r($data);
Expand Down

0 comments on commit f7c4338

Please sign in to comment.