This is an automated email from the git hooks/post-receive script.

alexm-guest pushed a commit to branch master
in repository pkg-perl-tools.

commit a57fbd82f3ac0cb6f898c141947038dff0ff1999
Author: Alex Muntada <al...@alexm.org>
Date:   Mon Nov 28 05:02:35 2016 +0100

    Convert Debian::PkgPerl::GitHub into a class
---
 lib/Debian/PkgPerl/GitHub.pm | 382 ++++++++++++++++++++++++++-----------------
 scripts/forward              |  61 +------
 t/github.t                   |  33 +---
 3 files changed, 245 insertions(+), 231 deletions(-)

diff --git a/lib/Debian/PkgPerl/GitHub.pm b/lib/Debian/PkgPerl/GitHub.pm
index a46125d..bf30589 100644
--- a/lib/Debian/PkgPerl/GitHub.pm
+++ b/lib/Debian/PkgPerl/GitHub.pm
@@ -10,28 +10,25 @@ Debian::PkgPerl::GitHub - Help forwarding a bug or a patch 
to GitHub
 
 =head1 SYNOPSIS
 
-    $gh = Net::GitHub->new(
-        access_token => $token,
-    );
+    use Debian::PkgPerl::GitHub;
     
-    create_fork($gh, $owner, $repo, $orgname)
-        unless fork_exists($gh, $orgname, $repo);
+    my $url = 'https://github.com/foo/bar/issues';
+    my $gh  = Debian::PkgPerl::GitHub->new(tracker => $url);
     
-    clone_branch_patch_push($orgname, $repo, $branch, $patch, $comment);
+    my %params = (
+        subject => $summary,
+        body    => $comment,
+    );
     
-    $url = create_pull_request($gh, $owner, $repo, {
-        title => $comment,
-        body  => $body,
-        head  => "$orgname:$branch",
-        base  => $base,
-    });
+    my $pull  = $gh->patch($file)->forward(%params);
+    my $issue = $gh->bug($ticket)->forward(%params);
 
 =head1 DESCRIPTION
 
 This module exports several helpers that that provide support for
 forwarding patches to GitHub as pull requests.
 
-=head1 SUBROUTINES/METHODS
+=head1 METHODS
 
 =cut
 
@@ -40,76 +37,104 @@ use File::Temp qw(tempdir);
 use Git::Repository;
 use Cwd 'realpath';
 
-use Exporter 'import';
-our @EXPORT_OK = qw(
-    fork_exists
-    create_fork
-    clone_branch_patch_push
-    create_pull_request
-    forward_patch_as_pull_request
-    forward_bug_as_issue
-);
-our %EXPORT_TAGS = ( all => \@EXPORT_OK );
 
-=head2 fork_exists($gh, $user, $repo)
-
-Checks if a GitHub user has already forked a repository.
+=head2 new(%params)
 
 =head3 Parameters:
 
 =over
 
-=item * $gh
+=item * tracker
 
-Net::GitHub object that must be already authenticated.
+Issue tracker URL for an upstream project.
 
-=item * $user
+=back
 
-GitHub username.
+=head3 Environment:
 
-=item * $repo
+=over
 
-GitHub repository name.
+=item * DPT_GITHUB_OAUTH
 
-=back
+GitHub personal API token. No default.
 
-=head3 Returns:
+=item * DPT_GITHUB_ORGNAME
 
