|
| 1 | +name: Create Google Calendar Event on Issue Assignment |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [assigned] |
| 6 | + |
| 7 | +jobs: |
| 8 | + create-calendar-event: |
| 9 | + if: github.event.assignee.login != null |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Checkout Repo |
| 13 | + uses: actions/checkout@v3 |
| 14 | + |
| 15 | + - name: Set up Python |
| 16 | + uses: actions/setup-python@v4 |
| 17 | + with: |
| 18 | + python-version: '3.11' |
| 19 | + |
| 20 | + - name: Install dependencies |
| 21 | + run: | |
| 22 | + python -m pip install --upgrade pip |
| 23 | + pip install google-api-python-client google-auth google-auth-httplib2 google-auth-oauthlib |
| 24 | +
|
| 25 | + - name: Create Calendar Event |
| 26 | + env: |
| 27 | + GCAL_CREDENTIALS_JSON: ${{ secrets.GCAL_CREDENTIALS_JSON }} |
| 28 | + run: | |
| 29 | + import os |
| 30 | + import json |
| 31 | + import datetime |
| 32 | + import base64 |
| 33 | + from google.oauth2 import service_account |
| 34 | + from googleapiclient.discovery import build |
| 35 | +
|
| 36 | + # Load GitHub event |
| 37 | + import yaml |
| 38 | + with open(os.environ['GITHUB_EVENT_PATH'], 'r') as f: |
| 39 | + event = yaml.safe_load(f) |
| 40 | +
|
| 41 | + assignee = event["assignee"]["login"] |
| 42 | + issue = event["issue"] |
| 43 | + title = issue["title"] |
| 44 | + url = issue["html_url"] |
| 45 | + body = issue.get("body", "") |
| 46 | +
|
| 47 | + # Try to extract a due date from the body (format: Due: YYYY-MM-DD) |
| 48 | + import re |
| 49 | + match = re.search(r"(?i)due[:\s]+(\d{4}-\d{2}-\d{2})", body) |
| 50 | + if not match: |
| 51 | + print("No due date found in issue body.") |
| 52 | + exit(0) |
| 53 | +
|
| 54 | + due_date = match.group(1) |
| 55 | +
|
| 56 | + credentials_info = json.loads(os.environ["GCAL_CREDENTIALS_JSON"]) |
| 57 | + creds = service_account.Credentials.from_service_account_info( |
| 58 | + credentials_info, |
| 59 | + scopes=["https://www.googleapis.com/auth/calendar"] |
| 60 | + ) |
| 61 | +
|
| 62 | + service = build("calendar", "v3", credentials=creds) |
| 63 | + calendar_id = credentials_info.get("calendar_id", "primary") |
| 64 | +
|
| 65 | + event = { |
| 66 | + "summary": f"Issue: {title}", |
| 67 | + "description": f"{url}", |
| 68 | + "start": {"date": due_date}, |
| 69 | + "end": {"date": due_date}, |
| 70 | + } |
| 71 | +
|
| 72 | + created_event = service.events().insert(calendarId=calendar_id, body=event).execute() |
| 73 | + print(f"Event created: {created_event.get('htmlLink')}") |
0 commit comments