diff --git a/.github/workflows/posthog_pipeline.yaml b/.github/workflows/posthog_pipeline.yaml new file mode 100644 index 000000000..c8d3ab140 --- /dev/null +++ b/.github/workflows/posthog_pipeline.yaml @@ -0,0 +1,33 @@ +name: Push GitHub Data to PostHog + +on: + schedule: + - cron: '0 0 * * *' # Runs every day at midnight + workflow_dispatch: + +jobs: + push-data: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install requests posthog + + - name: Run data extraction and push script + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }} + POSTHOG_HOST: ${{ secrets.POSTHOG_HOST }} + GITHUB_REPOSITORY: ${{ github.repository }} + run: | + python tools/push_to_posthog.py diff --git a/tools/push_to_posthogh.py b/tools/push_to_posthogh.py new file mode 100644 index 000000000..b044b7a31 --- /dev/null +++ b/tools/push_to_posthogh.py @@ -0,0 +1,67 @@ +# extract_and_push_github_data.py + +import requests +import os +from posthog import Posthog + +# Get environment variables +GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') +REPO = os.getenv('GITHUB_REPOSITORY') +POSTHOG_API_KEY = os.getenv('POSTHOG_API_KEY') # Your PostHog Project API Key +POSTHOG_HOST = os.getenv('POSTHOG_HOST', 'https://app.posthog.com') # Default PostHog Cloud + +headers = { + "Authorization": f"token {GITHUB_TOKEN}", + "Accept": "application/vnd.github.v3+json" +} + +# Initialize PostHog client +posthog = Posthog( + api_key=POSTHOG_API_KEY, + host=POSTHOG_HOST +) + +def get_repo_info(): + url = f"https://api.github.com/repos/{REPO}" + response = requests.get(url, headers=headers) + if response.status_code == 200: + return response.json() + else: + print(f"Error fetching repo info: {response.status_code}") + return None + +def main(): + repo_info = get_repo_info() + + if repo_info: + # Prepare data to send to PostHog + properties = { + 'repo_name': repo_info.get('full_name'), + 'stars': repo_info.get('stargazers_count'), + 'forks': repo_info.get('forks_count'), + 'open_issues': repo_info.get('open_issues_count'), + 'watchers': repo_info.get('subscribers_count'), + 'created_at': repo_info.get('created_at'), + 'updated_at': repo_info.get('updated_at'), + 'pushed_at': repo_info.get('pushed_at'), + 'language': repo_info.get('language'), + 'license': repo_info.get('license').get('name') if repo_info.get('license') else None, + 'topics': repo_info.get('topics') + } + + # Send event to PostHog + posthog.capture( + distinct_id='github_repo', # You can customize this identifier + event='GitHub Repo Stats', + properties=properties + ) + + print("Data sent to PostHog successfully.") + else: + print("Failed to retrieve repository information.") + + # Close PostHog client + posthog.shutdown() + +if __name__ == "__main__": + main()