-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind-ref.sh
79 lines (69 loc) · 2.11 KB
/
find-ref.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
75
76
77
78
79
#!/bin/bash -e
# Copyright 2021 Google Inc. Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
if [ -z "$PR_BRANCH" ]; then
# Remove the "refs/heads/" prefix
current_branch="${CURRENT_REF/refs\/heads\//}"
else
current_branch="$PR_BRANCH"
fi
if [[ "$current_branch" == feature.* ]]; then
default="$current_branch"
else
default="$DEFAULT_REF"
fi
if [[ -z "$default" ]]; then
skip="true"
else
skip="false"
fi
# We don't have a PR_BRANCH so we are not in a pull request, so there's no
# linked PR to find.
if [ -z "$PR_BRANCH" ]; then
if [[ -z "$default" ]]; then
echo "Not a pull request, skipping checkout"
else
echo "Not a pull request, using default ref $default"
fi
echo "skip=$skip" >> "$GITHUB_OUTPUT"
echo "ref=$default" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "::group::Pull request body"
echo "$PR_BODY"
echo "::endgroup::"
# Echoes the sass/sass Git ref that should be checked out for the current GitHub
# Actions run. If we're running specs for a pull request which refers to a
# sass/sass pull request, we'll run against the latter rather than sass/sass
# main.
echo "::group::Finding pull request reference"
for link in "$(echo "$PR_BODY" | grep -Eo "${REPO}(#|/pull/)[0-9]+")"; do
if [[ "$link" = *#* ]]; then
number="${link#*#}"
else
number="${link#*/pull/}"
fi
json="$(
curl --fail --silent \
--header "Authorization: token $TOKEN" \
--header "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$REPO/pulls/${number}"
)"
if [[ "$?" == 0 && "$(echo "$json" | jq .state -r)" == "open" ]]; then
echo "Linked to pull request $number"
echo "skip=false" >> "$GITHUB_OUTPUT"
echo "ref=refs/pull/$number/head" >> "$GITHUB_OUTPUT"
exit 0
else
echo "$link isn't a pull request."
fi
done
if [[ -z "$default" ]]; then
echo "No linked pull request, skipping checkout"
else
echo "No linked pull request, using default ref $default"
fi
echo "skip=$skip" >> "$GITHUB_OUTPUT"
echo "ref=$default" >> "$GITHUB_OUTPUT"
echo "::endgroup::"