From f93a9d944e010534cb38950474a7b8bffe8a641a Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 19:25:39 -0800 Subject: [PATCH] Support creation of target branch --- action.yml | 5 +++++ entrypoint.sh | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/action.yml b/action.yml index 815415f..629038d 100644 --- a/action.yml +++ b/action.yml @@ -48,6 +48,10 @@ inputs: description: '[Optional] The directory to wipe and replace in the target repository' default: '' required: false + create-target-branch: + description: '[Optional] Boolean indicating whether to create target branch if it does not exist' + default: 'false' + required: false runs: using: docker @@ -64,6 +68,7 @@ runs: - '${{ inputs.target-branch }}' - '${{ inputs.commit-message }}' - '${{ inputs.target-directory }}' + - '${{ inputs.create-target-branch }}' branding: icon: git-commit color: green diff --git a/entrypoint.sh b/entrypoint.sh index 83a61f1..3c643c2 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -15,6 +15,7 @@ DESTINATION_REPOSITORY_USERNAME="${8}" TARGET_BRANCH="${9}" COMMIT_MESSAGE="${10}" TARGET_DIRECTORY="${11}" +CREATE_TARGET_BRANCH="${12}" if [ -z "$DESTINATION_REPOSITORY_USERNAME" ] then @@ -65,16 +66,20 @@ echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME" git config --global user.email "$USER_EMAIL" git config --global user.name "$USER_NAME" -{ - git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR" -} || { - echo "::error::Could not clone the destination repository. Command:" - echo "::error::git clone --single-branch --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR" - echo "::error::(Note that if they exist USER_NAME and API_TOKEN is redacted by GitHub)" - echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" - exit 1 +if ! git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REPOSITORY" "$CLONE_DIR"; then + if ${CREATE_TARGET_BRANCH} && git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR"; then + echo "[+] Creating branch ${TARGET_BRANCH}" + git branch ${TARGET_BRANCH} + git switch ${TARGET_BRANCH} + else + echo "::error::Could not clone the destination repository. Command:" + echo "::error::git clone --single-branch --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR" + echo "::error::(Note that if they exist USER_NAME and API_TOKEN is redacted by GitHub)" + echo "::error::Please verify that the target repository exist AND that it contains the destination branch name, and is accesible by the API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" + exit 1 + fi +fi -} ls -la "$CLONE_DIR" TEMP_DIR=$(mktemp -d)