| 1 | package SVN::Hooks::CheckJira; | ||
| 2 | # ABSTRACT: Integrate Subversion with the JIRA ticketing system. | ||
| 3 | $SVN::Hooks::CheckJira::VERSION = '1.35'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use Data::Util qw(:check); | ||
| 9 | use SVN::Hooks; | ||
| 10 | use JIRA::REST; | ||
| 11 | |||
| 12 | use Exporter qw/import/; | ||
| 13 | my $HOOK = 'CHECK_JIRA'; | ||
| 14 | our @EXPORT = qw/CHECK_JIRA_CONFIG CHECK_JIRA CHECK_JIRA_DISABLE/; | ||
| 15 | |||
| 16 | |||
| 17 | my ($BaseURL, $Login, $Passwd, $MatchLog, $MatchKey); | ||
| 18 | my $JIRA; | ||
| 19 | my @Checks; | ||
| 20 | my %Defaults = ( | ||
| 21 | require => 1, | ||
| 22 | valid => 1, | ||
| 23 | unresolved => 1, | ||
| 24 | by_assignee => 0, | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub CHECK_JIRA_CONFIG { | ||
| 28 | ($BaseURL, $Login, $Passwd, $MatchLog, $MatchKey) = @_; | ||
| 29 | |||
| 30 | if (defined $MatchKey) { | ||
| 31 | is_rx($MatchKey) or croak "CHECK_JIRA_CONFIG: fifth argument must be a Regexp.\n"; | ||
| 32 | } else { | ||
| 33 | $MatchKey = qr/[A-Z]{2,}/; | ||
| 34 | } | ||
| 35 | |||
| 36 | if (defined $MatchLog) { | ||
| 37 | is_rx($MatchLog) or croak "CHECK_JIRA_CONFIG: fourth argument must be a Regexp.\n"; | ||
| 38 | } else { | ||
| 39 | $MatchLog = qr/(.*)/; | ||
| 40 | } | ||
| 41 | |||
| 42 | @_ >= 3 && @_ <= 5 | ||
| 43 | or croak "CHECK_JIRA_CONFIG: requires three, four, or five arguments.\n"; | ||
| 44 | |||
| 45 | $BaseURL =~ s/\/+$//; | ||
| 46 | |||
| 47 | return 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | sub _validate_projects { | ||
| 52 | my ($opt, $val) = @_; | ||
| 53 | is_string($val) && $val =~ /^[A-Z,\s]+$/ | ||
| 54 | or croak "$HOOK: $opt\'s value must be a string matching /^[A-Z,\\s]+\$/.\n"; | ||
| 55 | my %projects = map {$_ => undef} grep {/./} split /\s*,\s*/, $val; | ||
| 56 | return \%projects; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _validate_bool { | ||
| 60 | my ($opt, $val) = @_; | ||
| 61 | defined $val or croak "$HOOK: undefined $opt\'s value.\n"; | ||
| 62 | return $val; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _validate_code { | ||
| 66 | my ($opt, $val) = @_; | ||
| 67 | is_code_ref($val) or croak "$HOOK: $opt\'s value must be a CODE-ref.\n"; | ||
| 68 | return $val; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _validate_regex { | ||
| 72 | my ($opt, $val) = @_; | ||
| 73 | is_rx($val) or croak "$HOOK: $opt\'s value must be a qr/REGEX/.\n"; | ||
| 74 | return $val; | ||
| 75 | } | ||
| 76 | |||
| 77 | my %opt_checks = ( | ||
| 78 | projects => \&_validate_projects, | ||
| 79 | require => \&_validate_bool, | ||
| 80 | valid => \&_validate_bool, | ||
| 81 | unresolved => \&_validate_bool, | ||
| 82 | by_assignee => \&_validate_bool, | ||
| 83 | check_one => \&_validate_code, | ||
| 84 | check_all => \&_validate_code, | ||
| 85 | check_all_svnlook => \&_validate_code, | ||
| 86 | post_action => \&_validate_code, | ||
| 87 | exclude => \&_validate_regex, | ||
| 88 | ); | ||
| 89 | |||
| 90 | sub CHECK_JIRA { | ||
| 91 | my ($regex, $opts) = @_; | ||
| 92 | is_rx($regex) || (is_string($regex) && $regex eq 'default') | ||
| 93 | or croak "$HOOK: first arg must be a qr/Regexp/ or the string 'default'.\n"; | ||
| 94 | ! defined $opts || is_hash_ref($opts) | ||
| 95 | or croak "$HOOK: second argument must be a HASH-ref.\n"; | ||
| 96 | |||
| 97 | $opts = {} unless defined $opts; | ||
| 98 | foreach my $opt (keys %$opts) { | ||
| 99 | exists $opt_checks{$opt} or croak "$HOOK: unknown option '$opt'.\n"; | ||
| 100 | $opts->{$opt} = $opt_checks{$opt}->($opt, $opts->{$opt}); | ||
| 101 | } | ||
| 102 | |||
| 103 | if (ref $regex) { | ||
| 104 | push @Checks, [$regex => $opts]; | ||
| 105 | } else { | ||
| 106 | while (my ($opt, $val) = each %$opts) { | ||
| 107 | $Defaults{$opt} = $val; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | PRE_COMMIT(\&pre_commit); | ||
| 111 | POST_COMMIT(\&post_commit) if exists $opts->{post_action}; | ||
| 112 | |||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | my $Disabled; | ||
| 118 | |||
| 119 | sub CHECK_JIRA_DISABLE { | ||
| 120 | $Disabled = 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub _pre_checks { | ||
| 124 | my ($svnlook, $keys, $opts) = @_; | ||
| 125 | |||
| 126 | # Grok and check each JIRA issue | ||
| 127 | my @issues; | ||
| 128 | foreach my $key (@$keys) { | ||
| 129 | my $issue = eval {$JIRA->GET("/issue/$key")}; | ||
| 130 | if ($opts->{valid}) { | ||
| 131 | croak "$HOOK: issue $key is not valid: $@\n" if $@; | ||
| 132 | } | ||
| 133 | $issue or next; | ||
| 134 | if ($opts->{unresolved}) { | ||
| 135 | croak "$HOOK: issue $key is already resolved.\n" | ||
| 136 | if defined $issue->{fields}{resolution}; | ||
| 137 | } | ||
| 138 | if ($opts->{by_assignee}) { | ||
| 139 | my $author = $svnlook->author(); | ||
| 140 | my $assignee = $issue->{fields}{assignee}{name}; | ||
| 141 | croak "$HOOK: committer ($author) is different from issue ${key}'s assignee ($assignee).\n" | ||
| 142 | if $author ne $assignee; | ||
| 143 | } | ||
| 144 | if (my $check = $opts->{check_one}) { | ||
| 145 | $check->($JIRA, $issue, $svnlook); | ||
| 146 | } | ||
| 147 | push @issues, $issue; | ||
| 148 | } | ||
| 149 | |||
| 150 | if (my $check = $opts->{check_all}) { | ||
| 151 | $check->($JIRA, @issues) if @issues; | ||
| 152 | } | ||
| 153 | |||
| 154 | if (my $check = $opts->{check_all_svnlook}) { | ||
| 155 | $check->($svnlook, $JIRA, @issues) if @issues; | ||
| 156 | } | ||
| 157 | |||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _post_action { | ||
| 162 | my ($svnlook, $keys, $opts) = @_; | ||
| 163 | |||
| 164 | if (my $action = $opts->{post_action}) { | ||
| 165 | $action->($JIRA, $svnlook, @$keys); | ||
| 166 | } | ||
| 167 | |||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub _check_if_needed { | ||
| 172 | my ($svnlook, $docheck) = @_; | ||
| 173 | |||
| 174 | return if $Disabled; | ||
| 175 | |||
| 176 | defined $BaseURL | ||
| 177 | or croak "$HOOK: plugin not configured. Please, use the CHECK_JIRA_CONFIG directive.\n"; | ||
| 178 | |||
| 179 | my @files = $svnlook->changed(); | ||
| 180 | |||
| 181 | foreach my $check (@Checks) { | ||
| 182 | my ($regex, $opts) = @$check; | ||
| 183 | |||
| 184 | for my $file (@files) { | ||
| 185 | if ($file =~ $regex) { | ||
| 186 | # skip exclusions | ||
| 187 | next if exists $opts->{exclude} && $file =~ $opts->{exclude}; | ||
| 188 | |||
| 189 | # Grok the JIRA issue keys from the commit log | ||
| 190 | my ($match) = ($svnlook->log_msg() =~ $MatchLog); | ||
| 191 | my @keys = defined $match ? $match =~ /\b$MatchKey-\d+\b/g : (); | ||
| 192 | |||
| 193 | my %opts = (%Defaults, %$opts); | ||
| 194 | |||
| 195 | if ($opts{require}) { | ||
| 196 | croak "$HOOK: you must cite at least one JIRA issue key in the commit message.\n" | ||
| 197 | unless @keys; | ||
| 198 | } | ||
| 199 | |||
| 200 | return unless @keys; | ||
| 201 | |||
| 202 | # Check if there is a restriction on the project keys allowed | ||
| 203 | if (exists $opts->{projects}) { | ||
| 204 | foreach my $key (@keys) { | ||
| 205 | my ($pkey, $pnum) = split /-/, $key; | ||
| 206 | croak "$HOOK: issue $key is not allowed. You must cite only JIRA issues for the following projects: ", join(', ', sort keys %{$opts->{projects}}), ".\n" | ||
| 207 | unless exists $opts->{projects}{$pkey}; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # Connect to JIRA if not yet connected. | ||
| 212 | unless (defined $JIRA) { | ||
| 213 | $JIRA = eval {JIRA::REST->new($BaseURL, $Login, $Passwd)}; | ||
| 214 | croak "CHECK_JIRA_CONFIG: cannot connect to the JIRA server: $@\n" if $@; | ||
| 215 | } | ||
| 216 | |||
| 217 | $docheck->($svnlook, \@keys, \%opts); | ||
| 218 | last; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | return; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub pre_commit { | ||
| 227 | my ($svnlook) = @_; | ||
| 228 | _check_if_needed($svnlook, \&_pre_checks); | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub post_commit { | ||
| 233 | my ($svnlook) = @_; | ||
| 234 | _check_if_needed($svnlook, \&_post_action); | ||
| 235 | return; | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; # End of SVN::Hooks::CheckJira | ||
| 239 | |||
| 240 | __END__ |