From ca9e02379d35782f684c6881d3bdba2b24357fa6 Mon Sep 17 00:00:00 2001
From: bo0tzz <git@bo0tzz.me>
Date: Fri, 21 Feb 2025 17:54:11 +0100
Subject: [PATCH] feat: remove preview label on pr close (#16249)

---
 .github/workflows/preview-comment.yaml | 17 -------------
 .github/workflows/preview-label.yaml   | 33 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 17 deletions(-)
 delete mode 100644 .github/workflows/preview-comment.yaml
 create mode 100644 .github/workflows/preview-label.yaml

diff --git a/.github/workflows/preview-comment.yaml b/.github/workflows/preview-comment.yaml
deleted file mode 100644
index f49c271fe5..0000000000
--- a/.github/workflows/preview-comment.yaml
+++ /dev/null
@@ -1,17 +0,0 @@
-name: Preview comment
-
-on:
-  pull_request:
-    types: [labeled]
-
-jobs:
-  comment-status:
-    runs-on: ubuntu-latest
-    if: ${{ github.event.label.name == 'preview' }}
-    permissions:
-      pull-requests: write
-    steps:
-      - uses: mshick/add-pr-comment@v2
-        with:
-          message-id: "preview-status"
-          message: "Deploying preview environment to https://pr-${{ github.event.pull_request.number }}.preview.internal.immich.cloud/"
diff --git a/.github/workflows/preview-label.yaml b/.github/workflows/preview-label.yaml
new file mode 100644
index 0000000000..6468d05e80
--- /dev/null
+++ b/.github/workflows/preview-label.yaml
@@ -0,0 +1,33 @@
+name: Preview label
+
+on:
+  pull_request:
+    types: [labeled]
+
+jobs:
+  comment-status:
+    runs-on: ubuntu-latest
+    if: ${{ github.event.action == 'labeled' && github.event.label.name == 'preview' }}
+    permissions:
+      pull-requests: write
+    steps:
+      - uses: mshick/add-pr-comment@v2
+        with:
+          message-id: "preview-status"
+          message: "Deploying preview environment to https://pr-${{ github.event.pull_request.number }}.preview.internal.immich.cloud/"
+
+  remove-label:
+    runs-on: ubuntu-latest
+    if: ${{ github.event.action == 'closed' && contains(github.event.pull_request.labels.*.name, 'preview') }}
+    permissions:
+      pull-requests: write
+    steps:
+      - uses: actions/github-script@v7
+        with:
+          script: |
+            github.rest.issues.removeLabel({
+              issue_number: context.payload.pull_request.number,
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              name: 'preview'
+            })