From 9c337223e66da69268d3562c30a8a6bb4ae33a44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= <git@roschaefer.de>
Date: Mon, 10 Jun 2024 00:48:02 +0530
Subject: [PATCH] ci: automatically apply PR labels (#10064)

Motivation
----------
For me as a new contributor it is frustrating to submit a PR and it will always fail. Even worse: I have to wait for another contributor with more power to assign the label for me.

This will improve developer experience, as some of the labels can be assigned automatically based on changed files.

How to test
-----------
1. Merge this PR
2. Submit a couple of PRs with changes in the respective directories
3. Labels should be automatically applied
4. "Enforce PR labels" github workflow will re-run when "Pull Request Labeler" completes
---
 .github/labeler.yml                    | 23 +++++++++++++++++++++++
 .github/workflows/pr-labeler.yml       | 12 ++++++++++++
 .github/workflows/pr-require-label.yml |  4 ++++
 3 files changed, 39 insertions(+)
 create mode 100644 .github/labeler.yml
 create mode 100644 .github/workflows/pr-labeler.yml

diff --git a/.github/labeler.yml b/.github/labeler.yml
new file mode 100644
index 0000000000..47f755f43c
--- /dev/null
+++ b/.github/labeler.yml
@@ -0,0 +1,23 @@
+cli:
+- changed-files:
+  - any-glob-to-any-file: cli/**
+
+documentation:
+- changed-files:
+  - any-glob-to-any-file: docs/**
+
+🖥️web:
+- changed-files:
+  - any-glob-to-any-file: web/**
+
+📱mobile:
+- changed-files:
+  - any-glob-to-any-file: mobile/**
+
+🗄️server:
+- changed-files:
+  - any-glob-to-any-file: server/**
+
+🧠machine-learning:
+- changed-files:
+  - any-glob-to-any-file: machine-learning/**
diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml
new file mode 100644
index 0000000000..e57cd86e2b
--- /dev/null
+++ b/.github/workflows/pr-labeler.yml
@@ -0,0 +1,12 @@
+name: "Pull Request Labeler"
+on:
+- pull_request_target
+
+jobs:
+  labeler:
+    permissions:
+      contents: read
+      pull-requests: write
+    runs-on: ubuntu-latest
+    steps:
+    - uses: actions/labeler@v5
diff --git a/.github/workflows/pr-require-label.yml b/.github/workflows/pr-require-label.yml
index 6bf54eacf1..ef15710332 100644
--- a/.github/workflows/pr-require-label.yml
+++ b/.github/workflows/pr-require-label.yml
@@ -3,6 +3,10 @@ name: Enforce PR labels
 on:
   pull_request:
     types: [labeled, unlabeled, opened, edited, synchronize]
+  workflow_run:
+    workflows: ["Pull Request Labeler"]
+    types:
+      - completed
 jobs:
   enforce-label:
     name: Enforce label