Example #1
0
def test_set_display_name():
    """Test the Team class method set_display_name(display_name)."""
    team = Team("1", "brussel-sprouts", "Brussel Sprouts")
    team.display_name = "Corn Cobs"
    assert team.display_name == "Corn Cobs"
Example #2
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
Example #3
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