-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·139 lines (111 loc) · 3.23 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/sh -l
# Enable Debug Mode
#set -x
# Enable color output
export TERM=xterm-color
# Get command line arguments
REMOTE_REPO=$1
REMOTE_REF=$2
TARGET_REF=$3
GH_PAT=$4
REBASE=$5
TARGET_REPO=$GITHUB_REPOSITORY
# Return value
SYNC_STATUS="failed"
# Setting GH Action Output Colors
RED="\033[91m"
YELLOW="\033[93m"
GREEN="\033[32m"
BOLD="\e[1m"
DEFAULT="\033[m"
# get action directory for sourcing subscripts
ACTION_PARENT_DIR=$(dirname "$(dirname "$0")")
write_log() {
case $1 in
# Bold/Green
[Gg])
echo -e "${BOLD}${GREEN}$2${DEFAULT}" 1>&1
;;
# Bold/Yellow
[Gg])
echo -e "${BOLD}${GREEN}$2${DEFAULT}" 1>&1
;;
# Default log message
[Dd])
echo "$2" 1>&1
;;
# Exit without issues
[Ee])
echo -e "${BOLD}${GREEN}$2${DEFAULT}" 1>&2
echo "::set-output name=sync-status::${2}"
exit 0
;;
# If something failed, exit with red
*)
echo -e "${BOLD}${RED}ERROR: ${DEFAULT} exit $1" 1>&2
echo "::set-output name=sync-status::${$2}"
exit "$1"
;;
esac
}
git_config() {
# Configure git client
git config --global user.name "GitHub Sync Bot"
git config --global user.email "[email protected]"
git config --global pull.rebase $REBASE
write_log "g" "git client configured"
}
set_upstream() {
# Add the upstream repository
git remote add upstream "${REMOTE_REPO}"
write_log "g" "Remote added to repository"
}
checkout() {
# Show all branches
git branch -v
# Checkout our target branch (should be already done within the checkout stage)
echo "Checking out ${TARGET_REF}"
git checkout ${TARGET_REF}
STATUS=$?
if [ "${STATUS}" != 0 ]; then
# checkout failed
write_log "$STATUS" "Target branch '${TARGET_REF}' could not be checked out."
fi
write_log "g" "Checked out ${TARGET_REF}"
}
sync_branches() {
git pull --no-edit upstream "${REMOTE_REF}"
STATUS=$?
if [ "${STATUS}" != 0 ]; then
# exit on commit pull fail
write_log "$STATUS" "Could not get commits"
fi
git remote set-url origin "https://${GITHUB_ACTOR}:${GH_PAT}@github.com/${TARGET_REPO}.git"
git push origin "${TARGET_REF}"
STATUS=$?
if [ "${STATUS}" != 0 ]; then
# exit on commit pull fail
write_log "$STATUS" "Could not push changes to target"
fi
write_log "e" "Sync successful!"
}
check_updates() {
git fetch upstream ${REMOTE_REF}
LOCAL_COMMIT_HASH=$(git rev-parse "${TARGET_REF}")
UPSTREAM_COMMIT_HASH=$(git rev-parse "upstream/${REMOTE_REF}")
if [ -z "${LOCAL_COMMIT_HASH}" ] || [ -z "${UPSTREAM_COMMIT_HASH}" ]; then
write_log "1" "Error on checking for new commits"
elif [ "${LOCAL_COMMIT_HASH}" = "${UPSTREAM_COMMIT_HASH}" ]; then
write_log "e" "Nothing to do, no new commits to sync."
else
git log upstream/"${REMOTE_REF}" "${LOCAL_COMMIT_HASH}"..HEAD --pretty=oneline
write_log "g" "Found new commits, will sync the branches"
sync_branches
fi
exit 0
}
git_config
checkout
set_upstream
check_updates
echo "::set-output name=sync-status::$SYNC_STATUS"