-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr-builder.sh
executable file
·75 lines (62 loc) · 2.04 KB
/
pr-builder.sh
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 -e
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
gh_user=$1
gh_key=$2
gh_repo=$3
base=$4
log_base_url=${5}
context=${6}
run_command=${7-"./test.sh"}
db=db
if [ -z $LATEST_COMMIT ]; then
jq="map(.head.sha) | .[]"
api="repos/${gh_repo}/pulls?state=open&base=${base}"
git_fetch="refs/pull/*/head:refs/remotes/origin/pr/*"
ci="${base}"
else
jq=".sha"
api="repos/${gh_repo}/commits/${base}"
git_fetch="${base}"
ci="${base}_latest"
fi
workspace="workspace_$ci"
commits="commits_$ci.txt"
mkdir -p $db
touch $db/$commits
if [ ! -d $workspace ]; then
git clone git@github.com:$gh_repo $workspace
fi
( cd $workspace && git fetch origin --force )
shas=`curl -s -u $gh_user:$gh_key "https://api.github.com/$api" | jq -r "$jq"`
function post_status(){
local sha=$1
local log_file=$2
local state=$3
local desc=$4
curl -s -u $gh_user:$gh_key -X POST https://api.github.com/repos/${gh_repo}/statuses/${sha} \
-d "{\"state\":\"${state}\", \"target_url\":\"${log_base_url}/$log_file\", \"description\": \"${desc}\", \"context\":\"${context} ${ci}\"}" \
> /dev/null
}
while read -r sha; do
[[ -z $sha ]] && break
if ! grep $sha $db/$commits > /dev/null; then
log_file="${sha}_${ci}.txt"
post_status $sha $log_file "pending" "Pending $(date)"
echo "Processing ${sha} ..."
sleep 2 # code isnt' always immediately availabel, even if the gh api says it is
start_t=$(date +%s)
if ( ! ( cd $workspace && git checkout -q $sha && echo $sha >> "${script_dir}/$db/$commits" && PR_BUILDER_BASE=$base $run_command &> "${script_dir}/${db}/$log_file" ) ); then
echo "Failure"
end_t=$(date +%s)
post_status $sha $log_file "failure" "`expr $end_t - $start_t` seconds"
else
echo "Success"
end_t=$(date +%s)
post_status $sha $log_file "success" "`expr $end_t - $start_t` seconds"
fi
else
echo "Skipping ${sha}"
fi;
done <<< "$shas"
echo "done."