def METHOD_NAME(
    db: Session, user, identity, profile: 'base.UserProfile'
) -> User:
    identity.username = profile['login']
    user.profile.name = profile['name']
    user.profile.avatar_url = profile['avatar_url']
    # check if any email already registered
    emails = []
    for e in profile.get('emails', []):
        if not e["verified"]:
            continue
        user_email = db.query(Email).filter(Email.email == e["email"]).one_or_none()
        if user_email and user_email.user_id != user.id:
            raise IntegrityError(
                f"User {user.profile.name} already registered"
                f" with email {user_email.email}",
                "",
                "",
            )
        if user_email and e["verified"]:
            emails.append(user_email)
            user_email.primary = True if e["primary"] else False
        else:
            emails.append(
                Email(
                    email=e["email"],
                    verified=e["verified"],
                    primary=e["primary"],
                    provider=identity.provider,
                    identity_id=identity.identity_id,
                )
            )
    user.emails = emails
    db.commit()
    db.refresh(user)
    return user