blob: 02f152082954dc2b0a9dd24917fdc46828d0c62c [file] [log] [blame]
Akron88ddd4a2022-11-08 10:41:35 +01001#!/usr/bin/env perl
2use strict;
3use warnings;
4use Mojo::File 'tempfile';
Akronc7aeafd2026-04-29 12:05:49 +02005use Mojo::JSON qw'decode_json encode_json';
6use Mojo::ByteStream 'b';
Akron88ddd4a2022-11-08 10:41:35 +01007use Mojo::UserAgent;
Akron88ddd4a2022-11-08 10:41:35 +01008
Akronc7aeafd2026-04-29 12:05:49 +02009$ENV{MOJO_NO_JSON_XS} = 0;
10
Akron88ddd4a2022-11-08 10:41:35 +010011our @ARGV;
12
Akron081c5d52024-01-11 13:10:22 +010013my $JAVA8_HOME = '/usr/lib/jvm/java-1.8.0-openjdk-amd64';
14my $JAVA11_HOME = '/usr/lib/jvm/java-1.11.0-openjdk-amd64';
15my $JAVA17_HOME = '/usr/lib/jvm/java-1.17.0-openjdk-amd64';
Akronc0638a22025-11-17 10:18:48 +010016my $JAVA21_HOME = '/usr/lib/jvm/java-1.21.0-openjdk-amd64';
Akron081c5d52024-01-11 13:10:22 +010017
Akron57625182024-01-24 13:45:27 +010018unless (-d $JAVA17_HOME) {
Akron081c5d52024-01-11 13:10:22 +010019 $JAVA17_HOME = '/opt/java/jdk-17.0.2';
20};
21
Akronc0638a22025-11-17 10:18:48 +010022unless (-d $JAVA21_HOME) {
23 $JAVA21_HOME = '/opt/java/jdk-21.0.0';
24};
25
Akron807994a2026-05-08 09:54:47 +020026# Check which main branch exists locally
27my $branches = `git branch --list main master`;
28chomp $branches;
29
30# Split into lines and clean up formatting (remove whitespace and asterisks)
31my @branch_list = split /\n/, $branches;
32@branch_list = map { s/^\s*\*?\s*//; $_ } @branch_list;
33
34# Determine which branch to use (prefer main over master)
35my $main_branch;
36if (grep { $_ eq 'main' } @branch_list) {
37 $main_branch = 'main';
38} elsif (grep { $_ eq 'master' } @branch_list) {
39 $main_branch = 'master';
40} else {
41 die "Error: Neither 'main' nor 'master' branch found locally\n";
42}
43
Akronc0638a22025-11-17 10:18:48 +010044
Akron88ddd4a2022-11-08 10:41:35 +010045my $url = $ARGV[0];
46
47sub _check {
48 my $cmd = shift;
Akron53d18212022-11-15 15:25:17 +010049 CHECK:
Akron88ddd4a2022-11-08 10:41:35 +010050 print "\n\n" . 'Do "' . $cmd . '"? (y/n)', "\n";
51
52 my $x = <STDIN>;
53 chomp($x);
54 if ($x eq 'y' || $x eq 'Y') {
55 system $cmd;
Akron53d18212022-11-15 15:25:17 +010056 } elsif ($x eq 'n' || $x eq 'N') {
Akron88ddd4a2022-11-08 10:41:35 +010057 exit;
Akron53d18212022-11-15 15:25:17 +010058 } else {
59 print "Please answer with 'y' or 'n'.\n";
60 goto CHECK;
Akron88ddd4a2022-11-08 10:41:35 +010061 };
62};
63
Akron74994cf2022-11-15 14:28:00 +010064unless ($url) {
Helge4d96c362023-06-21 17:06:32 +020065 print 'git_gerrit_pull https://github.com/KorAP/.../pull/..' . "\n";
Akron88ddd4a2022-11-08 10:41:35 +010066 exit;
67};
68
Akron807994a2026-05-08 09:54:47 +020069_check('git checkout ' . $main_branch);
Akron88ddd4a2022-11-08 10:41:35 +010070
Akron807994a2026-05-08 09:54:47 +020071_check('git pull origin ' . $main_branch);
Akron88ddd4a2022-11-08 10:41:35 +010072
Akron74994cf2022-11-15 14:28:00 +010073$url =~ m!KorAP/([^/]+?)/pull/(\d+?)$!;
Akron88ddd4a2022-11-08 10:41:35 +010074
75my $ua = Mojo::UserAgent->new;
76
77print "Fetch $url...\n";
78
Akron74994cf2022-11-15 14:28:00 +010079my $project = $1;
80my $pr = $2;
Akronc7aeafd2026-04-29 12:05:49 +020081my $pr_html = $ua->get($url)->res->dom;
82my $branch = $pr_html->at('.commit-ref.head-ref');
83
84# Legacy path
85if ($branch) {
86 $branch = $branch->all_text;
87}
88
89# JSON path
90elsif (my $json = $pr_html->at('script[data-target=react-app.embeddedData]')) {
91
92 $json = decode_json(b($json->all_text)->encode->to_string);
93
94 $branch = $json
95 ->{payload}
96 ->{pullRequestsLayoutRoute}
97 ->{pullRequest}
98 ->{headBranch};
99
100};
101
102unless ($branch) {
103 warn 'Unable to retrieve branch name';
104 exit;
105};
Akron88ddd4a2022-11-08 10:41:35 +0100106
107_check('git fetch github pull/' . $pr . '/head:' . $branch);
108_check('git checkout ' . $branch);
Akron807994a2026-05-08 09:54:47 +0200109_check('git rebase ' . $main_branch);
Akron88ddd4a2022-11-08 10:41:35 +0100110
111
Akron88ddd4a2022-11-08 10:41:35 +0100112# Kustvakt
Akron74994cf2022-11-15 14:28:00 +0100113if ($project =~ m!^Kustvakt$!i) {
Akron01ea9212024-01-24 13:42:01 +0100114 _check('JAVA_HOME="'.$JAVA17_HOME.'" mvn clean test');
Akron88ddd4a2022-11-08 10:41:35 +0100115}
116
117# KalamarExport-Plugin
Akron74994cf2022-11-15 14:28:00 +0100118elsif ($project =~ m!^Kalamar-Plugin-Export$!i) {
Helge7cd2a612024-07-17 12:23:51 +0200119 _check('JAVA_HOME="'.$JAVA17_HOME.'" mvn clean test');
Akron88ddd4a2022-11-08 10:41:35 +0100120}
121
Akron39dae912022-11-23 09:41:53 +0100122# Koral
123elsif ($project =~ m!^Koral$!i) {
Akron9f85a202026-01-13 22:18:26 +0100124 _check('JAVA_HOME="'.$JAVA21_HOME.'" mvn clean test');
Akron39dae912022-11-23 09:41:53 +0100125}
126
Akron36ae3642024-01-24 13:42:56 +0100127# Krill
128elsif ($project =~ m!^Krill$!i) {
Akronc0638a22025-11-17 10:18:48 +0100129 _check('JAVA_HOME="'.$JAVA21_HOME.'" mvn clean test');
Akron36ae3642024-01-24 13:42:56 +0100130}
131
Akron579efe02023-02-28 09:52:26 +0100132# Kalamar-Plugin-ExternalResources
133elsif ($project =~ m!^Kalamar-Plugin-ExternalResources$!i) {
134 _check('go test ./...');
135}
136
Akron88ddd4a2022-11-08 10:41:35 +0100137# KorapSRU
Akron74994cf2022-11-15 14:28:00 +0100138elsif ($project =~ m!^KorapSRU$!i) {
Akron081c5d52024-01-11 13:10:22 +0100139 _check('JAVA_HOME="'.$JAVA8_HOME.'" mvn clean test');
Akron88ddd4a2022-11-08 10:41:35 +0100140}
141
Akron081c5d52024-01-11 13:10:22 +0100142# Korap-Tokenizer
143elsif ($project =~ m!^KorAP-Tokenizer$!i) {
144 _check('JAVA_HOME="'.$JAVA8_HOME.'" mvn clean test');
145}
146
147# Datok
Akron579efe02023-02-28 09:52:26 +0100148elsif ($project =~ m!^Datok$!i) {
149 _check('make test');
150}
151
Akron74994cf2022-11-15 14:28:00 +0100152# Unknown Project
Akron88ddd4a2022-11-08 10:41:35 +0100153else {
Akron74994cf2022-11-15 14:28:00 +0100154 print "Unknown project $project!\n\n";
Akron88ddd4a2022-11-08 10:41:35 +0100155 exit(1);
156};
157
158
159
160_check('git commit --amend --no-edit');
161
162{
163 my $ret = `git log -1 --pretty=%B`;
164 my $newret = '';
165 my $change_id = '';
166 my $first = 1;
167 foreach my $line (split "\n", $ret) {
168 if ($first) {
169 chomp $line;
170 $line .= " (closes #$pr)\n";
171 $first = 0;
172 }
173 elsif ($line =~ /^Change-Id: /) {
174 $change_id = $line;
175 next;
176 };
177 $newret .= $line . "\n";
178 };
179
180 if ($change_id) {
181 $newret .= "\n" . $change_id."\n\n";
182 };
183
184 my $msg_file = tempfile();
185 $msg_file->spurt($newret);
186
187 print "===================================\n";
188 print $newret;
189 print "===================================\n";
190
191 _check('git commit -F ' . $msg_file . ' --amend');
192};
193
Akron807994a2026-05-08 09:54:47 +0200194_check('git push origin HEAD:refs/for/' . $main_branch);
Akron88ddd4a2022-11-08 10:41:35 +0100195
Akron807994a2026-05-08 09:54:47 +0200196_check('git checkout ' . $main_branch);
Akron88ddd4a2022-11-08 10:41:35 +0100197_check('git branch -D ' . $branch);
198
199print "\n\nEverything done - thank you!\n\n"
200
201__END__
202
203=pod
204
205This script is an alternative to C<import_github_pull_requests>
206that ignores CI results on github and just requires the
207URL of a specific GH pull request to send it through Gerrit.
Helge4d96c362023-06-21 17:06:32 +0200208
209Prerequisites:
210Remote repository "github"
211For Kustvakt and Koral Java 11 at /usr/lib/jvm/java-1.11.0-openjdk-amd6
Helge7cd2a612024-07-17 12:23:51 +0200212For KorapSRU Java 8 at /usr/lib/jvm/java-1.8.0-openjdk-amd6
213For KalamarExport-Plugin Java 17 at /usr/lib/jvm/java-17-openjdk-amd64