This file is indexed.

/usr/share/perl5/Debian/PkgPerl/GitHub.pm is in pkg-perl-tools 0.42.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package Debian::PkgPerl::GitHub;

use strict;
use warnings;


=head1 NAME

Debian::PkgPerl::GitHub - Help forwarding a bug or a patch to GitHub

=head1 SYNOPSIS

    use Debian::PkgPerl::Bug;
    use Debian::PkgPerl::Patch;
    use Debian::PkgPerl::Message;
    use Debian::PkgPerl::GitHub;
    
    my %params = (
        url     => 'https://github.com/foo/bar/issues',
        tracker => 'github',
        dist    => 'Foo-Bar',
        name    => 'My Name',
        email   => 'my.name@example.com',
        mailto  => 'your.name@example.com',
    );
    
    my %bug_info = Debian::PkgPerl::Bug
        ->new( bug => 123 )
        ->retrieve_bug_info();
    my $bug_msg = Debian::PkgPerl::Message->new(
        %params,
        info => \%bug_info,
    );
    print Debian::PkgPerl::GitHub->new(
        message => $bug_msg,
        ticket  => 456,
    )->forward();
    
    my %patch_info = Debian::PkgPerl::Patch
        ->new( patch => 'foo-bar.patch' )
        ->retrieve_patch_info();
    my $patch_msg = Debian::PkgPerl::Message->new(
        %params,
        info => \%patch_info,
    );
    print Debian::PkgPerl::GitHub->new(
        message  => $patch_msg,
        fallback => 0,
    )->forward();

=head1 DESCRIPTION

This module exports several helpers that that provide support for
forwarding patches to GitHub as pull requests.

=head1 METHODS

=cut


use File::Temp qw(tempdir);
use Git::Repository;
use Cwd 'realpath';


=head2 new(%params)

=head3 Parameters:

=over

=item * message

Message to be forwarded to an upstream project.

=item * ticket

Existing issue number to forward the bug to.

=item * fallback

Enables fallback to forwarding a patch as an issue if pull-request
fails for any reason. Default is false.

=back

=head3 Environment:

=over

=item * DPT_GITHUB_OAUTH

GitHub personal API token. No default.

=item * DPT_GITHUB_ORGNAME

Organization used to fork upstreams. Defaults to I<pkg-perl-tools>.

=item * DPT_GITHUB_BRANCH

Branch name for pull requests. Defaults to C<pkg-perl-$^T>.

=back

=cut

