forked from repo-sync/pull-request
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·132 lines (114 loc) · 3.7 KB
/
entrypoint.sh
File metadata and controls
executable file
·132 lines (114 loc) · 3.7 KB
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
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env bash
run_pull_request_command() {
if [ "$(git rev-parse --revs-only "$SOURCE_BRANCH")" = "$(git rev-parse --revs-only "$1")" ]; 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 "$1" "$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"
COMMAND="hub pull-request \
-b $1 \
-h $SOURCE_BRANCH \
--no-edit \
$PR_ARG \
|| true"
echo "$COMMAND"
PR_URL=$(sh -c "$COMMAND")
if [[ "$?" != "0" ]]; then
exit 1
fi
echo ${PR_URL}
# shellcheck disable=SC2082
echo "::set-output name=destination_branch::$1"
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
}
run_merge(){
COMMAND="hub merge ${PR_URL}"
echo "$COMMAND"
MERGE=$(sh -c "$COMMAND")
if [[ "$?" != "0" ]]; then
exit 1
fi
echo "${MERGE}"
}
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
# Github actions no longer auto set the username and GITHUB_TOKEN
git remote set-url origin "https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_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
echo "INPUT_DESTINATION_BRANCH_REGEX = ${INPUT_DESTINATION_BRANCH_REGEX}"
if [ -z "${INPUT_DESTINATION_BRANCH_REGEX}" ]; then
DESTINATION_BRANCH="${INPUT_DESTINATION_BRANCH:-"master"}"
else
branches=$(git --no-pager branch -a | grep "${INPUT_DESTINATION_BRANCH_REGEX}")
echo "branches = ${branches}"
readarray -t <<<"${branches}"
for branch in "${MAPFILE[@]}"; do
echo "branch = ${branch}"
branch_trim=$(echo "${branch}" | xargs)
echo "branch_trim = ${branch_trim}"
if [[ "${branch_trim}" != "${INPUT_DESTINATION_BRANCH_REGEX}" ]] && [[ "${branch_trim}" != *"*"* ]] && [[ "${branch_trim}" != remote* ]]; then
echo "in with ${branch_trim}"
run_pull_request_command "${branch_trim}"
run_merge
fi
done
fi
#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