| 1 |
#!/usr/bin/perl |
| 2 |
use utf8; |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use feature 'say'; |
| 6 |
use WWW::Mechanize; |
| 7 |
use File::Basename; |
| 8 |
use Term::ANSIColor; |
| 9 |
use Getopt::Std; |
| 10 |
|
| 11 |
use constant SCRIPTURL => 'http://www.colivre.coop.br'; |
| 12 |
|
| 13 |
# Getopt::Std method for --version and --help |
| 14 |
sub VERSION_MESSAGE { |
| 15 |
our $VERSION = '0.02'; |
| 16 |
say basename($0) . " [-q] [VERSION]\n"; |
| 17 |
say "check if all actionitems have at least one commit message on git repository"; |
| 18 |
say "if version not specified then will try read version from lib/<project_name>.rb file.\n"; |
| 19 |
say basename($0), " v$VERSION by Joenio Costa"; |
| 20 |
exit 0; |
| 21 |
} |
| 22 |
|
| 23 |
sub get_project_version { |
| 24 |
my $project_name = shift; |
| 25 |
open my $VERSION_FILE, '<', "lib/$project_name.rb" or return undef; |
| 26 |
local $/ = undef; |
| 27 |
my $version_file_content = <$VERSION_FILE>; |
| 28 |
my ($version) = $version_file_content =~ m/VERSION = '(\d+\.\d+\.\d+)'/; |
| 29 |
return $version; |
| 30 |
} |
| 31 |
|
| 32 |
sub get_version_topic { |
| 33 |
my $name = $_[0]; |
| 34 |
my $version = sprintf "%02dx%02dx%02d", split(/\./, $_[1]); |
| 35 |
my $mech = WWW::Mechanize->new(agent => 'colivre-robot', autocheck => 1 ) or die $!; |
| 36 |
my $topic = "$name.${name}Version$version"; |
| 37 |
$mech->get(SCRIPTURL . "/$topic"); |
| 38 |
return $mech->response->decoded_content; |
| 39 |
} |
| 40 |
|
| 41 |
sub find_missing_action_items { |
| 42 |
my $html_topic = shift; |
| 43 |
my $pre_block_for_each_commit = shift; |
| 44 |
my $pos_block_for_each_commit = shift; |
| 45 |
my @missing_action_item = (); |
| 46 |
while($html_topic =~ m{<a href="/[\d\w]+/(ActionItem\d+)" class="twikiLink">\#\d+</a>}sigo) { |
| 47 |
my $action_item = $1; |
| 48 |
&$pre_block_for_each_commit($action_item); |
| 49 |
open my $GIT_LOG, '-|', 'git', 'log', "--grep=$action_item", '--oneline'; |
| 50 |
my $git_log_result = <$GIT_LOG>; |
| 51 |
close $GIT_LOG; |
| 52 |
$git_log_result = $git_log_result ? chomp $git_log_result : undef; |
| 53 |
&$pos_block_for_each_commit($git_log_result); |
| 54 |
unless($git_log_result) { |
| 55 |
push @missing_action_item, $action_item; |
| 56 |
} |
| 57 |
} |
| 58 |
return @missing_action_item; |
| 59 |
} |
| 60 |
|
| 61 |
sub get_current_branch { |
| 62 |
open my $GIT_STATUS, '-|', 'git', 'status'; |
| 63 |
my $git_status = <$GIT_STATUS>; |
| 64 |
close $GIT_STATUS; |
| 65 |
return $git_status =~ m/# On branch (.+)\n/s ? $1 : undef; |
| 66 |
} |
| 67 |
|
| 68 |
#---- main |
| 69 |
|
| 70 |
our $opt_q; |
| 71 |
getopts('q'); |
| 72 |
close STDERR if $opt_q; |
| 73 |
|
| 74 |
my $project_name = basename($ENV{PWD}); |
| 75 |
my $version = $#ARGV < 0 ? get_project_version($project_name) : $ARGV[0]; |
| 76 |
die "please, inform version!" unless $version; |
| 77 |
|
| 78 |
print STDERR "accessing topic for $project_name v$version... "; |
| 79 |
my $html_topic = get_version_topic(ucfirst $project_name, $version); |
| 80 |
say STDERR "ok"; |
| 81 |
|
| 82 |
my $current_git_branch = get_current_branch; |
| 83 |
say STDERR "checking '$current_git_branch' branch... "; |
| 84 |
|
| 85 |
my @missing_action_item = find_missing_action_items( |
| 86 |
$html_topic, |
| 87 |
sub { print STDERR $_[0], "\t" }, |
| 88 |
sub { say STDERR $_[0] ? colored('ok', 'green') : colored('nok', 'red') } |
| 89 |
); |
| 90 |
|
| 91 |
print ucfirst $project_name, " v$version "; |
| 92 |
say @missing_action_item ? ('has ', colored('missing', 'red'), ' ActionItems!') : ('was ', colored('ok!', 'green')); |