sub new {
    my $class = shift;
    my %params = @_;

    eval { require Net::GitHub; }
        or die "Net::GitHub not available.\n"
        . "Please install libnet-github-perl and try again.";

    die "github requires DPT_GITHUB_OAUTH setting.\n"
        . "See dpt-config(5) and dpt-github-oauth.\n"
        unless $ENV{DPT_GITHUB_OAUTH};

    die "Unable to determine github issue tracker URL.\n"
        unless $params{message} and $params{message}->get_url();

    my ( $owner, $repo, $opts )
        = $params{message}->get_url()
        =~ m{^https?://github.com/([^/]+)/([^/]+)/issues(?:/?|\?(.*))$};

    my @labels;
    @labels = split /,/, $1 if $opts and $opts =~ m{labels=([^;&]+)};

    die "Unable to determine github user and repository\n"
        . "from " . $params{message}->get_url()
        unless $owner and $repo;

    my $github = Net::GitHub::V3->new(
        access_token => $ENV{DPT_GITHUB_OAUTH},
    );

    my %obj = (
        %params,
        github  => $github,
        owner   => $owner,
        repo    => $repo,
        labels  => \@labels,
        orgname => $ENV{'DPT_GITHUB_ORGNAME'} || 'pkg-perl-tools',
        branch  => $ENV{'DPT_GITHUB_BRANCH' } || "pkg-perl-$^T",
    );

    return bless \%obj, $class;
}

=head2 fork_exists()

Checks if any member in the GitHub organization has already forked a repository.

=head3 Returns:

Boolean value.

=cut

sub fork_exists {
    my $self = shift;

    my $gh   = $self->{github};
    my $user = $self->{orgname};
    my $repo = $self->{repo};

    return !!eval{ $gh->repos->get($user, $repo) };
}

=head2 create_fork()

Creates a repository fork by an organization.

=head3 Returns:

The new forked repository.

=cut

sub create_fork {
    my $self = shift;

    my $gh = $self->{github};
    my $owner = $self->{owner};
    my $repo  = $self->{repo};
    my $orgname = $self->{orgname};

    $gh->repos->set_default_user_repo($owner, $repo);
    return $gh->repos->create_fork($orgname);
}

=head2 clone_branch_patch_push()

Clones a repository in a temporary directory, creates a new branch,
applies the patch, commits the change, pushes it and removes the
temporary working copy.

=head3 Returns:

Nothing.

=cut

sub clone_branch_patch_push {
    my $self = shift;
    my %params = @_;

    my $orgname = $self->{orgname};
    my $user    = $self->{owner};
    my $repo    = $self->{repo};
    my $branch  = $self->{branch};
    my $comment = $self->{message}->get_subject();
    my $patch   = $self->{message}->get_patch();

    # Clone
    my $workdir = tempdir( CLEANUP => 1 );
    Git::Repository->run(
        clone => "git\@github.com:$orgname/$repo.git",
        $workdir,
        { quiet => 1 },
    );
    my $r = Git::Repository->new(
        work_tree => $workdir,
        { quiet => 1 },
    );

    # Sync
    $r->run( qw( remote add upstream ) => "https://github.com/$user/$repo.git" );
    $r->run( pull => '--ff-only', qw( upstream master ) );
    $r->run( push => qw( origin master ) );

    # Branch
    $r->run( checkout => '-b', $branch );

    # Patch
    $r->run( apply => '-p1', realpath($patch), { quiet => 0, fatal => "!0" } );

    # Push
    $r->run( add => '.' );
    $r->run( commit => '-m', $comment );
    $r->run( push => 'origin', $branch );

    return;
}

=head2 create_pull_request()

=head3 Returns:

Pull request URL on success, nothing otherwise.

=cut

sub create_pull_request {
    my $self = shift;

    my $gh   = $self->{github};
    my $user = $self->{owner};
    my $repo = $self->{repo};

    $gh->pull_request->set_default_user_repo($user, $repo);

    my $pull = $gh->pull_request->create_pull({
        title  => $self->{message}->get_subject(),
        body   => $self->{message}->prepare_body(),
        head   => join(":", $self->{orgname}, $self->{branch}),
        base   => 'master',
        labels => $self->{labels},
    });
    return $pull && $pull->{html_url};
}

=head2 forward_patch_as_pull_request()

=cut

sub forward_patch_as_pull_request {
    my $self = shift;

    my $gh = $self->{github};
    my $orgname = $self->{orgname};
    die "Cannot find your GitHub user in $orgname organization"
        unless $gh->user->show($orgname)->{login} eq $orgname;

    $self->create_fork() unless $self->fork_exists();
    $self->clone_branch_patch_push();
    my $issue_url = $self->create_pull_request();
    return $issue_url;
}

=head2 forward_patch_as_ticket()

Fallback to forwarding a patch as a bug.

=head3 Returns:

Issue URL on success, nothing otherwise.

=cut

sub forward_patch_as_ticket {
    my $self = shift;

    # Do nothing unless fallback is enabled.
    return unless $self->{fallback};

    my $gh      = $self->{github};
    my $owner   = $self->{owner};
    my $repo    = $self->{repo};
    my $ticket  = $self->{ticket};
    my $title   = $self->{message}->get_subject();
    my $comment = $self->{message}->prepare_body();

    $gh->set_default_user_repo( $owner, $repo );
    my $issue;
    if ($ticket) {
        $issue = $gh->issue->issue($ticket);
        $gh->issue->create_comment( $ticket, { body => $comment } );
    }
    else {
        $issue = $gh->issue->create_issue({
            title  => $title,
            body   => $comment,
            labels => $self->{labels},
        });
    }

    return $issue && $issue->{html_url};
}

=head2 forward_bug_as_issue()

=cut

sub forward_bug_as_issue {
    my $self = shift;

    my $gh      = $self->{github};
    my $user    = $self->{owner};
    my $repo    = $self->{repo};
    my $ticket  = $self->{ticket};
    my $title   = $self->{message}->get_subject();
    my $comment = $self->{message}->prepare_body();

    $gh->set_default_user_repo( $user, $repo );

    my $issue;
    if ($ticket) {
        $issue = $gh->issue->issue($ticket);
        $gh->issue->create_comment( $ticket, { body => $comment } );
    }
    else {
        $issue = $gh->issue->create_issue({
            title  => $title,
            body   => $comment,
            labels => $self->{labels},
        });
    }

    return $issue->{html_url};
}

=head2 forward()

Forward an issue as a pull request or bug.

=cut

sub forward {
    my $self = shift;

    my $issue_url;
    $issue_url = $self->forward_bug_as_issue()
        unless $self->{message}->get_patch();

    $issue_url = eval { $self->forward_patch_as_pull_request() }
        unless defined $issue_url;

    warn "WARNING! Pull request failed...\n$@" if $@;

    $issue_url = $self->forward_patch_as_ticket()
        unless defined $issue_url;

    return $issue_url;
}

=head2 test()

Support for off-line testing.

=cut

sub test {
    my $self = shift;

    my $gh_user = $self->{owner};
    my $gh_repo = $self->{repo};

    return "https://github.com/$gh_user/$gh_repo/issues/DUMMY";
}

=head1 LICENSE AND COPYRIGHT

=over

=item Copyright 2016 Alex Muntada.

=back

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=cut

1;