Akron | 88ddd4a | 2022-11-08 10:41:35 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Mojo::File 'tempfile'; |
| 5 | use Mojo::UserAgent; |
| 6 | use Cwd qw(); |
| 7 | |
| 8 | our @ARGV; |
| 9 | |
| 10 | my $url = $ARGV[0]; |
| 11 | |
| 12 | sub _check { |
| 13 | my $cmd = shift; |
| 14 | print "\n\n" . 'Do "' . $cmd . '"? (y/n)', "\n"; |
| 15 | |
| 16 | my $x = <STDIN>; |
| 17 | chomp($x); |
| 18 | if ($x eq 'y' || $x eq 'Y') { |
| 19 | system $cmd; |
| 20 | } else { |
| 21 | exit; |
| 22 | }; |
| 23 | }; |
| 24 | |
| 25 | unless ($url ) { |
| 26 | print 'git-gerrit-pull https://github.com/KorAP/.../pull/..' . "\n"; |
| 27 | exit; |
| 28 | }; |
| 29 | |
| 30 | _check('git checkout master'); |
| 31 | |
| 32 | _check('git pull origin master'); |
| 33 | |
| 34 | $url =~ m!/pull/(\d+?)$!; |
| 35 | |
| 36 | my $ua = Mojo::UserAgent->new; |
| 37 | |
| 38 | print "Fetch $url...\n"; |
| 39 | |
| 40 | my $pr = $1; |
| 41 | my $branch = $ua->get($url)->res->dom->at('.commit-ref.head-ref')->all_text; |
| 42 | |
| 43 | _check('git fetch github pull/' . $pr . '/head:' . $branch); |
| 44 | _check('git checkout ' . $branch); |
| 45 | _check('git rebase master'); |
| 46 | |
| 47 | |
| 48 | my $path = Cwd::abs_path(); |
| 49 | |
| 50 | # Kustvakt |
| 51 | if ($path =~ m!Kustvakt/?$!) { |
| 52 | _check('cd core && JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" mvn clean install'); |
| 53 | _check('cd lite && JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" mvn clean test'); |
| 54 | _check('cd full && JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" mvn clean test'); |
| 55 | } |
| 56 | |
| 57 | # KalamarExport-Plugin |
| 58 | elsif ($path =~ m!KalamarExportPlugin/?$!) { |
| 59 | _check('JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" mvn clean test'); |
| 60 | } |
| 61 | |
| 62 | # KorapSRU |
| 63 | elsif ($path =~ m!KorapSRU/?$!) { |
| 64 | _check('JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-amd64" mvn clean test'); |
| 65 | } |
| 66 | |
| 67 | # Unknown |
| 68 | else { |
| 69 | print "Unknown directory!\n\n"; |
| 70 | exit(1); |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | |
| 75 | _check('git commit --amend --no-edit'); |
| 76 | |
| 77 | { |
| 78 | my $ret = `git log -1 --pretty=%B`; |
| 79 | my $newret = ''; |
| 80 | my $change_id = ''; |
| 81 | my $first = 1; |
| 82 | foreach my $line (split "\n", $ret) { |
| 83 | if ($first) { |
| 84 | chomp $line; |
| 85 | $line .= " (closes #$pr)\n"; |
| 86 | $first = 0; |
| 87 | } |
| 88 | elsif ($line =~ /^Change-Id: /) { |
| 89 | $change_id = $line; |
| 90 | next; |
| 91 | }; |
| 92 | $newret .= $line . "\n"; |
| 93 | }; |
| 94 | |
| 95 | if ($change_id) { |
| 96 | $newret .= "\n" . $change_id."\n\n"; |
| 97 | }; |
| 98 | |
| 99 | my $msg_file = tempfile(); |
| 100 | $msg_file->spurt($newret); |
| 101 | |
| 102 | print "===================================\n"; |
| 103 | print $newret; |
| 104 | print "===================================\n"; |
| 105 | |
| 106 | _check('git commit -F ' . $msg_file . ' --amend'); |
| 107 | }; |
| 108 | |
| 109 | _check('git-gerrit'); |
| 110 | |
| 111 | _check('git checkout master'); |
| 112 | _check('git branch -D ' . $branch); |
| 113 | |
| 114 | print "\n\nEverything done - thank you!\n\n" |
| 115 | |
| 116 | __END__ |
| 117 | |
| 118 | =pod |
| 119 | |
| 120 | This script is an alternative to C<import_github_pull_requests> |
| 121 | that ignores CI results on github and just requires the |
| 122 | URL of a specific GH pull request to send it through Gerrit. |
| 123 | The detection paths for the repository specific actions may |
| 124 | differ. It's probably beneficial to make this dependent on the |
| 125 | GitHub URL instead of the local repository ... |