-Boolean value.
+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 fork_exists {
-    my ($gh, $user, $repo) = @_;
+sub new {
+    my $class = shift;
+    my %params = @_;
 
-    return !!eval{ $gh->repos->get($user, $repo) };
-}
+    eval { require Net::GitHub; }
+        or die "Net::GitHub not available.\n"
+        . "Please install libnet-github-perl and try again.";
 
-=head2 create_fork($gh, $owner, $repo, $orgname)
+    die "github requires DPT_GITHUB_OAUTH setting.\n"
+        . "See dpt-config(5) and dpt-github-oauth.\n"
+        unless $ENV{DPT_GITHUB_OAUTH};
 
-Creates a repository fork by an organization.
+    die "Unable to determine github issue tracker URL.\n"
+        unless $params{tracker};
 
-=head3 Parameters:
+    my ( $owner, $repo, $opts )
+        = $params{tracker}
+        =~ m{^https?://github.com/([^/]+)/([^/]+)/issues(?:/?|\?(.*))$};
 
-=over
+    my @labels;
+    @labels = split /,/, $1 if $opts and $opts =~ m{labels=([^;&]+)};
 
-=item * $gh
+    die "Unable to determine github user and repository\n"
+        . "from $params{tracker}"
+        unless $owner and $repo;
 
-Net::GitHub object that must be already authenticated.
+    my $github = Net::GitHub::V3->new(
+        access_token => $ENV{DPT_GITHUB_OAUTH},
+    );
 
-=item * $owner
+    my %obj = (
+        github  => $github,
+        owner   => $owner,
+        repo    => $repo,
+        labels  => \@labels,
+        orgname => $ENV{'DPT_GITHUB_ORGNAME'} || 'pkg-perl-tools',
+        branch  => $ENV{'DPT_GITHUB_BRANCH' } || "pkg-perl-$^T",
+    );
 
-Repository owner.
+    return bless \%obj, $class;
+}
 
-=item * $repo
+=head2 fork_exists()
 
-Repository being forked.
+Checks if any member in the GitHub organization has already forked a 
repository.
 
-=item * $orgname
+=head3 Returns:
 
-Organization that is forking the repository.
+Boolean value.
 
-=back
+=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:
 
@@ -118,13 +143,18 @@ The new forked repository.
 =cut
 
 sub create_fork {
-    my ($gh, $owner, $repo, $orgname) = @_;
+    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($user, $repo, $branch, $patch, $comment)
+=head2 clone_branch_patch_push(%params)
 
 Clones a repository in a temporary directory, creates a new branch,
 applies the patch, commits the change, pushes it and removes the
@@ -134,23 +164,7 @@ temporary working copy.
 
 =over
 
-=item * $user
-
-User cloning the repository.
-
-=item * $repo
-
-Repository being cloned.
-
-=item * $branch
-
-Name of the branch being created.
-
-=item * $patch
-
-Full pathname to the file containing the patch.
-
-=item * $comment
+=item * subject
 
 Message used in the commit.
 
@@ -163,7 +177,15 @@ Nothing.
 =cut
 
 sub clone_branch_patch_push {
-    my ($user, $repo, $orgname, $branch, $patch, $comment) = @_;
+    my $self = shift;
+    my %params = @_;
+
+    my $orgname = $self->{orgname};
+    my $user    = $self->{owner};
+    my $repo    = $self->{repo};
+    my $branch  = $self->{branch};
+    my $comment = $params{subject};
+    my $patch   = $self->{patch};
 
     # Clone
     my $workdir = tempdir( CLEANUP => 1 );
@@ -196,28 +218,19 @@ sub clone_branch_patch_push {
     return;
 }
 
-=head2 create_pull_request($gh, $user, $repo, $params)
+=head2 create_pull_request(%params)
 
 =head3 Parameters:
 
 =over
 
-=item * $gh
-
-Net::GitHub object that must be already authenticated.
+=item * subject
 
-=item * $user
+Title used in the pull request.
 
-User performing the pull request.
+=item * body
 
-=item * $repo
-
-Repository receiving the pull request.
-
-=item * $params
-
-Hash reference with pull request parameters (usually B<title>, B<body>,
-B<head> and B<base>.) See L<Net::GitHub::V3::PullRequests> for details.
+Comment used as the body of the pull request message.
 
 =back
 
@@ -228,138 +241,199 @@ Pull request URL on success, nothing otherwise.
 =cut
 
 sub create_pull_request {
-    my ($gh, $user, $repo, $params) = @_;
+    my $self = shift;
+    my %params = @_;
+
+    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($params);
+
+    my $pull = $gh->pull_request->create_pull({
+        title  => $params{subject},
+        body   => $params{body},
+        head   => join(":", $self->{orgname}, $self->{branch}),
+        base   => 'master',
+        labels => $self->{labels},
+    });
     return $pull && $pull->{html_url};
 }
 
-=head2 forward_patch_as_pull_request($gh, $user, $repo, $orgname, $branch, 
$patch, $title, $comment, $labels)
+=head2 forward_patch_as_pull_request(%params)
 
 =head3 Parameters:
 
 =over
 
-=item * $gh
+=item * subject
 
-Net::GitHub object that must be already authenticated.
+Title used in the pull request.
 
-=item * $user
+=item * body
 
-User forwarding the patch.
+Comment used as the body of the pull request message.
 
-=item * $repo
+=back
 
-Repository receiving the patch.
+=cut
 
-=item * $orgname
+sub forward_patch_as_pull_request {
+    my $self = shift;
+    my %params = @_;
 
-Organization that owns the forked repository.
+    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;
 
-=item * $branch
+    $self->create_fork() unless $self->fork_exists();
+    $self->clone_branch_patch_push(%params);
+    my $issue_url = $self->create_pull_request(%params);
+    return $issue_url;
+}
 
-Name of the branch being created.
+=head2 forward_bug_as_issue(%params)
 
-=item * $patch
+=head3 Parameters:
 
-Full pathname to the file containing the patch.
+=over
 
-=item * $title
+=item * subject
 
 Title of the bug report.
 
-=item * $comment
+=item * body
 
-Message used in the bug report.
-
-=item * $labels
-
-Labels to be set in the bug report.
+Comment used in the body of the bug report.
 
 =back
 
 =cut
 
-sub forward_patch_as_pull_request {
-    my ($gh, $user, $repo, $orgname, $branch, $patch, $title, $comment, 
$labels) = @_;
-
-    die "Cannot find your GitHub user in $orgname organization"
-        unless $gh->user->show($orgname)->{login} eq $orgname;
+sub forward_bug_as_issue {
+    my $self = shift;
+    my %params = @_;
 
-    create_fork($gh, $user, $repo, $orgname)
-        unless fork_exists($gh, $orgname, $repo);
+    my $gh      = $self->{github};
+    my $user    = $self->{owner};
+    my $repo    = $self->{repo};
+    my $ticket  = $self->{ticket};
+    my $title   = $params{subject};
+    my $comment = $params{body};
 
-    clone_branch_patch_push($user, $repo, $orgname, $branch, $patch, $title);
+    $gh->set_default_user_repo( $user, $repo );
 
-    $issue_url = create_pull_request($gh, $user, $repo, {
-        title  => $title,
-        body   => $comment,
-        head   => "$orgname:$branch",
-        base   => 'master',
-        labels => [ split( /,/, $labels ) ],
-    });
+    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_url;
+    return $issue->{html_url};
 }
 
-=head2 forward_bug_as_issue($gh, $user, $repo, $ticket, $title, $comment, 
$labels)
+=head2 patch($file)
+
+Set the filename of the patch to be forwarded.
 
 =head3 Parameters:
 
 =over
 
-=item * $gh
+=item * $file
+
+Filename containing a patch.
+
+=back
+
+=cut
+
+sub patch {
+    my $self = shift;
+    my ($file) = @_;
+
+    $self->{patch} = $file;
 
-Net::GitHub object that must be already authenticated.
+    return $self;
+}
 
-=item * $user
+=head2 bug($ticket)
 
-User forwarding the bug.
+Set the bug nuber of the issue to be forwarded.
 
-=item * $repo
+=head3 Parameters:
 
-Repository receiving the bug report.
+=over
 
 =item * $ticket
 
-Use an existing ticket number to make the report.
+An existing bug number. Forward will create a new one otherwise.
 
-=item * $title
+=back
 
-Title of the bug report.
+=cut
 
-=item * $comment
+sub bug {
+    my $self = shift;
+    my ($ticket) = @_;
 
-Message used in the bug report.
+    $self->{bug} = $ticket;
 
-=item * $labels
+    return $self;
+}
+
+=head2 forward(%params)
+
+Forward an issue as a pull request or bug.
+
+=head3 Parameters:
+
+=over
+
+=item * subject
 
-Labels to be set in the bug report.
+The title of the forwarded issue.
+
+=item * body
+
+The comment of the forwarded issue.
 
 =back
 
 =cut
 
-sub forward_bug_as_issue {
-    my ($gh, $user, $repo, $ticket, $title, $comment, $labels) = @_;
+sub forward {
+    my $self = shift;
 
-    $gh->set_default_user_repo( $user, $repo );
+    my $issue_url = $self->{patch}
+        ? $self->forward_patch_as_pull_request(@_)
+        : $self->forward_bug_as_issue(@_)
+        ;
 
-    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 => [ split( /,/, $labels ) ],
-        });
-    }
+    return $issue_url;
+}
 
-    return $issue->{html_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
diff --git a/scripts/forward b/scripts/forward
index 7b03bf8..d0cea27 100755
--- a/scripts/forward
+++ b/scripts/forward
@@ -16,7 +16,7 @@ use Text::Wrap qw(wrap);
 use Proc::InvokeEditor;
 use MIME::Lite;
 use YAML::XS qw(LoadFile);
-use Debian::PkgPerl::GitHub ':all';
+use Debian::PkgPerl::GitHub;
 
 use warnings;
 use strict;
@@ -571,26 +571,7 @@ sub submit_cpan_rt {
 
 sub submit_github {
 
-    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 $opt_tracker_url;
-
-    my ( $gh_user, $gh_repo, $gh_opts )
-        = $opt_tracker_url
-        =~ m{^https?://github.com/([^/]+)/([^/]+)/issues(?:/?|\?(.*))$};
-    my $gh_labels = '';
-    $gh_labels = $1 if $gh_opts and $gh_opts =~ m{labels=([^;&]+)};
-
-    die "Unable to determine github user and repository\n"
-        . "from $opt_tracker_url"
-        unless $gh_user and $gh_repo;
+    my $gh = Debian::PkgPerl::GitHub->new( tracker => $opt_tracker_url );
 
     # prepare subject
     my $subject = get_subject();
@@ -600,43 +581,17 @@ sub submit_github {
     my $issue_url;
 
     if ( $opt_offline_test ) {
-        $issue_url = "https://github.com/$gh_user/$gh_repo/issues/DUMMY";;
+        $issue_url = $gh->test();
         goto ISSUE_CREATED;
     }
 
-    # now create the issue
-    my $gh = Net::GitHub->new(    # Net::GitHub::V3
-        access_token => $ENV{DPT_GITHUB_OAUTH},
+    $gh->patch($patch);
+    $gh->bug($opt_ticket);
+    $issue_url = $gh->forward(
+        subject => $subject,
+        body    => $body,
     );
 
-    if ($patch) {
-        my $orgname = 'pkg-perl-tools';
-        my $branch = "pkg-perl-$^T";
-
-        $issue_url = forward_patch_as_pull_request(
-            $gh,
-            $gh_user,
-            $gh_repo,
-            $orgname,
-            $branch,
-            $patch,
-            $subject,
-            $body,
-            $gh_labels,
-        );
-    }
-    else {
-        $issue_url = forward_bug_as_issue(
-            $gh,
-            $gh_user,
-            $gh_repo,
-            $opt_ticket,
-            $subject,
-            $body,
-            $gh_labels,
-        );
-    }
-
 ISSUE_CREATED:
     mark_patch_as_forwarded($issue_url) if $patch;
     mark_bug_as_forwarded($issue_url)   if $bug;
diff --git a/t/github.t b/t/github.t
index aed816c..a04ddd0 100644
--- a/t/github.t
+++ b/t/github.t
@@ -13,42 +13,27 @@ BEGIN {
 
     plan skip_all
         => "GitHub tests require DPT_GITHUB_OAUTH token"
-         . " and Net::GitHub"
+         . " and a working Debian::PkgPerl::GitHub"
         unless $ENV{DPT_GITHUB_OAUTH}
         and $ENV{DPT_GITHUB_OAUTH} =~ /^\w+$/
-        and eval "use Net::GitHub; 1";
+        and eval { use Debian::PkgPerl::GitHub; 1 };
 }
 
 use Test::RequiresInternet ( 'github.com' => 22 );
-use Debian::PkgPerl::GitHub ':all';
 
 # Defaults
-my $owner   = 'alexm';
-my $repo    = 'pkg-perl-dummy';
-my $orgname = 'pkg-perl-tools';
-my $branch  = "pkg-perl-$^T";
+my $tracker = 'https://github.com/alexm/pkg-perl-dummy/issues';
 my $patch   = "$ENV{DPT_PACKAGES}/pkg-perl-tools/t/dummy.txt.patch";
 my $comment = "Test patch $^T";
 my $body    = 'Pull request by Debian pkg-perl team.';
-my $base    = 'master';
 
-# Test GitHub API access
-my $gh = new_ok('Net::GitHub::V3', [
-    access_token => $ENV{DPT_GITHUB_OAUTH},
-]);
-is( $gh->user->show($orgname)->{login}, $orgname, $orgname );
+my $gh = new_ok( 'Debian::PkgPerl::GitHub', [ tracker => $tracker ] );
+isa_ok( $gh->patch($patch), 'Debian::PkgPerl::GitHub', '$gh->patch' );
 
-create_fork($gh, $owner, $repo, $orgname)
-    unless fork_exists($gh, $orgname, $repo);
-
-clone_branch_patch_push($owner, $repo, $orgname, $branch, $patch, $comment);
-
-my $url = create_pull_request($gh, $owner, $repo, {
-    title => $comment,
-    body  => $body,
-    head  => "$orgname:$branch",
-    base  => $base,
-});
+my $url = $gh->forward(
+    subject => $comment,
+    body    => $body,
+);
 
 isnt( $url, undef, 'pull request' );
 diag $url;

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-perl/packages/pkg-perl-tools.git

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits

Reply via email to