示例#1
0
def makeTeams() -> List[Team]:
    t0 = Team('t0', 'TZ', 'T Zero Blasters')
    t0.platform = 'iOS'
    t0.team_leads = set(['u0', 'u1', 'u2'])
    t0.members = set(['u0', 'u1', 'u2'])
    t1 = Team('t1', 'T1', 'T 1 Blasters')
    t1.platform = 'iOS'
    t1.team_leads = set(['u0', 'u2'])
    t1.members = set(['u0', 'u2', 'u3'])
    return [t0, t1]
示例#2
0
def create_test_team(tid: str,
                     team_name: str,
                     display_name: str) -> Team:
    """
    Create a test team with team name, and with all other attributes the same.

    ==========  =============================
    Property    Preset
    ==========  =============================
    Github      ``tid``
    Name slug   ``team_name``
    Display     ``display_name``
    Platform    slack
    Members     ['abc_123']
    ==========  =============================

    :param tid: The github ID associated with the team
    :param team_name: The github team name slug
    :param display_name: The github team name
    :return: a filled-in team model (no empty strings)
    """
    t = Team(tid, team_name, display_name)
    t.platform = 'slack'
    t.add_member('abc_123')
    return t
示例#3
0
 def test_create_with_platform(self) -> None:
     """Test create team command API with platform."""
     self.mock_github.org_create_team.return_value = "team_gh_id"
     created = self.testapi.team_create("lead",
                                        "team_name",
                                        platform="platform")
     self.assertTrue(created)
     stored_team = Team("team_gh_id", "team_name", "")
     stored_team.platform = "platform"
     stored_team.add_member(self.lead_user.github_id)
     stored_team.add_team_lead(self.lead_user.github_id)
     self.mock_facade.store.assert_called_with(stored_team)
示例#4
0
def test_print():
    """Test print team class."""
    team = Team("1", "brussel-sprouts", "Brussel Sprouts")
    new_slack_id = "U0G9QF9C6"
    team.add_member(new_slack_id)
    team.add_team_lead(new_slack_id)
    team.platform = "web"
    assert str(team) == "{'github_team_id': '1'," \
                        " 'github_team_name': 'brussel-sprouts'," \
                        " 'display_name': 'Brussel Sprouts'," \
                        " 'platform': 'web'," \
                        " 'team_leads': {'U0G9QF9C6'}," \
                        " 'members': {'U0G9QF9C6'}}"
