From bfa19f6193e17a24e4b28743acabb15833d29783 Mon Sep 17 00:00:00 2001
From: hoangdv2429 <hoangdv2429@gmail.com>
Date: Tue, 14 May 2024 17:39:42 +0700
Subject: [PATCH] feat: new flow

---
 .github/workflows/changelog.yml | 60 +++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 18 deletions(-)

diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml
index 43ceca99a..63d0ae386 100644
--- a/.github/workflows/changelog.yml
+++ b/.github/workflows/changelog.yml
@@ -12,42 +12,66 @@ jobs:
       - name: Checkout the repository
         uses: actions/checkout@v3
         with:
-          fetch-depth: 0
+          fetch-depth: 1
 
       - name: Setup Node.js
         uses: actions/setup-node@v3
         with:
           node-version: '18'
 
-      - name: Generate Changelog Update
+      - name: Configure Git
         run: |
-          npm install -g conventional-changelog-cli
-          conventional-changelog -p angular -i CHANGELOG.md -s
+          git config --global user.email "github-actions[bot]@users.noreply.github.com"
+          git config --global user.name "github-actions[bot]"
 
-      - name: Check if "auto-changelog-update" branch exists and update if needed
+      - name: Check if "auto-changelog-update-do-not-create-manually" branch exists
         id: check_branch
         run: |
-          git fetch origin auto-changelog-update:auto-changelog-update --no-tags
-          if git rev-parse --verify --quiet auto-changelog-update; then
-            echo "::set-output name=branch_exists::true"
-            git checkout auto-changelog-update
-            git merge main --no-edit
+          if [ -n "$(git ls-remote --heads origin auto-changelog-update-do-not-create-manually)" ]; then
+            git fetch origin auto-changelog-update-do-not-create-manually
+            echo "branch_exists=true" >> $GITHUB_ENV
           else
-            echo "::set-output name=branch_exists::false"
-            git checkout -b auto-changelog-update
+            echo "branch_exists=false" >> $GITHUB_ENV
           fi
 
-      - name: Create or Update Pull Request
+      - name: Generate Changelog Update and update if branch exists
+        run: |
+          npm install -g conventional-changelog-cli
+          if [ "$branch_exists" == "true" ]; then
+            git checkout auto-changelog-update-do-not-create-manually
+            git merge main --strategy-option theirs --allow-unrelated-histories --no-edit
+            conventional-changelog -p angular -i CHANGELOG.md -s -r 0 --append
+            git add CHANGELOG.md
+            git commit -m "Update CHANGELOG.md [skip ci]"
+            git push origin auto-changelog-update-do-not-create-manually
+            exit 0
+          else
+            git checkout main
+            git checkout -b auto-changelog-update-do-not-create-manually
+            git push origin auto-changelog-update-do-not-create-manually
+            conventional-changelog -p angular -i CHANGELOG.md -s -r 0 --append
+          fi
+
+      - name: Create Pull Request
+        if: env.branch_exists == 'false'
         id: cpr
-        uses: peter-evans/create-pull-request@v4
+        uses: peter-evans/create-pull-request@v6
         with:
+          token: ${{ secrets.GITHUB_TOKEN }}
           commit-message: "Update CHANGELOG.md [skip ci]"
-          title: "Automated Changelog Update"
+          title: "Chore(changelog): Automated Changelog Update [skip ci]"
           body: "Update the CHANGELOG.md with recent pushes to branch main."
-          branch: "auto-changelog-update"  # Static branch name
+          base: "main"
+          branch: "auto-changelog-update-do-not-create-manually"
           delete-branch: true
-          branch-suffix: none
-      - name: Check if PR needs to be updated
+
+      - name: Check outputs
+        if: env.branch_exists == 'false'
+        run: |
+          echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
+          echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
+
+      - name: Log if PR updated
         if: steps.cpr.outputs.pull-request-operation == 'updated'
         run: |
           echo "Changelog PR updated due to new commit to main."