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