Skip to content

Instantly share code, notes, and snippets.

@artemrys
Last active September 11, 2019 00:27
Show Gist options
  • Save artemrys/83a9b7790ec04f50adb3bc18a5fa84dc to your computer and use it in GitHub Desktop.
Save artemrys/83a9b7790ec04f50adb3bc18a5fa84dc to your computer and use it in GitHub Desktop.
Github Repository Traffic Monitoring Google Cloud Function
import os
from datetime import date, datetime
from flask import jsonify
from typing import Generator, List
from github import Github
from github.Referrer import Referrer
from github.Repository import Repository
from google.cloud import firestore
def _build_github() -> Github:
"""Builds Github client."""
return Github(os.getenv("GITHUB_TOKEN"))
def _build_firestore() -> firestore.Client:
"""Builds Firestore client."""
return firestore.Client()
def _get_public_repos(github: Github) -> Generator[Repository, None, None]:
"""Gets public repositories from Github for your user.
The user is that user which owns GITHUB_TOKEN.
"""
for repo in github.get_user().get_repos(visibility="public"):
yield repo
def _get_repo_top_referrers(repo: Repository) -> List[Referrer]:
"""Gets repository top referrers."""
return repo.get_top_referrers()
def _add_top_referrers_to_firestore(
firestore_client: firestore.Client,
repos_top_referrers) -> None:
"""Adds top referrers for each repository to Firestore."""
today = date.today()
document = f"{today.year}-{today.month}-{today.day}"
doc_ref = (
firestore_client
.collection("referrers")
.document(document)
)
doc_ref.set(repos_top_referrers)
def parse_github_repos_traffic(request):
github = _build_github()
firestore_client = _build_firestore()
repos_top_referrers = dict()
repos_top_referrers["created_at"] = datetime.now()
for repo in _get_public_repos(github):
repo_top_referrers = _get_repo_top_referrers(repo)
if repo_top_referrers:
repos_top_referrers[repo.full_name] = [
{
"referrer": referrer.referrer,
"count": referrer.count,
"uniques": referrer.uniques,
}
for referrer in repo_top_referrers
]
_add_top_referrers_to_firestore(firestore_client, repos_top_referrers)
return jsonify(repos_top_referrers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment