| 1 | package Taskwarrior::Kusarigama::Plugin::Command::Github; | ||||||||||||||||||||
| 2 | 1 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||
| 3 | # ABSTRACT: sync tickets of a Github project | ||||||||||||||||||||
| 4 | 1 | $Taskwarrior::Kusarigama::Plugin::Command::Github::VERSION = '0.11.0'; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use 5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | extends 'Taskwarrior::Kusarigama::Plugin'; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | with 'Taskwarrior::Kusarigama::Hook::OnCommand'; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | has custom_uda => ( | ||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||
| 19 | default => sub{ +{ | ||||||||||||||||||||
| 20 | 0 | ▶ | gh_issue => 'github issue id', | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 21 | }}, | ||||||||||||||||||||
| 22 | 1 | ); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has projects => ( | ||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||
| 26 | lazy => 1, | ||||||||||||||||||||
| 27 | default => sub { | ||||||||||||||||||||
| 28 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | require List::MoreUtils; | |||||||||||||||||||
| 31 | 0 | ▶ | return [ List::MoreUtils::after( sub { $_ eq 'github' }, split ' ', $self->args ) ] | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||
| 33 | 1 | ); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | has github => ( | ||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||
| 38 | default => sub { | ||||||||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | require Net::GitHub; | |||||||||||||||||||
| 42 | Net::GitHub->new( | ||||||||||||||||||||
| 43 | access_token => $self->tw->config->{github}{oauth_token} | ||||||||||||||||||||
| 44 | 0 | ); | |||||||||||||||||||
| 45 | }, | ||||||||||||||||||||
| 46 | 1 | ); | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub on_command { | ||||||||||||||||||||
| 49 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
on_command: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | $self->update_project($_) for @{ $self->projects }; | |||||||||||||||||||
| 52 | }; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub project_tasks { | ||||||||||||||||||||
| 55 | 0 | ▶ | my ( $self, $project ) = @_; | ||||||||||||||||||
|
Subroutine
project_tasks: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | $self->run_task->export( { project => $project }, 'gh_issue.any:', '+PENDING' ); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub update_project { | ||||||||||||||||||||
| 61 | 0 | ▶ | my ( $self, $project ) = @_; | ||||||||||||||||||
|
Subroutine
update_project: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | my ($org, $repo) = split('/', | ||||||||||||||||||||
| 64 | 0 | eval { $self->tw->config->{project}{$project}{github_repo} } | |||||||||||||||||||
| 65 | 0 | ▶ | || join '/', $self->tw->config->{github}{user}, $project | ||||||||||||||||||
|
Condition0%0/3
eval { || join('/', $self->tw->config->{'github'}{'user'}, $project)
MC/DC0%0/2
eval {
do {
$self->tw->config->{'project'}{$project}{'github_repo'}
}
} || join('/', $self->tw->config->{'github'}{'user'}, $project)
eval {
do {
$self->tw->config->{'project'}{$project}{'github_repo'}
}
}
join('/', $self->tw->config->{'github'}{'user'}, $project)
Truth table0%0/3
A || B
A= eval {
do {
$self->tw->config->{'project'}{$project}{'github_repo'}
}
}B= join('/', $self->tw->config->{'github'}{'user'}, $project)
| |||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | require JSON; | |||||||||||||||||||
| 69 | 0 | my %filter = ( state => 'open' ); | |||||||||||||||||||
| 70 | 0 | ▶ | $filter{assignee} = $self->tw->config->{github}{user} unless $self->tw->config->{github}{user} eq $org; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | my $user_filter = eval { | |||||||||||||||||||
| 73 | JSON::from_json $self->tw->{config}{project}{$project}{filter} | ||||||||||||||||||||
| 74 | 0 | }; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | %filter = ( %filter, %$user_filter ) if $user_filter; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | say "syncing tickets for $org/$repo..."; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | my %tasks = map { $_->{gh_issue} => $_->{uuid} } $self->project_tasks($project); | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | say scalar(keys %tasks), " tasks already found locally"; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | say "fetching open tickets from Github..."; | |||||||||||||||||||
| 85 | 0 | say "using filter ", JSON::to_json( \%filter ); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | my @issues = $self->github->issue->repos_issues( | |||||||||||||||||||
| 88 | $org, $repo, \%filter | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | say scalar(@issues), " issues retrieved"; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # there is supposed to be a `task import` command. Check that out | ||||||||||||||||||||
| 94 | 0 | for my $issue ( @issues ) { | |||||||||||||||||||
| 95 | 0 | ▶ | if( my $task = delete $tasks{ $issue->{number} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | say "issue ", $issue->{number}, " already present as task ", $task; | |||||||||||||||||||
| 97 | 0 | next; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | my $task = $self->tw->new_task({ | ||||||||||||||||||||
| 101 | description => $issue->{title}, | ||||||||||||||||||||
| 102 | tags => [ qw/ github / ], | ||||||||||||||||||||
| 103 | project => $project, | ||||||||||||||||||||
| 104 | gh_issue => $issue->{number} | ||||||||||||||||||||
| 105 | 0 | }); | |||||||||||||||||||
| 106 | $task->add_note( | ||||||||||||||||||||
| 107 | 'https://github.com/' . $repo . '/issues/' . $issue->{number} | ||||||||||||||||||||
| 108 | 0 | ); | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | $task->save; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | say "task create: ", $task->{id}, " - ", $task->{description}; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | ▶ | while( my( $issue, $task ) = each %tasks ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 0 | say "issue $issue is no longer open, marking task $task as done"; | |||||||||||||||||||
| 117 | 0 | $self->run_task->done( $task ); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | 1; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =encoding UTF-8 | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head1 NAME | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | Taskwarrior::Kusarigama::Plugin::Command::Github - sync tickets of a Github project | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 VERSION | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | version 0.11.0 | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 SYNOPSIS | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # add the `github` command | ||||||||||||||||||||
| 140 | $ task-kusarigama add Command::Github | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # add our oauth creds | ||||||||||||||||||||
| 143 | # see https://github.com/settings/tokens | ||||||||||||||||||||
| 144 | $ task config github.oauth_token deadbeef | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # who is you? | ||||||||||||||||||||
| 147 | $ task config github.user yanick | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | # sync the project, baby | ||||||||||||||||||||
| 150 | $ task github List-Lazy | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =head1 DESCRIPTION | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Without any explicit configuration, the command will assume that | ||||||||||||||||||||
| 155 | the given project exists in your personal space. In other words, | ||||||||||||||||||||
| 156 | provided a C<github.user> set to C<yanick>, the command | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | $ task github List-Lazy | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | will fetch the tickets of C<https://github.com/yanick/List-Lazy>. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | If you want to explicitly set the repository of a project, you can | ||||||||||||||||||||
| 163 | do so via C<project.PROJECT.github_repo>. E.g.: | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | $ task config project.List-Lazy.github_repo yenzie/LLazy | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | The filter for the tickets to sync also follow a (hopefully) DWIM heuristic. | ||||||||||||||||||||
| 168 | If the organization is C<github.user>, then all open tickets are sync'ed. | ||||||||||||||||||||
| 169 | If the organization differ, the synced tickets defaults to be | ||||||||||||||||||||
| 170 | those assigned to C<github.user>. In all cases, the filter | ||||||||||||||||||||
| 171 | can be set explicitly via C<project.PROJECT.filter>, which takes a | ||||||||||||||||||||
| 172 | JSON structure. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | $ task config project.List-Lazy.filter '{"assignee":"yenzie"}' | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 AUTHOR | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | This software is copyright (c) 2018, 2017 by Yanick Champoux. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 185 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||