| 1 | package BioSAILs::Integrations::Github; | ||
| 2 | |||
| 3 | use MooseX::App::Role; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | use Git::Wrapper; | ||
| 7 | use Git::Wrapper::Plus::Ref::Tag; | ||
| 8 | use Git::Wrapper::Plus::Tags; | ||
| 9 | use Git::Wrapper::Plus::Branches; | ||
| 10 | |||
| 11 | use Try::Tiny; | ||
| 12 | |||
| 13 | use Sort::Versions; | ||
| 14 | use Version::Next qw/next_version/; | ||
| 15 | |||
| 16 | use Cwd; | ||
| 17 | use List::Uniq ':all'; | ||
| 18 | use File::Path qw(make_path); | ||
| 19 | use File::Slurp; | ||
| 20 | use File::Spec; | ||
| 21 | use File::Temp qw/ tempfile tempdir /; | ||
| 22 | use Capture::Tiny ':all'; | ||
| 23 | use Data::Dumper; | ||
| 24 | use DateTime; | ||
| 25 | |||
| 26 | with 'BioSAILs::Utils'; | ||
| 27 | |||
| 28 | option 'version' => ( | ||
| 29 | is => 'rw', | ||
| 30 | required => 0, | ||
| 31 | predicate => 'has_version', | ||
| 32 | documentation => | ||
| 33 | 'Submission version. Each version has a corresponding git tag.' | ||
| 34 | . ' See the difference between tags with `git diff tag1 tag2`.' | ||
| 35 | . ' Tags are always version numbers, starting with 0.01.', | ||
| 36 | ); | ||
| 37 | |||
| 38 | option 'autocommit' => ( | ||
| 39 | traits => ['Bool'], | ||
| 40 | is => 'rw', | ||
| 41 | isa => 'Bool', | ||
| 42 | default => 1, | ||
| 43 | documentation => 'Always commit files.', | ||
| 44 | handles => { no_autocommit => 'unset', }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | =head3 tags | ||
| 48 | |||
| 49 | Additional tags for commit message | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | option 'tags' => ( | ||
| 54 | traits => ['Array'], | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'ArrayRef', | ||
| 57 | documentation => 'Annotate with tags.', | ||
| 58 | required => 0, | ||
| 59 | default => sub { [] }, | ||
| 60 | handles => { | ||
| 61 | has_tags => 'count', | ||
| 62 | count_tags => 'count', | ||
| 63 | join_tags => 'join', | ||
| 64 | all_tags => 'elements', | ||
| 65 | }, | ||
| 66 | cmd_aliases => ['t'], | ||
| 67 | cmd_split => qr/,/, | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'message' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Str', | ||
| 73 | required => 0, | ||
| 74 | predicate => 'has_message', | ||
| 75 | cmd_aliases => ['m'], | ||
| 76 | default => 'hpcrunner commit', | ||
| 77 | documentation => 'Message to use for git commit', | ||
| 78 | ); | ||
| 79 | |||
| 80 | has 'git_dir' => ( | ||
| 81 | is => 'rw', | ||
| 82 | isa => 'Str', | ||
| 83 | default => sub { return cwd() }, | ||
| 84 | predicate => 'has_git_dir', | ||
| 85 | ); | ||
| 86 | |||
| 87 | has 'git' => ( | ||
| 88 | is => 'rw', | ||
| 89 | predicate => 'has_git', | ||
| 90 | required => 0, | ||
| 91 | ); | ||
| 92 | |||
| 93 | has 'current_branch' => ( | ||
| 94 | is => 'rw', | ||
| 95 | isa => 'Str', | ||
| 96 | required => 0, | ||
| 97 | predicate => 'has_current_branch', | ||
| 98 | ); | ||
| 99 | |||
| 100 | has 'remote' => ( | ||
| 101 | is => 'rw', | ||
| 102 | isa => 'Str', | ||
| 103 | required => 0, | ||
| 104 | predicate => 'has_remote', | ||
| 105 | ); | ||
| 106 | |||
| 107 | has 'tag_prefix' => ( | ||
| 108 | is => 'rw', | ||
| 109 | isa => 'Str', | ||
| 110 | default => 'hpcrunner', | ||
| 111 | ); | ||
| 112 | |||
| 113 | =head2 Subroutines | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | =head3 init_git | ||
| 118 | |||
| 119 | Create a new Git::Wrapper object | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub init_git { | ||
| 124 | my $self = shift; | ||
| 125 | |||
| 126 | my $git = Git::Wrapper->new( cwd() ) | ||
| 127 | or die print "Could not initialize Git::Wrapper $!\n"; | ||
| 128 | |||
| 129 | try { | ||
| 130 | my @output = $git->rev_parse(qw(--show-toplevel)); | ||
| 131 | $self->git_dir( $output[0] ); | ||
| 132 | $git = Git::Wrapper->new( $self->git_dir ); | ||
| 133 | $self->git($git); | ||
| 134 | } | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | sub git_info { | ||
| 139 | my $self = shift; | ||
| 140 | |||
| 141 | return unless $self->has_git; | ||
| 142 | |||
| 143 | $self->branch_things; | ||
| 144 | $self->get_version; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head3 dirty_run | ||
| 148 | |||
| 149 | Check for uncommited files | ||
| 150 | #TODO add in option for autocommiting | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub dirty_run { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | return unless $self->has_git; | ||
| 158 | |||
| 159 | my $dirty_flag = $self->git->status->is_dirty; | ||
| 160 | |||
| 161 | if ( $dirty_flag && !$self->autocommit ) { | ||
| 162 | $self->app_log->warn( | ||
| 163 | "There are uncommited files in your repo!\n\tPlease commit these files." | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | elsif ( $dirty_flag && $self->autocommit ) { | ||
| 167 | $self->commit_git_files; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub commit_git_files { | ||
| 172 | my $self = shift; | ||
| 173 | $self->app_log->warn( "There are uncommited files in your repo!\n\t" | ||
| 174 | . "We will try to commit these files before running.\n" ); | ||
| 175 | |||
| 176 | try { | ||
| 177 | my $cmd = 'git add -A'; | ||
| 178 | $self->run_short_command($cmd); | ||
| 179 | |||
| 180 | my $msg = $self->gen_git_commit_message; | ||
| 181 | my ( $fh, $filename ) = tempfile(); | ||
| 182 | write_file( $filename, $msg ); | ||
| 183 | |||
| 184 | $cmd = 'git commit -F ' . $filename; | ||
| 185 | my $res = $self->run_short_command($cmd); | ||
| 186 | |||
| 187 | $self->app_log->info("Successfully commited files to git."); | ||
| 188 | } | ||
| 189 | catch { | ||
| 190 | $self->app_log->warn("Were not able to commit files to git"); | ||
| 191 | $self->app_log->warn( "STDERR: " . $_->error ); | ||
| 192 | $self->app_log->warn( "STDOUT: " . $_->output ); | ||
| 193 | $self->app_log->warn( "STATUS: " . $_->status ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub gen_git_commit_message { | ||
| 198 | my $self = shift; | ||
| 199 | |||
| 200 | my $dt = DateTime->now( time_zone => 'local' ); | ||
| 201 | my $text = ""; | ||
| 202 | $text .= $self->message . "\n"; | ||
| 203 | $text .= "[DateTime]: $dt\n"; | ||
| 204 | if ( $self->count_tags ) { | ||
| 205 | $text .= "[ Tags ]: " . $self->join_tags(', ') . "\n"; | ||
| 206 | } | ||
| 207 | |||
| 208 | return $text; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub branch_things { | ||
| 212 | my $self = shift; | ||
| 213 | |||
| 214 | return unless $self->has_git; | ||
| 215 | my $current; | ||
| 216 | |||
| 217 | try { | ||
| 218 | my $branches = Git::Wrapper::Plus::Branches->new( git => $self->git ); | ||
| 219 | |||
| 220 | for my $branch ( $branches->current_branch ) { | ||
| 221 | $self->current_branch( $branch->name ); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | catch { | ||
| 225 | $self->current_branch('master'); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | sub git_config { | ||
| 230 | my ($self) = @_; | ||
| 231 | |||
| 232 | return unless $self->has_git; | ||
| 233 | |||
| 234 | #First time we run this we want the name, username, and email | ||
| 235 | my @output = $self->git->config(qw(--list)); | ||
| 236 | |||
| 237 | my %config = (); | ||
| 238 | foreach my $c (@output) { | ||
| 239 | my @words = split /=/, $c; | ||
| 240 | $config{ $words[0] } = $words[1]; | ||
| 241 | } | ||
| 242 | return \%config; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub git_logs { | ||
| 246 | my ($self) = shift; | ||
| 247 | |||
| 248 | return unless $self->has_git; | ||
| 249 | my @logs = $self->git->log; | ||
| 250 | return \@logs; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub get_version { | ||
| 254 | my ($self) = shift; | ||
| 255 | |||
| 256 | return unless $self->has_git; | ||
| 257 | return if $self->has_version; | ||
| 258 | |||
| 259 | my $cmd = | ||
| 260 | 'git describe --tags ' | ||
| 261 | . '$(git rev-list --tags --max-count=1 )' | ||
| 262 | . ' --match "hpcrunner*"'; | ||
| 263 | |||
| 264 | my $res = $self->run_short_command($cmd); | ||
| 265 | |||
| 266 | my @versions; | ||
| 267 | ##These exit codes are screwy... | ||
| 268 | if ( defined $res->{exit_code} ) { | ||
| 269 | foreach my $buf ( @{ $res->{full_buffer} } ) { | ||
| 270 | chomp($buf); | ||
| 271 | if ( $buf =~ m/^hpcrunner-\d+\.\d+/ ) { | ||
| 272 | push( @versions, $buf ); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | } | ||
| 276 | @versions = () unless @versions; | ||
| 277 | |||
| 278 | ##TODO Make this dynamic - should be the prefix | ||
| 279 | if ( @versions && $#versions >= 0 ) { | ||
| 280 | my $version = $versions[0]; | ||
| 281 | $version =~ s/hpcrunner-//g; | ||
| 282 | my $pv = next_version($version); | ||
| 283 | $pv = "$pv"; | ||
| 284 | $self->version( 'hpcrunner-' . $pv ); | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | $self->version('hpcrunner-0.01'); | ||
| 288 | } | ||
| 289 | |||
| 290 | $self->git_create_tags; | ||
| 291 | $self->git_push_tags; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub git_create_tags { | ||
| 295 | my $self = shift; | ||
| 296 | my ( $fh, $filename ) = tempfile(); | ||
| 297 | |||
| 298 | my $text = "BioSAILs Commit\n\n"; | ||
| 299 | |||
| 300 | if ( $self->count_tags ) { | ||
| 301 | $text .= "[ Version ]: " . $self->version . "\n"; | ||
| 302 | $text .= "[ Tags ]: " . $self->join_tags(', ') . "\n"; | ||
| 303 | } | ||
| 304 | write_file( $filename, $text ); | ||
| 305 | my $git_command = "git tag -a " . $self->version . " -F " . $filename; | ||
| 306 | my $res = $self->run_short_command($git_command); | ||
| 307 | } | ||
| 308 | |||
| 309 | #TODO Make this an option | ||
| 310 | sub git_push_tags { | ||
| 311 | my ($self) = shift; | ||
| 312 | |||
| 313 | $self->app_log->debug('Pushing git tags...'); | ||
| 314 | return unless $self->has_git; | ||
| 315 | return unless $self->has_version; | ||
| 316 | |||
| 317 | if ( $self->git->status->is_dirty ) { | ||
| 318 | $self->app_log->fatal( | ||
| 319 | "We have noticed a dirty commit while trying to generate tags...\n" | ||
| 320 | ); | ||
| 321 | } | ||
| 322 | return if $self->git->status->is_dirty; | ||
| 323 | |||
| 324 | my @remote = $self->git->remote; | ||
| 325 | |||
| 326 | foreach my $remote (@remote) { | ||
| 327 | $self->git->push( { tags => 1 }, $remote ); | ||
| 328 | } | ||
| 329 | |||
| 330 | } | ||
| 331 | |||
| 332 | 1; |