Extract users' email addresses using Github API

55 Views Asked by At

I have a Python function that pulls the issues in the repository using the Github API, and I want to pull the emails of the users who closed and opened the issue with this function. I tried below code but it does not work (it extracts null type) . Is there a way to do this?

def extract_issues(repo_url, access_token): try: g = Github(access_token)

    # Extract username and repository name from the URL
    repo_url_parts = repo_url.strip('/').split('/')
    username, repo_name = repo_url_parts[-2:]

    repo = g.get_repo(f"{username}/{repo_name}")

    issues_data = []
    for issue in repo.get_issues(state='all'):
        issue_comments = issue.get_comments()

        opened_by = issue.user.login if issue.user else None
        opened_by_email = None
        if issue.user:
            user = g.get_user(issue.user.login)
            opened_by_email = user.email

        issue_data = {
            'id': issue.number,
            'title': issue.title,
            'description': issue.body,
            'state': issue.state,
            'created_at': issue.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'closed_at': issue.closed_at.strftime('%Y-%m-%d %H:%M:%S') if issue.closed_at else None,
            'closed_by': issue.closed_by.login if issue.closed_by else None,
            'closer_mail': issue.closed_by.email if issue.closed_by.email else "not closed",
            'opened_by': opened_by,
            'opener_mail': opened_by_email,
            'comments': [{'author': comment.user.login,
                          'comment_date': comment.created_at.strftime('%Y-%m-%d %H:%M:%S'),
                          'comment_text': comment.body} for comment in issue_comments]
        }
        issues_data.append(issue_data)

    return issues_data
except Exception as e:
    print(f"An error occurred: {str(e)}")
    return None
1

There are 1 best solutions below

2
12944qwerty On

According to the GitHub API, the email field will return null if the specified user has not set a public user or if you didn't provide a token that can read a user's emails.

Double check if your token has the permission to read the user's email and make sure that the specified user has also set a public email.