-
Notifications
You must be signed in to change notification settings - Fork 7
/
rt-to-github.pl
executable file
·228 lines (193 loc) · 6.24 KB
/
rt-to-github.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use Carp;
use CPAN::Meta;
use Encode qw/decode/;
use IO::Prompt::Tiny qw/prompt/;
use Net::GitHub;
use Path::Tiny;
use RT::Client::REST;
use RT::Client::REST::Ticket;
use RT::Client::REST::User;
use Try::Tiny;
use Getopt::Long;
use Config::Identity::PAUSE;
binmode( STDOUT, ":utf8" );
my $dry_run;
my $ticket;
GetOptions(
"dry-run|n" => \$dry_run,
"ticket|t=i" => \$ticket
);
my $RT_SERVER = "rt.cpan.org";
my $pause_rc = path( $ENV{HOME}, ".pause" );
my %pause;
my %user_cache;
sub _git_config {
my $key = shift;
chomp( my $value = `git config --get $key` );
croak "Unknown $key" unless $value;
return $value;
}
sub _pause_rc {
my $key = shift;
if ( $pause_rc->exists && !%pause ) {
%pause = Config::Identity::PAUSE->load_check;
}
return $pause{$key} // '';
}
sub _dist_name {
my ($meta) = grep { -r } qw/MYMETA.json MYMETA.yml META.json META.yml/;
if ($meta) {
my $cm = CPAN::Meta->load_file($meta);
return $cm->name;
}
elsif ( -r 'dist.ini' ) {
# dzil only for now
my $dist = path("dist.ini");
my ($first) = $dist->lines( { count => 1 } );
my ($name) = $first =~ m/name\s*=\s*(\S+)/;
return $name if defined $name;
}
return '';
}
sub _find_from {
my ( $xact ) = @_;
# hack for 'http' or 'https' user which does not exist
if ( $xact->creator =~ m{^https?://} ) {
return sprintf("From %s on %s:", $xact->creator, $xact->created);
}
my $user = $user_cache{ $xact->creator } ||= RT::Client::REST::User->new(
id => $xact->creator,
rt => $xact->rt,
);
$user->retrieve;
return sprintf("From %s on %s:", lc( $user->email_address // "unknown" ), $xact->created);
}
my $github_user = prompt( "github user: ", _git_config("github.user") );
my $github_token = prompt( "github token: ", _git_config("github.token") );
my $github_repo_owner = prompt( "repo owner: ", $github_user );
my $github_repo = prompt( "repo name: ", path(".")->absolute->basename );
my $rt_user = prompt( "PAUSE ID: ", _pause_rc("user") );
my $rt_password =
_pause_rc("password") ? _pause_rc("password") : prompt("PAUSE password: ");
my $rt_dist = prompt( "RT dist name: ", _dist_name() );
my $gh = Net::GitHub->new( access_token => $github_token );
$gh->set_default_user_repo( $github_repo_owner, $github_repo );
my $gh_issue = $gh->issue;
my $rt = RT::Client::REST->new( server => "https://$RT_SERVER/" );
$rt->login(
username => $rt_user,
password => $rt_password
);
# see which tickets we already have on the github side
my @gh_issues =
$gh_issue->repos_issues( $github_repo_owner, $github_repo, { state => 'all' } );
# repos_issues may not return all issues, so need to check
# if there are more and keep going until we have them all
while ( $gh_issue->has_next_page ) {
my @next_page = $gh_issue->next_page;
push( @gh_issues,@next_page );
}
my %rt_gh_map;
for my $i (@gh_issues) {
if ( $i->{title} =~ /\[rt\.cpan\.org #(\d+)\]/ ) {
$rt_gh_map{$1} = $i;
}
}
my @rt_tickets = $rt->search(
type => 'ticket',
query => qq{
Queue = '$rt_dist'
and
( Status = 'new' or Status = 'open' or Status = 'stalled')
},
);
TICKET: for my $id (@rt_tickets) {
# maybe only a single ticket
next TICKET if $ticket && $id != $ticket;
print "Importing RT #$id\n";
# skip if already migrated
if ( my $issue = $rt_gh_map{$id} ) {
say "ticket #$id already on github as $issue->{number} ($issue->{html_url})";
next;
}
# get the information from RT
my $ticket = RT::Client::REST::Ticket->new(
rt => $rt,
id => $id,
);
$ticket->retrieve;
# subject and initial body text
my $subject = $ticket->subject;
my $trunc_subject =
length($subject) <= 20 ? $subject : ( substr( $subject, 0, 20 ) . "..." );
my $status = $ticket->status;
my $body =
"Migrated from [$RT_SERVER#$id](https://$RT_SERVER/Ticket/Display.html?id=$id) (status was '$status')\n";
# requestor email addresses
my $requestors = join( "", map { "* $_\n" } $ticket->requestors );
$body .= "\nRequestors:\n$requestors";
# attachment URLs (if any)
my @attach_links;
my $attach = $ticket->attachments->get_iterator;
while ( my $i = $attach->() ) {
my $xact = $i->transaction_id;
my $id = $i->id;
my $name = $i->file_name or next;
push @attach_links, "[$name](https://$RT_SERVER/Ticket/Attachment/$xact/$id/$name)";
}
if (@attach_links) {
my $attach_list = join( "", map { "* $_\n" } @attach_links );
$body .= "\nAttachments:\n$attach_list\n";
}
# initial ticket text
my $create = $ticket->transactions( type => 'Create' )->get_iterator->();
my $op = _find_from($create);
$body .= sprintf( "\n$op\n```\n%s\n```\n", $create->content );
# subsequent ticket discussion
my $comments = $ticket->transactions( type => 'Correspond' )->get_iterator;
while ( my $c = $comments->() ) {
my $from = _find_from($c);
$body .= sprintf( "\n$from\n```\n%s\n```\n", $c->content );
}
if ( $dry_run ) {
say "ticket #$id ($trunc_subject) would be copied to github as:";
$body =~ s/^/ /gm;
say " Subject: $subject\n\n$body\n";
}
else {
utf8::encode($subject);
utf8::encode($body);
my $isu;
try {
$isu = $gh_issue->create_issue(
{
"title" => "$subject [$RT_SERVER #$id]",
"body" => $body,
}
);
}
catch {
say "ticket #$id ($trunc_subject) had an error posting to Github: $_";
next TICKET;
};
my $gh_id = $isu->{number};
my $gh_url = $isu->{html_url};
say "ticket #$id ($trunc_subject) copied to github as #$gh_id ($gh_url)";
try {
$rt->correspond(
ticket_id => $id,
message => "Ticket migrated to github as $gh_url"
);
$ticket->status("resolved");
$ticket->store;
}
catch {
say "Error closing ticket #$id";
next TICKET;
};
}
}