From 78c799dee96e601f9c8e5f7618d46a01cacbd871 Mon Sep 17 00:00:00 2001
From: David Schach <dschach@x2od.com>
Date: Wed, 8 Nov 2023 16:20:45 -0800
Subject: [PATCH] ci(dependabot): auto-rename dependabot update PRs

---
 .github/dependabot.yml                        |  6 ++--
 .../dependabot-approve-and-auto-merge.yml     | 36 ++++++++++++++++++-
 release-please-config.json                    |  1 +
 3 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index 5fb76a6..a23fcbf 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -8,14 +8,16 @@ updates:
   - package-ecosystem: 'npm' # See documentation for possible values
     directory: '/' # Location of package manifests
     commit-message:
-      prefix: 'build'
+      prefix: 'deps' # packaged dependencies
+      prefix-development: 'build' # dev-dependencies
+      #include: 'scope'
     rebase-strategy: 'auto'
     schedule:
       interval: 'daily'
   - package-ecosystem: 'github-actions' # See documentation for possible values
     directory: '/' # Location of package manifests
     commit-message:
-      prefix: 'ci(actions)'
+      prefix: 'ci'
     rebase-strategy: 'auto'
     schedule:
       interval: 'daily'
diff --git a/.github/workflows/dependabot-approve-and-auto-merge.yml b/.github/workflows/dependabot-approve-and-auto-merge.yml
index e4b07a4..8cf390f 100644
--- a/.github/workflows/dependabot-approve-and-auto-merge.yml
+++ b/.github/workflows/dependabot-approve-and-auto-merge.yml
@@ -1,13 +1,47 @@
 name: Dependabot Pull Request Approve and Merge
-on: pull_request_target
+on: pull_request #_target
 permissions:
   pull-requests: write
   contents: write
 jobs:
+  pull-request-title-change:
+    runs-on: ubuntu-latest
+    steps:
+      # Checkout the source code
+      - name: 'Checkout source code'
+        uses: actions/checkout@v4
+
+      - name: 'Fetch Dependabot metadata'
+        id: dependabot-fetch
+        uses: dependabot/fetch-metadata@v1
+
+      - name: 'Split title and update'
+        id: split-title
+        if: ${{ ! contains(github.event.pull_request.title, '):' ) && contains(github.event.pull_request.title, ':')  && steps.dependabot-fetch.outcome == 'success' }}
+        env:
+          TITLE: ${{ github.event.pull_request.title }}
+          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+          PR_URL: ${{github.event.pull_request.html_url}}
+        run: |
+          echo $TITLE  
+          title_pt2="${TITLE##*:}"
+          title_pt1="${TITLE%%:*}"
+          gh pr checkout "$PR_URL" # sets the upstream metadata for `gh pr status`
+          packagename="${{ steps.dependabot-fetch.outputs.dependency-names }}"
+          #echo "packagename is $packagename"
+          #echo "$title_pt1"
+          #echo "$title_pt2"
+          parensOpen="("
+          parensClose="):"
+          pr_title=$title_pt1$parensOpen$packagename$parensClose$title_pt2
+          echo "FINAL TITLE: $pr_title"
+          gh pr edit "$PR_URL" --title "$pr_title"
+
   # Auto merge Dependabot PRs for:
   # - patch updates on prod dependencies
   # - minor updates on dev dependencies
   dependabot-auto-merge:
+    needs: pull-request-title-change
     # Only run for Dependabot PRs
     if: ${{ github.actor == 'dependabot[bot]' }}
     runs-on: ubuntu-latest
diff --git a/release-please-config.json b/release-please-config.json
index 28f4af6..6a8aa4e 100644
--- a/release-please-config.json
+++ b/release-please-config.json
@@ -34,6 +34,7 @@
 		{ "type": "docs", "section": "Documentation", "hidden": false },
 		{ "type": "perf", "section": "Performance Improvements", "hidden": false },
 		{ "type": "test", "section": "Tests", "hidden": false },
+		{ "type": "deps", "section": "Dependencies", "hidden": false },
 		{ "type": "build", "section": "Build System", "hidden": false },
 		{ "type": "ci", "section": "Continuous Integration", "hidden": false }
 	]