示例#5
0
    def team_create(self,
                    caller_id: str,
                    gh_team_name: str,
                    display_name: str = None,
                    platform: str = None,
                    channel: str = None,
                    lead_id: str = None) -> bool:
        """
        Create a team both in the Rocket database and Github organization.

        :param caller_id: Slack ID of the user who is calling the API
        :param gh_team_name: desired team name to give the team on Github
        :param display_name: display name to give the team when displayed
                             in various places
        :param platform: main platform this team's projects are based on
        :param channel: name of the Slack channel whose channel members
                        are to be added to the team - its members will be
                        added to the team in the Rocket database and added
                        to the Github team as well
        :param lead_id: Slack ID of the user who will be made the lead of
                        this team
        :raises: LookupError if the calling user or tech lead cannot be found
                 in the database
        :raises: PermissionError if the calling user has insufficient
                 permissions to create a team, or if the specified user with
                 lead_id does not have the permission to be a lead
        :raises: SlackAPIError if a channel name is provided by an error
                 is encountered retrieving the members of that channel
        :raises: GithubAPIException if an error occurs on team creation or
                 Github team member addition
        :raises: Exception for any other generic error
        :return: True if the team creation was successful, False otherwise
        """
        logging.info("Team create command API called")
        command_user = self._db_facade.retrieve(User, caller_id)
        logging.debug(f"Calling user: {command_user.__str__()}")

        if not check_permissions(command_user, None):
            msg = f"Calling user with Slack ID {caller_id} has permission" \
                f" level {str(command_user.permissions_level)}, " \
                "insufficient for creating a team!"
            logging.error(msg)
            raise PermissionError(msg)

        if not command_user.github_id:
            msg = f"User {command_user.slack_id} has yet to register a"\
                f" Github username in this system."\
                f" Register with `/rocket user edit --github username`."
            logging.error(msg)
            raise Exception(msg)

        gh_team_id = str(self._gh_interface.org_create_team(gh_team_name))
        logging.debug(f"Github team {gh_team_name} created with "
                      f"Github team ID {gh_team_id}")
        team = Team(gh_team_id, gh_team_name, "")

        if display_name is not None:
            logging.debug(f"Attaching display name {display_name} "
                          f"to {gh_team_name}")
            team.display_name = display_name

        if platform is not None:
            logging.debug(f"Attaching platform {platform} to {gh_team_name}")
            team.platform = platform

        if channel is not None:
            logging.debug(f"Adding channel members of #{channel} "
                          f"to {gh_team_name}")
            try:
                channel_member_ids = \
                    self._slack_client.get_channel_users(channel)
                logging.debug(f"Member IDs of members found in #{channel}: "
                              f"{channel_member_ids}")
            except SlackAPIError as e:
                msg = f"Channel member query on channel #{channel} failed: " \
                    f"{e.error}"
                logging.error(msg)
                raise SlackAPIError(msg)

            channel_members = \
                self._db_facade.bulk_retrieve(User,
                                              list(channel_member_ids.keys()))

            if len(channel_members) is not len(channel_member_ids):
                retrieved_members_ids = [
                    member.slack_id for member in channel_members
                ]
                unaccounted_member_ids = [
                    member_id for member_id in channel_member_ids
                    if member_id not in retrieved_members_ids
                ]
                logging.warning("Users not found for following Slack IDs: "
                                f"{unaccounted_member_ids}")

            for member in channel_members:
                self._gh_interface.add_team_member(member.github_username,
                                                   gh_team_id)
                team.add_member(member.github_id)
                logging.debug(f"Member with ID {member.slack_id} added "
                              f"to {gh_team_name}")
        else:
            self._gh_interface.add_team_member(command_user.github_username,
                                               gh_team_id)
            team.add_member(command_user.github_id)
            logging.debug(f"Calling user with ID {command_user.slack_id} "
                          f"added to {gh_team_name}")

        if lead_id is not None:
            lead = self._db_facade.retrieve(User, lead_id)

            if check_permissions(lead, None):
                lead_in_team = self._gh_interface.has_team_member(
                    lead.github_username, gh_team_id)
                if not lead_in_team:
                    self._gh_interface.add_team_member(lead.github_username,
                                                       gh_team_id)

                team.add_member(lead.github_id)
                team.add_team_lead(lead.github_id)
                logging.debug(f"User with ID {lead_id} set as tech lead of "
                              f"{gh_team_name}")
            else:
                msg = f"User specified with lead ID {lead_id} has" \
                    f" permission level {str(lead.permissions_level)}, " \
                    "insufficient to lead a team!"
                logging.error(msg)
                raise PermissionError(msg)
        else:
            team.add_team_lead(command_user.github_id)
            logging.debug(f"Calling user with ID {command_user.github_id} set"
                          f" as tech lead of {gh_team_name}")

        created = self._db_facade.store(team)
        return created
示例#6
0
def test_set_platform():
    """Test the Team class method set_platform(platform)."""
    team = Team("1", "brussel-sprouts", "Brussel Sprouts")
    team.platform = "web"
    assert team.platform == "web"
示例#7
0
    def create_helper(self, param_list, user_id) -> ResponseTuple:
        """
        Create team and calls GitHub API to create the team in GitHub.

        If ``param_list[name] is not None``, will add a display name. If
        ``param_list[channel] is not None``, will add all members of channel in
        which the command was called into the team.

        :param param_list: List of parameters for creating team
        :param user_id: Slack ID of user who called command
        :return: error message if team created unsuccessfully otherwise returns
                 success message
        """
        try:
            command_user = self.facade.retrieve(User, user_id)
            if not check_permissions(command_user, None):
                return self.permission_error, 200
            if not command_user.github_id:
                msg = f"User {command_user.slack_id} has yet to register a"\
                    f" Github username in this system."\
                    f" Register with `/rocket user edit --github username`."
                logging.error(msg)
                return msg, 200
            msg = f"New team created: {param_list['team_name']}, "
            team_id = str(self.gh.org_create_team(param_list['team_name']))
            team = Team(team_id, param_list['team_name'], "")
            if param_list["name"] is not None:
                msg += f"name: {param_list['name']}, "
                team.display_name = param_list['name']
            if param_list["platform"] is not None:
                msg += f"platform: {param_list['platform']}, "
                team.platform = param_list['platform']
            if param_list["folder"] is not None:
                msg += f"folder: {param_list['folder']}"
                team.folder = param_list['folder']
            if param_list["channel"] is not None:
                msg += "added channel, "
                for member_id in self.sc.get_channel_users(
                        param_list["channel"]):
                    try:
                        member = self.facade.retrieve(User, member_id)
                        self.gh.add_team_member(member.github_username,
                                                team_id)
                        team.add_member(member.github_id)
                    except LookupError:
                        pass
            else:
                self.gh.add_team_member(command_user.github_username, team_id)
                team.add_member(command_user.github_id)
            if param_list["lead"] is not None:
                msg += "added lead"
                lead_user = self.facade.retrieve(User, param_list["lead"])
                team.add_team_lead(lead_user.github_id)
                if not self.gh.has_team_member(lead_user.github_username,
                                               team_id):
                    self.gh.add_team_member(lead_user.github_username, team_id)
            else:
                team.add_team_lead(command_user.github_id)

            self.facade.store(team)
            return msg, 200
        except GithubAPIException as e:
            logging.error(f"Team creation error with {e.data}")
            return f"Team creation unsuccessful with the" \
                   f" following error: {e.data}", 200
        except LookupError:
            logging.error(f"User(uid={user_id}) isn't in database")
            return self.lookup_error, 200
        except SlackAPIError as e:
            logging.error(f"Slack error with {e.error}")
            return f"Team creation unsuccessful with the" \
                   f" following error: {e.error}", 200
