summaryrefslogtreecommitdiffstats
path: root/extras/scripts/vpp-review
blob: 8ce4bd2d9c1ba84ed19f145156ea9b662cd3401e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/perl

use Data::Dumper;
use Env;
use File::Glob ':glob';

#
# Use this script to determine the reviewers for a given commit, like so:
#
# ayourtch@ayourtch-lnx:~/vpp$ ./extras/scripts/vpp-review HEAD
# commit 7ea63c597f82412b68b5a565df10e13669b1decb
# Author: Andrew Yourtchenko <ayourtch@gmail.com>
# Date:   Wed Jul 14 22:44:05 2021 +0200
#
#     misc: experimental script to get the list of the reviewers for a commit
#
#     accepts zero or one argument (the commit hash), and outputs
#     the detected components, the component maintainers,
#     and the final suggested reviewer list
#
#     Change-Id: Ief671fe837c6201bb11fd05d02af881822b0bb33
#     Type: docs
#     Signed-off-by: Andrew Yourtchenko <ayourtch@gmail.com>
#
# :000000 100755 000000000 635dde59f A	extras/scripts/vpp-review
#
#
# Components of files in the commit:
#     misc: extras/scripts/vpp-review
#
# Component misc maintainers:
#    vpp-dev Mailing List <vpp-dev@fd.io>
#
#
# Final reviewer list:
#    vpp-dev Mailing List <vpp-dev@fd.io>
# ayourtch@ayourtch-lnx:~/vpp$
#

#
# Read the maintainers file into a hash, indexed by short component name.
#
sub read_maintainers() {
        open(F, "MAINTAINERS") || die("Could not open MAINTAINERS file");
        my $short_name = "";
        my $long_name = "";
        my $in_component = 0;
	my $maintainers = [];
	my $paths = [];
	my $exclude_paths = [];
	my $feature_yaml = [];
	my $comments = [];
        my $out = {};
        while (<F>) {
                chomp();
                my $aLine = $_;
                # print ("LINE: $aLine\n");
                if (/^([A-Z]):\s+(.*)$/) {
                        my $aLetter = $1;
                        my $aValue = $2;
			# print ("LETTER: $aLetter, VALUE: $aValue\n");
                        if ($aLetter eq "I") {
                                $short_name = $aValue;
                        }
			elsif ($aLetter eq "M") {
                                push(@{$maintainers}, $aValue);
                        }
                        elsif ($aLetter eq "F") {
                                push(@{$paths}, $aValue);
                        }
                        elsif ($aLetter eq "E") {
                                push(@{$exclude_paths}, $aValue);
                        }
                        elsif ($aLetter eq "Y") {
                                push(@{$feature_yaml}, $aValue);
			}
                        elsif ($aLetter eq "C") {
                                push(@{$comments}, $aValue);
                        } else {
                           print ("LETTER: $aLetter, VALUE: $aValue\n");
			}
                        # FIXME: deal with all the other letters here
                } elsif (/^(\s*)$/) {
                        if ($in_component) {
                                $in_component = 0;
                                if ($short_name ne "") {
                                        my $comp = {};
                                        $comp->{'short_name'} = $short_name;
                                        $comp->{'name'} = $long_name;
					$comp->{'maintainers'} = $maintainers;
					$comp->{'paths'} = $paths;
					$comp->{'comments'} = $comments;
					$comp->{'exclude_paths'} = $exclude_paths;
					$comp->{'feature_yaml'} = $feature_yaml;
                                        $out->{$short_name} = $comp;
                                        # print("FEATURE: $short_name => $long_name\n");
                                        $short_name = "";
                                        $long_name = "";
					$maintainers = [];
					$paths = [];
					$exclude_paths = [];
					$comments = [];
					$feature_yaml = [];
                                }
                        }
                        # print ("END\n");
                } elsif (/^([^\s].*)$/) {
                        $in_component = 1;
                        $long_name = $1;
                }
        }

	if ($in_component) {
		$in_component = 0;
		if ($short_name ne "") {
			my $comp = {};
			$comp->{'short_name'} = $short_name;
		       	$comp->{'name'} = $long_name;
			$comp->{'maintainers'} = $maintainers;
			$comp->{'paths'} = $paths;
			$comp->{'comments'} = $comments;
			$comp->{'exclude_paths'} = $exclude_paths;
			$comp->{'feature_yaml'} = $feature_yaml;
			$out->{$short_name} = $comp;
		}

	}

        return($out);
}

sub match_my_path {
	my $p = $_[0];
	my $apath = $_[1];
	my $root = $_[2];

	my $p1 = $p;
	$p1 =~ s#\*#[^/]*#g;

	my $pattern = "$root$p1";

	if ($apath =~ /$pattern/) {
		return 1;
	} else {
		return 0;
	}
}

sub match_path_to_list {
	my $path_list = $_[0];
	my $apath = $_[1];
	my $root = $_[2];
	foreach $p (@{$path_list}) {
		if (match_my_path($p, $apath, $root)) {
				return 1;
		}
	}
	return 0;
}

sub match_path_to_comp_hash {
	my $chash = $_[0];
	my $apath = $_[1];
	my $root = $_[2];
	my $is_included = match_path_to_list($chash->{'paths'}, $apath, $root);
	my $is_excluded = match_path_to_list($chash->{'exclude_paths'}, $apath, $root);
	return ($is_included && !$is_excluded);
}


sub match_path {
	my $components = $_[0];
	my $apath = $_[1];
	my $root = $_[2];
	my $out = [];

        foreach $aCompName (keys %{$components}) {
		my $chash = $components->{$aCompName};
		if (match_path_to_comp_hash($chash, $apath, $root)) {
			push(@{$out}, $aCompName);
		}
	}
	my $out1 = $out;

	if (scalar @{$out} > 1) {
		# not very efficient way to filter out "misc" but oh well.
		$out1 = [];
		foreach $aval (@{$out}) {
			if ($aval ne "misc") {
				push(@{$out1}, $aval);
			}
		}
	}

	return ($out1);
}

my $commit_id = $ARGV[0];
my $commit_log = `git log --raw -n 1 $commit_id`;
my $files = [];
foreach my $aLine (split(/[\r\n]+/, $commit_log)) {
	if ($aLine =~ /^:[0-7]+\s+[0-7]+\s+[0-9a-f]+\s+[0-9a-f]+\s+\S+\s+(\S+)$/) {
		push(@{$files}, $1);
	}
}

my $comp = read_maintainers();
my $matched_comps = {};
my $matched_reviewers = {};

print("$commit_log\n\n");

print("Components of files in the commit:\n");

foreach $aFile (@{$files}) {
	my $matches = match_path($comp, $aFile, '');
        my $matches_str = join(" ", @{$matches});
	print ("    $matches_str: $aFile\n");

	foreach my $aComp (@{$matches}) {
		$matched_comps->{$aComp} = 1;
	}
}

foreach my $aKey (keys %{$matched_comps}) {
	print("\nComponent $aKey maintainers:\n");
	foreach my $aRV (@{$comp->{$aKey}->{'maintainers'}}) {
		print("   $aRV\n");
		$matched_reviewers->{$aRV} = 1;
	}
}

print("\n\nFinal reviewer list:\n");

foreach my $aRV (keys %{$matched_reviewers}) {
	print("   $aRV\n");
}
# print Dumper(\$comp);