From f980b48b416daac40ac606b0656e5364923e5eaf Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 21:50:53 -0300 Subject: [PATCH 1/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 47fda29..ddb66cf 100644 --- a/action.yml +++ b/action.yml @@ -52,7 +52,8 @@ outputs: runs: using: "composite" steps: - - name: Git push and commit + - name: Git push and commit origin + if: ${{ inputs.remote_repository }} == 'origin' run: | CURRENT_BRANCH=${GITHUB_REF} case $CURRENT_BRANCH in "refs/heads/"*) @@ -83,7 +84,7 @@ runs: git config --local user.email "${{ inputs.email }}" git config --local user.name "${{ inputs.name }}" - + if [[ `git status --porcelain` ]]; then git add ${{ inputs.files }} -v git commit -m "${{ inputs.commit_message }}" $EMPTY @@ -97,7 +98,76 @@ runs: else echo "WARNING: No changes were detected. git commit push action aborted." fi + shell: bash + - name: Git push and commit remote + if: ${{ inputs.remote_repository }} != 'origin' + run: | + if [ -z "${{ inputs.access_token }}" ]; then + echo "WARNING: The access_token input is mandatory to push files to another repository." + exit 1 + fi + + REGEX="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)$" + + if [[ ${{ inputs.remote_repository }} =~ $REGEX ]]; then + PROTOCOL=${BASH_REMATCH[1]} + SEPARATOR=${BASH_REMATCH[2]} + HOSTNAME=${BASH_REMATCH[3]} + DESTINATION_OWNER=${BASH_REMATCH[4]} + DESTINATION_REPOSITORY=${BASH_REMATCH[5]} + DESTINATION_REPOSITORY=${DESTINATION_REPOSITORY//.git/ } + + CLONE_DIRECTORY=$(mktemp -d) + + echo "##### Cloning destination Github repository #####" + # Setup git + git config --global user.email "${{ inputs.email }}" + git config --global user.name "${{ inputs.name }}" + git config -l | grep 'http\..*\.extraheader' | cut -d= -f1 | xargs -L1 git config --unset-all + git clone "https://${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY.git" "$CLONE_DIRECTORY" + ls -la "$CLONE_DIRECTORY" + + echo + echo "##### Copying contents to destination Github repository #####" + if [ -z "${{ inputs.target_dir }}" ]; then + cp -rvf "${{ inputs.source_files }}" "$CLONE_DIRECTORY" + else + mkdir -p "$CLONE_DIRECTORY/${{ inputs.target_dir }}" + cp -rvf "${{ inputs.source_files }}" "$CLONE_DIRECTORY/${{ inputs.target_dir }}" + fi + + cd "$CLONE_DIRECTORY" + + # Create new branch + REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY + git remote set-url origin $REMOTE_URL + git fetch origin + git checkout -b ${{ inputs.target_branch }} + git add . + git status + + # Won't commit if no changes were made + git diff-index --quiet HEAD || git commit --message "${{ inputs.commit_message }}" + + echo + echo "##### Pushing git commit #####" + # --set-upstream: sets the branch when pushing to a branch that does not exist + git push -f -u origin "${{ inputs.target_branch }}" + + else + echo + echo "WARNING: Couldn't read remote_repository URL input." + echo "ACCEPTED FORMAT:" + echo "git://github.com//.git" + echo "or" + echo "git@github.com:/.git" + echo "or" + echo "https://github.com//.git" + echo "or" + echo "https://github.com//" + exit 1 + fi shell: bash branding: From 5f659c1817ae906512f5008c83586947f6dce6c0 Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 22:04:04 -0300 Subject: [PATCH 2/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index ddb66cf..39e9cba 100644 --- a/action.yml +++ b/action.yml @@ -53,8 +53,9 @@ runs: using: "composite" steps: - name: Git push and commit origin - if: ${{ inputs.remote_repository }} == 'origin' + if: ${{ inputs.remote_repository == 'origin' }} run: | + echo "##### Git Push and Commit ORIGIN #####" CURRENT_BRANCH=${GITHUB_REF} case $CURRENT_BRANCH in "refs/heads/"*) CURRENT_BRANCH=$(echo "$CURRENT_BRANCH" | sed "s@refs/heads/@@") @@ -101,8 +102,9 @@ runs: shell: bash - name: Git push and commit remote - if: ${{ inputs.remote_repository }} != 'origin' + if: ${{ inputs.remote_repository != 'origin' }} run: | + echo "##### Git Push and Commit REMOTE #####" if [ -z "${{ inputs.access_token }}" ]; then echo "WARNING: The access_token input is mandatory to push files to another repository." exit 1 From 49269c1e8234427a9b4fff2c0bac478fb13e14de Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 22:08:29 -0300 Subject: [PATCH 3/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 39e9cba..dcc73c1 100644 --- a/action.yml +++ b/action.yml @@ -132,12 +132,7 @@ runs: echo echo "##### Copying contents to destination Github repository #####" - if [ -z "${{ inputs.target_dir }}" ]; then - cp -rvf "${{ inputs.source_files }}" "$CLONE_DIRECTORY" - else - mkdir -p "$CLONE_DIRECTORY/${{ inputs.target_dir }}" - cp -rvf "${{ inputs.source_files }}" "$CLONE_DIRECTORY/${{ inputs.target_dir }}" - fi + cp -rvf "${{ inputs.files }}" "$CLONE_DIRECTORY" cd "$CLONE_DIRECTORY" From 4f78503c4bfa118f9db7b2bcd6cb0c9291aebdd7 Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 22:13:47 -0300 Subject: [PATCH 4/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/action.yml b/action.yml index dcc73c1..d38f9fe 100644 --- a/action.yml +++ b/action.yml @@ -55,7 +55,6 @@ runs: - name: Git push and commit origin if: ${{ inputs.remote_repository == 'origin' }} run: | - echo "##### Git Push and Commit ORIGIN #####" CURRENT_BRANCH=${GITHUB_REF} case $CURRENT_BRANCH in "refs/heads/"*) CURRENT_BRANCH=$(echo "$CURRENT_BRANCH" | sed "s@refs/heads/@@") @@ -104,7 +103,6 @@ runs: - name: Git push and commit remote if: ${{ inputs.remote_repository != 'origin' }} run: | - echo "##### Git Push and Commit REMOTE #####" if [ -z "${{ inputs.access_token }}" ]; then echo "WARNING: The access_token input is mandatory to push files to another repository." exit 1 @@ -140,7 +138,7 @@ runs: REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY git remote set-url origin $REMOTE_URL git fetch origin - git checkout -b ${{ inputs.target_branch }} + git checkout ${{ inputs.target_branch }} git add . git status From d53ba3b49f796ae458b7bdcd5eca0bddafc5d463 Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 22:24:08 -0300 Subject: [PATCH 5/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/action.yml b/action.yml index d38f9fe..c858e21 100644 --- a/action.yml +++ b/action.yml @@ -138,6 +138,10 @@ runs: REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY git remote set-url origin $REMOTE_URL git fetch origin + git ls-remote --heads origin ${{ inputs.target_branch }} | wc -l + if [ "$?" != "1" ]; then + git checkout -b ${{ inputs.target_branch }} + fi git checkout ${{ inputs.target_branch }} git add . git status From 865a51b5807c3666695bb4f1d2c0c022211839e9 Mon Sep 17 00:00:00 2001 From: GuillaumeFalourd Date: Thu, 6 Jan 2022 22:30:25 -0300 Subject: [PATCH 6/6] update git push to remote action Signed-off-by: GuillaumeFalourd --- action.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index c858e21..ee33527 100644 --- a/action.yml +++ b/action.yml @@ -138,11 +138,13 @@ runs: REMOTE_URL=https://$DESTINATION_OWNER:${{ inputs.access_token }}@github.com/$DESTINATION_OWNER/$DESTINATION_REPOSITORY git remote set-url origin $REMOTE_URL git fetch origin - git ls-remote --heads origin ${{ inputs.target_branch }} | wc -l - if [ "$?" != "1" ]; then + BE=$(git ls-remote --heads origin ${{ inputs.target_branch }} | wc -l) + if [[ $BE == *"0"* ]]; then + echo "##### Target branch doesn't exist. Creating new branch #####" git checkout -b ${{ inputs.target_branch }} + else + git checkout ${{ inputs.target_branch }} fi - git checkout ${{ inputs.target_branch }} git add . git status