summaryrefslogtreecommitdiff
path: root/pw-trigger.sh
blob: 08a72413020dff4371cbcd27018045c0065c31e3 (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
#!/bin/bash

set -euf -o pipefail +x

scripts=$(dirname $0)
# shellcheck source=jenkins-helpers.sh
. $scripts/jenkins-helpers.sh
# shellcheck source=pw-helpers.sh
. $scripts/pw-helpers.sh

convert_args_to_variables "$@"

obligatory_variables ci_bot project pw_token out_dir
declare              ci_bot project pw_token out_dir

verbose="${verbose-true}"

if $verbose; then
    set -x
fi

# BE CAREFUL WITH $pw_token
# shellcheck disable=SC2064
trap "pw_deinit $project" EXIT
(set +x; pw_init "$project" "$pw_token")

yaml=$(mktemp)
# shellcheck disable=SC2064
trap "pw_deinit $project; rm -f $yaml" EXIT

git -C "$project" pw series list -f yaml > "$yaml"
mkdir -p "$out_dir"

len=$(shyaml get-length < "$yaml")
i=$len
j=0
# Go through series with the oldest first.
while [ "$i" -gt "0" ]; do
    i=$(($i - 1))
    series_id=$(shyaml get-value $i.id < "$yaml")

    if ! pw_series_complete_p "$project" "$series_id"; then
	continue
    fi

    # Look for an untested patch in the series.
    num_patch=0
    while true; do
	patch_id=$(pw_get_patch_from_series \
		       "$project" "$series_id" "$num_patch" || true)
	if [ "$patch_id" = "" ]; then
	    break
	fi

	check_state=$(pw_patch_check_state "$patch_id" "$ci_bot")
	if [ "$check_state" = "pending" ]; then
	    break
	fi
	num_patch=$(($num_patch + 1))
    done

    if [ "$patch_id" = "" ]; then
	continue
    fi

    j=$(($j + 1))
    cat > "$out_dir/trigger-precommit-$project-$j-$series_id" <<EOF
${project}_git=pw://series/$series_id
EOF
done

rm "$yaml"

echo "Processed $len series and created $j pre-commit triggers"