summaryrefslogtreecommitdiffstats
path: root/email/aiaiai-email-test-patchset
blob: 1988a7164d216e6a6f5a335c110cda032a032b39 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/sh -efu

# Copyright 2011-2012 Intel Corporation
# Author: Artem Bityutskiy
# License: GPLv2

srcdir="$(readlink -ev -- ${0%/*}/..)"
PATH="$srcdir:$srcdir/helpers:$srcdir/email:$srcdir/helpers/libshell:$PATH"

. shell-error
. shell-args
. shell-signal
. aiaiai-sh-functions
. aiaiai-email-sh-functions

PROG="${0##*/}"
export message_time="yes"

# This is a small trick to make sure the script is portable - check if 'dash'
# is present, and if yes - use it.
if can_switch_to_dash; then
	exec dash -euf -- "$srcdir/$PROG" "$@"
	exit $?
fi

show_usage()
{
	cat <<-EOF
Usage: $PROG [options] <cfgfile.ini>

The mbox file containing the patches to test is expected to come from stdin
(unless --input option is specified).

<cfgfile.ini>          - the configuration file.

Options:
  -i, --input=MBOX       use the MBOX file instead of stdin;
  -C, --confdir=CDIR     path to the directory containing the defconfig files
                         (those you specify in the config file); by default the
                         defconfig files are assumed to be part of the
                         <kernel-tree>; this option makes it possible to use
                         stand-alone defconfig files instead;
  -p, --preserve         preserve all the temporary files - do not clean up;
      --test-mode        test mode - work as usually, but do not actually
                         send replies;
      --bisectability    test bisectability;
      --sparse           check with sparse while building;
      --smatch           check with smatch while building;
      --cppcheck         check with cppcheck;
      --coccinelle       check with coccinelle;
  -v, --verbose          be verbose;
  -h, --help             show this text and exit.
EOF
}

fail_usage()
{
	[ -z "$1" ] || printf "%s\n" "$1"
	show_usage
	exit 1
}

preserve=
verbose=
mbox=
tmpdir=
cleanup_handler()
{
	rm $verbose -rf -- "$mbox" >&2

	if [ -n "$preserve" ]; then
		verbose "Preserved tmpdir: $tmpdir"
	else
		[ -z "$tmpdir" ] || verbose "Removing $tmpdir";
		rm -rf -- "$tmpdir" >&2
	fi
}
set_cleanup_handler cleanup_handler

# List currently supported projects.
list_projects()
{
	local prj

	LC_ALL=C sed -n -e "s/^\[prj_\(.*\)\]$/\1/p" "$cfgfile" | \
	    while read -r prj; do
		# Get project description
		local descr
		ini_config_get_or_die descr "$cfgfile" "prj_$prj" "description"

		local email="$cfg_ownmail_local+$prj@$cfg_ownmail_domain"
		printf "* %s\n" "$prj ($email): $descr"
	done
}

# Send an e-mail reply to the patch submitter.
send_email()
{
	compose_email "$reply_to" "$reply_cc" "$reply_subj" "$reply_id" \
		> "$tmpdir/mail"

	[ -z "$verbose" ] || cat -- "$tmpdir/mail" >&2

	if [ -z "$test_mode" ]; then
		mutt -x -H "$tmpdir/mail" </dev/null
	else
		verbose "Do not actually send the email - we are in test mode"
	fi
}

# This function is called when the target project for the patch under test was
# not specified. It sends a sensible reply back to the submitter, without
# carbon-copying anyone else.
error_no_project_specified()
{
	send_email <<EOF
Sorry, but you have not specified the project name. Please, specify it
using the "+" symbol in the e-mail address of $cfg_ownname. For example,
"$cfg_ownmail_local+XYZ@$cfg_ownmail_domain" would mean project "XYZ".

List of projects $cfg_ownname supports:

$(list_projects)

Please, contact "$cfg_adminname" <$cfg_adminmail>
if you have any questions.
EOF
	exit 0
}

# This function is called when the patch submitter specifies a non-existing
# project. It sends a sensible reply back, without carbon-copying anyone else.
error_project_not_found()
{
	send_email <<EOF
Sorry, but project "$prj" is not supported. List of projects $cfg_ownname
currently supports:

$(list_projects)

Please, contact "$cfg_adminname" <$cfg_adminmail>
if you have any questions.
EOF
	exit 0
}

# This function is called when the 'aiaiai-test-patchest' fails. This most
# probably means a bug or configuration issues. This function sends
# corresponding e-mail notification.
error_test_patchset_failed()
{
	send_email <<EOF
Sorry, but an internal $cfg_ownname error happened. Please, contact
"$cfg_adminname" <$cfg_adminmail>.
EOF
	exit 0
}

# This is a helper function which sends a notification about the patch under
# test being accepted for testing.
send_accepted_email()
{
	send_email <<EOF
Your patch or patch-set:

$(fetch_header_per_patch "Subject" < "$mbox" | sort)

has been accepted by $cfg_ownname and scheduled for testing.
EOF
}

# This is a helper function which sends the reply e-mail with the results of
# testing.
send_results_email()
{
	send_email <<EOF
$cfg_built_preamble

$(fetch_header_per_patch "Subject" < "$mbox" | sort)

Project: $pcfg_name ($pcfg_description)

Configurations: $pcfg_configs

$(cat -- $tmpdir/test-patchset.log)
EOF
}

