import requests from collections import defaultdict from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi from credentials import mongo_db_name, mongo_uri, organization_name, access_token def count_total_commits(username, organization_name, repo_name, headers): commits_url = f"https://api.github.com/repos/{organization_name}/{repo_name}/commits" total_commits = 0 page = 1 per_page = 100 # Adjust per_page as needed while True: params = {"author": username, "page": page, "per_page": per_page} commits_data = requests.get(commits_url, headers=headers, params=params).json() if not commits_data: break total_commits += len(commits_data) page += 1 return total_commits def count_total_issues(username, organization_name, repo_name, headers): issues_url = f"https://api.github.com/repos/{organization_name}/{repo_name}/issues" total_issues = 0 page = 1 per_page = 100 # Adjust per_page as needed while True: params = {"creator": username, "state": "all", "page": page, "per_page": per_page} issues_data = requests.get(issues_url, headers=headers, params=params).json() if not issues_data: break total_issues += len(issues_data) page += 1 return total_issues # Replace these with your GitHub personal access token and the organization name # Initialize the MongoDB client mongo_client = MongoClient(mongo_uri, server_api=ServerApi('1'), tls=True) try: mongo_client.admin.command('ping') print("Pinged your deployment. You successfully connected to MongoDB!") except Exception as e: print(e) mongo_db = mongo_client[mongo_db_name] # Collect organization data using GitHub REST API organization_url = f"https://api.github.com/orgs/{organization_name}" headers = {"Authorization": f"token {access_token}"} organization_data = requests.get(organization_url, headers=headers).json() # Print organization data print("Organization Data:") print(f"Name: {organization_data['name']}") print(f"Description: {organization_data['description']}") print(f"Location: {organization_data['location']}") # Collect repositories data using GitHub REST API repositories_url = f"https://api.github.com/orgs/{organization_name}/repos" repositories_data = requests.get(repositories_url, headers=headers).json() repo_contributors = {} repo_prs = {} contributor_experience = defaultdict(int) contributors_collection = mongo_db["contributors"] for repo_data in repositories_data: repo_name = repo_data['name'] contributors = [] repo_contributors[repo_name] = [] # Collect contributors data using GitHub REST API contributors_url = repo_data['contributors_url'] contributors_data = requests.get(contributors_url, headers=headers).json() for contributor_data in contributors_data: if contributor_data['type'] != "Bot" and contributor_data['login'] != "bors": contributors.append(contributor_data) repo_contributors[repo_name] = contributors count = 20 for contributor_data in repo_contributors[repo_name]: count = count - 1 contributors_collection = mongo_db["contributors"] username = contributor_data['login'] # Retrieve the user's contributions in the repository # commits_url = f"https://api.github.com/repos/{organization_name}/{repo_name}/commits" total_commits = count_total_commits(username, organization_name, repo_name, headers) # issues_url = f"https://api.github.com/repos/{organization_name}/{repo_name}/issues" # issues_data = requests.get(issues_url, headers=headers, params={"creator": username, "state": "all"}).json() total_issues = count_total_issues(username, organization_name, repo_name, headers) query = {"username": username} saved_contributor = contributors_collection.find_one(query) contributor_details = { "username": username, "rust_repos": [repo_name], "num_of_commits": [total_commits], "num_of_issues": [total_issues], } if saved_contributor is None: user_url = f"https://api.github.com/users/{username}" user_data = requests.get(user_url, headers=headers).json() contributor_details["bio"] = user_data.get("bio", "") contributor_details["location"] = user_data.get("location", "") contributor_details["id"] = user_data.get("id", "") contributor_details["url"] = user_data.get("url", "") contributor_details["created_at"] = user_data.get("created_at", "") contributor_details["updated_at"] = user_data.get("updated_at", "") contributor_details["email"] = user_data.get("email", "") contributor_details["blog"] = user_data.get("blog", "") contributor_details["public_repos"] = user_data.get("public_repos", "") contributor_details["public_gists"] = user_data.get("public_gists", "") contributor_details["followers"] = user_data.get("followers", "") contributor_details["following"] = user_data.get("following", "") contributor_details["type"] = user_data.get("type", "") contributor_details["company"] = user_data.get("company", "") contributor_details["hireable"] = user_data.get("hireable", "") contributors_collection.insert_one(contributor_details) else: saved_contributor["num_of_commits"].append(len(commits_data)) saved_contributor["num_of_issues"].append(len(issues_data)) saved_contributor["rust_repos"].append(repo_name) contributors_collection.update_one(query, { "$set": { "num_of_commits": saved_contributor["num_of_commits"], "num_of_issues": saved_contributor["num_of_issues"], "rust_repos": saved_contributor["rust_repos"] } }) print(contributor_details) if count < 0: break mongo_client.close()