@@ -166,18 +166,23 @@ def from_identifier(cls, id: None) -> None: ...
166
166
def from_identifier (cls , id : int | str ) -> "Actor" : ...
167
167
168
168
@classmethod
169
- def from_identifier (cls , id : str | int | None ) -> "Actor | None" :
169
+ def from_identifier (
170
+ cls , id : str | int | None , organization_id : int | None = None
171
+ ) -> "Actor | None" :
170
172
"""
171
173
Parse an actor identifier into an Actor
172
174
173
175
Forms `id` can take:
174
176
1231 -> look up User by id
175
177
"1231" -> look up User by id
176
178
"user:1231" -> look up User by id
179
+ "user:maiseythedog" -> look up user by username
177
180
"team:1231" -> look up Team by id
181
+ "team:team-name" -> look up Team by name (must provide organization_id)
178
182
"maiseythedog" -> look up User by username
179
183
"[email protected] " -> look up User by primary email
180
184
"""
185
+ from sentry .models .team import Team
181
186
from sentry .users .services .user .service import user_service
182
187
183
188
if not id :
@@ -192,10 +197,23 @@ def from_identifier(cls, id: str | int | None) -> "Actor | None":
192
197
return cls (id = int (id ), actor_type = ActorType .USER )
193
198
194
199
if id .startswith ("user:" ):
195
- return cls (id = int (id [5 :]), actor_type = ActorType .USER )
200
+ remainder = id [5 :]
201
+ if remainder .isdigit ():
202
+ return cls (id = int (remainder ), actor_type = ActorType .USER )
203
+ # pass this on to get to the user lookup below
204
+ id = remainder
196
205
197
206
if id .startswith ("team:" ):
198
- return cls (id = int (id [5 :]), actor_type = ActorType .TEAM )
207
+ remainder = id [5 :]
208
+ if remainder .isnumeric ():
209
+ return cls (id = int (remainder ), actor_type = ActorType .TEAM )
210
+
211
+ if organization_id is not None :
212
+ team = Team .objects .filter (name = remainder , organization_id = organization_id )
213
+ if team .exists ():
214
+ return cls (id = team .first ().id , actor_type = ActorType .TEAM )
215
+
216
+ raise cls .InvalidActor (f"Unable to resolve team name: { remainder } " )
199
217
200
218
try :
201
219
user = user_service .get_by_username (username = id )[0 ]
@@ -280,7 +298,7 @@ def parse_and_validate_actor(actor_identifier: str | None, organization_id: int)
280
298
return None
281
299
282
300
try :
283
- actor = Actor .from_identifier (actor_identifier )
301
+ actor = Actor .from_identifier (actor_identifier , organization_id )
284
302
except Exception :
285
303
raise serializers .ValidationError (
286
304
"Could not parse actor. Format should be `type:id` where type is `team` or `user`."
0 commit comments