def get_phonebook_dump()

in bugbot/iam.py [0:0]


def get_phonebook_dump(output_dir=""):
    data = None
    if output_dir:
        path = os.path.join(output_dir, "iam_dump.json")
        if os.path.isfile(path):
            with open(path, "r") as In:
                data = json.load(In)
    if not data:
        data = get_all_info(output_dir=output_dir)

    all_cns = {}
    all_dns = {}

    must_have = {
        "first_name",
        "last_name",
        "identities",
        "access_information",
        "staff_information",
    }
    new_data = {}
    for person in data["users"]:
        person = person["profile"]

        if not (must_have < set(person.keys())):
            continue

        if not person["access_information"]["hris"]["values"]:
            continue
        mail = person["access_information"]["hris"]["values"]["primary_work_email"]
        dn = person["identities"]["mozilla_ldap_id"]["value"]
        manager_mail = person["access_information"]["hris"]["values"][
            "managers_primary_work_email"
        ]
        if not manager_mail:
            manager_mail = mail

        _mail = person["identities"]["mozilla_ldap_primary_email"]["value"]
        assert mail == _mail

        ismanager = person["staff_information"]["manager"]["value"]
        isdirector = person["staff_information"]["director"]["value"]
        cn = "{} {}".format(person["first_name"]["value"], person["last_name"]["value"])
        bugzillaEmail = ""
        bugzillaID = None
        if "bugzilla_mozilla_org_primary_email" in person["identities"]:
            bugzillaEmail = person["identities"]["bugzilla_mozilla_org_primary_email"][
                "value"
            ]
            bugzillaID = person["identities"]["bugzilla_mozilla_org_id"]["value"]

        im = None

        values = person.get("usernames", {}).get("values", None)
        if values is not None:
            if not bugzillaEmail and "HACK#BMOMAIL" in values:
                bugzillaEmail = values["HACK#BMOMAIL"]

            values.pop("LDAP-posix_id", None)
            values.pop("LDAP-posix_uid", None)
            im = list(values.values())

        if bugzillaEmail is None:
            bugzillaEmail = ""

        title = person["staff_information"]["title"]["value"]
        all_cns[mail] = cn
        all_dns[mail] = dn

        new = {
            "mail": mail,
            "manager": {"cn": "", "dn": manager_mail},
            "ismanager": "TRUE" if ismanager else "FALSE",
            "isdirector": "TRUE" if isdirector else "FALSE",
            "cn": cn,
            "dn": dn,
            "bugzillaEmail": bugzillaEmail,
            "bugzillaID": bugzillaID,
            "title": title,
        }

        if im:
            new["im"] = im

        new_data[mail] = new

    to_remove = []
    for mail, person in new_data.items():
        manager_mail = person["manager"]["dn"]
        if manager_mail not in all_cns:
            # no manager
            to_remove.append(mail)
            continue
        manager_cn = all_cns[manager_mail]
        manager_dn = all_dns[manager_mail]
        person["manager"]["cn"] = manager_cn
        person["manager"]["dn"] = manager_dn

    for mail in to_remove:
        del new_data[mail]

    update_bugzilla_emails(new_data)
    new_data = list(new_data.values())

    with open("./configs/people.json", "w") as Out:
        json.dump(new_data, Out, sort_keys=True, indent=4, separators=(",", ": "))