Created
September 30, 2024 19:03
-
-
Save Sdy603/69bd751479bd3026354cf7ed228837ba to your computer and use it in GitHub Desktop.
This is used to pull custom repo properties from GitHub and send them to DX.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import requests | |
import psycopg2 | |
from datetime import datetime | |
def lambda_handler(event, context): | |
# GitHub API URL for custom properties | |
repo_owner = os.getenv("REPO_OWNER") | |
repo_name = os.getenv("REPO_NAME") | |
github_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/custom-properties" | |
# Headers for GitHub API request | |
headers = { | |
"Authorization": f"Bearer {os.getenv('GITHUB_TOKEN')}", | |
"Accept": "application/vnd.github+json" | |
} | |
# Fetch custom properties from GitHub API | |
response = requests.get(github_url, headers=headers) | |
if response.status_code != 200: | |
raise Exception(f"Failed to retrieve data from GitHub API: {response.status_code} - {response.text}") | |
custom_properties = response.json() | |
# Connect to Postgres database | |
conn = psycopg2.connect( | |
host=os.getenv("POSTGRES_HOST"), | |
database=os.getenv("POSTGRES_DB"), | |
user=os.getenv("POSTGRES_USER"), | |
password=os.getenv("POSTGRES_PASSWORD") | |
) | |
cursor = conn.cursor() | |
# Insert custom properties into Postgres table | |
for prop in custom_properties: | |
cursor.execute( | |
""" | |
INSERT INTO github_custom_properties (repo_name, custom_property_name, custom_property_value, retrieved_at) | |
VALUES (%s, %s, %s, %s) | |
""", | |
(repo_name, prop['name'], json.dumps(prop['value']), datetime.utcnow()) | |
) | |
# Commit the transaction and close the connection | |
conn.commit() | |
cursor.close() | |
conn.close() | |
return { | |
'statusCode': 200, | |
'body': json.dumps(f"Successfully inserted {len(custom_properties)} custom properties into the database.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment