Skip to content

Commit 27e498e

Browse files
committed
Harden benchmark script against pull request changes
Require a new `TARGET` variable specifying the expected SHA of the commit at the HEAD of the pull request when running the benchmark script for pull requests.
1 parent 353fc6a commit 27e498e

File tree

1 file changed

+25
-1
lines changed
  • benchmarks/community-benchmark

1 file changed

+25
-1
lines changed

benchmarks/community-benchmark/run.sh

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
#!/bin/bash
2+
function assertShaLike() {
3+
local shaVarName="${1}"
4+
local sha="${!1}"
5+
if [ -z "${sha}" ] || ! echo "${sha}" | grep -qE '^[0-9a-fA-F]+$'; then
6+
echo "${shaVarName} (${sha}) does not look like a commit SHA"
7+
exit
8+
fi
9+
}
10+
11+
function assertMatchesPullRequest() {
12+
local shaVarName="${1}"
13+
local sha="${!1}"
14+
local pr_id="${!2}"
15+
if [[ ! "$(git ls-remote origin refs/pull/${pr_id}/head)" =~ ^${sha}.*refs/pull/${pr_id}/head$ ]]; then
16+
echo "${shaVarName} (${sha}) does not match HEAD for pull request ${pr_id}"
17+
exit
18+
fi
19+
}
20+
221
function mandatory() {
322
if [ -z "${!1}" ]; then
423
echo "${1} not set"
@@ -27,6 +46,7 @@ echo "The script expects the following variables to be set:"
2746
echo "CATEGORY = a category of tests to run - folders in benchmark/"
2847
echo "BRANCH = the branch the test should be based off. e.g. master"
2948
echo "PULL_ID = the pull request that contains changes to test"
49+
echo "TARGET = the SHA of the commit HEAD that contains changes to test"
3050
echo "-------------------------------------------------------------"
3151
echo "Use case 2: We want to compare two branches, tags or commits."
3252
echo "To run this, declare:"
@@ -50,6 +70,8 @@ else
5070
export USE_CASE=1
5171
mandatory BRANCH
5272
mandatory PULL_ID
73+
mandatory TARGET
74+
assertShaLike TARGET
5375
fi
5476
mandatory CATEGORY
5577
optional RUNS
@@ -63,6 +85,8 @@ git clone https://github.com/nodejs/node.git
6385
cd node
6486
case $USE_CASE in
6587
1)
88+
# Validate TARGET and pull request HEAD are consistent
89+
assertMatchesPullRequest TARGET PULL_ID
6690
git checkout $BRANCH
6791
;;
6892
2)
@@ -81,7 +105,7 @@ mv out/Release/node ./node-master
81105
# build pr
82106
case $USE_CASE in
83107
1)
84-
curl -L https://github.com/nodejs/node/pull/${PULL_ID}.patch|git apply -3
108+
curl -L "https://github.com/nodejs/node/compare/$(git rev-parse HEAD)...${TARGET}.patch"|git apply -3
85109
;;
86110
2)
87111
git checkout $TARGET

0 commit comments

Comments
 (0)