Skip to content

Commit

Permalink
feat!: ✨ (#401) add option to force file deletion (#435)
Browse files Browse the repository at this point in the history
* feat: ✨ (#401) add option to force file deletion

* fix: 🐛 (#401)

* fix: 🐛 (#401)

* fix: 🐛 (#401) fix some small issues

* refactor: ♻️ enhance readability

* docs: 📝 (#401) some docs addons

* style: 🚨 lint issue
  • Loading branch information
AndreasAugustin authored Feb 4, 2024
1 parent c94c349 commit 341abe8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ jobs:
| is_allow_hooks | `[optional]` set to `true` if you want to enable lifecycle hooks. Use this with caution! | `false` | `false` |
| is_pr_cleanup | `[optional]` set to `true` if you want to cleanup older PRs targeting the same branch. Use this with caution! | `false` | `false` |
| is_not_source_github | `[optional]` set to `true` if the source git provider is not GitHub | `false` | `false` |
| is_force_deletion | `[optional]` set to `true` if you want to force delete files which are deleted within the source repository even if they contain changes. You need to also adjust `git_remote_pull_params` (see below for details) | `false` | `false` |
| git_user_name | `[optional]` set the committer git user.name | `false` | `${GITHUB_ACTOR}` |
| git_user_email | `[optional]` set the committer git user.email | `false` | `github-action@actions-template-sync.noreply.${SOURCE_REPO_HOSTNAME}` |
| git_remote_pull_params | `[optional]` set remote pull parameters | `false` | `--allow-unrelated-histories --squash --strategy=recursive -X theirs` |
Expand Down Expand Up @@ -351,8 +352,16 @@ Associating a label with the generated PRs helps keeping track of them and allow

Depending on your way of working, you may end up with multiple pull requests related to template syncing pointing to the same branch.
If you want to avoid this situation, you can instruct this action to clean up older PRs (search based on labels defined with the `pr_labels` config parameter).

:warning: this feature will close all pull requests with labels configured with `pr_labels` config parameter.

## Force deletion

This feature will force delete files if those are deelted within the source repository.

:warning: it is highly related to the `git_remote_pull_params` config parameter and won't work with the default.
You need to change the default one e.g. to `git_remote_pull_params: --allow-unrelated-histories --strategy=recursive --no-edit`.

## Troubleshooting

* refusing to allow a GitHub App to create or update workflow `.github/workflows/******.yml` without `workflows` permission
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ inputs:
is_not_source_github:
description: "[optional] set to true if the source repository is not a github related repository. Useful e.q. if the source is GitLab"
default: "false"
is_force_deletion:
description: "[optional] set to true if you want to force delete files which are deleted within the source repository even if they contain changes"
default: "false"
git_user_name:
description: "[optional] set the committer git user.name for the merge commit"
git_user_email:
Expand All @@ -67,6 +70,7 @@ runs:
IS_ALLOW_HOOKS: ${{ inputs.is_allow_hooks }}
IS_PR_CLEANUP: ${{ inputs.is_pr_cleanup}}
IS_NOT_SOURCE_GITHUB: ${{ inputs.is_not_source_github }}
IS_FORCE_DELETION: ${{ inputs.is_force_deletion }}
GIT_USER_NAME: ${{ inputs.git_user_name }}
GIT_USER_EMAIL: ${{ inputs.git_user_email }}
GIT_REMOTE_PULL_PARAMS: ${{ inputs.git_remote_pull_params }}
10 changes: 5 additions & 5 deletions src/sync_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -e
# Arguments:
# message to print.
#######################################
err() {
function err() {
echo "::error::[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2;
}

Expand All @@ -18,7 +18,7 @@ err() {
# Arguments:
# message to print.
#######################################
debug() {
function debug() {
echo "::debug::$*";
}

Expand All @@ -27,7 +27,7 @@ debug() {
# Arguments:
# message to print.
#######################################
warn() {
function warn() {
echo "::warn::$*";
}

Expand All @@ -36,7 +36,7 @@ warn() {
# Arguments:
# message to print.
#######################################
info() {
function info() {
echo "::info::$*";
}

Expand All @@ -46,7 +46,7 @@ info() {
# hook -> the hook to use
#
####################################3#
cmd_from_yml_file() {
function cmd_from_yml_file() {
local FILE_NAME="templatesync.yml"
local HOOK=$1
local YML_PATH=".hooks.${HOOK}.commands"
Expand Down
30 changes: 25 additions & 5 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ GIT_REMOTE_PULL_PARAMS="${GIT_REMOTE_PULL_PARAMS:---allow-unrelated-histories --

cmd_from_yml_file "install"

LOCAL_CURRENT_GIT_HASH=$(git rev-parse HEAD)

info "current git hash: ${LOCAL_CURRENT_GIT_HASH}"

TEMPLATE_SYNC_IGNORE_FILE_PATH=".templatesyncignore"
TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk '{print $1}')
NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}")
Expand All @@ -44,7 +48,7 @@ debug "new Git HASH ${NEW_TEMPLATE_GIT_HASH}"

echo "::group::Check new changes"

check_branch_remote_existing() {
function check_branch_remote_existing() {
git ls-remote --exit-code --heads origin "${NEW_BRANCH}" || BRANCH_DOES_NOT_EXIST=true

if [[ "${BRANCH_DOES_NOT_EXIST}" != true ]]; then
Expand Down Expand Up @@ -95,6 +99,22 @@ 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

cmd_from_yml_file "precommit"

echo "::group::commit changes"
Expand Down Expand Up @@ -123,7 +143,7 @@ git commit -m "${PR_COMMIT_MSG}"

echo "::endgroup::"

cleanup_older_prs () {
function cleanup_older_prs () {
older_prs=$(gh pr list \
--base "${UPSTREAM_BRANCH}" \
--state open \
Expand Down Expand Up @@ -157,7 +177,7 @@ fi
echo "::endgroup::"


maybe_create_labels () {
function maybe_create_labels () {
all_labels=${PR_LABELS//,/$'\n'}
for label in $all_labels
do
Expand Down Expand Up @@ -190,12 +210,12 @@ fi

echo "::endgroup::"

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

create_pr () {
function create_pr () {
gh pr create \
--title "${PR_TITLE}" \
--body "Merge ${SOURCE_REPO_PATH} ${NEW_TEMPLATE_GIT_HASH}" \
Expand Down

0 comments on commit 341abe8

Please sign in to comment.