This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
forked from repo-sync/pull-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·121 lines (94 loc) · 3.4 KB
/
entrypoint.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
set -e
set -o pipefail
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN environment variable."
exit 1
fi
if [[ ! -z "$INPUT_SOURCE_BRANCH" ]]; then
SOURCE_BRANCH="$INPUT_SOURCE_BRANCH"
elif [[ ! -z "$GITHUB_REF" ]]; then
SOURCE_BRANCH=${GITHUB_REF/refs\/heads\//} # Remove branch prefix
else
echo "Set the INPUT_SOURCE_BRANCH environment variable or trigger from a branch."
exit 1
fi
DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"master"}"
# Determine repository url
if [[ -z "$INPUT_DESTINATION_REPOSITORY" ]]; then
# Try to query local repository's remote url if INPUT_DESTINATION_REPOSITORY is null
local_origin=$(git config --get remote.origin.url)
if [[ "$local_origin" == *.git ]]; then
origin_no_suffix="${local_origin%.*}"
else
origin_no_suffix="$local_origin"
fi
repo="$(basename "${origin_no_suffix}")"
owner="$(basename "${origin_no_suffix%/${repo}}")"
if [[ ! -z "$repo" ]]; then
CHECKOUT_REPOSITORY="$owner/$repo"
fi
fi
# Fallback to GITHUB_REPOSITORY if both INPUT_DESTINATION_REPOSITORY and CHECKOUT_REPOSITORY are unavailable
DESTINATION_REPOSITORY="${INPUT_DESTINATION_REPOSITORY:-${CHECKOUT_REPOSITORY:-${GITHUB_REPOSITORY}}}"
git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@${GITHUB_SERVER_URL#https://}/$DESTINATION_REPOSITORY"
# Pull all branches references down locally so subsequent commands can see them
git fetch origin '+refs/heads/*:refs/heads/*' --update-head-ok
# Print out all branches
git --no-pager branch -a -vv
if [ "$(git rev-parse --revs-only "$SOURCE_BRANCH")" = "$(git rev-parse --revs-only "$DESTINATION_BRANCH")" ]; then
echo "Source and destination branches are the same."
exit 0
fi
# Do not proceed if there are no file differences, this avoids PRs with just a merge commit and no content
LINES_CHANGED=$(git diff --name-only "$DESTINATION_BRANCH" "$SOURCE_BRANCH" -- | wc -l | awk '{print $1}')
if [[ "$LINES_CHANGED" = "0" ]] && [[ ! "$INPUT_PR_ALLOW_EMPTY" == "true" ]]; then
echo "No file changes detected between source and destination branches."
exit 0
fi
# Workaround for `hub` auth error https://github.com/github/hub/issues/2149#issuecomment-513214342
export GITHUB_USER="$GITHUB_ACTOR"
PR_ARG="$INPUT_PR_TITLE"
if [[ ! -z "$PR_ARG" ]]; then
PR_ARG="-m \"$PR_ARG\""
if [[ ! -z "$INPUT_PR_TEMPLATE" ]]; then
sed -i 's/`/\\`/g; s/\$/\\\$/g' "$INPUT_PR_TEMPLATE"
PR_ARG="$PR_ARG -m \"$(echo -e "$(cat "$INPUT_PR_TEMPLATE")")\""
elif [[ ! -z "$INPUT_PR_BODY" ]]; then
PR_ARG="$PR_ARG -m \"$INPUT_PR_BODY\""
fi
fi
if [[ ! -z "$INPUT_PR_REVIEWER" ]]; then
PR_ARG="$PR_ARG -r \"$INPUT_PR_REVIEWER\""
fi
if [[ ! -z "$INPUT_PR_ASSIGNEE" ]]; then
PR_ARG="$PR_ARG -a \"$INPUT_PR_ASSIGNEE\""
fi
if [[ ! -z "$INPUT_PR_LABEL" ]]; then
PR_ARG="$PR_ARG -l \"$INPUT_PR_LABEL\""
fi
if [[ ! -z "$INPUT_PR_MILESTONE" ]]; then
PR_ARG="$PR_ARG -M \"$INPUT_PR_MILESTONE\""
fi
if [[ "$INPUT_PR_DRAFT" == "true" ]]; then
PR_ARG="$PR_ARG -d"
fi
COMMAND="hub pull-request \
-b $DESTINATION_BRANCH \
-h $SOURCE_BRANCH \
--no-edit \
$PR_ARG \
|| true"
echo "$COMMAND"
PR_URL=$(sh -c "$COMMAND")
if [[ "$?" != "0" ]]; then
exit 1
fi
echo ${PR_URL}
echo "::set-output name=pr_url::${PR_URL}"
echo "::set-output name=pr_number::${PR_URL##*/}"
if [[ "$LINES_CHANGED" = "0" ]]; then
echo "::set-output name=has_changed_files::false"
else
echo "::set-output name=has_changed_files::true"
fi