forked from Automattic/action-pr-is-up-to-date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·99 lines (85 loc) · 2.89 KB
/
run.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
set -eo pipefail
BASE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source "$BASE/funcs.sh"
GITHUB_API_URL="${GITHUB_API_URL:-https://api.github.com}"
GITHUB_SERVER_URL="${GITHUB_SERVER_URL:-https://github.com}"
[[ -n "$API_TOKEN" ]] || die "API_TOKEN must be set"
[[ -n "$GITHUB_REPOSITORY" ]] || die "GITHUB_REPOSITORY must be set"
[[ -n "$BRANCH" ]] || die "BRANCH must be set"
CONTEXT="${CONTEXT:-PR is up to date}"
DESCRIPTION_FAIL="${DESCRIPTION_FAIL:-This PR needs a $BRANCH merge or rebase.}"
DESCRIPTION_OK="${DESCRIPTION_OK:-}"
DATA_FAIL="$(jq --arg url "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" --arg context "$CONTEXT" --arg desc "$DESCRIPTION_FAIL" -n '{ "state": "failure", "target_url": $url, "context": $context, "description": $desc }')"
DATA_OK="$(jq --arg url "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" --arg context "$CONTEXT" --arg desc "$DESCRIPTION_OK" -n '{ "state": "success", "target_url": $url, "context": $context, "description": $desc }')"
case "$GITHUB_EVENT_NAME" in
pull_request|pull_request_target)
if [[ -n "$TAGS" ]]; then
IFS=$' \t\r\n' read -d '' -ra TAGS <<<"$TAGS" || true
elif [[ -n "$TAG" ]]; then
# Back compat.
echo "Deprecation: TAGS should be set instead of TAG on $GITHUB_EVENT_NAME"
TAGS=( "$TAG" )
else
die "TAGS (or TAG) must be set on $GITHUB_EVENT_NAME"
fi
PR="$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH")"
if [[ -z "$PR" ]]; then
echo "::error::Could not determine PR number from event data"
cat "$GITHUB_EVENT_PATH"
exit 1
fi
;;
push)
[[ -n "$TAG" ]] || die "TAG must be set on $GITHUB_EVENT_NAME"
TAGS=( "$TAG" )
if [[ -n "$PATHS" ]]; then
mapfile -t PATHS <<<"$PATHS"
else
PATHS=()
fi
;;
*)
die "Unsupported event $GITHUB_EVENT_NAME"
;;
esac
TMPDIR="${TMPDIR:-/tmp}"
DIR="$(mktemp -d "${TMPDIR%/}/pr-is-up-to-date.XXXXXXXX")"
trap 'rm -rf $DIR' EXIT
cd "$DIR"
init_repo
NOTIFY_SUCCESS=false
case "$GITHUB_EVENT_NAME" in
pull_request|pull_request_target)
NOTIFY_SUCCESS=true
echo "::group::Fetching PR $PR"
fetch_prs "$PR"
echo "::endgroup::"
process_pr "$PR"
;;
push)
declare -i PAGE=1
declare -i DEEPENBY=100
while :; do
echo "::group::Fetching PRs (page $PAGE)"
JSON=$(curl --fail --get --header "authorization: Bearer $API_TOKEN" --data-urlencode "base=${BRANCH}" "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls?state=open&per_page=100&page=${PAGE}")
PRS=()
mapfile -t PRS < <( jq -r '.[].number' <<<"$JSON" )
[[ ${#PRS[@]} != 0 ]] || break
fetch_prs "${PRS[@]}"
echo "::endgroup::"
for PR in "${PRS[@]}"; do
if should_process_pr "$PR"; then
process_pr "$PR"
else
printf "\e[1;34mPR #%d does not touch the specified paths\e[0m\n" "$PR"
fi
done
jq -e 'length == 100' <<<"$JSON" >/dev/null || break
PAGE+=1
done
;;
*)
die "Unsupported event $GITHUB_EVENT_NAME"
;;
esac