예제 #1
0
function add_changes_for_ticket($ticket, $ticketLabels)
{
    global $trac_db, $tickets, $labels, $users_list, $milestones, $skip_comments, $time_in_us, $verbose;
    $res = $trac_db->query("SELECT ticket_change.* FROM ticket_change, ticket WHERE ticket.id = ticket_change.ticket AND ticket = {$ticket} ORDER BY ticket, time, field <> 'comment'");
    foreach ($res->fetchAll() as $row) {
        if ($verbose) {
            print_r($row);
        }
        if (!isset($tickets[$row['ticket']])) {
            echo "Skipping comment " . $row['time'] . " on unknown ticket " . $row['ticket'] . "\n";
            continue;
        }
        $timestamp = date("j M Y H:i e", $row['time'] / ($time_in_us ? 1000000 : 1));
        if ($row['field'] == 'comment') {
            if ($row['newvalue'] != '') {
                $text = '**Comment by ' . $row['author'] . ' on ' . $timestamp . "**\n" . $row['newvalue'];
            } else {
                $text = '**Modified by ' . $row['author'] . ' on ' . $timestamp . "**";
            }
            $resp = github_add_comment($tickets[$row['ticket']], translate_markup($text));
        } else {
            if (in_array($row['field'], ['component', 'priority', 'type', 'resolution'])) {
                if (in_array($labels[strtoupper($row['field'])[0]][crc32($row['oldvalue'])], $ticketLabels)) {
                    $index = array_search($labels[strtoupper($row['field'])[0]][crc32($row['oldvalue'])], $ticketLabels);
                    $ticketLabels[$index] = $labels[strtoupper($row['field'])[0]][crc32($row['newvalue'])];
                } else {
                    $ticketLabels[] = $labels[strtoupper($row['field'])[0]][crc32($row['newvalue'])];
                }
                $resp = github_update_issue($tickets[$ticket], array('labels' => array_values(array_filter($ticketLabels, 'strlen'))));
            } else {
                if ($row['field'] == 'status') {
                    $resp = github_update_issue($tickets[$ticket], array('state' => $row['newvalue'] == 'closed' ? 'closed' : 'open'));
                } else {
                    if ($row['field'] == 'summary') {
                        $resp = github_update_issue($tickets[$ticket], array('title' => $row['newvalue']));
                    } else {
                        if (false and $row['field'] == 'description') {
                            // TODO?
                            $body = make_body($row['newvalue']);
                            $timestamp = date("j M Y H:i e", $row['time'] / ($time_in_us ? 1000000 : 1));
                            // TODO:
                            //$body = '**Reported by ' . obfuscate_email($row['reporter']) . ' on ' . $timestamp . "**\n" . $body;
                            $resp = github_update_issue($tickets[$ticket], array('body' => $body));
                        } else {
                            if ($row['field'] == 'owner') {
                                if (!empty($row['newvalue'])) {
                                    $assignee = isset($users_list[$row['newvalue']]) ? $users_list[$row['newvalue']] : NULL;
                                } else {
                                    $assignee = NULL;
                                }
                                $resp = github_update_issue($tickets[$ticket], array('assignee' => $assignee));
                            } else {
                                if ($row['field'] == 'milestone') {
                                    if (empty($row['newvalue'])) {
                                        $milestone = NULL;
                                    } else {
                                        $milestone = $milestones[crc32($row['newvalue'])];
                                    }
                                    $resp = github_update_issue($tickets[$ticket], array('milestone' => $milestone));
                                } else {
                                    echo "WARNING: ignoring change of {$row['field']} to {$row['newvalue']}\n";
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (isset($resp['url'])) {
            // OK
            echo "Added change {$resp['url']}\n";
        } else {
            // Error
            $error = print_r($resp, 1);
            echo "Failed to add a comment for " . $row['ticket'] . ": {$error}\n";
            return false;
        }
        // Wait 1sec to ensure the next event will be after
        // just added (apparently github can reorder
        // changes/comments if added too fast)
        sleep(1);
    }
    return true;
}
예제 #2
0
        } else {
            // Error
            $error = print_r($resp, 1);
            echo "Failed to convert a ticket #{$row['id']}: {$error}\n";
        }
    }
    // Serialize to restore in future
    file_put_contents($save_tickets, serialize($tickets));
}
if (!$skip_comments) {
    // Export all comments
    $limit = $comments_limit > 0 ? "LIMIT {$comments_offset}, {$comments_limit}" : '';
    $res = $trac_db->query("SELECT * FROM `ticket_change` where `field` = 'comment' AND `newvalue` != '' ORDER BY `ticket`, `time` {$limit}");
    foreach ($res->fetchAll() as $row) {
        $text = strtolower($row['author']) == strtolower($username) ? $row['newvalue'] : '**Author: ' . $row['author'] . "**\n" . $row['newvalue'];
        $resp = github_add_comment($tickets[$row['ticket']], translate_markup($text));
        if (isset($resp['url'])) {
            // OK
            echo "Added comment {$resp['url']}\n";
        } else {
            // Error
            $error = print_r($resp, 1);
            echo "Failed to add a comment: {$error}\n";
        }
    }
}
echo "Done whatever possible, sorry if not.\n";
function github_post($url, $json, $patch = false)
{
    global $username, $password;
    $ch = curl_init();