示例#8
0
    def create_helper(self, args: Namespace, user_id: str) -> ResponseTuple:
        """
        Create team and calls GitHub API to create the team in GitHub.

        If ``args.displayname is not None``, will add a display name. If
        ``args.channel is not None``, will add all members of channel in
        which the command was called into the team.
        :param args: Parameters for creating team
        :param user_id: Slack ID of user who called command
        :return: error message if team created unsuccessfully otherwise returns
                 success message
        """
        try:
            command_user = self.facade.retrieve(User, user_id)
            if not check_permissions(command_user, None):
                return self.permission_error, 200
            if not command_user.github_id:
                msg = f"User {command_user.slack_id} has yet to register a"\
                    f" Github username in this system."\
                    f" Register with `/rocket user edit --github username`."
                logging.error(msg)
                return msg, 200
            msg = f"New team created: {args.team_name}, "
            team_id = str(self.gh.org_create_team(args.team_name))
            team = Team(team_id, args.team_name, "")
            if args.displayname is not None:
                msg += f"displayname: {args.displayname}, "
                team.displayname = args.displayname
            if args.platform is not None:
                msg += f"platform: {args.platform}, "
                team.platform = args.platform
            if args.folder is not None:
                msg += f"folder: {args.folder}"
                team.folder = args.folder
            if args.channel is not None:
                msg += "added channel"
                channel_users = self.sc.get_channel_users(
                    args.channel)
                users_no_ghid = []
                for member_id in channel_users:
                    try:
                        member = self.facade.retrieve(User, member_id)
                        if not member.github_username:
                            users_no_ghid.append(member_id)
                            continue
                        self.gh.add_team_member(member.github_username,
                                                team_id)
                        team.add_member(member.github_id)
                    except (LookupError, GithubAPIException):
                        users_no_ghid.append(member_id)

                if users_no_ghid:
                    users_escaped = ' '.join(
                        [f'<@{uid}>' for uid in users_no_ghid])
                    no_gh_reminder =\
                        ' (users who forgot to set Github accounts or forgot '\
                        'to register into database: ' +\
                        users_escaped + ')'
                    msg += no_gh_reminder
                msg += ', '
            else:
                self.gh.add_team_member(command_user.github_username, team_id)
                team.add_member(command_user.github_id)
            if args.lead is not None:
                msg += "added lead"
                lead_user = self.facade.retrieve(User, args.lead)
                team.add_team_lead(lead_user.github_id)
                if not self.gh.has_team_member(lead_user.github_username,
                                               team_id):
                    self.gh.add_team_member(lead_user.github_username, team_id)
            else:
                team.add_team_lead(command_user.github_id)

            self.facade.store(team)
            return msg, 200
        except GithubAPIException as e:
            logging.error(f"Team creation error with {e.data}")
            return f"Team creation unsuccessful with the" \
                   f" following error: {e.data}", 200
        except LookupError:
            logging.error(f"User(uid={user_id}) isn't in database")
            return self.lookup_error, 200
        except SlackAPIError as e:
            logging.error(f"Slack error with {e.error}")
            return f"Team creation unsuccessful with the" \
                   f" following error: {e.error}", 200