Skip to content

Commit

Permalink
refact: 🧑‍💻 small restructure of the code (#491)
Browse files Browse the repository at this point in the history
* docs: 📝 update docs

* refact: 🧑‍💻 small restructur
  • Loading branch information
AndreasAugustin authored Mar 5, 2024
1 parent efe3793 commit d9e61fc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 75 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ E.q. for the installation phase you need to use commands like `apk add --update
```yml
- name: Test action step
uses: AndreasAugustin/actions-template-sync@v1
env:
MY_VAR: "foo" # possible to define envrionment variables
with:
source_repo_path: AndreasAugustin/template.git
upstream_branch: main
Expand Down
36 changes: 26 additions & 10 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ set -e
# shellcheck source=src/sync_common.sh
source sync_common.sh

###########################################
# Precheks
##########################################

if [[ -z "${GITHUB_TOKEN}" ]]; then
err "Missing input 'github_token: \${{ secrets.GITHUB_TOKEN }}'.";
exit 1;
Expand All @@ -16,6 +20,10 @@ if [[ -z "${SOURCE_REPO_PATH}" ]]; then
exit 1
fi

############################################
# Variables
############################################

DEFAULT_REPO_HOSTNAME="github.com"
SOURCE_REPO_HOSTNAME="${HOSTNAME:-${DEFAULT_REPO_HOSTNAME}}"
GIT_USER_NAME="${GIT_USER_NAME:-${GITHUB_ACTOR}}"
Expand All @@ -24,6 +32,10 @@ GIT_USER_EMAIL="${GIT_USER_EMAIL:[email protected].${
# In case of ssh template repository this will be overwritten
SOURCE_REPO_PREFIX="https://${SOURCE_REPO_HOSTNAME}/"

################################################
# Functions
################################################

function ssh_setup() {
echo "::group::ssh setup"

Expand Down Expand Up @@ -53,18 +65,9 @@ function gpg_setup() {
git config --global gpg.program /bin/gpg_no_tty.sh

info "done prepare gpg"
echo "::endgroup::"for fpr in
echo "::endgroup::"
}

# Forward to /dev/null to swallow the output of the private key
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
ssh_setup
elif [[ "${SOURCE_REPO_HOSTNAME}" != "${DEFAULT_REPO_HOSTNAME}" ]]; then
gh auth login --git-protocol "https" --hostname "${SOURCE_REPO_HOSTNAME}" --with-token <<< "${GITHUB_TOKEN}"
fi

export SOURCE_REPO="${SOURCE_REPO_PREFIX}${SOURCE_REPO_PATH}"

function git_init() {
echo "::group::git init"
info "set git global configuration"
Expand All @@ -86,6 +89,19 @@ function git_init() {
echo "::endgroup::"
}

###################################################
# Logic
###################################################

# Forward to /dev/null to swallow the output of the private key
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
ssh_setup
elif [[ "${SOURCE_REPO_HOSTNAME}" != "${DEFAULT_REPO_HOSTNAME}" ]]; then
gh auth login --git-protocol "https" --hostname "${SOURCE_REPO_HOSTNAME}" --with-token <<< "${GITHUB_TOKEN}"
fi

export SOURCE_REPO="${SOURCE_REPO_PREFIX}${SOURCE_REPO_PATH}"

git_init

if [[ -n "${GPG_PRIVATE_KEY}" ]] &>/dev/null; then
Expand Down
142 changes: 79 additions & 63 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ set -e
# shellcheck source=src/sync_template.sh
source sync_common.sh

############################################
# Prechecks
############################################

if [[ -z "${PR_COMMIT_MSG}" ]]; then
err "Missing env variable 'PR_COMMIT_MSG'";
exit 1;
Expand All @@ -22,6 +26,10 @@ if ! [ -x "$(command -v gh)" ]; then
exit 1;
fi

########################################################
# Variables
########################################################

if [[ -z "${UPSTREAM_BRANCH}" ]]; then
UPSTREAM_BRANCH="$(git remote show origin | awk '/HEAD branch/ {print $NF}')"
info "Missing env variable 'UPSTREAM_BRANCH' setting to remote default ${UPSTREAM_BRANCH}";
Expand Down Expand Up @@ -49,6 +57,10 @@ debug "new Git HASH ${NEW_TEMPLATE_GIT_HASH}"

echo "::group::Check new changes"

#####################################################
# Functions
#####################################################

function set_github_action_outputs() {
echo "::group::set gh action outputs"
if [[ -z "${GITHUB_RUN_ID}" ]]; then
Expand All @@ -71,6 +83,73 @@ function check_branch_remote_existing() {
fi
}

function force_delete_files() {
echo "::group::force file deletion"
warn "force file deletion is enabled. Deleting files which are deleted within the target repository"
FILES_TO_DELETE=$(git log --diff-filter D --pretty="format:" --name-only "${LOCAL_CURRENT_GIT_HASH}"..HEAD | sed '/^$/d')
warn "files to delete: ${FILES_TO_DELETE}"
if [[ -n "${FILES_TO_DELETE}" ]]; then
echo "${FILES_TO_DELETE}" | xargs rm
fi

echo "::endgroup::"
}

function cleanup_older_prs () {
older_prs=$(gh pr list \
--base "${UPSTREAM_BRANCH}" \
--state open \
--label "${PR_LABELS}" \
--json number \
--template '{{range .}}{{printf "%v" .number}}{{"\n"}}{{end}}')

for older_pr in $older_prs
do
gh pr close "$older_pr"
debug "Closed PR #${older_pr}"
done
}

function maybe_create_labels () {
readarray -t labels_array < <(awk -F',' '{ for( i=1; i<=NF; i++ ) print $i }' <<<"${PR_LABELS}")
for label in "${labels_array[@]}"
do
search_result=$(gh label list \
--search "${label}" \
--limit 1 \
--json name \
--template '{{range .}}{{printf "%v" .name}}{{"\n"}}{{end}}')

if [ "${search_result}" = "${label##[[:space:]]}" ]; then
info "label '${label##[[:space:]]}' was found in the repository"
else
if gh label create "${label}"; then
info "label '${label}' was missing and has been created"
else
warn "label creation did not work. For any reason the former check sometimes is failing"
fi
fi
done
}

function push () {
debug "push changes"
git push --set-upstream origin "${NEW_BRANCH}"
}

function create_pr () {
gh pr create \
--title "${PR_TITLE}" \
--body "${PR_BODY}" \
--base "${UPSTREAM_BRANCH}" \
--label "${PR_LABELS}" \
--reviewer "${PR_REVIEWERS}"
}

########################################################
# Logic
#######################################################

check_branch_remote_existing

git cat-file -e "${TEMPLATE_REMOTE_GIT_HASH}" || COMMIT_NOT_IN_HIST=true
Expand Down Expand Up @@ -113,18 +192,6 @@ if [ -s "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]; then
echo "::endgroup::"
fi

function force_delete_files() {
echo "::group::force file deletion"
warn "force file deletion is enabled. Deleting files which are deleted within the target repository"
FILES_TO_DELETE=$(git log --diff-filter D --pretty="format:" --name-only "${LOCAL_CURRENT_GIT_HASH}"..HEAD | sed '/^$/d')
warn "files to delete: ${FILES_TO_DELETE}"
if [[ -n "${FILES_TO_DELETE}" ]]; then
echo "${FILES_TO_DELETE}" | xargs rm
fi

echo "::endgroup::"
}

if [ "$IS_FORCE_DELETION" == "true" ]; then
force_delete_files
fi
Expand Down Expand Up @@ -157,20 +224,6 @@ git commit --signoff -m "${PR_COMMIT_MSG}"

echo "::endgroup::"

function cleanup_older_prs () {
older_prs=$(gh pr list \
--base "${UPSTREAM_BRANCH}" \
--state open \
--label "${PR_LABELS}" \
--json number \
--template '{{range .}}{{printf "%v" .number}}{{"\n"}}{{end}}')

for older_pr in $older_prs
do
gh pr close "$older_pr"
debug "Closed PR #${older_pr}"
done
}
echo "::group::cleanup older PRs"

if [ "$IS_DRY_RUN" != "true" ]; then
Expand All @@ -190,29 +243,6 @@ fi

echo "::endgroup::"


function maybe_create_labels () {
readarray -t labels_array < <(awk -F',' '{ for( i=1; i<=NF; i++ ) print $i }' <<<"${PR_LABELS}")
for label in "${labels_array[@]}"
do
search_result=$(gh label list \
--search "${label}" \
--limit 1 \
--json name \
--template '{{range .}}{{printf "%v" .name}}{{"\n"}}{{end}}')

if [ "${search_result}" = "${label##[[:space:]]}" ]; then
info "label '${label##[[:space:]]}' was found in the repository"
else
if gh label create "${label}"; then
info "label '${label}' was missing and has been created"
else
warn "label creation did not work. For any reason the former check sometimes is failing"
fi
fi
done
}

echo "::group::check for missing labels"

if [[ -z "${PR_LABELS}" ]]; then
Expand All @@ -227,20 +257,6 @@ fi

echo "::endgroup::"

function push () {
debug "push changes"
git push --set-upstream origin "${NEW_BRANCH}"
}

function create_pr () {
gh pr create \
--title "${PR_TITLE}" \
--body "${PR_BODY}" \
--base "${UPSTREAM_BRANCH}" \
--label "${PR_LABELS}" \
--reviewer "${PR_REVIEWERS}"
}

echo "::group::push changes and create PR"

if [ "$IS_DRY_RUN" != "true" ]; then
Expand Down

0 comments on commit d9e61fc

Please sign in to comment.