| 1 | # A quick Bot::BasicBot::Pluggable module to announce new/changed issues | ||
| 2 | # and soon, pushes. | ||
| 3 | # | ||
| 4 | # David Precious <davidp@preshweb.co.uk> | ||
| 5 | |||
| 6 | package Bot::BasicBot::Pluggable::Module::GitHub::Announce; | ||
| 7 | use strict; | ||
| 8 | use Bot::BasicBot::Pluggable::Module::GitHub; | ||
| 9 | use base 'Bot::BasicBot::Pluggable::Module::GitHub'; | ||
| 10 | use JSON; | ||
| 11 | |||
| 12 | our $VERSION = 0.02; | ||
| 13 | |||
| 14 | sub help { | ||
| 15 | return <<HELPMSG; | ||
| 16 | Announce new/changed issues and pull requests, and, soon, pushes. | ||
| 17 | HELPMSG | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | sub tick { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | my $issue_state_file = 'last-issues-state.json'; | ||
| 25 | |||
| 26 | my $seconds_between_checks = $self->get('poll_issues_interval') || 60 * 5; | ||
| 27 | return if time - $self->get('last_issues_poll') < $seconds_between_checks; | ||
| 28 | $self->set('last_issues_poll', time); | ||
| 29 | |||
| 30 | # Grab details of the issues we know about already: | ||
| 31 | # Have to handle storing & loading old issue state myself - I don't know | ||
| 32 | # why, but the bot storage doesn't want to work for this. | ||
| 33 | open my $fh, '<', $issue_state_file | ||
| 34 | or die "Failed to open $issue_state_file - $!"; | ||
| 35 | my $json; | ||
| 36 | { local $/; $json = <$fh> } | ||
| 37 | close $fh; | ||
| 38 | my $seen_issues = $json ? JSON::from_json($json) : {}; | ||
| 39 | |||
| 40 | |||
| 41 | # OK, for each channel, pull details of all issues from the API, and look | ||
| 42 | # for changes | ||
| 43 | my $channels_and_projects = $self->channels_and_projects; | ||
| 44 | channel: | ||
| 45 | for my $channel (keys %$channels_and_projects) { | ||
| 46 | my $project = $channels_and_projects->{$channel}; | ||
| 47 | my %notifications; | ||
| 48 | warn "Looking for issues for $project for $channel"; | ||
| 49 | |||
| 50 | my $ng = $self->ng($project) or next channel; | ||
| 51 | |||
| 52 | my $issues = $ng->issue->list('open'); | ||
| 53 | |||
| 54 | # Go through all currently-open issues and look for new/reopened ones | ||
| 55 | for my $issue (@$issues) { | ||
| 56 | my $issuenum = $issue->{number}; | ||
| 57 | my $details = { | ||
| 58 | title => $issue->{title}, | ||
| 59 | url => $issue->{html_url}, | ||
| 60 | created_by => $issue->{user}, | ||
| 61 | }; | ||
| 62 | |||
| 63 | if (my $existing = $seen_issues->{$project}{$issuenum}) { | ||
| 64 | if ($existing->{state} eq 'closed') { | ||
| 65 | # It was closed before, but is now in the open feed, so it's | ||
| 66 | # been re-opened | ||
| 67 | push @{ $notifications{reopened} }, | ||
| 68 | [ $issuenum, $details ]; | ||
| 69 | $existing->{state} = 'open'; | ||
| 70 | } | ||
| 71 | } else { | ||
| 72 | # A new issue we haven't seen before | ||
| 73 | push @{ $notifications{opened} }, | ||
| 74 | [ $issuenum, $details ]; | ||
| 75 | $seen_issues->{$project}{$issuenum} = { | ||
| 76 | state => 'open', | ||
| 77 | details => $details, | ||
| 78 | }; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # Now, go through ones we already know about - if we knew about them, | ||
| 83 | # and they were open, but weren't in the list of open issues we fetched | ||
| 84 | # above, they must now be closed | ||
| 85 | for my $issuenum (keys %{ $seen_issues->{$project} }) { | ||
| 86 | my $existing = $seen_issues->{$project}{$issuenum}; | ||
| 87 | my $current = grep { | ||
| 88 | $_->{number} == $issuenum | ||
| 89 | } @$issues; | ||
| 90 | |||
| 91 | if ($existing->{state} eq 'open' && !$current) { | ||
| 92 | # It was open before, but isn't in the list now - it must have | ||
| 93 | # been closed. | ||
| 94 | push @{ $notifications{closed} }, | ||
| 95 | [ $issuenum, $existing->{details} ]; | ||
| 96 | $existing->{state} = 'closed'; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | # Announce any changes | ||
| 101 | for my $type (keys %notifications) { | ||
| 102 | my $s = scalar $notifications{$type} > 1 ? 's':''; | ||
| 103 | |||
| 104 | $self->say( | ||
| 105 | channel => $channel, | ||
| 106 | body => "Issue$s $type : " | ||
| 107 | . join ', ', map { | ||
| 108 | sprintf "%d (%s) by %s : %s", | ||
| 109 | $_->[0], # issue number | ||
| 110 | @{$_->[1]}{qw(title created_by url)} | ||
| 111 | } @{ $notifications{$type} } | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | my $store_json = JSON::to_json($seen_issues); | ||
| 117 | # Store the updated issue details: | ||
| 118 | open my $storefh, '>', $issue_state_file | ||
| 119 | or die "Failed to write to $issue_state_file - $!"; | ||
| 120 | print {$storefh} $store_json; | ||
| 121 | close $storefh; | ||
| 122 | return; | ||
| 123 | |||
| 124 | } | ||
| 125 |