-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpull_request.sh
executable file
·78 lines (68 loc) · 2.78 KB
/
pull_request.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
#!/bin/bash
# shellcheck disable=SC2086
set -euo pipefail
if [[ ${INPUT_DEBUG} == "true" ]]; then
set -x
fi
fetch() {
git -c protocol.version=2 fetch -q \
--no-tags \
--no-recurse-submodules \
"$@"
}
MINIMUM_CHECK_RUN_ID_VERSION=1.7.0
if [[ ${INPUT_GITHUB_REF_NAME} == "${GITHUB_EVENT_PULL_REQUEST_NUMBER}/merge" ]]; then
# If we have checked out the merge commit then fetch enough history to use HEAD^1 as the upstream.
# We use this instead of github.event.pull_request.base.sha which can be incorrect sometimes.
head_sha=$(git rev-parse HEAD)
fetch --depth=2 origin "${head_sha}"
upstream=$(git rev-parse HEAD^1)
git_commit=$(git rev-parse HEAD^2)
echo "Detected merge commit, using HEAD^1 (${upstream}) as upstream and HEAD^2 (${git_commit}) as github commit"
fi
if [[ -z ${upstream+x} ]]; then
# Otherwise use github.event.pull_request.base.sha as the upstream.
upstream="${GITHUB_EVENT_PULL_REQUEST_BASE_SHA}"
git_commit="${GITHUB_EVENT_PULL_REQUEST_HEAD_SHA}"
fetch origin "${upstream}"
fi
save_annotations=${INPUT_SAVE_ANNOTATIONS}
if [[ ${save_annotations} == "auto" && ${GITHUB_EVENT_PULL_REQUEST_HEAD_REPO_FORK} == "true" ]]; then
echo "Fork detected, saving annotations to an artifact."
save_annotations=true
fi
if [[ -n ${INPUT_CHECK_RUN_ID} ]]; then
trunk_version="$(${TRUNK_PATH} version)"
# trunk-ignore-begin(shellcheck/SC2312): the == will fail if anything inside the $() fails
if sort_result=$(printf "%s\n%s\n" "${MINIMUM_CHECK_RUN_ID_VERSION}" "${trunk_version}" | sort --version-sort); then
if [[ $(echo "${sort_result}" | head -n 1) == "${trunk_version}" ]]; then
echo "::error::Please update your CLI to ${MINIMUM_CHECK_RUN_ID_VERSION} or higher (current version ${trunk_version})."
exit 1
fi
else
echo "::warning::sort --version-sort failed - continuing without checking CLI version"
fi
# trunk-ignore-end(shellcheck/SC2312)
annotation_argument=--trunk-annotate=${INPUT_CHECK_RUN_ID}
elif [[ ${save_annotations} == "true" ]]; then
annotation_argument=--github-annotate-file=${TRUNK_TMPDIR}/annotations.bin
# Signal that we need to upload an annotations artifact
echo "TRUNK_UPLOAD_ANNOTATIONS=true" >>"${GITHUB_ENV}"
else
annotation_argument=--github-annotate
fi
if [[ -n ${INPUT_AUTOFIX_AND_PUSH} ]]; then
"${TRUNK_PATH}" check --ci --upstream "${upstream}" --fix "${annotation_argument}" ${INPUT_ARGUMENTS}
git config --global user.email ""
git config --global user.name "${GITHUB_ACTOR}"
git commit --all --allow-empty --message "Trunk Check applied autofixes"
git push origin "${INPUT_GITHUB_REF_NAME}"
else
"${TRUNK_PATH}" check \
--ci \
--upstream "${upstream}" \
--github-commit "${git_commit}" \
--github-label "${INPUT_LABEL}" \
"${annotation_argument}" \
${INPUT_ARGUMENTS}
fi