Skip to content

Commit

Permalink
Check/add severity labels; remove second page of labels due to proble…
Browse files Browse the repository at this point in the history
…ms if empty; further slow down requests to prevent perceived abuse
  • Loading branch information
karlmsmith committed Nov 22, 2017
1 parent 2115e49 commit 811bdfa
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions trac2github.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
$labels['C'] = array();
$labels['P'] = array();
$labels['R'] = array();
$labels['S'] = array();

if (!$skip_labels) {
// Export all "labels"
Expand All @@ -129,11 +130,17 @@
FROM ticket WHERE COALESCE(priority, '') <> ''
UNION
SELECT DISTINCT 'R' AS label_type, resolution AS name, '55ff55' AS color
FROM ticket WHERE COALESCE(resolution, '') <> ''");
FROM ticket WHERE COALESCE(resolution, '') <> ''
UNION
SELECT DISTINCT 'S' AS label_type, severity AS name, 'ff55ff' AS color
FROM ticket WHERE COALESCE(severity, '') <> ''");

$existing_labels = array();
foreach (github_get_labels() as $l) {
$existing_labels[] = urldecode($l['name']);
if ($verbose) {
echo "found GitHub label {$l['name']}\n";
}
}
foreach ($res->fetchAll() as $row) {
$label_name = $row['label_type'] . ': ' . str_replace(",", "", $row['name']);
Expand Down Expand Up @@ -161,7 +168,7 @@
} else {
// Error
$error = print_r($resp, 1);
echo "Failed to convert label {$row['name']}: $error\n";
echo "Failed to convert trac field {$label_name}: $error\n";
}
}
}
Expand Down Expand Up @@ -206,7 +213,7 @@
}
if (!$skip_comments) {
// restore original values (at ticket creation time), to restore modification history later
foreach (['owner', 'priority', 'resolution', 'milestone', 'type', 'component', 'description', 'summary'] as $f) {
foreach ( array('owner', 'priority', 'resolution', 'milestone', 'type', 'component', 'description', 'summary') as $f ) {
$row[$f] = trac_orig_value($row, $f);
}
}
Expand All @@ -226,6 +233,9 @@
if (!empty($labels['R'][crc32($row['resolution'])])) {
$ticketLabels[] = $labels['R'][crc32($row['resolution'])];
}
if (!empty($labels['S'][crc32($row['severity'])])) {
$ticketLabels[] = $labels['S'][crc32($row['severity'])];
}

$body = make_body($row['description']);
$timestamp = date("j M Y H:i e", $row['time']/1000000);
Expand Down Expand Up @@ -313,7 +323,7 @@ function add_changes_for_ticket($ticket, $ticketLabels) {
$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'])) {
} else if (in_array($row['field'], array('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'])];
Expand Down Expand Up @@ -374,13 +384,14 @@ function add_changes_for_ticket($ticket, $ticketLabels) {
// Wait 1sec to ensure the next event will be after
// just added (apparently github can reorder
// changes/comments if added too fast)
sleep(1);
// Change to 10 sec to slow things down to prevent perceived abuse of GitHub
sleep(10);
}
return true;
}

function github_req($url, $json, $patch = false, $post = true) {
global $username, $password, $request_count;
global $username, $password, $request_count, $project, $user_email;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, "https://api.github.com$url");
Expand All @@ -389,7 +400,7 @@ function github_req($url, $json, $patch = false, $post = true) {
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_USERAGENT, "trac2github for $project, [email protected]");
curl_setopt($ch, CURLOPT_USERAGENT, "trac2github for $project, $user_email");
if ($patch) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
} else if ($post) {
Expand All @@ -411,11 +422,11 @@ function github_req($url, $json, $patch = false, $post = true) {

if ($patch || $post) {
$request_count++;
if($request_count > 50) {
if($request_count > 20) {
// Slow things down to prevent perceived abuse of GitHub
sleep(70);
$request_count = 0;
}

}

return $body;
Expand Down Expand Up @@ -460,7 +471,7 @@ function github_get_milestones() {
function github_get_labels() {
global $project, $repo, $verbose;
if ($verbose) print_r($body);
return array_merge(json_decode(github_req("/repos/$project/$repo/labels?per_page=100", false, false, false), true), json_decode(github_req("/repos/$project/$repo/labels?page=2&per_page=100", false, false, false), true));
return json_decode(github_req("/repos/$project/$repo/labels?per_page=100", false, false, false), true);
}

function make_body($description) {
Expand Down

0 comments on commit 811bdfa

Please sign in to comment.