Module: Legion::Extensions::MicrosoftTeams::Runners::Ownership

Includes:
Helpers::Lex, Helpers::Client
Included in:
Client
Defined in:
lib/legion/extensions/microsoft_teams/runners/ownership.rb

Constant Summary collapse

TEAMS_FILTER =
"resourceProvisioningOptions/Any(x:x eq 'Team')"
TEAMS_SELECT =
'id,displayName,mail'
OWNERS_SELECT =
'id,displayName,mail'

Instance Method Summary collapse

Methods included from Helpers::Client

#bot_connection, #graph_connection, #oauth_connection, #user_path

Instance Method Details

#detect_orphansObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/microsoft_teams/runners/ownership.rb', line 38

def detect_orphans(**)
  log.debug('Ownership#detect_orphans')
  conn = graph_connection(**)
  teams = fetch_all_teams(conn: conn)
  orphaned = []

  teams.each do |team|
    owners = fetch_owners(conn: conn, team_id: team['id'])
    orphaned << { id: team['id'], display_name: team['displayName'], mail: team['mail'] } if owners.empty?
  end

  log.info("Ownership#detect_orphans found #{orphaned.length}/#{teams.length} orphaned teams")
  { orphaned_teams: orphaned, total_scanned: teams.length, orphan_count: orphaned.length }
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'Ownership#detect_orphans')
  { error: e.message }
end

#get_team_owners(team_id:) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/legion/extensions/microsoft_teams/runners/ownership.rb', line 56

def get_team_owners(team_id:, **)
  log.debug("Ownership#get_team_owners team_id=#{team_id}")
  conn = graph_connection(**)
  owners = fetch_owners(conn: conn, team_id: team_id)
  { team_id: team_id, owners: owners }
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'Ownership#get_team_owners', team_id: team_id)
  { error: e.message }
end

#sync_owners(team_id: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/microsoft_teams/runners/ownership.rb', line 20

def sync_owners(team_id: nil, **)
  log.debug("Ownership#sync_owners team_id=#{team_id.inspect}")
  conn = graph_connection(**)
  if team_id
    owners = fetch_owners(conn: conn, team_id: team_id)
    { owners: owners, team_count: 1, synced_at: Time.now.utc.iso8601 }
  else
    teams = fetch_all_teams(conn: conn)
    all_owners = teams.flat_map do |team|
      fetch_owners(conn: conn, team_id: team['id']).map { |o| o.merge('team_id' => team['id']) }
    end
    { owners: all_owners, team_count: teams.length, synced_at: Time.now.utc.iso8601 }
  end
rescue StandardError => e
  handle_exception(e, level: :error, operation: 'Ownership#sync_owners', team_id: team_id)
  { error: e.message }
end