from github import Github import pymongo from credentials import mongo_db_name, mongo_uri, organization_name, access_token # Initialize the PyGitHub client g = Github(access_token) # Get the organization organization = g.get_organization(organization_name) # Get the repositories repositories = organization.get_repos() # Initialize MongoDB client mongo_client = pymongo.MongoClient(mongo_uri) # Create or use the "pull_requests" database mongo_db = mongo_client[mongo_db_name] # prs_collection = mongo_db["pull_requests"] for repo in repositories: # Get pull requests in the repository pull_requests = repo.get_pulls(state="all") # Iterate through pull requests and store data in MongoDB for pr in pull_requests: reviews = pr.get_reviews() reviews_x = [review for review in reviews], print(reviews_x) pr_data = { "number": pr.number, "title": pr.title, "state": pr.state, "labels": [label.name for label in pr.labels], "merged": pr.merged, "creator": pr.user.login, "reviewers": [review.user.login for review in reviews], "repo": repo.name } # Check if the pull request exists in the database by its number existing_pr = mongo_db.pull_requests.find_one({"number": pr_data["number"]}) if existing_pr: # Update the existing document with the latest reviewers mongo_db.pull_requests.update_one( {"number": pr_data["number"]}, {"$set": {"reviewers": pr_data["reviewers"]}} ) else: # Insert the new pull request data into the database mongo_db.pull_requests.insert_one(pr_data) # print(pr_data) # Close the MongoDB client mongo_client.close()