mirror of
https://github.com/octoleo/octosync.git
synced 2024-11-22 04:25:11 +00:00
Code formated. Added the direct merge option. Added the success message. Fixed a few Bugs.
This commit is contained in:
parent
a2db3506ba
commit
150c8767c7
163
src/sync.sh
163
src/sync.sh
@ -15,21 +15,24 @@ command -v git >/dev/null 2>&1 || {
|
||||
STARTBUILD=$(date +"%s")
|
||||
# use UTC+00:00 time also called zulu
|
||||
STARTDATE=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)")
|
||||
# main project Header
|
||||
HEADERTITLE="Github Sync Bot v1.0"
|
||||
# BOT name
|
||||
BOT_NAME="Github Sync Bot v1.0"
|
||||
|
||||
# main function ˘Ô≈ôﺣ
|
||||
function main() {
|
||||
# with test we also show config details
|
||||
if (("$TEST" == 1)); then
|
||||
# always show the config values
|
||||
showConfValues
|
||||
fi
|
||||
# check that all needed values are set
|
||||
checkConfValues
|
||||
# clone all needed repos
|
||||
cloneRepos
|
||||
if checkConfValues && cloneRepos; then
|
||||
# move the files and folders
|
||||
moveFoldersFiles
|
||||
# crazy but lets check anyway
|
||||
if [ -d "${ROOT_TARGET_FOLDER}" ]; then
|
||||
# move to root target folder
|
||||
cd "${ROOT_TARGET_FOLDER}"
|
||||
# merge all changes
|
||||
if mergeChanges; then
|
||||
# check what action to take to get
|
||||
# the changes into the target repository
|
||||
if (("$TARGET_REPO_ACTION" == 1)); then
|
||||
@ -39,22 +42,19 @@ function main() {
|
||||
# we should create a pull request
|
||||
makePullRequestAgainstTarget
|
||||
fi
|
||||
}
|
||||
|
||||
# merge the changes into the target repository
|
||||
function makeMergeToTarget() {
|
||||
# go into repo folder
|
||||
cd "${ROOT_TARGET_FOLDER}"
|
||||
# make a commit of the changes
|
||||
echo "merge:$PWD"
|
||||
}
|
||||
|
||||
# create a pull request against the target repository
|
||||
function makePullRequestAgainstTarget() {
|
||||
# go into repo folder
|
||||
cd "${ROOT_TARGET_FOLDER}"
|
||||
# make a commit of the changes
|
||||
echo "PR:$PWD"
|
||||
# check if all went well
|
||||
if [ $? -eq 0 ]; then
|
||||
# give the final success message
|
||||
finalMessage "========= Successfully synced ========================" ">=>"
|
||||
fi
|
||||
else
|
||||
# show that there was noting to do...
|
||||
finalMessage "========= Tried to sync (but nothing changed) ========" "==="
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
echo "There was a serious error."
|
||||
exit 22
|
||||
}
|
||||
|
||||
# show the configuration values
|
||||
@ -64,9 +64,9 @@ function checkConfValues () {
|
||||
|
||||
# make sure SOURCE_REPO is set
|
||||
[[ ! "${SOURCE_REPO}" == *"/"* ]] && echo "SOURCE_REPO:${SOURCE_REPO} is not a repo path!" && ERROR=1
|
||||
[[ ! `wget -S --spider "https://github.com/${SOURCE_REPO}" 2>&1 | grep 'HTTP/1.1 200 OK'` ]] \
|
||||
&& echo "SOURCE_REPO:https://github.com/${SOURCE_REPO} is not set correctly, or the guthub user does not have access!" \
|
||||
&& ERROR=1
|
||||
[[ ! $(wget -S --spider "https://github.com/${SOURCE_REPO}" 2>&1 | grep 'HTTP/1.1 200 OK') ]] &&
|
||||
echo "SOURCE_REPO:https://github.com/${SOURCE_REPO} is not set correctly, or the guthub user does not have access!" &&
|
||||
ERROR=1
|
||||
|
||||
# make sure SOURCE_REPO_BRANCH is set
|
||||
[ ${#SOURCE_REPO_BRANCH} -le 1 ] && echo "SOURCE_REPO_BRANCH:${SOURCE_REPO_BRANCH} is not set correctly!" && ERROR=1
|
||||
@ -76,9 +76,9 @@ function checkConfValues () {
|
||||
|
||||
# make sure TARGET_REPO is set
|
||||
[[ ! "${TARGET_REPO}" == *"/"* ]] && echo "TARGET_REPO:${TARGET_REPO} is not a repo path!" && ERROR=1
|
||||
[[ ! `wget -S --spider "https://github.com/${TARGET_REPO}" 2>&1 | grep 'HTTP/1.1 200 OK'` ]] \
|
||||
&& echo "TARGET_REPO:https://github.com/${TARGET_REPO} is not set correctly, or the guthub user does not have access!" \
|
||||
&& ERROR=1
|
||||
[[ ! $(wget -S --spider "https://github.com/${TARGET_REPO}" 2>&1 | grep 'HTTP/1.1 200 OK') ]] &&
|
||||
echo "TARGET_REPO:https://github.com/${TARGET_REPO} is not set correctly, or the guthub user does not have access!" &&
|
||||
ERROR=1
|
||||
|
||||
# make sure TARGET_REPO_BRANCH is set
|
||||
[ ${#TARGET_REPO_BRANCH} -le 1 ] && echo "TARGET_REPO_BRANCH:${TARGET_REPO_BRANCH} is not set correctly!" && ERROR=1
|
||||
@ -92,13 +92,15 @@ function checkConfValues () {
|
||||
# make sure TARGET_REPO_FORK is set correctly if set
|
||||
if [ ${#TARGET_REPO_FORK} -ge 1 ]; then
|
||||
[[ ! "${TARGET_REPO_FORK}" == *"/"* ]] && echo "TARGET_REPO_FORK:${TARGET_REPO_FORK} is not a repo path!" && ERROR=1
|
||||
[[ ! `wget -S --spider "https://github.com/${TARGET_REPO_FORK}" 2>&1 | grep 'HTTP/1.1 200 OK'` ]] \
|
||||
&& echo "TARGET_REPO_FORK:https://github.com/${TARGET_REPO_FORK} is not set correctly, or the guthub user does not have access!" \
|
||||
&& ERROR=1
|
||||
[[ ! $(wget -S --spider "https://github.com/${TARGET_REPO_FORK}" 2>&1 | grep 'HTTP/1.1 200 OK') ]] &&
|
||||
echo "TARGET_REPO_FORK:https://github.com/${TARGET_REPO_FORK} is not set correctly, or the guthub user does not have access!" &&
|
||||
ERROR=1
|
||||
fi
|
||||
|
||||
# if error found exit
|
||||
(("$ERROR" == 1)) && exit 19
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# clone the repo
|
||||
@ -119,9 +121,11 @@ function cloneRepos () {
|
||||
# we need access on this one, so we use git@github.com:
|
||||
cloneRepo "${TARGET_REPO}" "git@github.com:${TARGET_REPO_BRANCH}.git" "${ROOT_TARGET_FOLDER}"
|
||||
else
|
||||
echo "You must set TARGET_REPO:${TARGET_REPO} to target.repo.merge=1 if no target.repo.fork is given!"
|
||||
echo "You use a forked target (target.repo.fork=org/forked_repo) or set the (target.repo.merge=1) to merge directly into target."
|
||||
exit 20
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# clone the repo
|
||||
@ -147,10 +151,11 @@ function cloneRepo () {
|
||||
exit 21
|
||||
fi
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# rebase repo with its upstream (old school)
|
||||
# rebase repo with its upstream
|
||||
function rebaseWithUpstream() {
|
||||
# current folder
|
||||
local current_folder=$PWD
|
||||
@ -297,7 +302,7 @@ function moveFolderFiles () {
|
||||
# 2 = only all sub-folders and their files
|
||||
elif (("$source_files" == 2)); then
|
||||
echo "This command:2 means to copy only all sub-folders and their files."
|
||||
echo 'Yet this file command:${source_files} for ${ROOT_SOURCE_FOLDER}/${source_folder} is not ready to be used... so nothing was copied!';
|
||||
echo 'Yet this file command:${source_files} for ${ROOT_SOURCE_FOLDER}/${source_folder} is not ready to be used... so nothing was copied!'
|
||||
# could be a file (name as number) so we try to copy it
|
||||
else
|
||||
# copy file/folder recursive by force
|
||||
@ -374,6 +379,88 @@ function moveFolder () {
|
||||
fi
|
||||
}
|
||||
|
||||
# merge changes
|
||||
function mergeChanges() {
|
||||
# we first check if there are changes
|
||||
if [[ -z $(git status --porcelain) ]]; then
|
||||
echo "There has been no changes to the target repository, so noting to commit."
|
||||
return 1
|
||||
else
|
||||
# make a commit of the changes
|
||||
git add .
|
||||
git commit -am"$BOT_NAME [merge:${STARTDATE}]"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Changes were committed."
|
||||
return 0
|
||||
else
|
||||
echo "Failed to commit changed"
|
||||
retunr 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# merge the changes into the target repository
|
||||
function makeMergeToTarget() {
|
||||
# we dont make changes to remote repos while testing
|
||||
if (("$TEST" == 1)); then
|
||||
echo "changes where not merged (test mode)"
|
||||
return 0
|
||||
fi
|
||||
# push the changes
|
||||
git push
|
||||
# check if this is a fork (since then we are not done)
|
||||
if [[ "${TARGET_REPO_FORK}" == *"/"* ]]; then
|
||||
# in a fork we must create a pull request against the target repo, and then merge it.
|
||||
createPullRequest && return 0
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# create a pull request against the target repository
|
||||
function makePullRequestAgainstTarget() {
|
||||
# we dont make changes to remote repos while testing
|
||||
if (("$TEST" == 1)); then
|
||||
echo "pull request was not made (test mode)"
|
||||
return 0
|
||||
fi
|
||||
# check if this is a fork (should always be)
|
||||
if [[ "${TARGET_REPO_FORK}" == *"/"* ]]; then
|
||||
# we need to push the changes up
|
||||
git push
|
||||
# creat the pull request
|
||||
createPullRequest && return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# create the pull request
|
||||
function createPullRequest() {
|
||||
echo "we need github CLI to do this"
|
||||
return 0
|
||||
}
|
||||
|
||||
# give the final
|
||||
function finalMessage() {
|
||||
# set the build time
|
||||
ENDBUILD=$(date +"%s")
|
||||
SECONDSBUILD=$((ENDBUILD - STARTBUILD))
|
||||
# use UTC+00:00 time also called zulu
|
||||
ENDDATE=$(TZ=":ZULU" date +"%m/%d/%Y @ %R (UTC)")
|
||||
echo "======================================================"
|
||||
echo "$1"
|
||||
echo
|
||||
echo " (selected folders/files)"
|
||||
echo " source:${SOURCE_REPO}"
|
||||
echo " $2"
|
||||
echo " target:${TARGET_REPO}"
|
||||
echo
|
||||
echo "====> date:${STARTDATE}"
|
||||
echo "====> duration:${SECONDSBUILD} seconds"
|
||||
echo "======================================================"
|
||||
echo
|
||||
exit 0
|
||||
}
|
||||
|
||||
# set any/all configuration values
|
||||
function setConfValues() {
|
||||
if [ -f $CONFIG_FILE ]; then
|
||||
@ -388,7 +475,7 @@ function setConfValues() {
|
||||
TARGET_REPO_FOLDERS=$(getConfVal "target\.repo\.folders" "${TARGET_REPO_FOLDERS}")
|
||||
# To merge or just make a PR (0 = PR; 1 = Merge)
|
||||
TARGET_REPO_ACTION=$(getConfVal "target\.repo\.merge" "${TARGET_REPO_ACTION}")
|
||||
# Target fork is rebased (if out of sync with upstream target) then updated and used to make a PR or Merge
|
||||
# Target fork is rebased to upstream target then updated and used to make a PR or Merge
|
||||
TARGET_REPO_FORK=$(getConfVal "target\.repo\.fork" "${TARGET_REPO_FORK}")
|
||||
fi
|
||||
}
|
||||
@ -403,7 +490,7 @@ function getConfVal() {
|
||||
# show the configuration values
|
||||
function showConfValues() {
|
||||
echo "======================================================"
|
||||
echo " ${HEADERTITLE}"
|
||||
echo " ${BOT_NAME}"
|
||||
echo "======================================================"
|
||||
echo "CONFIG_FILE: ${CONFIG_FILE}"
|
||||
echo "TEST: ${TEST}"
|
||||
@ -457,7 +544,7 @@ Usage: ${0##*/:-} [OPTION...]
|
||||
example: ${0##*/:-} -h
|
||||
example: ${0##*/:-} --help
|
||||
======================================================
|
||||
${HEADERTITLE}
|
||||
${BOT_NAME}
|
||||
======================================================
|
||||
EOF
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user