From f93a9d944e010534cb38950474a7b8bffe8a641a Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 19:25:39 -0800 Subject: [PATCH 1/7] 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) From 4f6fb412f18d76d67e872524cb66090d3097fbba Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 19:54:43 -0800 Subject: [PATCH 2/7] Make /github/workspace safe directory --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 3c643c2..8bb661e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -65,6 +65,7 @@ echo "[+] Cloning destination git repository $DESTINATION_REPOSITORY_NAME" # Setup git git config --global user.email "$USER_EMAIL" git config --global user.name "$USER_NAME" +git config --global --add safe.directory /github/workspace 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 From 4caaae456b79877dca10baf953a58c093919bcfe Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 20:05:27 -0800 Subject: [PATCH 3/7] Fix target branch creation --- entrypoint.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8bb661e..24189ca 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -69,9 +69,7 @@ git config --global --add safe.directory /github/workspace 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} + echo "[+] Creating target branch ${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" From a9444e55cd32b10032cf07d861bd9de66aa6b2bc Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 20:31:38 -0800 Subject: [PATCH 4/7] Another attempt on making target branch creation actually work --- entrypoint.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 24189ca..ae8e1d2 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -57,6 +57,7 @@ fi CLONE_DIR=$(mktemp -d) +new_target_branch=0 echo "[+] Git version" git --version @@ -69,7 +70,7 @@ git config --global --add safe.directory /github/workspace 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 target branch ${TARGET_BRANCH}" + new_target_branch=1 else echo "::error::Could not clone the destination repository. Command:" echo "::error::git clone --single-branch --branch $TARGET_BRANCH $GIT_CMD_REPOSITORY $CLONE_DIR" @@ -138,6 +139,12 @@ echo "[+] Set directory is safe ($CLONE_DIR)" # TODO: review before releasing it as a version git config --global --add safe.directory "$CLONE_DIR" +if [ ${new_target_branch} -ne 0 ]; then + echo "[+] Creating target branch ${TARGET_BRANCH}" + git branch ${TARGET_BRANCH} + git switch b1 +fi + echo "[+] Adding git commit" git add . From 6b4c1a27fbe8cd1e487109d8c2fd473be19b2a9e Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 19 Dec 2022 20:42:21 -0800 Subject: [PATCH 5/7] Fix stupid typo in target branch creation --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index ae8e1d2..b9e76ff 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -142,7 +142,7 @@ git config --global --add safe.directory "$CLONE_DIR" if [ ${new_target_branch} -ne 0 ]; then echo "[+] Creating target branch ${TARGET_BRANCH}" git branch ${TARGET_BRANCH} - git switch b1 + git switch ${TARGET_BRANCH} fi echo "[+] Adding git commit" From 0d318503bcbf1612af2c347390638bffb49884e1 Mon Sep 17 00:00:00 2001 From: martin Date: Fri, 30 Dec 2022 16:54:02 -0800 Subject: [PATCH 6/7] Fixed up error messsage. --- entrypoint.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index b9e76ff..699b4eb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -72,10 +72,8 @@ if ! git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REP if ${CREATE_TARGET_BRANCH} && git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR"; then new_target_branch=1 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" + echo "::error::Could not clone the destination repository." + echo "::error::Please verify that the target repository exists and is accesible with your API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" exit 1 fi fi From 91dc6c8e2bacbf3289b969090e2c1db7edb2d329 Mon Sep 17 00:00:00 2001 From: martin Date: Fri, 30 Dec 2022 17:03:09 -0800 Subject: [PATCH 7/7] One more error message improvement --- entrypoint.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 699b4eb..de41aaf 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -72,8 +72,14 @@ if ! git clone --single-branch --depth 1 --branch "$TARGET_BRANCH" "$GIT_CMD_REP if ${CREATE_TARGET_BRANCH} && git clone --single-branch --depth 1 "$GIT_CMD_REPOSITORY" "$CLONE_DIR"; then new_target_branch=1 else - echo "::error::Could not clone the destination repository." - echo "::error::Please verify that the target repository exists and is accesible with your API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" + echo "::error::Could not clone the destination repository ('$GIT_CMD_REPOSITORY')." + echo -n "::error::Please verify that the target repository exists and is accesible with your API_TOKEN_GITHUB OR SSH_DEPLOY_KEY" + if ${CREATE_TARGET_BRANCH}; then + echo "." + else + echo "" + echo "::error::and that it contains the target branch ('$TARGET_BRANCH')." + fi exit 1 fi fi