Skip to content

Commit

Permalink
feat(#482): ✨ add option to force push and pr
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Augustin <dev@andreas-augustin.org>
  • Loading branch information
AndreasAugustin committed Mar 13, 2024
1 parent 62cdf5e commit 3ec8b72
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ jobs:
| is_dry_run | `[optional]` set to `true` if you do not want to push the changes and not want to create a PR | `false` | |
| is_allow_hooks | `[optional]` set to `true` if you want to enable lifecycle hooks. Use this with caution! | `false` | `false` |
| hooks | `[optional]` please check the lifecycle hooks section below | `false` | |
| is_force_push_pr | `[optional]` set to `true` if you want to force push and pr update | `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` |
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ inputs:
default: "false"
hooks:
description: "[optional] define the hooks as yaml string input"
is_force_push_pr:
description: "[optional] set to true if you want to force push and pr update"
default: "false"
is_pr_cleanup:
description: "[optional] set to true if you want to cleanup older PRs targeting the same branch."
default: "false"
Expand Down Expand Up @@ -79,6 +82,7 @@ runs:
IS_DRY_RUN: ${{ inputs.is_dry_run }}
IS_ALLOW_HOOKS: ${{ inputs.is_allow_hooks }}
HOOKS: ${{ inputs.hooks }}
IS_FORCE_PUSH_PR: ${{ inputs.is_force_push_pr }}
IS_PR_CLEANUP: ${{ inputs.is_pr_cleanup}}
IS_NOT_SOURCE_GITHUB: ${{ inputs.is_not_source_github }}
IS_FORCE_DELETION: ${{ inputs.is_force_deletion }}
Expand Down
59 changes: 54 additions & 5 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if [[ -n "${SRC_SSH_PRIVATEKEY_ABS_PATH}" ]]; then
export GIT_SSH_COMMAND="ssh -i ${SRC_SSH_PRIVATEKEY_ABS_PATH}"
fi

IS_FORCE_PUSH_PR="${IS_FORCE_PUSH_PR:-"false"}"
GIT_REMOTE_PULL_PARAMS="${GIT_REMOTE_PULL_PARAMS:---allow-unrelated-histories --squash --strategy=recursive -X theirs}"

cmd_from_yml "install"
Expand Down Expand Up @@ -238,11 +239,19 @@ function eventual_create_labels () {
# push the changes
# Arguments:
# branch
# is_force
##############################
function push () {
info "push changes"
local branch=$1
git push --set-upstream origin "${branch}"
local is_force=$2

if [ "$is_force" == true ] ; then
warn "forcing the push."
git push --force --set-upstream origin "${branch}"
else
git push --set-upstream origin "${branch}"
fi
}

####################################
Expand All @@ -254,7 +263,7 @@ function push () {
# labels
# reviewers
###################################
function create_pr () {
function create_pr() {
info "create pr"
local title=$1
local body=$2
Expand All @@ -267,7 +276,38 @@ function create_pr () {
--body "${body}" \
--base "${branch}" \
--label "${labels}" \
--reviewer "${reviewers}"
--reviewer "${reviewers}" || create_pr_has_issues=true

if [ "$create_pr_has_issues" == true ] ; then
warn "Creating the PR failed."
warn "Eventually it is already existent."
return 1
fi
return 0
}

####################################
# creates or edits a pr if already existent
# Arguments:
# title
# body
# branch
# labels
# reviewers
###################################
function create_or_edit_pr() {
info "create pr or edit the pr"
local title=$1
local body=$2
local branch=$3
local labels=$4
local reviewers=$5

create_pr "${title}" "${body}" "${branch}" "${labels}" "${reviewers}" || gh pr edit \
--title "${title}" \
--body "${body}" \
--add-label "${labels}" \
--add-reviewer "${reviewers}"
}

#########################################
Expand Down Expand Up @@ -313,6 +353,10 @@ function handle_templatesyncignore() {
function prechecks() {
info "prechecks"
echo "::group::prechecks"
if [ "${IS_FORCE_PUSH_PR}" == "true" ]; then
warn "skipping prechecks because we force push and pr"
return 0
fi
check_branch_remote_existing "${NEW_BRANCH}"

check_if_commit_already_in_hist_graceful_exit "${TEMPLATE_REMOTE_GIT_HASH}"
Expand Down Expand Up @@ -391,9 +435,14 @@ function push_prepare_pr_create_pr() {
echo "::group::push changes and create PR"

cmd_from_yml "prepush"
push "${NEW_BRANCH}"
push "${NEW_BRANCH}" "${IS_FORCE_PUSH_PR}"
cmd_from_yml "prepr"
create_pr "${PR_TITLE}" "${PR_BODY}" "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${PR_REVIEWERS}"
if [ "$IS_FORCE_PUSH_PR" == true ] ; then
create_or_edit_pr "${PR_TITLE}" "${PR_BODY}" "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${PR_REVIEWERS}"
else
create_pr "${PR_TITLE}" "${PR_BODY}" "${UPSTREAM_BRANCH}" "${PR_LABELS}" "${PR_REVIEWERS}"
fi


echo "::endgroup::"
}
Expand Down

0 comments on commit 3ec8b72

Please sign in to comment.