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