Esempio n. 1
0
def set_team_syncing(team, login_service_name, config):
    """ Sets the given team to sync to the given service using the given config. """
    login_service = LoginService.get(name=login_service_name)
    return TeamSync.create(team=team,
                           transaction_id="",
                           service=login_service,
                           config=json.dumps(config))
Esempio n. 2
0
def update_sync_status(team_sync_info):
    """ Attempts to update the transaction ID and last updated time on a TeamSync object. If the
      transaction ID on the entry in the DB does not match that found on the object, this method
      returns False, which indicates another caller updated it first.
  """
    new_transaction_id = str(uuid.uuid4())
    query = TeamSync.update(
        transaction_id=new_transaction_id, last_updated=datetime.now()).where(
            TeamSync.id == team_sync_info.id,
            TeamSync.transaction_id == team_sync_info.transaction_id)
    return query.execute() == 1
Esempio n. 3
0
def get_stale_team(stale_timespan):
    """ Returns a team that is setup to sync to an external group, and who has not been synced in
      now - stale_timespan. Returns None if none found.
  """
    stale_at = datetime.now() - stale_timespan

    try:
        candidates = (TeamSync.select(
            TeamSync.id).where((TeamSync.last_updated <= stale_at) | (
                TeamSync.last_updated >> None)).limit(500).alias("candidates"))

        found = TeamSync.select(candidates.c.id).from_(candidates).order_by(
            db_random_func()).get()

        if found is None:
            return

        return TeamSync.select(
            TeamSync, Team).join(Team).where(TeamSync.id == found.id).get()
    except TeamSync.DoesNotExist:
        return None
Esempio n. 4
0
def get_team_sync_information(orgname, teamname):
    """ Returns the team syncing information for the team with the given name under the organization
      with the given name or None if none.
  """
    query = (TeamSync.select(TeamSync, LoginService).join(Team).join(
        User).switch(TeamSync).join(LoginService).where(
            Team.name == teamname, User.organization == True,
            User.username == orgname))

    try:
        return query.get()
    except TeamSync.DoesNotExist:
        return None
Esempio n. 5
0
def get_teams_within_org(organization, has_external_auth=False):
    """
    Returns a AttrDict of team info (id, name, description), its role under the org, the number of
    repositories on which it has permission, and the number of members.
    """
    query = Team.select().where(
        Team.organization == organization).join(TeamRole)

    def _team_view(team):
        return {
            "id": team.id,
            "name": team.name,
            "description": team.description,
            "role_name": Team.role.get_name(team.role_id),
            "repo_count": 0,
            "member_count": 0,
            "is_synced": False,
        }

    teams = {team.id: _team_view(team) for team in query}
    if not teams:
        # Just in case. Should ideally never happen.
        return []

    # Add repository permissions count.
    permission_tuples = (RepositoryPermission.select(
        RepositoryPermission.team, fn.Count(RepositoryPermission.id)).where(
            RepositoryPermission.team << list(teams.keys())).group_by(
                RepositoryPermission.team).tuples())

    for perm_tuple in permission_tuples:
        teams[perm_tuple[0]]["repo_count"] = perm_tuple[1]

    # Add the member count.
    members_tuples = (TeamMember.select(TeamMember.team, fn.Count(
        TeamMember.id)).where(TeamMember.team << list(teams.keys())).group_by(
            TeamMember.team).tuples())

    for member_tuple in members_tuples:
        teams[member_tuple[0]]["member_count"] = member_tuple[1]

    # Add syncing information.
    if has_external_auth:
        sync_query = TeamSync.select(
            TeamSync.team).where(TeamSync.team << list(teams.keys()))
        for team_sync in sync_query:
            teams[team_sync.team_id]["is_synced"] = True

    return [AttrDict(team_info) for team_info in list(teams.values())]