TEMP=`getopt -n $PROG -o i:,C:,p,v,h --long test-mode,input:,confdir:,preserve,bisectability,sparse,smatch,cppcheck,coccinelle,verbose,help -- "$@"` ||
	fail_usage ""
eval set -- "$TEMP"

mbox=
confdir=
test_mode=
bisectability=
sparse=
smatch=
cppcheck=
coccinelle=

while true; do
	case "$1" in
	-i|--input)
		mbox="$(opt_check_read "$1" "$2")"
		shift
		;;
	-C|--confdir)
		confdir="$(opt_check_dir "$1" "$2")"
		shift
		;;
	-p|--preserve)
		preserve="--preserve"
		;;
	--test-mode)
		test_mode=y
		;;
	--bisectability)
		bisectability="--bisectability"
		;;
	--sparse)
		sparse="--sparse"
		;;
	--smatch)
		smatch="--smatch"
		;;
	--cppcheck)
		cppcheck="--cppcheck"
		;;
	--coccinelle)
		coccinelle="--coccinelle"
		;;
	-v|--verbose) verbose=-v
		;;
	-h|--help)
		show_usage
		exit 0
		;;
	--) shift; break
                ;;
	*) fail_usage "Unrecognized option: $1"
		;;
	esac
	shift
done

[ "$#" -eq 1 ] || die "Insufficient or too many arguments"

program_required "mutt" ""
program_required "grep" ""
program_required "sed" ""
program_required "formail" ""

cfgfile="$1"; shift

# Save the mbox to a temporary file if it comes from stdin
if [ -z "$mbox" ]; then
	mbox="$(mktemp -t "$PROG.mbox.XXXX")"
	cat > "$mbox"
fi

cat "$mbox"
# Use the cover letter subject and message ID if possible. If the cover letter
# was present, 'aiaiai-email-lda' would save them in special privat headers.
subj="$(fetch_header "X-Aiaiai-Cover-Letter-Subject" < "$mbox")"
[ -n "$subj" ] || fetch_header_or_die subj "Subject" < "$mbox"

id="$(fetch_header "X-Aiaiai-Cover-Letter-Message-Id" < "$mbox")"
[ -n "$id" ] || fetch_header_or_die id "Message-Id" < "$mbox"

fetch_header_or_die from "From" < "$mbox"
to="$(fetch_header "To" < "$mbox")"
cc="$(fetch_header "Cc" < "$mbox")"

# Either "To:" or "Cc:" must exist
if [ -z "$to" ] && [ -z "$cc" ]; then
       die "Neither \"To:\" nor \"Cc:\" header found"
fi

printf "\n"
verbose "Testing mbox: \"$from: $subj (Message-Id: $id)\""
verbose "parsing config file \"$cfgfile\""

parse_config "$cfgfile"

mkdir $verbose -p -- "$cfg_workdir" >&2

tmpdir="$(mktemp --tmpdir="$cfg_workdir" -dt "$PROG.XXXX")"
mv $verbose -- "$mbox" "$tmpdir/mbox" >&2
mbox="$tmpdir/mbox"

# Find out the project name
prj="$(fetch_project_name "$to" "$cfg_ownmail")"
verbose "Project \"$prj\""

# Replies will refer the first patch of the patch-set under test
reply_subj="$subj"
reply_id="$id"
# Replies will be sent to the patch submitter
reply_to="$from"
# And do not Cc anyone thus far
reply_cc=

# Reject the e-mail if the project has not been specified
if [ -z "$prj" ]; then
	error_no_project_specified
fi

# Get the project configuration
parse_prj_config "$cfgfile" "$prj"

# Check if the project specified by the submitter exists
if [ -z "$pcfg_name" ]; then
	error_project_not_found
fi

# Create the Cc list for replies that we'll be sending
if [ "$pcfg_reply_to_all" != "1" ]; then
	# Replies all the patch recipients will be CCed
	reply_cc="$(merge_addresses "$to" "$cc")"
	reply_cc="$(merge_addresses "$reply_cc" "$pcfg_always_cc")"
	reply_cc=$(strip_address "$reply_cc" "$cfg_ownmail")
else
	reply_cc="$pcfg_always_cc"
fi

# Notify the sender that the patches have been accepted
if [ "$pcfg_accept_notify" = "1" ]; then
	verbose "Sending \"accepted\" e-mail"
	send_accepted_email
fi

# Test the path (or patch-set)
verbose "Test configs \"$pcfg_configs\" branch \"$pcfg_branch\" of \"$pcfg_path\""
aiaiai-test-patchset $verbose $preserve \
	$bisectability $sparse $smatch $cppcheck $coccinelle \
	-i "$mbox" -j "$cfg_jobs" -c "$pcfg_branch" -w "$tmpdir" \
	${confdir:+-C "$confdir"} \
	${pcfg_unwanted_keywords:+-K "$pcfg_unwanted_keywords"} -- \
	${pcfg_kmake_opts:+-M "$pcfg_kmake_opts"} \
	"$pcfg_path" "$pcfg_configs" > "$tmpdir/test-patchset.log" ||
{
	verbose "aiaiai-test-patchset failed"
	error_test_patchset_failed
}

# Mail the results of testing
verbose "Test is finished, sending back the results"
send_results_email