Bot-BasicBot-Pluggable-Module-GitHub

 view release on metacpan or  search on metacpan

lib/Bot/BasicBot/Pluggable/Module/GitHub/EasyLinks.pm  view on Meta::CPAN


        my $project = $+{project} || $self->github_project($mess->{channel});
        return unless $project;

        # Get the Net::GitHub::V2 object we'll be using.  (If we don't get one,
        # for some reason, we can't do anything useful.)
        my $ng = $self->ng($project) or return;

        # First, extract what kind of thing we're looking at, and normalise it a
        # little, then go on to handle it.
        my $thing    = $+{thing};
        my $thingnum = $+{num};

        if ($+{sha}) {
            $thing    = 'commit';
            $thingnum = $+{sha};
        }

        warn "OK, about to try to handle $thing $thingnum for $project";

        # Right, handle it in the approriate way
        if ($thing =~ /Issue|GH/i) {
            warn "Handling issue $thingnum";
            my $issue = $ng->issue->view($thingnum);
            if (exists $issue->{error}) {
                push @return, $issue->{error};
                next match;
            }
            push @return, sprintf "Issue %d (%s) - %s",
                $thingnum,
                $issue->{title},
                $issue->{html_url};
        }

        # Similarly, pull requests:
        if ($thing =~ /(?:pr|pull request)/i) {
            warn "Handling pull request $thingnum";
            # TODO: send a pull request to add support for fetching details of
            # pull requests to Net::GitHub::V2, so we can handle PRs on private
            # repos appropriately.
            my $pull_url = "https://github.com/$project/pull/$thingnum";
            my $title = URI::Title::title($pull_url);
            push @return, "Pull request $thingnum ($title) - $pull_url";
        }

        # If it was a commit:
        if ($thing eq 'commit') {
            warn "Handling commit $thingnum";
            my $commit = $ng->commit->show($thingnum);
            if ($commit && !exists $commit->{error}) {
                my $title = ( split /\n+/, $commit->{message} )[0];
                my $url = $commit->{url};
                
                # Currently, the URL given doesn't include the host, but that
                # might perhaps change in future, so play it safe:
                $url = "https://github.com$url" unless $url =~ /^http/;
                push @return, sprintf "Commit $thingnum (%s) - %s",
                    $title,
                    $url;
            } else {
                # We purposefully don't show a message on IRC here, as we guess
                # what might be a SHA, so we could be annoying saying that we
                # didn't match a commit when someone said a word that just
                # happened to look like it could be the start of a SHA.
                warn "No commit details for $thingnum \@ $project/$thingnum";
            }
        }
    }
    
    return join "\n", @return;
}





1;



( run in 0.895 second using v1.01-cache-2.11-cpan-39bf76dae61 )