summaryrefslogtreecommitdiff
path: root/.github/workflows/tag-codeowner-on-issue.yml
blob: a4fe365d4c34f769ea1f3d837b9ca459e236cf57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
name: Tag Codeowner on Sample Issue

on:
  issues:
    types: [opened]

jobs:
  tag-codeowner:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: pip install pyyaml requests

      - name: Extract selected path and tag codeowner
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ISSUE_BODY: ${{ github.event.issue.body }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
        run: |
          python3 - <<EOF
          import os
          import re
          import requests

          issue_body = os.environ['ISSUE_BODY']
          selected_path = None

          # Try to extract the selected path from the issue body
          match = re.search(r'### Which is the area where the sample lives\?\s*\n(.+)', issue_body, re.MULTILINE)
          if match:
              selected_path = match.group(1).strip()

          if not selected_path:
              print("No sample path found in issue body.")
              exit(0)

          # Read CODEOWNERS
          with open(".github/CODEOWNERS", "r") as f:
              lines = f.readlines()

          codeowner = None
          sample_section = False
          for line in lines:
              line = line.strip()
              if line.startswith("# Samples"):
                  sample_section = True
                  continue
              if sample_section:
                  if line.startswith("#") or not line:
                      continue
                  path, owner = line.split()
                  if path == selected_path:
                      codeowner = owner
                      break

          if codeowner is None:
              print(f"No codeowner found for path: {selected_path}")
              exit(0)

          # Post a comment tagging the owner
          comment = f"{codeowner} can you please take a look at this issue related to {selected_path}?"
          repo = os.environ['GITHUB_REPOSITORY']
          token = os.environ['GITHUB_TOKEN']
          issue_number = os.environ['ISSUE_NUMBER']

          url = f"https://api.github.com/repos/{repo}/issues/{issue_number}/comments"
          headers = {
              "Authorization": f"Bearer {token}",
              "Accept": "application/vnd.github.v3+json"
          }
          response = requests.post(url, headers=headers, json={"body": comment})
          print("Comment posted:", response.status_code, response.text)
          EOF