from collections import defaultdict from github import Github from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi from credentials import mongo_db_name, mongo_uri, organization_name, access_token # Initialize the PyGithub client g = Github(access_token) 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] # Get the organization organization = g.get_organization(organization_name) # Collect organization data print("Organization Data:") print(f"Name: {organization.name}") print(f"Description: {organization.description}") print(f"Location: {organization.location}") org_members = organization.get_members() # Collect repositories data repositories = organization.get_repos() repo_contributors = {} repo_prs = {} # Function to fetch contributor roles using GitHub REST API # def get_contributor_role(repo, username): # url = f"https://api.github.com/repos/{organization_name}/{repo.name}/collaborators/{username}/permission" # response = requests.get(url, headers={"Authorization": f"token {access_token}"}) # if response.status_code == 200: # return response.json() # return None # reviews and roles (not enough access) # issues include PRs too contributor_experience = defaultdict(int) contributors_collection = mongo_db["contributors"] for repo in repositories: repo_name = repo.name # repo_collaborators[repo_name] = repo.get_collaborators() contributors = [] repo_prs[repo_name] = repo.get_pulls(state="all") for contributor in repo.get_contributors(): if contributor.type != "Bot" and contributor.login != "bors": # print(contributor.type) contributors.append(contributor) repo_contributors[repo_name] = contributors # count = 20 for contributor in repo_contributors[repo_name]: # count = count - 1 # contributors_collection = mongo_db["contributors"] username = contributor.login user = g.get_user(username) contributor_details = {} contributor_details["username"] = username # Retrieve the user's contributions in the repository commits = repo.get_commits(author=contributor) issues = repo.get_issues(creator=contributor, state="all", sort="created", direction="asc", ) query = {"username":username} saved_contributor = contributors_collection.find_one(query) if saved_contributor is None: contributor_details["bio"] = user.bio contributor_details["location"] = user.location contributor_details["id"] = user.id contributor_details["url"] = user.url contributor_details["created_at"] = user.created_at contributor_details["updated_at"] = user.updated_at contributor_details["email"] = user.email contributor_details["blog"] = user.blog contributor_details["public_repos"] = user.public_repos contributor_details["public_gists"] = user.public_gists contributor_details["followers"] = user.followers contributor_details["following"] = user.following contributor_details["type"] = user.type contributor_details["company"] = user.company contributor_details["hireable"] = user.hireable contributor_details["rust_repos"] = [repo_name] contributor_details["num_of_commits"] = [commits.totalCount] contributor_details["num_of_issues"] = [issues.totalCount] contributors_collection.insert_one(contributor_details) else: saved_contributor["num_of_commits"].append(commits.totalCount) saved_contributor["num_of_issues"].append(issues.totalCount) saved_contributor["rust_repos"].append(repo_name) # collection.update_one(query, {"$set": {"list_field": document["list_field"]}}) 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()