Пример #1
0
def jira_rescan_users():
    """
    This task goes through all users on JIRA and ensures that they are assigned
    to the correct group based on the user's email address. It's meant to be
    run regularly: once an hour or so.
    """
    # a mapping of group name to email domain
    domain_groups = {
        "edx-employees": "@edx.org",
        "clarice": "@claricetechnologies.com",
        "bnotions": "@bnotions.com",
        "qualcomm": ".qualcomm.com",
        "ubc": "@cs.ubc.ca",
        "ubc": "@ubc.ca",
    }
    if request.method == "GET":
        return render_template("jira_rescan_users.html", domain_groups=domain_groups)

    failures = defaultdict(dict)

    requested_group = request.form.get("group")
    if requested_group:
        if requested_group not in domain_groups:
            resp = jsonify({"error": "Not found", "groups": domain_groups.keys()})
            resp.status_code = 404
            return resp
        requested_groups = {requested_group: domain_groups[requested_group]}
    else:
        requested_groups = domain_groups

    for groupname, domain in requested_groups.items():
        users_in_group = jira_group_members(groupname, session=jira, debug=True)
        usernames_in_group = set(u["name"] for u in users_in_group)
        bugsnag_context = {
            "groupname": groupname,
            "usernames_in_group": usernames_in_group,
        }
        bugsnag.configure_request(meta_data=bugsnag_context)

        for user in jira_users(filter=domain, session=jira, debug=True):
            if not user["email"].endswith(domain):
                pass
            username = user["name"]
            if username not in usernames_in_group:
                # add the user to the group!
                resp = jira.post(
                    "/rest/api/2/group/user?groupname={}".format(groupname),
                    json={"name": username},
                )
                if not resp.ok:
                    failures[groupname][username] = resp.text

    resp = jsonify(failures)
    resp.status_code = 502 if failures else 200
    return resp
Пример #2
0
def rescan_users(domain_groups):
    jira = jira_bp.session
    failures = defaultdict(dict)
    for groupname, domain in domain_groups.items():
        users_in_group = jira_group_members(groupname,
                                            session=jira,
                                            debug=True)
        usernames_in_group = set(u["name"] for u in users_in_group)
        sentry.client.extra_context({
            "groupname": groupname,
            "usernames_in_group": usernames_in_group,
        })

        for user in jira_users(filter=domain, session=jira, debug=True):
            if not user["email"].endswith(domain):
                pass
            username = user["name"]
            if username not in usernames_in_group:
                # add the user to the group!
                resp = jira.post(
                    "/rest/api/2/group/user?groupname={}".format(groupname),
                    json={"name": username},
                )
                if not resp.ok:
                    # Is this a failure saying that the user is already in
                    # the group? If so, ignore it.
                    nothing_to_do_msg = (
                        "Cannot add user '{username}', "
                        "user is already a member of '{groupname}'").format(
                            username=username, groupname=groupname)
                    error = resp.json()
                    if error.get("errorMessages", []) == [nothing_to_do_msg]:
                        continue
                    else:
                        # it's some other kind of failure, so log it
                        failures[groupname][username] = resp.text

    if failures:
        logger.error(
            "Failures in trying to rescan JIRA users: {}".format(failures))
    return failures
Пример #3
0
def rescan_users(domain_groups):
    jira = jira_bp.session
    failures = defaultdict(dict)
    for groupname, domain in domain_groups.items():
        users_in_group = jira_group_members(groupname, session=jira, debug=True)
        usernames_in_group = set(u["name"] for u in users_in_group)
        sentry.client.extra_context({
            "groupname": groupname,
            "usernames_in_group": usernames_in_group,
        })

        for user in jira_users(filter=domain, session=jira, debug=True):
            if not user["email"].endswith(domain):
                pass
            username = user["name"]
            if username not in usernames_in_group:
                # add the user to the group!
                resp = jira.post(
                    "/rest/api/2/group/user?groupname={}".format(groupname),
                    json={"name": username},
                )
                if not resp.ok:
                    # Is this a failure saying that the user is already in
                    # the group? If so, ignore it.
                    nothing_to_do_msg = (
                        "Cannot add user '{username}', "
                        "user is already a member of '{groupname}'"
                    ).format(username=username, groupname=groupname)
                    error = resp.json()
                    if error.get("errorMessages", []) == [nothing_to_do_msg]:
                        continue
                    else:
                        # it's some other kind of failure, so log it
                        failures[groupname][username] = resp.text

    if failures:
        logger.error("Failures in trying to rescan JIRA users: {}".format(failures))
    return failures