From a68c37aa21e73402b14d223a0f9af465717b6736 Mon Sep 17 00:00:00 2001 From: zugdev Date: Mon, 18 Nov 2024 14:19:06 -0300 Subject: [PATCH] feat: check if branch already exists --- .github/workflows/sync-template.yml | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sync-template.yml b/.github/workflows/sync-template.yml index 58afa69..a676062 100644 --- a/.github/workflows/sync-template.yml +++ b/.github/workflows/sync-template.yml @@ -38,7 +38,7 @@ jobs: - name: Sync branch to template env: GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} - WHITELIST_FILES: ".github/workflows/sync-template.yml .github/workflows/build.yml .github/workflows/conventional-commits.yml .github/workflows/cspell.yml .github/workflows/cypress-testing.yml .github/workflows/knip-reporter.yml .github/workflows/knip.yml .github/workflows/no-empty-strings.yml .github/workflows/release-please.yml .github/pull_request_template.md .husky/ static/fonts/ .eslintrc .nvmrc .prettierrc .yarnrc.yml tsconfig.json" + WHITELIST_FILES: ".github/workflows/sync-template.yml .github/workflows/build.yml .github/workflows/conventional-commits.yml .github/workflows/cspell.yml .github/workflows/cypress-testing.yml .github/workflows/knip-reporter.yml .github/workflows/knip.yml .github/workflows/no-empty-strings.yml .github/workflows/release-please.yml .github/pull_request_template.md .husky/ .eslintrc .nvmrc .prettierrc .yarnrc.yml tsconfig.json" ADDITIONAL_FILES: ${{ github.event.inputs.additional_files }} run: | branch_name=$(git rev-parse --abbrev-ref HEAD) @@ -46,7 +46,18 @@ jobs: pr_branch="sync-template/${branch_name}" git config --global user.email "github-actions[bot]@users.noreply.github.com" git config --global user.name "github-actions[bot]" - git checkout -b "$pr_branch" + + # Check if the sync branch already exists + if git ls-remote --exit-code --heads origin "$pr_branch"; then + echo "Branch $pr_branch already exists. Fetching and updating." + git fetch origin "$pr_branch" + git checkout "$pr_branch" + git rebase "origin/$pr_branch" + else + echo "Creating new branch $pr_branch." + git checkout -b "$pr_branch" + fi + git clone https://github.com/ubiquity/ts-template template-repo # Convert ADDITIONAL_FILES input to an array @@ -70,7 +81,13 @@ jobs: # Clean up rm -rf template-repo/ + + # Commit git add . git commit -m "chore: sync template" + + # Push git push "$original_remote" "$pr_branch" + + # Create PR gh pr create --title "Sync branch to template" --body "This pull request merges changes from the template repository, overwriting or removing the following files:${file_list}" --head "$pr_branch" --base "$branch_name" \ No newline at end of file