summaryrefslogtreecommitdiff
path: root/pw-apply.sh
blob: 176ee1b15d00d7f85cdc23d6ea282827f684882e (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
#!/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_url pw_token pw_dir build_url
declare              ci_bot project pw_url pw_token pw_dir build_url

verbose="${verbose-true}"

if $verbose; then
    set -x
fi

case "$pw_url" in
    "pw://series/"*)
	series_id=$(echo "$pw_url" | cut -d/ -f 4)
	;;
    *) assert_with_msg "pw_url does not match pw://series/*" false ;;
esac

retrigger=false
case "$pw_url/" in
    "pw://series/"*"/retrigger/"*) retrigger=true ;;
esac

url=$(get_baseline_git "${project}_url")
rev=$(get_baseline_git "${project}_rev")
echo "Fetching baseline $url#$rev"
clone_or_update_repo "$project" "$rev" "$url" > /dev/null

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

if ! pw_series_complete_p "$project" "$series_id"; then
    echo "ERROR: Series $series_id is not complete"
    exit 2
fi

# Look for the first 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" = "" ] || $retrigger; 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
    echo "ERROR: All patches in series $series_id are already tested"
    exit 3
fi

# Fetch and setup glibc-cicd.git.  We use check.py to send status updates
# to the patchwork instance.
clone_or_update_repo glibc-cicd main \
		     https://gitlab.com/djdelorie/glibc-cicd.git > /dev/null
(
    # BE CAREFUL WITH $pw_token
    set +x;
    if [ "$pw_token" != "" ]; then
	cat >> glibc-cicd/cicd-config.py <<EOF

patchwork_token = "$pw_token"
EOF
    fi
)
pw_check_cmd=(glibc-cicd/check.py --patch_id "$patch_id" --context "$ci_bot")

patch_url="https://patchwork.sourceware.org/patch/$patch_id"
patch_message_id=$(pw_get_patch_data "$project" "$patch_id" "Message ID")
patch_submitter=$(pw_get_patch_data "$project" "$patch_id" "Submitter" \
		      | sed -e "s/.*(\(.*\)).*/\1/")

# Create a state file for this patch.  This is sourced by pw-report.sh to avoid
# passing a bunch of parameters on the command line.
mkdir -p "$pw_dir"
cat > "$pw_dir/$project" <<EOF
pw[project]='$project'
pw[${project}]='$project'
pw[${project}_patch_id]='$patch_id'
pw[${project}_check_cmd]='${pw_check_cmd[*]}'
pw[${project}_build_url]='$build_url'
pw[${project}_patch_url]='$patch_url'
pw[${project}_patch_message_id]='$patch_message_id'
pw[${project}_patch_submitter]='$patch_submitter'
EOF

# Below calls to pw-report.sh will run their own pw_init/pw_deinit
pw_deinit "$project"
trap "" EXIT

(
    set +x
    $scripts/pw-report.sh --check triggered --result pass --pw_dir "$pw_dir" \
			  __pw_token "$pw_token"
)

prev_head=$(git -C "$project" rev-parse HEAD)

apply_series_with_retry "$project" $prev_head pw "$series_id" &
res=0 && wait $! || res=$?
patches_applied=$(git -C "$project" rev-list --count HEAD "^$prev_head")

if [ "$patches_applied" -le "$num_patch" ]; then
    apply_result="fail"
    if [ "$res" = "0" ] && [ "$patches_applied" = "0" ]; then
	# "series apply" finished successfully, but no patch was applied;
	# this means that the patch is already merged.
	apply_result="merged"
    fi

    (
	set +x
	$scripts/pw-report.sh --check apply --result "$apply_result" \
			      --pw_dir "$pw_dir" __pw_token "$pw_token"
    )

    if [ $res != 0 ]; then
	exit $res
    fi

    echo "WARNING: patch series applied fewer patches than expected"
    echo "WARNING: applied $patches_applied vs expected $(($num_patch + 1))"
    echo "WARNING: most likely the patch series is now merged into mainline"

    exit 5
fi

git -C "$project" checkout --detach HEAD~$num_patch

# Some debug traces, to check which files were modified and their time stamps
(
    set +e
    cd "$project"
    git status
    git status -s | awk '{print $NF;}' | while IFS='' read -r line
    do
	ls -l "$line"
    done
)

(
    set +x
    $scripts/pw-report.sh --check apply --result pass --pw_dir "$pw_dir" \
			  __pw_token "$pw_token"
)