| 1 | package RTx::ToGitHub; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use namespace::autoclean; | ||
| 8 | |||
| 9 | our $VERSION = '0.09'; | ||
| 10 | |||
| 11 | use CPAN::Meta; | ||
| 12 | use Carp; | ||
| 13 | use Data::Dumper::Concise; | ||
| 14 | use Encode qw( encode ); | ||
| 15 | use IO::Prompt::Tiny; | ||
| 16 | use IPC::System::Simple qw( capturex ); | ||
| 17 | use Path::Tiny; | ||
| 18 | use Pithub; | ||
| 19 | use RT::Client::REST::Ticket; | ||
| 20 | use RT::Client::REST::User; | ||
| 21 | use RT::Client::REST; | ||
| 22 | use Specio::Declare; | ||
| 23 | use Specio::Library::Builtins; | ||
| 24 | use Specio::Library::Numeric; | ||
| 25 | use Specio::Library::Perl; | ||
| 26 | use Specio::Library::String; | ||
| 27 | use Try::Tiny; | ||
| 28 | use URI::Escape qw( uri_escape ); | ||
| 29 | |||
| 30 | use Moo; | ||
| 31 | use MooX::Options; | ||
| 32 | |||
| 33 | option dry => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => t('Bool'), | ||
| 36 | default => 0, | ||
| 37 | doc => 'Dry run only. No changes will be made to RT or GitHub.', | ||
| 38 | ); | ||
| 39 | |||
| 40 | option prompt => ( | ||
| 41 | is => 'ro', | ||
| 42 | isa => t('Bool'), | ||
| 43 | negativable => 1, | ||
| 44 | default => 1, | ||
| 45 | doc => 'Set this to false to disable all prompts for information. The' | ||
| 46 | . ' command will die if it cannot determine all the needed parameters' | ||
| 47 | . ' from CLI options or other means.', | ||
| 48 | ); | ||
| 49 | |||
| 50 | option github_user => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => t('NonEmptyStr'), | ||
| 53 | format => 's', | ||
| 54 | lazy => 1, | ||
| 55 | builder => '_build_github_user', | ||
| 56 | doc => | ||
| 57 | 'The GitHub user to use. Will default to github.user in your git config if available.' | ||
| 58 | . ' Otherwise it will look at the remote URL and try to guess.', | ||
| 59 | ); | ||
| 60 | |||
| 61 | option github_token => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => t('NonEmptyStr'), | ||
| 64 | format => 's', | ||
| 65 | lazy => 1, | ||
| 66 | builder => '_build_github_token', | ||
| 67 | doc => | ||
| 68 | 'The GitHub token to use. Will default to github.token in your git config if available.', | ||
| 69 | ); | ||
| 70 | |||
| 71 | option repo => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => t('NonEmptyStr'), | ||
| 74 | format => 's', | ||
| 75 | lazy => 1, | ||
| 76 | builder => '_build_repo', | ||
| 77 | doc => | ||
| 78 | 'The GitHub repo to operate against. By default this will be determined' | ||
| 79 | . ' by looking at URL for the origin remote.', | ||
| 80 | ); | ||
| 81 | |||
| 82 | option pause_id => ( | ||
| 83 | is => 'ro', | ||
| 84 | isa => t('NonEmptyStr'), | ||
| 85 | format => 's', | ||
| 86 | lazy => 1, | ||
| 87 | builder => '_build_pause_id', | ||
| 88 | doc => | ||
| 89 | 'Your PAUSE ID. This will be found by looking at ~/.pause if that file exists.', | ||
| 90 | ); | ||
| 91 | |||
| 92 | option pause_password => ( | ||
| 93 | is => 'ro', | ||
| 94 | isa => t('NonEmptyStr'), | ||
| 95 | format => 's', | ||
| 96 | lazy => 1, | ||
| 97 | builder => '_build_pause_password', | ||
| 98 | doc => | ||
| 99 | 'Your PAUSE password. This will be found by looking at ~/.pause if that file exists.', | ||
| 100 | ); | ||
| 101 | |||
| 102 | option dist => ( | ||
| 103 | is => 'ro', | ||
| 104 | isa => t('DistName'), | ||
| 105 | format => 's', | ||
| 106 | lazy => 1, | ||
| 107 | builder => '_build_dist', | ||
| 108 | doc => | ||
| 109 | 'The distribution name as seen on RT. By default this will be determined by' | ||
| 110 | . ' looking at [MY]META.* files in the working directory, a dist.ini, or the repo name.', | ||
| 111 | ); | ||
| 112 | |||
| 113 | option force => ( | ||
| 114 | is => 'ro', | ||
| 115 | isa => t('Bool'), | ||
| 116 | lazy => 1, | ||
| 117 | default => sub { $_[0]->test ? 1 : 0 }, | ||
| 118 | doc => 'Create issues in GitHub even if one already exists.', | ||
| 119 | ); | ||
| 120 | |||
| 121 | option resolve => ( | ||
| 122 | is => 'ro', | ||
| 123 | isa => t('Bool'), | ||
| 124 | negativable => 1, | ||
| 125 | lazy => 1, | ||
| 126 | default => sub { $_[0]->test ? 0 : 1 }, | ||
| 127 | doc => | ||
| 128 | 'Set this to false to to disable resolving RT tickets as they are converted.', | ||
| 129 | ); | ||
| 130 | |||
| 131 | option ticket => ( | ||
| 132 | is => 'ro', | ||
| 133 | isa => t('PositiveInt'), | ||
| 134 | format => 'i', | ||
| 135 | predicate => '_has_ticket', | ||
| 136 | doc => 'Only operate on the given RT ticket.', | ||
| 137 | ); | ||
| 138 | |||
| 139 | option test => ( | ||
| 140 | is => 'ro', | ||
| 141 | isa => t('Bool'), | ||
| 142 | lazy => 1, | ||
| 143 | default => 0, | ||
| 144 | doc => | ||
| 145 | 'Run in test mode. This is equivalent to setting --no-resolve and --force.' | ||
| 146 | . ' It also changes how GitHub tickets are formatted to avoid including' | ||
| 147 | . ' @mentions of other people so they do not get a flood of email while you test.', | ||
| 148 | ); | ||
| 149 | |||
| 150 | option debug_ua => ( | ||
| 151 | is => 'ro', | ||
| 152 | isa => t('Bool'), | ||
| 153 | lazy => 1, | ||
| 154 | default => 0, | ||
| 155 | doc => 'Use LWP::ConsoleLogger to debug the interactions with RT', | ||
| 156 | ); | ||
| 157 | |||
| 158 | has _default_dist_name => ( | ||
| 159 | is => 'ro', | ||
| 160 | isa => t('Str'), | ||
| 161 | lazy => 1, | ||
| 162 | builder => '_build_default_dist_name', | ||
| 163 | ); | ||
| 164 | |||
| 165 | has _pause_rc_values => ( | ||
| 166 | is => 'ro', | ||
| 167 | isa => t( 'HashRef', of => t('NonEmptyStr') ), | ||
| 168 | lazy => 1, | ||
| 169 | builder => '_build_pause_rc_values', | ||
| 170 | ); | ||
| 171 | |||
| 172 | has _git_config => ( | ||
| 173 | is => 'ro', | ||
| 174 | isa => t('HashRef'), | ||
| 175 | lazy => 1, | ||
| 176 | builder => '_build_git_config', | ||
| 177 | ); | ||
| 178 | |||
| 179 | has _rt_client => ( | ||
| 180 | is => 'ro', | ||
| 181 | isa => object_isa_type('RT::Client::REST'), | ||
| 182 | lazy => 1, | ||
| 183 | builder => '_build_rt_client', | ||
| 184 | ); | ||
| 185 | |||
| 186 | has _pithub => ( | ||
| 187 | is => 'ro', | ||
| 188 | isa => object_isa_type('Pithub'), | ||
| 189 | lazy => 1, | ||
| 190 | builder => '_build_pithub', | ||
| 191 | ); | ||
| 192 | |||
| 193 | has _rt_to_github_map => ( | ||
| 194 | is => 'ro', | ||
| 195 | isa => t('HashRef'), | ||
| 196 | lazy => 1, | ||
| 197 | builder => '_build_rt_to_github_map', | ||
| 198 | ); | ||
| 199 | |||
| 200 | has _contributor_map => ( | ||
| 201 | is => 'ro', | ||
| 202 | isa => t('HashRef'), | ||
| 203 | lazy => 1, | ||
| 204 | builder => '_build_contributor_map', | ||
| 205 | ); | ||
| 206 | |||
| 207 | sub run { | ||
| 208 | my $self = shift; | ||
| 209 | |||
| 210 | $self->_ensure_all_config; | ||
| 211 | |||
| 212 | my $msg = sprintf( | ||
| 213 | 'Converting %s from the %s RT queue to the %s/%s GitHub repo.', | ||
| 214 | ( $self->ticket ? 'ticket #' . $self->ticket : 'all tickets' ), | ||
| 215 | $self->dist, | ||
| 216 | $self->github_user, | ||
| 217 | $self->repo | ||
| 218 | ); | ||
| 219 | say $msg or die $!; | ||
| 220 | if ( $self->resolve ) { | ||
| 221 | say 'Will resolve all RT tickets.' or die $!; | ||
| 222 | } | ||
| 223 | unless ( $self->force ) { | ||
| 224 | say 'Will skip tickets that are already converted.' or die $!; | ||
| 225 | } | ||
| 226 | |||
| 227 | $self->_convert_tickets; | ||
| 228 | |||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _ensure_all_config { | ||
| 233 | my $self = shift; | ||
| 234 | |||
| 235 | # Each of these will look for a default and/or prompt the user if not set | ||
| 236 | # on the CLI. If the value it comes up with is not valid (an empty string, | ||
| 237 | # for example) it will die. | ||
| 238 | $self->github_user; | ||
| 239 | $self->github_token; | ||
| 240 | $self->repo; | ||
| 241 | $self->pause_id; | ||
| 242 | $self->pause_password; | ||
| 243 | $self->dist; | ||
| 244 | |||
| 245 | return; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub _build_git_config { | ||
| 249 | my $self = shift; | ||
| 250 | |||
| 251 | my %config; | ||
| 252 | for my $line ( split /\n/, capturex(qw( git config --list )) ) { | ||
| 253 | my ( $k, $v ) = split /=/, $line, 2; | ||
| 254 | $config{$k} = $v; | ||
| 255 | } | ||
| 256 | |||
| 257 | return \%config; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub _build_github_user { | ||
| 261 | my $self = shift; | ||
| 262 | |||
| 263 | my $name = $self->_git_config->{'github.user'}; | ||
| 264 | if ( !defined $name | ||
| 265 | && ( $self->_git_config->{'remote.origin.url'} // q{} ) | ||
| 266 | =~ m{github\.com[:/]([^/]+)} ) { | ||
| 267 | |||
| 268 | $name = $1; | ||
| 269 | } | ||
| 270 | |||
| 271 | $name = IO::Prompt::Tiny::prompt( 'GitHub user:', ( $name // q{} ) ) | ||
| 272 | if $self->prompt; | ||
| 273 | |||
| 274 | return $name; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _build_github_token { | ||
| 278 | my $self = shift; | ||
| 279 | |||
| 280 | my $token = $self->_git_config->{'github.token'}; | ||
| 281 | |||
| 282 | $token = IO::Prompt::Tiny::prompt( 'GitHub token:', ( $token // q{} ) ) | ||
| 283 | if $self->prompt; | ||
| 284 | |||
| 285 | return $token; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _build_repo { | ||
| 289 | my $self = shift; | ||
| 290 | |||
| 291 | my ($repo) | ||
| 292 | = ( $self->_git_config->{'remote.origin.url'} // q{} ) | ||
| 293 | =~ m{github\.com[:/].+?/(.+?)(?:\.git)?$}; | ||
| 294 | |||
| 295 | $repo = IO::Prompt::Tiny::prompt( 'GitHub repo:', ( $repo // q{} ) ) | ||
| 296 | if $self->prompt; | ||
| 297 | |||
| 298 | return $repo; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub _build_pause_id { | ||
| 302 | my $self = shift; | ||
| 303 | |||
| 304 | my $id = $self->_pause_rc_values->{user}; | ||
| 305 | |||
| 306 | $id = IO::Prompt::Tiny::prompt( 'PAUSE ID:', ( $id // q{} ) ) | ||
| 307 | if $self->prompt; | ||
| 308 | |||
| 309 | return $id; | ||
| 310 | } | ||
| 311 | |||
| 312 | sub _build_pause_password { | ||
| 313 | my $self = shift; | ||
| 314 | |||
| 315 | my $password = $self->_pause_rc_values->{password}; | ||
| 316 | |||
| 317 | $password | ||
| 318 | = IO::Prompt::Tiny::prompt( 'PAUSE password:', ( $password // q{} ) ) | ||
| 319 | if $self->prompt; | ||
| 320 | |||
| 321 | return $password; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub _build_pause_rc_values { | ||
| 325 | my $pause_rc = path( $ENV{HOME}, '.pause' ); | ||
| 326 | return {} unless $pause_rc->exists; | ||
| 327 | |||
| 328 | my %config; | ||
| 329 | for my $line ( $pause_rc->lines( { chomp => 1 } ) ) { | ||
| 330 | my ( $k, $v ) = split / +/, $line, 2; | ||
| 331 | $config{$k} = $v; | ||
| 332 | } | ||
| 333 | |||
| 334 | return \%config; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub _build_dist { | ||
| 338 | my $self = shift; | ||
| 339 | |||
| 340 | my $dist = $self->_dist_from_local_files; | ||
| 341 | $dist //= $self->repo; | ||
| 342 | |||
| 343 | $dist = IO::Prompt::Tiny::prompt( 'RT distro name:', ( $dist // q{} ) ) | ||
| 344 | if $self->prompt; | ||
| 345 | |||
| 346 | return $dist; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub _dist_from_local_files { | ||
| 350 | my ($meta) = grep {-r} qw( MYMETA.json MYMETA.yml META.json META.yml ); | ||
| 351 | if ($meta) { | ||
| 352 | my $cm = CPAN::Meta->load_file($meta); | ||
| 353 | return $cm->name; | ||
| 354 | } | ||
| 355 | elsif ( -r 'dist.ini' ) { | ||
| 356 | my $dist = path('dist.ini'); | ||
| 357 | for my $line ( $dist->lines( { chomp => 1 } ) ) { | ||
| 358 | my ($name) = $line =~ /name\s*=\s*(\S+)/; | ||
| 359 | return $name if defined $name; | ||
| 360 | |||
| 361 | # We'll assume that plugin config always comes after core dzil | ||
| 362 | # config | ||
| 363 | last if $line =~ /^\[/; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | sub _convert_tickets { | ||
| 369 | my $self = shift; | ||
| 370 | |||
| 371 | my $query = sprintf( <<'EOF', $self->dist ); | ||
| 372 | Queue = '%s' | ||
| 373 | and | ||
| 374 | ( Status = 'new' or Status = 'open' or Status = 'stalled' or Status = 'patched') | ||
| 375 | EOF | ||
| 376 | $query .= sprintf( 'and id = %s', $self->ticket ) if $self->_has_ticket; | ||
| 377 | |||
| 378 | my @rt_tickets = $self->_rt_client->search( | ||
| 379 | type => 'ticket', | ||
| 380 | query => $query, | ||
| 381 | ); | ||
| 382 | |||
| 383 | for my $id (@rt_tickets) { | ||
| 384 | unless ( $self->force ) { | ||
| 385 | if ( my $issue = $self->_rt_to_github_map->{$id} ) { | ||
| 386 | say | ||
| 387 | "Ticket #$id is already on GitHub as $issue->{number} ($issue->{html_url})" | ||
| 388 | or die $!; | ||
| 389 | next; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | $self->_convert_one_ticket($id); | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _build_rt_client { | ||
| 398 | my $self = shift; | ||
| 399 | |||
| 400 | my $rt = RT::Client::REST->new( server => 'https://rt.cpan.org/' ); | ||
| 401 | |||
| 402 | if ( $self->debug_ua ) { | ||
| 403 | require LWP::ConsoleLogger; | ||
| 404 | my $logger = LWP::ConsoleLogger->new( | ||
| 405 | dump_params => 1, | ||
| 406 | ); | ||
| 407 | $rt->_ua->add_handler( | ||
| 408 | 'response_done', | ||
| 409 | sub { $logger->response_callback(@_) } | ||
| 410 | ); | ||
| 411 | $rt->_ua->add_handler( | ||
| 412 | 'request_send', | ||
| 413 | sub { $logger->request_callback(@_) } | ||
| 414 | ); | ||
| 415 | } | ||
| 416 | |||
| 417 | $rt->login( | ||
| 418 | username => $self->pause_id, | ||
| 419 | password => $self->pause_password, | ||
| 420 | ); | ||
| 421 | |||
| 422 | return $rt; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub _build_pithub { | ||
| 426 | my $self = shift; | ||
| 427 | |||
| 428 | return Pithub->new( | ||
| 429 | user => $self->github_user, | ||
| 430 | repo => $self->repo, | ||
| 431 | token => $self->github_token, | ||
| 432 | ); | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _build_rt_to_github_map { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | my $issues = $self->_pithub->issues->list; | ||
| 439 | $issues->auto_pagination(1); | ||
| 440 | |||
| 441 | my %map; | ||
| 442 | while ( my $issue = $issues->next ) { | ||
| 443 | next unless exists $issue->{body}; | ||
| 444 | if ( $issue->{body} =~ /\[rt\.cpan\.org #(\d+)\]/ ) { | ||
| 445 | $map{$1} = $issue; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | |||
| 449 | return \%map; | ||
| 450 | } | ||
| 451 | |||
| 452 | sub _convert_one_ticket { | ||
| 453 | my $self = shift; | ||
| 454 | my $id = shift; | ||
| 455 | |||
| 456 | my ( $ticket, $trunc_subject, $main, @comments ) | ||
| 457 | = $self->_extract_ticket_data($id); | ||
| 458 | |||
| 459 | my @args = ( $id, $trunc_subject, $main, @comments ); | ||
| 460 | if ( $self->dry ) { | ||
| 461 | $self->_print_dry_ticket(@args); | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | my $gh_url = $self->_make_github_issue(@args) | ||
| 465 | or return; | ||
| 466 | $self->_close_rt_ticket( $id, $ticket, $gh_url ); | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | sub _extract_ticket_data { | ||
| 471 | my $self = shift; | ||
| 472 | my $id = shift; | ||
| 473 | |||
| 474 | my $ticket = RT::Client::REST::Ticket->new( | ||
| 475 | rt => $self->_rt_client, | ||
| 476 | id => $id, | ||
| 477 | ); | ||
| 478 | $ticket->retrieve; | ||
| 479 | |||
| 480 | my $subject = $ticket->subject; | ||
| 481 | my $trunc_subject | ||
| 482 | = length($subject) <= 24 | ||
| 483 | ? $subject | ||
| 484 | : ( substr( $subject, 0, 20 ) . ' ...' ); | ||
| 485 | my $status = $ticket->status; | ||
| 486 | my $body | ||
| 487 | = "Migrated from [rt.cpan.org #$id](https://rt.cpan.org/Ticket/Display.html?id=$id) (status was '$status')\n"; | ||
| 488 | |||
| 489 | $body .= "\nRequestors:\n"; | ||
| 490 | $body .= join q{}, | ||
| 491 | map {"* $_\n"} | ||
| 492 | map { $self->_maybe_tag_email($_) } $ticket->requestors; | ||
| 493 | |||
| 494 | my @attach_links; | ||
| 495 | my $attach = $ticket->attachments->get_iterator; | ||
| 496 | while ( my $i = $attach->() ) { | ||
| 497 | my $xact = $i->transaction_id; | ||
| 498 | my $att_id = $i->id; | ||
| 499 | my $name = $i->file_name or next; | ||
| 500 | push @attach_links, sprintf( | ||
| 501 | '[%s](https://rt.cpan.org/Ticket/Attachment/%s/%s/%s)', | ||
| 502 | $name, | ||
| 503 | $xact, | ||
| 504 | $att_id, | ||
| 505 | uri_escape($name), | ||
| 506 | ); | ||
| 507 | } | ||
| 508 | if (@attach_links) { | ||
| 509 | my $attach_list = join( q{}, map {"* $_\n"} @attach_links ); | ||
| 510 | $body .= "\nAttachments:\n$attach_list\n"; | ||
| 511 | } | ||
| 512 | |||
| 513 | my $create = $ticket->transactions( type => 'Create' )->get_iterator->(); | ||
| 514 | my $from = $self->_transaction_from($create); | ||
| 515 | $body .= sprintf( "\n%s\n\n%s\n\n", $from, $create->content ); | ||
| 516 | |||
| 517 | my @comments; | ||
| 518 | my $rt_comments | ||
| 519 | = $ticket->transactions( type => 'Correspond' )->get_iterator; | ||
| 520 | while ( my $c = $rt_comments->() ) { | ||
| 521 | my $c_from = $self->_transaction_from($c); | ||
| 522 | push @comments, | ||
| 523 | { body => sprintf( "%s\n\n%s\n", $c_from, $c->content ) }; | ||
| 524 | } | ||
| 525 | |||
| 526 | return ( | ||
| 527 | $ticket, | ||
| 528 | $trunc_subject, | ||
| 529 | { | ||
| 530 | title => $subject, | ||
| 531 | body => $body, | ||
| 532 | labels => [ ( $status eq 'stalled' ? 'stalled' : () ) ], | ||
| 533 | }, | ||
| 534 | @comments, | ||
| 535 | ); | ||
| 536 | } | ||
| 537 | |||
| 538 | { | ||
| 539 | my %cache; | ||
| 540 | |||
| 541 | sub _transaction_from { | ||
| 542 | my $self = shift; | ||
| 543 | my $trans = shift; | ||
| 544 | |||
| 545 | my $user = $cache{ $trans->creator } ||= do { | ||
| 546 | try { | ||
| 547 | my $u = RT::Client::REST::User->new( | ||
| 548 | id => $trans->creator, | ||
| 549 | rt => $trans->rt, | ||
| 550 | ); | ||
| 551 | $u->retrieve; | ||
| 552 | $u; | ||
| 553 | }; | ||
| 554 | }; | ||
| 555 | |||
| 556 | my $email; | ||
| 557 | $email = lc $user->email_address if $user; | ||
| 558 | $email //= lc $trans->creator; | ||
| 559 | |||
| 560 | return sprintf( | ||
| 561 | 'From %s on %s:', | ||
| 562 | $self->_maybe_tag_email($email), | ||
| 563 | $trans->created | ||
| 564 | ); | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | sub _maybe_tag_email { | ||
| 569 | my $self = shift; | ||
| 570 | my $email = shift; | ||
| 571 | |||
| 572 | my $text = $email; | ||
| 573 | my $contributors = $self->_contributor_map; | ||
| 574 | $text | ||
| 575 | .= ' (@' | ||
| 576 | . ( $self->test ? q{ } : q{} ) | ||
| 577 | . $contributors->{ lc $email } . ')' | ||
| 578 | if $contributors->{ lc $email }; | ||
| 579 | |||
| 580 | return $text; | ||
| 581 | } | ||
| 582 | |||
| 583 | sub _build_contributor_map { | ||
| 584 | local $@ = undef; | ||
| 585 | ## no critic (BuiltinFunctions::ProhibitStringyEval) | ||
| 586 | my $map = eval do { local $/ = undef; <DATA> }; | ||
| 587 | die $@ if $@; | ||
| 588 | return $map; | ||
| 589 | } | ||
| 590 | |||
| 591 | sub _print_dry_ticket { | ||
| 592 | my $self = shift; | ||
| 593 | my $id = shift; | ||
| 594 | my $trunc_subject = shift; | ||
| 595 | my $main = shift; | ||
| 596 | my @comments = @_; | ||
| 597 | |||
| 598 | say "----------------------------------\n" or die $!; | ||
| 599 | |||
| 600 | say "ticket #$id ($trunc_subject) would be copied to github as:" | ||
| 601 | or die $!; | ||
| 602 | |||
| 603 | $main->{body} =~ s/^/ /gm; | ||
| 604 | say " Subject: $main->{title}\n\n$main->{body}" or die $!; | ||
| 605 | |||
| 606 | for my $c (@comments) { | ||
| 607 | say ' ----' or die $!; | ||
| 608 | $c->{body} =~ s/^/ /gm; | ||
| 609 | say $c->{body} or die $!; | ||
| 610 | } | ||
| 611 | say "\n" or die $!; | ||
| 612 | |||
| 613 | return; | ||
| 614 | } | ||
| 615 | |||
| 616 | sub _make_github_issue { | ||
| 617 | my $self = shift; | ||
| 618 | my $id = shift; | ||
| 619 | my $trunc_subject = shift; | ||
| 620 | my $main = shift; | ||
| 621 | my @comments = @_; | ||
| 622 | |||
| 623 | my $result; | ||
| 624 | return unless try { | ||
| 625 | $result = $self->_pithub->issues->create( | ||
| 626 | data => { | ||
| 627 | title => encode( 'UTF-8', $main->{title} ), | ||
| 628 | body => encode( 'UTF-8', $main->{body} ), | ||
| 629 | } | ||
| 630 | ); | ||
| 631 | 1; | ||
| 632 | } | ||
| 633 | catch { | ||
| 634 | say "Ticket #$id ($trunc_subject) had an error posting to Github: $_" | ||
| 635 | or die $!; | ||
| 636 | 0; | ||
| 637 | }; | ||
| 638 | |||
| 639 | unless ( $result->success ) { | ||
| 640 | say "Ticket #$id ($trunc_subject) had an error posting to Github:" | ||
| 641 | or die $!; | ||
| 642 | say Dumper( $result->content ) or die $!; | ||
| 643 | return; | ||
| 644 | } | ||
| 645 | my $issue = $result->first; | ||
| 646 | |||
| 647 | my $gh_id = $issue->{number}; | ||
| 648 | my $gh_url = $issue->{html_url}; | ||
| 649 | say "Ticket #$id ($trunc_subject) copied to GitHub as #$gh_id ($gh_url)" | ||
| 650 | or die $!; | ||
| 651 | |||
| 652 | for my $c (@comments) { | ||
| 653 | my $c_result = $self->_pithub->issues->comments->create( | ||
| 654 | issue_id => $gh_id, | ||
| 655 | data => { body => encode( 'UTF-8', $c->{body} ) }, | ||
| 656 | ); | ||
| 657 | unless ( $c_result->success ) { | ||
| 658 | say "Error adding a comment to issue #$gh_id:" or die $!; | ||
| 659 | say Dumper( $c_result->content ) or die $!; | ||
| 660 | return; | ||
| 661 | } | ||
| 662 | } | ||
| 663 | |||
| 664 | return $gh_url; | ||
| 665 | } | ||
| 666 | |||
| 667 | sub _close_rt_ticket { | ||
| 668 | my $self = shift; | ||
| 669 | my $id = shift; | ||
| 670 | my $ticket = shift; | ||
| 671 | my $gh_url = shift; | ||
| 672 | |||
| 673 | return unless $self->resolve; | ||
| 674 | |||
| 675 | return unless try { | ||
| 676 | $self->_rt_client->correspond( | ||
| 677 | ticket_id => $id, | ||
| 678 | message => "Ticket migrated to GitHub as $gh_url" | ||
| 679 | ); | ||
| 680 | $ticket->status('resolved'); | ||
| 681 | $ticket->store; | ||
| 682 | 1; | ||
| 683 | } | ||
| 684 | catch { | ||
| 685 | say "Error closing ticket #$id on RT" or die $!; | ||
| 686 | 0; | ||
| 687 | }; | ||
| 688 | |||
| 689 | say "Closed ticket #$id on RT" or die $!; | ||
| 690 | } | ||
| 691 | |||
| 692 | if ( RT::Client::REST->VERSION <= 0.50 ) { | ||
| 693 | no warnings 'redefine'; | ||
| 694 | |||
| 695 | ## no critic (ValuesAndExpressions::ProhibitInterpolationOfLiterals, RegularExpressions::ProhibitUnusualDelimiters, ValuesAndExpressions::ProhibitCommaSeparatedStatements) | ||
| 696 | |||
| 697 | #<<< | ||
| 698 | *RT::Client::REST::Object::from_form = sub { | ||
| 699 | my $self = shift; | ||
| 700 | |||
| 701 | unless (@_) { | ||
| 702 | RT::Client::REST::Object::NoValuesProvidedException->throw; | ||
| 703 | } | ||
| 704 | |||
| 705 | my $hash = shift; | ||
| 706 | |||
| 707 | unless ('HASH' eq ref($hash)) { | ||
| 708 | RT::Client::REST::Object::InvalidValueException->throw( | ||
| 709 | "Expecting a hash reference as argument to 'from_form'", | ||
| 710 | ); | ||
| 711 | } | ||
| 712 | |||
| 713 | # lowercase hash keys | ||
| 714 | my $i = 0; | ||
| 715 | $hash = { map { ($i++ & 1) ? $_ : lc } %$hash }; | ||
| 716 | |||
| 717 | my $attributes = $self->_attributes; | ||
| 718 | my %rest2attr; # Mapping of REST names to our attributes; | ||
| 719 | while (my ($attr, $value) = each(%$attributes)) { | ||
| 720 | my $rest_name = (exists($attributes->{$attr}{rest_name}) ? | ||
| 721 | lc($attributes->{$attr}{rest_name}) : $attr); | ||
| 722 | $rest2attr{$rest_name} = $attr; | ||
| 723 | } | ||
| 724 | |||
| 725 | # Now set attributes: | ||
| 726 | while (my ($key, $value) = each(%$hash)) { | ||
| 727 | # Handle custom fields, ideally /(?(1)})/ would be appened to RE | ||
| 728 | if( $key =~ m%^(?:cf|customfield)(?:-|\.\{)([#\s\w_:()?/-]+)% ){ | ||
| 729 | $key = $1; | ||
| 730 | |||
| 731 | # XXX very sketchy. Will fail on long form data e.g; wiki CF | ||
| 732 | if ($value =~ /,/) { | ||
| 733 | $value = [ split(/\s*,\s*/, $value) ]; | ||
| 734 | } | ||
| 735 | |||
| 736 | $self->cf($key, $value); | ||
| 737 | next; | ||
| 738 | } | ||
| 739 | |||
| 740 | unless (exists($rest2attr{$key})) { | ||
| 741 | warn "Unknown key: $key\n"; | ||
| 742 | next; | ||
| 743 | } | ||
| 744 | |||
| 745 | # Fix for https://rt.cpan.org/Ticket/Display.html?id=118729 | ||
| 746 | if ($key ne 'content' && $value =~ m/not set/i) { | ||
| 747 | $value = undef; | ||
| 748 | } | ||
| 749 | |||
| 750 | my $method = $rest2attr{$key}; | ||
| 751 | if (exists($attributes->{$method}{form2value})) { | ||
| 752 | $value = $attributes->{$method}{form2value}($value); | ||
| 753 | } elsif ($attributes->{$method}{list}) { | ||
| 754 | $value = [split(/\s*,\s*/, $value)], | ||
| 755 | } | ||
| 756 | |||
| 757 | $self->$method($value); | ||
| 758 | } | ||
| 759 | |||
| 760 | return; | ||
| 761 | } | ||
| 762 | #>>> | ||
| 763 | } | ||
| 764 | |||
| 765 | 1; | ||
| 766 | |||
| 767 | # ABSTRACT: Convert rt.cpan.org tickets to GitHub issues | ||
| 768 | |||
| 769 | =pod | ||
| 770 | |||
| 771 | =encoding UTF-8 | ||
| 772 | |||
| 773 | =head1 NAME | ||
| 774 | |||
| 775 | RTx::ToGitHub - Convert rt.cpan.org tickets to GitHub issues | ||
| 776 | |||
| 777 | =head1 VERSION | ||
| 778 | |||
| 779 | version 0.09 | ||
| 780 | |||
| 781 | =head1 SYNOPSIS | ||
| 782 | |||
| 783 | $> rt-to-github.pl | ||
| 784 | |||
| 785 | =head1 DESCRIPTION | ||
| 786 | |||
| 787 | This is a tool to convert RT tickets to GitHub issues. When you run it, it | ||
| 788 | will: | ||
| 789 | |||
| 790 | =over 4 | ||
| 791 | |||
| 792 | =item 1. Prompt you for any info it needs | ||
| 793 | |||
| 794 | Run with C<--no-prompt> to disable prompts, in which case it will either use | ||
| 795 | the command line options you provide or look in various config files and C<git | ||
| 796 | config> for needed info. | ||
| 797 | |||
| 798 | =item 2. Make GitHub issues for each RT ticket | ||
| 799 | |||
| 800 | The body of the ticket will be the new issue body, with replies converted to | ||
| 801 | comments. Requestors and others participating in the discussion will be | ||
| 802 | converted to C<@username> mentions on GitHub. The conversion is based on a | ||
| 803 | one-time data dump made by pulling author data from MetaCPAN to make an email | ||
| 804 | address to GitHub username map. Patches to this map are welcome. | ||
| 805 | |||
| 806 | Only tickets with the "new", "open", "patched", or "stalled" status are | ||
| 807 | converted. Stalled tickets are given a "stalled" label on GitHub. | ||
| 808 | |||
| 809 | =item 3. Close the RT ticket | ||
| 810 | |||
| 811 | Unless you pass the C<--no-resolve> option. | ||
| 812 | |||
| 813 | =back | ||
| 814 | |||
| 815 | =for Pod::Coverage .* | ||
| 816 | |||
| 817 | =head1 COMMAND LINE OPTIONS | ||
| 818 | |||
| 819 | This command accepts the following flags: | ||
| 820 | |||
| 821 | =head2 --dry | ||
| 822 | |||
| 823 | Run in dry-run mode. No issues will be created and no RT tickets will be | ||
| 824 | resolved. This will just print some output to indicate what _would_ have | ||
| 825 | happened. | ||
| 826 | |||
| 827 | =head2 --no-prompt | ||
| 828 | |||
| 829 | By default you will be prompted to enter various bits of info, even if you | ||
| 830 | give everything needed on the CLI. If you pass this flag, then only CLI | ||
| 831 | options and inferred config values will be used. | ||
| 832 | |||
| 833 | =head2 --github-user | ||
| 834 | |||
| 835 | The github user to use. This defaults to looking for a "github.user" config | ||
| 836 | item in your git config. | ||
| 837 | |||
| 838 | =head2 --github-token | ||
| 839 | |||
| 840 | The github token to use. This defaults to looking for a "github.token" config | ||
| 841 | item in your git config. | ||
| 842 | |||
| 843 | =head2 --repo | ||
| 844 | |||
| 845 | The repo name to use. By default this is determined by looking at the URL for | ||
| 846 | the remote named "origin". This should just be the repo name by itself, | ||
| 847 | without a username. So pass "Net-Foo", not "username/Net-Foo". | ||
| 848 | |||
| 849 | =head2 --pause-id | ||
| 850 | |||
| 851 | Your PAUSE ID. If you have a F<~/.pause> file this will be parsed for your | ||
| 852 | username. | ||
| 853 | |||
| 854 | =head2 --pause-password | ||
| 855 | |||
| 856 | Your PAUSE password. If you have a F<~/.pause> file this will be parsed for | ||
| 857 | your password. | ||
| 858 | |||
| 859 | =head2 --dist | ||
| 860 | |||
| 861 | The distribution name which is used for your RT queue name. By default, this | ||
| 862 | is taken by looking for F<[MY]META.*> files or looking in a F<dist.ini> in the | ||
| 863 | current directory. This falls back to the repo name. | ||
| 864 | |||
| 865 | =head2 --no-resolve | ||
| 866 | |||
| 867 | If you pass this flag then the RT tickets are not marked as closed as they are | ||
| 868 | converted. | ||
| 869 | |||
| 870 | =head2 --ticket | ||
| 871 | |||
| 872 | You can specify a single RT ticket to convert by giving a ticket ID number. | ||
| 873 | |||
| 874 | =head2 --force | ||
| 875 | |||
| 876 | By default, if a matching issue already exists on GitHub, the ticket will not | ||
| 877 | be converted. Pass this flag to force a new issue to be created anyway. | ||
| 878 | |||
| 879 | =head1 CREDITS | ||
| 880 | |||
| 881 | Much of the code in this module was taken from David Golden's conversion | ||
| 882 | script at L<https://github.com/dagolden/zzz-rt-to-github>. | ||
| 883 | |||
| 884 | =head1 SUPPORT | ||
| 885 | |||
| 886 | Bugs may be submitted at L<https://github.com/houseabsolute/RTx-ToGitHub/issues>. | ||
| 887 | |||
| 888 | I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. | ||
| 889 | |||
| 890 | =head1 SOURCE | ||
| 891 | |||
| 892 | The source code repository for RTx-ToGitHub can be found at L<https://github.com/houseabsolute/RTx-ToGitHub>. | ||
| 893 | |||
| 894 | =head1 DONATIONS | ||
| 895 | |||
| 896 | If you'd like to thank me for the work I've done on this module, please | ||
| 897 | consider making a "donation" to me via PayPal. I spend a lot of free time | ||
| 898 | creating free software, and would appreciate any support you'd care to offer. | ||
| 899 | |||
| 900 | Please note that B<I am not suggesting that you must do this> in order for me | ||
| 901 | to continue working on this particular software. I will continue to do so, | ||
| 902 | inasmuch as I have in the past, for as long as it interests me. | ||
| 903 | |||
| 904 | Similarly, a donation made in this way will probably not make me work on this | ||
| 905 | software much more, unless I get so many donations that I can consider working | ||
| 906 | on free software full time (let's all have a chuckle at that together). | ||
| 907 | |||
| 908 | To donate, log into PayPal and send money to autarch@urth.org, or use the | ||
| 909 | button at L<https://www.urth.org/fs-donation.html>. | ||
| 910 | |||
| 911 | =head1 AUTHOR | ||
| 912 | |||
| 913 | Dave Rolsky <autarch@urth.org> | ||
| 914 | |||
| 915 | =head1 CONTRIBUTORS | ||
| 916 | |||
| 917 | =for stopwords Dan Stewart Michiel Beijen | ||
| 918 | |||
| 919 | =over 4 | ||
| 920 | |||
| 921 | =item * | ||
| 922 | |||
| 923 | Dan Stewart <danielandrewstewart@gmail.com> | ||
| 924 | |||
| 925 | =item * | ||
| 926 | |||
| 927 | Michiel Beijen <michiel.beijen@gmail.com> | ||
| 928 | |||
| 929 | =back | ||
| 930 | |||
| 931 | =head1 COPYRIGHT AND LICENSE | ||
| 932 | |||
| 933 | This software is Copyright (c) 2020 by David Golden and Dave Rolsky. | ||
| 934 | |||
| 935 | This is free software, licensed under: | ||
| 936 | |||
| 937 | The Apache License, Version 2.0, January 2004 | ||
| 938 | |||
| 939 | The full text of the license can be found in the | ||
| 940 | F<LICENSE> file included with this distribution. | ||
| 941 | |||
| 942 | =cut | ||
| 943 | |||
| 944 | __DATA__ |