test_organization_group_index.py 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  1. from datetime import timedelta
  2. from unittest.mock import Mock, patch
  3. from uuid import uuid4
  4. from dateutil.parser import parse as parse_datetime
  5. from django.test import override_settings
  6. from django.urls import reverse
  7. from django.utils import timezone
  8. from freezegun import freeze_time
  9. from rest_framework import status
  10. from sentry import options
  11. from sentry.models import (
  12. GROUP_OWNER_TYPE,
  13. Activity,
  14. ApiToken,
  15. Commit,
  16. ExternalIssue,
  17. Group,
  18. GroupAssignee,
  19. GroupBookmark,
  20. GroupHash,
  21. GroupHistory,
  22. GroupInbox,
  23. GroupInboxReason,
  24. GroupLink,
  25. GroupOwner,
  26. GroupOwnerType,
  27. GroupResolution,
  28. GroupSeen,
  29. GroupShare,
  30. GroupSnooze,
  31. GroupStatus,
  32. GroupSubscription,
  33. GroupTombstone,
  34. Integration,
  35. OrganizationIntegration,
  36. Release,
  37. ReleaseCommit,
  38. ReleaseStages,
  39. Repository,
  40. UserOption,
  41. add_group_to_inbox,
  42. remove_group_from_inbox,
  43. )
  44. from sentry.models.commitauthor import CommitAuthor
  45. from sentry.models.grouphistory import GroupHistoryStatus, record_group_history
  46. from sentry.search.events.constants import (
  47. RELEASE_STAGE_ALIAS,
  48. SEMVER_ALIAS,
  49. SEMVER_BUILD_ALIAS,
  50. SEMVER_PACKAGE_ALIAS,
  51. )
  52. from sentry.testutils import APITestCase, SnubaTestCase
  53. from sentry.testutils.helpers import apply_feature_flag_on_cls, parse_link_header
  54. from sentry.testutils.helpers.datetime import before_now, iso_format
  55. from sentry.testutils.silo import region_silo_test
  56. from sentry.types.activity import ActivityType
  57. from sentry.types.issues import GroupType
  58. from sentry.utils import json
  59. @apply_feature_flag_on_cls("organizations:release-committer-assignees")
  60. @region_silo_test
  61. class GroupListTest(APITestCase, SnubaTestCase):
  62. endpoint = "sentry-api-0-organization-group-index"
  63. def setUp(self):
  64. super().setUp()
  65. self.min_ago = before_now(minutes=1)
  66. def _parse_links(self, header):
  67. # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
  68. links = {}
  69. for url, attrs in parse_link_header(header).items():
  70. links[attrs["rel"]] = attrs
  71. attrs["href"] = url
  72. return links
  73. def get_response(self, *args, **kwargs):
  74. if not args:
  75. org = self.project.organization.slug
  76. else:
  77. org = args[0]
  78. return super().get_response(org, **kwargs)
  79. def test_sort_by_date_with_tag(self):
  80. # XXX(dcramer): this tests a case where an ambiguous column name existed
  81. event = self.store_event(
  82. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  83. project_id=self.project.id,
  84. )
  85. group = event.group
  86. self.login_as(user=self.user)
  87. response = self.get_success_response(sort_by="date", query="is:unresolved")
  88. assert len(response.data) == 1
  89. assert response.data[0]["id"] == str(group.id)
  90. def test_sort_by_trend(self):
  91. group = self.store_event(
  92. data={
  93. "timestamp": iso_format(before_now(seconds=10)),
  94. "fingerprint": ["group-1"],
  95. },
  96. project_id=self.project.id,
  97. ).group
  98. self.store_event(
  99. data={
  100. "timestamp": iso_format(before_now(seconds=10)),
  101. "fingerprint": ["group-1"],
  102. },
  103. project_id=self.project.id,
  104. )
  105. self.store_event(
  106. data={
  107. "timestamp": iso_format(before_now(hours=13)),
  108. "fingerprint": ["group-1"],
  109. },
  110. project_id=self.project.id,
  111. )
  112. group_2 = self.store_event(
  113. data={
  114. "timestamp": iso_format(before_now(seconds=5)),
  115. "fingerprint": ["group-2"],
  116. },
  117. project_id=self.project.id,
  118. ).group
  119. self.store_event(
  120. data={
  121. "timestamp": iso_format(before_now(hours=13)),
  122. "fingerprint": ["group-2"],
  123. },
  124. project_id=self.project.id,
  125. )
  126. self.login_as(user=self.user)
  127. response = self.get_success_response(
  128. sort="trend",
  129. query="is:unresolved",
  130. limit=1,
  131. start=iso_format(before_now(days=1)),
  132. end=iso_format(before_now(seconds=1)),
  133. )
  134. assert len(response.data) == 1
  135. assert [item["id"] for item in response.data] == [str(group.id)]
  136. header_links = parse_link_header(response["Link"])
  137. cursor = [link for link in header_links.values() if link["rel"] == "next"][0]["cursor"]
  138. response = self.get_success_response(
  139. sort="trend",
  140. query="is:unresolved",
  141. limit=1,
  142. start=iso_format(before_now(days=1)),
  143. end=iso_format(before_now(seconds=1)),
  144. cursor=cursor,
  145. )
  146. assert [item["id"] for item in response.data] == [str(group_2.id)]
  147. def test_sort_by_inbox(self):
  148. group_1 = self.store_event(
  149. data={
  150. "event_id": "a" * 32,
  151. "timestamp": iso_format(before_now(seconds=1)),
  152. "fingerprint": ["group-1"],
  153. },
  154. project_id=self.project.id,
  155. ).group
  156. inbox_1 = add_group_to_inbox(group_1, GroupInboxReason.NEW)
  157. group_2 = self.store_event(
  158. data={
  159. "event_id": "a" * 32,
  160. "timestamp": iso_format(before_now(seconds=1)),
  161. "fingerprint": ["group-2"],
  162. },
  163. project_id=self.project.id,
  164. ).group
  165. inbox_2 = add_group_to_inbox(group_2, GroupInboxReason.NEW)
  166. inbox_2.update(date_added=inbox_1.date_added - timedelta(hours=1))
  167. self.login_as(user=self.user)
  168. response = self.get_success_response(
  169. sort="inbox", query="is:unresolved is:for_review", limit=1
  170. )
  171. assert len(response.data) == 1
  172. assert response.data[0]["id"] == str(group_1.id)
  173. header_links = parse_link_header(response["Link"])
  174. cursor = [link for link in header_links.values() if link["rel"] == "next"][0]["cursor"]
  175. response = self.get_response(
  176. sort="inbox", cursor=cursor, query="is:unresolved is:for_review", limit=1
  177. )
  178. assert [item["id"] for item in response.data] == [str(group_2.id)]
  179. def test_sort_by_inbox_me_or_none(self):
  180. group_1 = self.store_event(
  181. data={
  182. "event_id": "a" * 32,
  183. "timestamp": iso_format(before_now(seconds=1)),
  184. "fingerprint": ["group-1"],
  185. },
  186. project_id=self.project.id,
  187. ).group
  188. inbox_1 = add_group_to_inbox(group_1, GroupInboxReason.NEW)
  189. group_2 = self.store_event(
  190. data={
  191. "event_id": "b" * 32,
  192. "timestamp": iso_format(before_now(seconds=1)),
  193. "fingerprint": ["group-2"],
  194. },
  195. project_id=self.project.id,
  196. ).group
  197. inbox_2 = add_group_to_inbox(group_2, GroupInboxReason.NEW)
  198. inbox_2.update(date_added=inbox_1.date_added - timedelta(hours=1))
  199. GroupOwner.objects.create(
  200. group=group_2,
  201. project=self.project,
  202. organization=self.organization,
  203. type=GroupOwnerType.OWNERSHIP_RULE.value,
  204. user=self.user,
  205. )
  206. owner_by_other = self.store_event(
  207. data={
  208. "event_id": "c" * 32,
  209. "timestamp": iso_format(before_now(seconds=1)),
  210. "fingerprint": ["group-3"],
  211. },
  212. project_id=self.project.id,
  213. ).group
  214. inbox_3 = add_group_to_inbox(owner_by_other, GroupInboxReason.NEW)
  215. inbox_3.update(date_added=inbox_1.date_added - timedelta(hours=1))
  216. other_user = self.create_user()
  217. GroupOwner.objects.create(
  218. group=owner_by_other,
  219. project=self.project,
  220. organization=self.organization,
  221. type=GroupOwnerType.OWNERSHIP_RULE.value,
  222. user=other_user,
  223. )
  224. owned_me_assigned_to_other = self.store_event(
  225. data={
  226. "event_id": "d" * 32,
  227. "timestamp": iso_format(before_now(seconds=1)),
  228. "fingerprint": ["group-4"],
  229. },
  230. project_id=self.project.id,
  231. ).group
  232. inbox_4 = add_group_to_inbox(owned_me_assigned_to_other, GroupInboxReason.NEW)
  233. inbox_4.update(date_added=inbox_1.date_added - timedelta(hours=1))
  234. GroupAssignee.objects.assign(owned_me_assigned_to_other, other_user)
  235. GroupOwner.objects.create(
  236. group=owned_me_assigned_to_other,
  237. project=self.project,
  238. organization=self.organization,
  239. type=GroupOwnerType.OWNERSHIP_RULE.value,
  240. user=self.user,
  241. )
  242. unowned_assigned_to_other = self.store_event(
  243. data={
  244. "event_id": "e" * 32,
  245. "timestamp": iso_format(before_now(seconds=1)),
  246. "fingerprint": ["group-5"],
  247. },
  248. project_id=self.project.id,
  249. ).group
  250. inbox_5 = add_group_to_inbox(unowned_assigned_to_other, GroupInboxReason.NEW)
  251. inbox_5.update(date_added=inbox_1.date_added - timedelta(hours=1))
  252. GroupAssignee.objects.assign(unowned_assigned_to_other, other_user)
  253. self.login_as(user=self.user)
  254. response = self.get_success_response(
  255. sort="inbox",
  256. query="is:unresolved is:for_review assigned_or_suggested:[me, none]",
  257. limit=10,
  258. )
  259. assert [item["id"] for item in response.data] == [str(group_1.id), str(group_2.id)]
  260. def test_trace_search(self):
  261. event = self.store_event(
  262. data={
  263. "event_id": "a" * 32,
  264. "timestamp": iso_format(before_now(seconds=1)),
  265. "contexts": {
  266. "trace": {
  267. "parent_span_id": "8988cec7cc0779c1",
  268. "type": "trace",
  269. "op": "foobar",
  270. "trace_id": "a7d67cf796774551a95be6543cacd459",
  271. "span_id": "babaae0d4b7512d9",
  272. "status": "ok",
  273. }
  274. },
  275. },
  276. project_id=self.project.id,
  277. )
  278. self.login_as(user=self.user)
  279. response = self.get_success_response(
  280. sort_by="date", query="is:unresolved trace:a7d67cf796774551a95be6543cacd459"
  281. )
  282. assert len(response.data) == 1
  283. assert response.data[0]["id"] == str(event.group.id)
  284. def test_feature_gate(self):
  285. # ensure there are two or more projects
  286. self.create_project(organization=self.project.organization)
  287. self.login_as(user=self.user)
  288. response = self.get_response()
  289. assert response.status_code == 400
  290. assert response.data["detail"] == "You do not have the multi project stream feature enabled"
  291. with self.feature("organizations:global-views"):
  292. response = self.get_response()
  293. assert response.status_code == 200
  294. def test_with_all_projects(self):
  295. # ensure there are two or more projects
  296. self.create_project(organization=self.project.organization)
  297. self.login_as(user=self.user)
  298. with self.feature("organizations:global-views"):
  299. response = self.get_success_response(project_id=[-1])
  300. assert response.status_code == 200
  301. def test_boolean_search_feature_flag(self):
  302. self.login_as(user=self.user)
  303. response = self.get_response(sort_by="date", query="title:hello OR title:goodbye")
  304. assert response.status_code == 400
  305. assert (
  306. response.data["detail"]
  307. == 'Error parsing search query: Boolean statements containing "OR" or "AND" are not supported in this search'
  308. )
  309. response = self.get_response(sort_by="date", query="title:hello AND title:goodbye")
  310. assert response.status_code == 400
  311. assert (
  312. response.data["detail"]
  313. == 'Error parsing search query: Boolean statements containing "OR" or "AND" are not supported in this search'
  314. )
  315. def test_invalid_query(self):
  316. now = timezone.now()
  317. self.create_group(last_seen=now - timedelta(seconds=1))
  318. self.login_as(user=self.user)
  319. response = self.get_response(sort_by="date", query="timesSeen:>1t")
  320. assert response.status_code == 400
  321. assert "Invalid number" in response.data["detail"]
  322. def test_valid_numeric_query(self):
  323. now = timezone.now()
  324. self.create_group(last_seen=now - timedelta(seconds=1))
  325. self.login_as(user=self.user)
  326. response = self.get_response(sort_by="date", query="timesSeen:>1k")
  327. assert response.status_code == 200
  328. def test_invalid_sort_key(self):
  329. now = timezone.now()
  330. self.create_group(last_seen=now - timedelta(seconds=1))
  331. self.login_as(user=self.user)
  332. response = self.get_response(sort="meow", query="is:unresolved")
  333. assert response.status_code == 400
  334. def test_simple_pagination(self):
  335. event1 = self.store_event(
  336. data={"timestamp": iso_format(before_now(seconds=2)), "fingerprint": ["group-1"]},
  337. project_id=self.project.id,
  338. )
  339. group1 = event1.group
  340. event2 = self.store_event(
  341. data={"timestamp": iso_format(before_now(seconds=1)), "fingerprint": ["group-2"]},
  342. project_id=self.project.id,
  343. )
  344. group2 = event2.group
  345. self.login_as(user=self.user)
  346. response = self.get_success_response(sort_by="date", limit=1)
  347. assert len(response.data) == 1
  348. assert response.data[0]["id"] == str(group2.id)
  349. links = self._parse_links(response["Link"])
  350. assert links["previous"]["results"] == "false"
  351. assert links["next"]["results"] == "true"
  352. response = self.client.get(links["next"]["href"], format="json")
  353. assert response.status_code == 200
  354. assert len(response.data) == 1
  355. assert response.data[0]["id"] == str(group1.id)
  356. links = self._parse_links(response["Link"])
  357. assert links["previous"]["results"] == "true"
  358. assert links["next"]["results"] == "false"
  359. def test_stats_period(self):
  360. # TODO(dcramer): this test really only checks if validation happens
  361. # on groupStatsPeriod
  362. now = timezone.now()
  363. self.create_group(last_seen=now - timedelta(seconds=1))
  364. self.create_group(last_seen=now)
  365. self.login_as(user=self.user)
  366. self.get_success_response(groupStatsPeriod="24h")
  367. self.get_success_response(groupStatsPeriod="14d")
  368. self.get_success_response(groupStatsPeriod="")
  369. response = self.get_response(groupStatsPeriod="48h")
  370. assert response.status_code == 400
  371. def test_environment(self):
  372. self.store_event(
  373. data={
  374. "fingerprint": ["put-me-in-group1"],
  375. "timestamp": iso_format(self.min_ago),
  376. "environment": "production",
  377. },
  378. project_id=self.project.id,
  379. )
  380. self.store_event(
  381. data={
  382. "fingerprint": ["put-me-in-group2"],
  383. "timestamp": iso_format(self.min_ago),
  384. "environment": "staging",
  385. },
  386. project_id=self.project.id,
  387. )
  388. self.login_as(user=self.user)
  389. response = self.get_success_response(environment="production")
  390. assert len(response.data) == 1
  391. response = self.get_response(environment="garbage")
  392. assert response.status_code == 404
  393. def test_auto_resolved(self):
  394. project = self.project
  395. project.update_option("sentry:resolve_age", 1)
  396. self.store_event(
  397. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  398. project_id=project.id,
  399. )
  400. event2 = self.store_event(
  401. data={"event_id": "b" * 32, "timestamp": iso_format(before_now(seconds=1))},
  402. project_id=project.id,
  403. )
  404. group2 = event2.group
  405. self.login_as(user=self.user)
  406. response = self.get_success_response()
  407. assert len(response.data) == 1
  408. assert response.data[0]["id"] == str(group2.id)
  409. def test_perf_issue(self):
  410. perf_group = self.create_group(type=GroupType.PERFORMANCE_N_PLUS_ONE_DB_QUERIES.value)
  411. self.login_as(user=self.user)
  412. with self.feature(
  413. [
  414. "organizations:issue-search-allow-postgres-only-search",
  415. "organizations:performance-issues",
  416. ]
  417. ):
  418. response = self.get_success_response(query="issue.category:performance")
  419. assert len(response.data) == 1
  420. assert response.data[0]["id"] == str(perf_group.id)
  421. def test_lookup_by_event_id(self):
  422. project = self.project
  423. project.update_option("sentry:resolve_age", 1)
  424. event_id = "c" * 32
  425. event = self.store_event(
  426. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  427. project_id=self.project.id,
  428. )
  429. self.login_as(user=self.user)
  430. response = self.get_success_response(query="c" * 32)
  431. assert response["X-Sentry-Direct-Hit"] == "1"
  432. assert len(response.data) == 1
  433. assert response.data[0]["id"] == str(event.group.id)
  434. assert response.data[0]["matchingEventId"] == event_id
  435. def test_lookup_by_event_id_incorrect_project_id(self):
  436. self.store_event(
  437. data={"event_id": "a" * 32, "timestamp": iso_format(self.min_ago)},
  438. project_id=self.project.id,
  439. )
  440. event_id = "b" * 32
  441. event = self.store_event(
  442. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  443. project_id=self.project.id,
  444. )
  445. other_project = self.create_project(teams=[self.team])
  446. user = self.create_user()
  447. self.create_member(organization=self.organization, teams=[self.team], user=user)
  448. self.login_as(user=user)
  449. with self.feature("organizations:global-views"):
  450. response = self.get_success_response(query=event_id, project=[other_project.id])
  451. assert response["X-Sentry-Direct-Hit"] == "1"
  452. assert len(response.data) == 1
  453. assert response.data[0]["id"] == str(event.group.id)
  454. assert response.data[0]["matchingEventId"] == event_id
  455. def test_lookup_by_event_id_with_whitespace(self):
  456. project = self.project
  457. project.update_option("sentry:resolve_age", 1)
  458. event_id = "c" * 32
  459. event = self.store_event(
  460. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  461. project_id=self.project.id,
  462. )
  463. self.login_as(user=self.user)
  464. response = self.get_success_response(query=" {} ".format("c" * 32))
  465. assert response["X-Sentry-Direct-Hit"] == "1"
  466. assert len(response.data) == 1
  467. assert response.data[0]["id"] == str(event.group.id)
  468. assert response.data[0]["matchingEventId"] == event_id
  469. def test_lookup_by_unknown_event_id(self):
  470. project = self.project
  471. project.update_option("sentry:resolve_age", 1)
  472. self.create_group()
  473. self.create_group()
  474. self.login_as(user=self.user)
  475. response = self.get_success_response(query="c" * 32)
  476. assert len(response.data) == 0
  477. def test_lookup_by_short_id(self):
  478. group = self.group
  479. short_id = group.qualified_short_id
  480. self.login_as(user=self.user)
  481. response = self.get_success_response(query=short_id, shortIdLookup=1)
  482. assert len(response.data) == 1
  483. def test_lookup_by_short_id_ignores_project_list(self):
  484. organization = self.create_organization()
  485. project = self.create_project(organization=organization)
  486. project2 = self.create_project(organization=organization)
  487. group = self.create_group(project=project2)
  488. user = self.create_user()
  489. self.create_member(organization=organization, user=user)
  490. short_id = group.qualified_short_id
  491. self.login_as(user=user)
  492. response = self.get_success_response(
  493. organization.slug, project=project.id, query=short_id, shortIdLookup=1
  494. )
  495. assert len(response.data) == 1
  496. def test_lookup_by_short_id_no_perms(self):
  497. organization = self.create_organization()
  498. project = self.create_project(organization=organization)
  499. group = self.create_group(project=project)
  500. user = self.create_user()
  501. self.create_member(organization=organization, user=user, has_global_access=False)
  502. short_id = group.qualified_short_id
  503. self.login_as(user=user)
  504. response = self.get_success_response(organization.slug, query=short_id, shortIdLookup=1)
  505. assert len(response.data) == 0
  506. def test_lookup_by_group_id(self):
  507. self.login_as(user=self.user)
  508. response = self.get_success_response(group=self.group.id)
  509. assert len(response.data) == 1
  510. assert response.data[0]["id"] == str(self.group.id)
  511. group_2 = self.create_group()
  512. response = self.get_success_response(group=[self.group.id, group_2.id])
  513. assert {g["id"] for g in response.data} == {str(self.group.id), str(group_2.id)}
  514. def test_lookup_by_group_id_no_perms(self):
  515. organization = self.create_organization()
  516. project = self.create_project(organization=organization)
  517. group = self.create_group(project=project)
  518. user = self.create_user()
  519. self.create_member(organization=organization, user=user, has_global_access=False)
  520. self.login_as(user=user)
  521. response = self.get_response(group=[group.id])
  522. assert response.status_code == 403
  523. def test_lookup_by_first_release(self):
  524. self.login_as(self.user)
  525. project = self.project
  526. project2 = self.create_project(name="baz", organization=project.organization)
  527. release = Release.objects.create(organization=project.organization, version="12345")
  528. release.add_project(project)
  529. release.add_project(project2)
  530. event = self.store_event(
  531. data={"release": release.version, "timestamp": iso_format(before_now(seconds=2))},
  532. project_id=project.id,
  533. )
  534. event2 = self.store_event(
  535. data={"release": release.version, "timestamp": iso_format(before_now(seconds=1))},
  536. project_id=project2.id,
  537. )
  538. with self.feature("organizations:global-views"):
  539. response = self.get_success_response(
  540. **{"query": 'first-release:"%s"' % release.version}
  541. )
  542. issues = json.loads(response.content)
  543. assert len(issues) == 2
  544. assert int(issues[0]["id"]) == event2.group.id
  545. assert int(issues[1]["id"]) == event.group.id
  546. def test_lookup_by_release(self):
  547. self.login_as(self.user)
  548. project = self.project
  549. release = Release.objects.create(organization=project.organization, version="12345")
  550. release.add_project(project)
  551. event = self.store_event(
  552. data={
  553. "timestamp": iso_format(before_now(seconds=1)),
  554. "tags": {"sentry:release": release.version},
  555. },
  556. project_id=project.id,
  557. )
  558. response = self.get_success_response(release=release.version)
  559. issues = json.loads(response.content)
  560. assert len(issues) == 1
  561. assert int(issues[0]["id"]) == event.group.id
  562. def test_lookup_by_release_wildcard(self):
  563. self.login_as(self.user)
  564. project = self.project
  565. release = Release.objects.create(organization=project.organization, version="12345")
  566. release.add_project(project)
  567. event = self.store_event(
  568. data={
  569. "timestamp": iso_format(before_now(seconds=1)),
  570. "tags": {"sentry:release": release.version},
  571. },
  572. project_id=project.id,
  573. )
  574. response = self.get_success_response(release=release.version[:3] + "*")
  575. issues = json.loads(response.content)
  576. assert len(issues) == 1
  577. assert int(issues[0]["id"]) == event.group.id
  578. def test_lookup_by_regressed_in_release(self):
  579. self.login_as(self.user)
  580. project = self.project
  581. release = self.create_release()
  582. event = self.store_event(
  583. data={
  584. "timestamp": iso_format(before_now(seconds=1)),
  585. "tags": {"sentry:release": release.version},
  586. },
  587. project_id=project.id,
  588. )
  589. record_group_history(event.group, GroupHistoryStatus.REGRESSED, release=release)
  590. response = self.get_success_response(query=f"regressed_in_release:{release.version}")
  591. issues = json.loads(response.content)
  592. assert [int(issue["id"]) for issue in issues] == [event.group.id]
  593. def test_pending_delete_pending_merge_excluded(self):
  594. events = []
  595. for i in "abcd":
  596. events.append(
  597. self.store_event(
  598. data={
  599. "event_id": i * 32,
  600. "fingerprint": [i],
  601. "timestamp": iso_format(self.min_ago),
  602. },
  603. project_id=self.project.id,
  604. )
  605. )
  606. events[0].group.update(status=GroupStatus.PENDING_DELETION)
  607. events[2].group.update(status=GroupStatus.DELETION_IN_PROGRESS)
  608. events[3].group.update(status=GroupStatus.PENDING_MERGE)
  609. self.login_as(user=self.user)
  610. response = self.get_success_response()
  611. assert len(response.data) == 1
  612. assert response.data[0]["id"] == str(events[1].group.id)
  613. def test_filters_based_on_retention(self):
  614. self.login_as(user=self.user)
  615. self.create_group(last_seen=timezone.now() - timedelta(days=2))
  616. with self.options({"system.event-retention-days": 1}):
  617. response = self.get_success_response()
  618. assert len(response.data) == 0
  619. def test_token_auth(self):
  620. token = ApiToken.objects.create(user=self.user, scope_list=["event:read"])
  621. response = self.client.get(
  622. reverse("sentry-api-0-organization-group-index", args=[self.project.organization.slug]),
  623. format="json",
  624. HTTP_AUTHORIZATION=f"Bearer {token.token}",
  625. )
  626. assert response.status_code == 200, response.content
  627. def test_date_range(self):
  628. with self.options({"system.event-retention-days": 2}):
  629. event = self.store_event(
  630. data={"timestamp": iso_format(before_now(hours=5))}, project_id=self.project.id
  631. )
  632. group = event.group
  633. self.login_as(user=self.user)
  634. response = self.get_success_response(statsPeriod="6h")
  635. assert len(response.data) == 1
  636. assert response.data[0]["id"] == str(group.id)
  637. response = self.get_success_response(statsPeriod="1h")
  638. assert len(response.data) == 0
  639. @patch("sentry.analytics.record")
  640. def test_advanced_search_errors(self, mock_record):
  641. self.login_as(user=self.user)
  642. response = self.get_response(sort_by="date", query="!has:user")
  643. assert response.status_code == 200, response.data
  644. assert not any(
  645. c[0][0] == "advanced_search.feature_gated" for c in mock_record.call_args_list
  646. )
  647. with self.feature({"organizations:advanced-search": False}):
  648. response = self.get_response(sort_by="date", query="!has:user")
  649. assert response.status_code == 400, response.data
  650. assert (
  651. "You need access to the advanced search feature to use negative "
  652. "search" == response.data["detail"]
  653. )
  654. mock_record.assert_called_with(
  655. "advanced_search.feature_gated",
  656. user_id=self.user.id,
  657. default_user_id=self.user.id,
  658. organization_id=self.organization.id,
  659. )
  660. # This seems like a random override, but this test needed a way to override
  661. # the orderby being sent to snuba for a certain call. This function has a simple
  662. # return value and can be used to set variables in the snuba payload.
  663. @patch("sentry.utils.snuba.get_query_params_to_update_for_projects")
  664. def test_assigned_to_pagination(self, patched_params_update):
  665. old_sample_size = options.get("snuba.search.hits-sample-size")
  666. assert options.set("snuba.search.hits-sample-size", 1)
  667. days = reversed(range(4))
  668. self.login_as(user=self.user)
  669. groups = []
  670. for day in days:
  671. patched_params_update.side_effect = [
  672. (self.organization.id, {"project": [self.project.id]})
  673. ]
  674. group = self.store_event(
  675. data={
  676. "timestamp": iso_format(before_now(days=day)),
  677. "fingerprint": [f"group-{day}"],
  678. },
  679. project_id=self.project.id,
  680. ).group
  681. groups.append(group)
  682. assigned_groups = groups[:2]
  683. for ag in assigned_groups:
  684. ag.update(status=GroupStatus.RESOLVED, resolved_at=before_now(seconds=5))
  685. GroupAssignee.objects.assign(ag, self.user)
  686. # This side_effect is meant to override the `calculate_hits` snuba query specifically.
  687. # If this test is failing it's because the -last_seen override is being applied to
  688. # different snuba query.
  689. def _my_patched_params(query_params, **kwargs):
  690. if query_params.aggregations == [
  691. ["uniq", "group_id", "total"],
  692. ["multiply(toUInt64(max(timestamp)), 1000)", "", "last_seen"],
  693. ]:
  694. return (
  695. self.organization.id,
  696. {"project": [self.project.id], "orderby": ["-last_seen"]},
  697. )
  698. else:
  699. return (self.organization.id, {"project": [self.project.id]})
  700. patched_params_update.side_effect = _my_patched_params
  701. response = self.get_response(limit=1, query=f"assigned:{self.user.email}")
  702. assert len(response.data) == 1
  703. assert response.data[0]["id"] == str(assigned_groups[1].id)
  704. header_links = parse_link_header(response["Link"])
  705. cursor = [link for link in header_links.values() if link["rel"] == "next"][0]["cursor"]
  706. response = self.get_response(limit=1, cursor=cursor, query=f"assigned:{self.user.email}")
  707. assert len(response.data) == 1
  708. assert response.data[0]["id"] == str(assigned_groups[0].id)
  709. assert options.set("snuba.search.hits-sample-size", old_sample_size)
  710. def test_assigned_me_none(self):
  711. self.login_as(user=self.user)
  712. groups = []
  713. for i in range(5):
  714. group = self.store_event(
  715. data={
  716. "timestamp": iso_format(before_now(minutes=10, days=i)),
  717. "fingerprint": [f"group-{i}"],
  718. },
  719. project_id=self.project.id,
  720. ).group
  721. groups.append(group)
  722. assigned_groups = groups[:2]
  723. for ag in assigned_groups:
  724. GroupAssignee.objects.assign(ag, self.user)
  725. response = self.get_response(limit=10, query="assigned:me")
  726. assert [row["id"] for row in response.data] == [str(g.id) for g in assigned_groups]
  727. response = self.get_response(limit=10, query="assigned:[me, none]")
  728. assert len(response.data) == 5
  729. GroupAssignee.objects.assign(assigned_groups[1], self.create_user("other@user.com"))
  730. response = self.get_response(limit=10, query="assigned:[me, none]")
  731. assert len(response.data) == 4
  732. def test_seen_stats(self):
  733. self.store_event(
  734. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  735. project_id=self.project.id,
  736. )
  737. before_now_300_seconds = iso_format(before_now(seconds=300))
  738. before_now_350_seconds = iso_format(before_now(seconds=350))
  739. event2 = self.store_event(
  740. data={"timestamp": before_now_300_seconds, "fingerprint": ["group-2"]},
  741. project_id=self.project.id,
  742. )
  743. group2 = event2.group
  744. group2.first_seen = before_now_350_seconds
  745. group2.times_seen = 55
  746. group2.save()
  747. before_now_250_seconds = iso_format(before_now(seconds=250))
  748. self.store_event(
  749. data={
  750. "timestamp": before_now_250_seconds,
  751. "fingerprint": ["group-2"],
  752. "tags": {"server": "example.com", "trace": "meow", "message": "foo"},
  753. },
  754. project_id=self.project.id,
  755. )
  756. self.store_event(
  757. data={
  758. "timestamp": iso_format(before_now(seconds=200)),
  759. "fingerprint": ["group-1"],
  760. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  761. },
  762. project_id=self.project.id,
  763. )
  764. before_now_150_seconds = iso_format(before_now(seconds=150))
  765. self.store_event(
  766. data={
  767. "timestamp": before_now_150_seconds,
  768. "fingerprint": ["group-2"],
  769. "tags": {"trace": "ribbit", "server": "example.com"},
  770. },
  771. project_id=self.project.id,
  772. )
  773. before_now_100_seconds = iso_format(before_now(seconds=100))
  774. self.store_event(
  775. data={
  776. "timestamp": before_now_100_seconds,
  777. "fingerprint": ["group-2"],
  778. "tags": {"message": "foo", "trace": "meow"},
  779. },
  780. project_id=self.project.id,
  781. )
  782. self.login_as(user=self.user)
  783. response = self.get_response(sort_by="date", limit=10, query="server:example.com")
  784. assert response.status_code == 200
  785. assert len(response.data) == 2
  786. assert int(response.data[0]["id"]) == group2.id
  787. assert response.data[0]["lifetime"] is not None
  788. assert response.data[0]["filtered"] is not None
  789. assert response.data[0]["filtered"]["stats"] is not None
  790. assert response.data[0]["lifetime"]["stats"] is None
  791. assert response.data[0]["filtered"]["stats"] != response.data[0]["stats"]
  792. assert response.data[0]["lifetime"]["firstSeen"] == parse_datetime(
  793. before_now_350_seconds # Should match overridden value, not event value
  794. ).replace(tzinfo=timezone.utc)
  795. assert response.data[0]["lifetime"]["lastSeen"] == parse_datetime(
  796. before_now_100_seconds
  797. ).replace(tzinfo=timezone.utc)
  798. assert response.data[0]["lifetime"]["count"] == "55"
  799. assert response.data[0]["filtered"]["count"] == "2"
  800. assert response.data[0]["filtered"]["firstSeen"] == parse_datetime(
  801. before_now_250_seconds
  802. ).replace(tzinfo=timezone.utc)
  803. assert response.data[0]["filtered"]["lastSeen"] == parse_datetime(
  804. before_now_150_seconds
  805. ).replace(tzinfo=timezone.utc)
  806. # Empty filter test:
  807. response = self.get_response(sort_by="date", limit=10, query="")
  808. assert response.status_code == 200
  809. assert len(response.data) == 2
  810. assert int(response.data[0]["id"]) == group2.id
  811. assert response.data[0]["lifetime"] is not None
  812. assert response.data[0]["filtered"] is None
  813. assert response.data[0]["lifetime"]["stats"] is None
  814. assert response.data[0]["lifetime"]["count"] == "55"
  815. assert response.data[0]["lifetime"]["firstSeen"] == parse_datetime(
  816. before_now_350_seconds # Should match overridden value, not event value
  817. ).replace(tzinfo=timezone.utc)
  818. assert response.data[0]["lifetime"]["lastSeen"] == parse_datetime(
  819. before_now_100_seconds
  820. ).replace(tzinfo=timezone.utc)
  821. def test_semver_seen_stats(self):
  822. release_1 = self.create_release(version="test@1.2.3")
  823. release_2 = self.create_release(version="test@1.2.4")
  824. release_3 = self.create_release(version="test@1.2.5")
  825. release_1_e_1 = self.store_event(
  826. data={
  827. "timestamp": iso_format(before_now(minutes=5)),
  828. "fingerprint": ["group-1"],
  829. "release": release_1.version,
  830. },
  831. project_id=self.project.id,
  832. )
  833. group_1 = release_1_e_1.group
  834. release_2_e_1 = self.store_event(
  835. data={
  836. "timestamp": iso_format(before_now(minutes=3)),
  837. "fingerprint": ["group-1"],
  838. "release": release_2.version,
  839. },
  840. project_id=self.project.id,
  841. )
  842. release_3_e_1 = self.store_event(
  843. data={
  844. "timestamp": iso_format(before_now(minutes=1)),
  845. "fingerprint": ["group-1"],
  846. "release": release_3.version,
  847. },
  848. project_id=self.project.id,
  849. )
  850. group_1.update(times_seen=3)
  851. self.login_as(user=self.user)
  852. response = self.get_success_response(
  853. sort_by="date", limit=10, query="release.version:1.2.3"
  854. )
  855. assert [int(row["id"]) for row in response.data] == [group_1.id]
  856. group_data = response.data[0]
  857. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  858. assert group_data["filtered"]["firstSeen"] == release_1_e_1.datetime
  859. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  860. assert group_data["filtered"]["lastSeen"] == release_1_e_1.datetime
  861. assert int(group_data["lifetime"]["count"]) == 3
  862. assert int(group_data["filtered"]["count"]) == 1
  863. response = self.get_success_response(
  864. sort_by="date", limit=10, query="release.version:>=1.2.3"
  865. )
  866. assert [int(row["id"]) for row in response.data] == [group_1.id]
  867. group_data = response.data[0]
  868. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  869. assert group_data["filtered"]["firstSeen"] == release_1_e_1.datetime
  870. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  871. assert group_data["filtered"]["lastSeen"] == release_3_e_1.datetime
  872. assert int(group_data["lifetime"]["count"]) == 3
  873. assert int(group_data["filtered"]["count"]) == 3
  874. response = self.get_success_response(
  875. sort_by="date", limit=10, query="release.version:=1.2.4"
  876. )
  877. assert [int(row["id"]) for row in response.data] == [group_1.id]
  878. group_data = response.data[0]
  879. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  880. assert group_data["filtered"]["firstSeen"] == release_2_e_1.datetime
  881. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  882. assert group_data["filtered"]["lastSeen"] == release_2_e_1.datetime
  883. assert int(group_data["lifetime"]["count"]) == 3
  884. assert int(group_data["filtered"]["count"]) == 1
  885. def test_inbox_search(self):
  886. self.store_event(
  887. data={
  888. "timestamp": iso_format(before_now(seconds=200)),
  889. "fingerprint": ["group-1"],
  890. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  891. },
  892. project_id=self.project.id,
  893. )
  894. event = self.store_event(
  895. data={
  896. "timestamp": iso_format(before_now(seconds=200)),
  897. "fingerprint": ["group-2"],
  898. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  899. },
  900. project_id=self.project.id,
  901. )
  902. self.store_event(
  903. data={
  904. "timestamp": iso_format(before_now(seconds=200)),
  905. "fingerprint": ["group-3"],
  906. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  907. },
  908. project_id=self.project.id,
  909. )
  910. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  911. self.login_as(user=self.user)
  912. response = self.get_response(
  913. sort_by="date", limit=10, query="is:unresolved is:for_review", expand=["inbox"]
  914. )
  915. assert response.status_code == 200
  916. assert len(response.data) == 1
  917. assert int(response.data[0]["id"]) == event.group.id
  918. assert response.data[0]["inbox"] is not None
  919. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  920. def test_inbox_search_outside_retention(self):
  921. self.login_as(user=self.user)
  922. response = self.get_response(
  923. sort="inbox",
  924. limit=10,
  925. query="is:unresolved is:for_review",
  926. collapse="stats",
  927. expand=["inbox", "owners"],
  928. start=iso_format(before_now(days=20)),
  929. end=iso_format(before_now(days=15)),
  930. )
  931. assert response.status_code == 200
  932. assert len(response.data) == 0
  933. def test_assigned_or_suggested_search(self):
  934. event = self.store_event(
  935. data={
  936. "timestamp": iso_format(before_now(seconds=180)),
  937. "fingerprint": ["group-1"],
  938. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  939. },
  940. project_id=self.project.id,
  941. )
  942. event1 = self.store_event(
  943. data={
  944. "timestamp": iso_format(before_now(seconds=185)),
  945. "fingerprint": ["group-2"],
  946. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  947. },
  948. project_id=self.project.id,
  949. )
  950. event2 = self.store_event(
  951. data={
  952. "timestamp": iso_format(before_now(seconds=190)),
  953. "fingerprint": ["group-3"],
  954. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  955. },
  956. project_id=self.project.id,
  957. )
  958. assigned_event = self.store_event(
  959. data={
  960. "timestamp": iso_format(before_now(seconds=195)),
  961. "fingerprint": ["group-4"],
  962. },
  963. project_id=self.project.id,
  964. )
  965. assigned_to_other_event = self.store_event(
  966. data={
  967. "timestamp": iso_format(before_now(seconds=195)),
  968. "fingerprint": ["group-5"],
  969. },
  970. project_id=self.project.id,
  971. )
  972. self.login_as(user=self.user)
  973. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  974. assert response.status_code == 200
  975. assert len(response.data) == 0
  976. GroupOwner.objects.create(
  977. group=assigned_to_other_event.group,
  978. project=assigned_to_other_event.group.project,
  979. organization=assigned_to_other_event.group.project.organization,
  980. type=0,
  981. team_id=None,
  982. user_id=self.user.id,
  983. )
  984. GroupOwner.objects.create(
  985. group=event.group,
  986. project=event.group.project,
  987. organization=event.group.project.organization,
  988. type=0,
  989. team_id=None,
  990. user_id=self.user.id,
  991. )
  992. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  993. assert response.status_code == 200
  994. assert len(response.data) == 2
  995. assert int(response.data[0]["id"]) == event.group.id
  996. assert int(response.data[1]["id"]) == assigned_to_other_event.group.id
  997. # Because assigned_to_other_event is assigned to self.other_user, it should not show up in assigned_or_suggested search for anyone but self.other_user. (aka. they are now the only owner)
  998. other_user = self.create_user("other@user.com", is_superuser=False)
  999. GroupAssignee.objects.create(
  1000. group=assigned_to_other_event.group,
  1001. project=assigned_to_other_event.group.project,
  1002. user=other_user,
  1003. )
  1004. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  1005. assert response.status_code == 200
  1006. assert len(response.data) == 1
  1007. assert int(response.data[0]["id"]) == event.group.id
  1008. response = self.get_response(
  1009. sort_by="date", limit=10, query=f"assigned_or_suggested:{other_user.email}"
  1010. )
  1011. assert response.status_code == 200
  1012. assert len(response.data) == 1
  1013. assert int(response.data[0]["id"]) == assigned_to_other_event.group.id
  1014. GroupAssignee.objects.create(
  1015. group=assigned_event.group, project=assigned_event.group.project, user=self.user
  1016. )
  1017. response = self.get_response(
  1018. sort_by="date", limit=10, query=f"assigned_or_suggested:{self.user.email}"
  1019. )
  1020. assert response.status_code == 200
  1021. assert len(response.data) == 2
  1022. assert int(response.data[0]["id"]) == event.group.id
  1023. assert int(response.data[1]["id"]) == assigned_event.group.id
  1024. response = self.get_response(
  1025. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1026. )
  1027. assert response.status_code == 200
  1028. assert len(response.data) == 0
  1029. GroupOwner.objects.create(
  1030. group=event.group,
  1031. project=event.group.project,
  1032. organization=event.group.project.organization,
  1033. type=0,
  1034. team_id=self.team.id,
  1035. user_id=None,
  1036. )
  1037. response = self.get_response(
  1038. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1039. )
  1040. assert response.status_code == 200
  1041. assert len(response.data) == 1
  1042. assert int(response.data[0]["id"]) == event.group.id
  1043. response = self.get_response(
  1044. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1045. )
  1046. assert response.status_code == 200
  1047. assert len(response.data) == 4
  1048. assert int(response.data[0]["id"]) == event.group.id
  1049. assert int(response.data[1]["id"]) == event1.group.id
  1050. assert int(response.data[2]["id"]) == event2.group.id
  1051. assert int(response.data[3]["id"]) == assigned_event.group.id
  1052. not_me = self.create_user(email="notme@sentry.io")
  1053. GroupOwner.objects.create(
  1054. group=event2.group,
  1055. project=event2.group.project,
  1056. organization=event2.group.project.organization,
  1057. type=0,
  1058. team_id=None,
  1059. user_id=not_me.id,
  1060. )
  1061. response = self.get_response(
  1062. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1063. )
  1064. assert response.status_code == 200
  1065. assert len(response.data) == 3
  1066. assert int(response.data[0]["id"]) == event.group.id
  1067. assert int(response.data[1]["id"]) == event1.group.id
  1068. assert int(response.data[2]["id"]) == assigned_event.group.id
  1069. GroupOwner.objects.create(
  1070. group=event2.group,
  1071. project=event2.group.project,
  1072. organization=event2.group.project.organization,
  1073. type=0,
  1074. team_id=None,
  1075. user_id=self.user.id,
  1076. )
  1077. # Should now include event2 as it has shared ownership.
  1078. response = self.get_response(
  1079. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1080. )
  1081. assert response.status_code == 200
  1082. assert len(response.data) == 4
  1083. assert int(response.data[0]["id"]) == event.group.id
  1084. assert int(response.data[1]["id"]) == event1.group.id
  1085. assert int(response.data[2]["id"]) == event2.group.id
  1086. assert int(response.data[3]["id"]) == assigned_event.group.id
  1087. # Assign group to another user and now it shouldn't show up in owner search for this team.
  1088. GroupAssignee.objects.create(
  1089. group=event.group,
  1090. project=event.group.project,
  1091. user=other_user,
  1092. )
  1093. response = self.get_response(
  1094. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1095. )
  1096. assert response.status_code == 200
  1097. assert len(response.data) == 0
  1098. def test_semver(self):
  1099. release_1 = self.create_release(version="test@1.2.3")
  1100. release_2 = self.create_release(version="test@1.2.4")
  1101. release_3 = self.create_release(version="test@1.2.5")
  1102. release_1_g_1 = self.store_event(
  1103. data={
  1104. "timestamp": iso_format(before_now(minutes=1)),
  1105. "fingerprint": ["group-1"],
  1106. "release": release_1.version,
  1107. },
  1108. project_id=self.project.id,
  1109. ).group.id
  1110. release_1_g_2 = self.store_event(
  1111. data={
  1112. "timestamp": iso_format(before_now(minutes=2)),
  1113. "fingerprint": ["group-2"],
  1114. "release": release_1.version,
  1115. },
  1116. project_id=self.project.id,
  1117. ).group.id
  1118. release_2_g_1 = self.store_event(
  1119. data={
  1120. "timestamp": iso_format(before_now(minutes=3)),
  1121. "fingerprint": ["group-3"],
  1122. "release": release_2.version,
  1123. },
  1124. project_id=self.project.id,
  1125. ).group.id
  1126. release_2_g_2 = self.store_event(
  1127. data={
  1128. "timestamp": iso_format(before_now(minutes=4)),
  1129. "fingerprint": ["group-4"],
  1130. "release": release_2.version,
  1131. },
  1132. project_id=self.project.id,
  1133. ).group.id
  1134. release_3_g_1 = self.store_event(
  1135. data={
  1136. "timestamp": iso_format(before_now(minutes=5)),
  1137. "fingerprint": ["group-5"],
  1138. "release": release_3.version,
  1139. },
  1140. project_id=self.project.id,
  1141. ).group.id
  1142. release_3_g_2 = self.store_event(
  1143. data={
  1144. "timestamp": iso_format(before_now(minutes=6)),
  1145. "fingerprint": ["group-6"],
  1146. "release": release_3.version,
  1147. },
  1148. project_id=self.project.id,
  1149. ).group.id
  1150. self.login_as(user=self.user)
  1151. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:>1.2.3")
  1152. assert response.status_code == 200, response.content
  1153. assert [int(r["id"]) for r in response.json()] == [
  1154. release_2_g_1,
  1155. release_2_g_2,
  1156. release_3_g_1,
  1157. release_3_g_2,
  1158. ]
  1159. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:>=1.2.3")
  1160. assert response.status_code == 200, response.content
  1161. assert [int(r["id"]) for r in response.json()] == [
  1162. release_1_g_1,
  1163. release_1_g_2,
  1164. release_2_g_1,
  1165. release_2_g_2,
  1166. release_3_g_1,
  1167. release_3_g_2,
  1168. ]
  1169. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:<1.2.4")
  1170. assert response.status_code == 200, response.content
  1171. assert [int(r["id"]) for r in response.json()] == [release_1_g_1, release_1_g_2]
  1172. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:<1.0")
  1173. assert response.status_code == 200, response.content
  1174. assert [int(r["id"]) for r in response.json()] == []
  1175. response = self.get_response(sort_by="date", limit=10, query=f"!{SEMVER_ALIAS}:1.2.4")
  1176. assert response.status_code == 200, response.content
  1177. assert [int(r["id"]) for r in response.json()] == [
  1178. release_1_g_1,
  1179. release_1_g_2,
  1180. release_3_g_1,
  1181. release_3_g_2,
  1182. ]
  1183. def test_release_stage(self):
  1184. replaced_release = self.create_release(
  1185. version="replaced_release",
  1186. environments=[self.environment],
  1187. adopted=timezone.now(),
  1188. unadopted=timezone.now(),
  1189. )
  1190. adopted_release = self.create_release(
  1191. version="adopted_release",
  1192. environments=[self.environment],
  1193. adopted=timezone.now(),
  1194. )
  1195. self.create_release(version="not_adopted_release", environments=[self.environment])
  1196. adopted_release_g_1 = self.store_event(
  1197. data={
  1198. "timestamp": iso_format(before_now(minutes=1)),
  1199. "fingerprint": ["group-1"],
  1200. "release": adopted_release.version,
  1201. "environment": self.environment.name,
  1202. },
  1203. project_id=self.project.id,
  1204. ).group.id
  1205. adopted_release_g_2 = self.store_event(
  1206. data={
  1207. "timestamp": iso_format(before_now(minutes=2)),
  1208. "fingerprint": ["group-2"],
  1209. "release": adopted_release.version,
  1210. "environment": self.environment.name,
  1211. },
  1212. project_id=self.project.id,
  1213. ).group.id
  1214. replaced_release_g_1 = self.store_event(
  1215. data={
  1216. "timestamp": iso_format(before_now(minutes=3)),
  1217. "fingerprint": ["group-3"],
  1218. "release": replaced_release.version,
  1219. "environment": self.environment.name,
  1220. },
  1221. project_id=self.project.id,
  1222. ).group.id
  1223. replaced_release_g_2 = self.store_event(
  1224. data={
  1225. "timestamp": iso_format(before_now(minutes=4)),
  1226. "fingerprint": ["group-4"],
  1227. "release": replaced_release.version,
  1228. "environment": self.environment.name,
  1229. },
  1230. project_id=self.project.id,
  1231. ).group.id
  1232. self.login_as(user=self.user)
  1233. response = self.get_response(
  1234. sort_by="date",
  1235. limit=10,
  1236. query=f"{RELEASE_STAGE_ALIAS}:{ReleaseStages.ADOPTED}",
  1237. environment=self.environment.name,
  1238. )
  1239. assert response.status_code == 200, response.content
  1240. assert [int(r["id"]) for r in response.json()] == [
  1241. adopted_release_g_1,
  1242. adopted_release_g_2,
  1243. ]
  1244. response = self.get_response(
  1245. sort_by="date",
  1246. limit=10,
  1247. query=f"!{RELEASE_STAGE_ALIAS}:{ReleaseStages.LOW_ADOPTION}",
  1248. environment=self.environment.name,
  1249. )
  1250. assert response.status_code == 200, response.content
  1251. assert [int(r["id"]) for r in response.json()] == [
  1252. adopted_release_g_1,
  1253. adopted_release_g_2,
  1254. replaced_release_g_1,
  1255. replaced_release_g_2,
  1256. ]
  1257. response = self.get_response(
  1258. sort_by="date",
  1259. limit=10,
  1260. query=f"{RELEASE_STAGE_ALIAS}:[{ReleaseStages.ADOPTED}, {ReleaseStages.REPLACED}]",
  1261. environment=self.environment.name,
  1262. )
  1263. assert response.status_code == 200, response.content
  1264. assert [int(r["id"]) for r in response.json()] == [
  1265. adopted_release_g_1,
  1266. adopted_release_g_2,
  1267. replaced_release_g_1,
  1268. replaced_release_g_2,
  1269. ]
  1270. response = self.get_response(
  1271. sort_by="date",
  1272. limit=10,
  1273. query=f"!{RELEASE_STAGE_ALIAS}:[{ReleaseStages.LOW_ADOPTION}, {ReleaseStages.REPLACED}]",
  1274. environment=self.environment.name,
  1275. )
  1276. assert response.status_code == 200, response.content
  1277. assert [int(r["id"]) for r in response.json()] == [
  1278. adopted_release_g_1,
  1279. adopted_release_g_2,
  1280. ]
  1281. def test_semver_package(self):
  1282. release_1 = self.create_release(version="test@1.2.3")
  1283. release_2 = self.create_release(version="test2@1.2.4")
  1284. release_1_g_1 = self.store_event(
  1285. data={
  1286. "timestamp": iso_format(before_now(minutes=1)),
  1287. "fingerprint": ["group-1"],
  1288. "release": release_1.version,
  1289. },
  1290. project_id=self.project.id,
  1291. ).group.id
  1292. release_1_g_2 = self.store_event(
  1293. data={
  1294. "timestamp": iso_format(before_now(minutes=2)),
  1295. "fingerprint": ["group-2"],
  1296. "release": release_1.version,
  1297. },
  1298. project_id=self.project.id,
  1299. ).group.id
  1300. release_2_g_1 = self.store_event(
  1301. data={
  1302. "timestamp": iso_format(before_now(minutes=3)),
  1303. "fingerprint": ["group-3"],
  1304. "release": release_2.version,
  1305. },
  1306. project_id=self.project.id,
  1307. ).group.id
  1308. self.login_as(user=self.user)
  1309. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_PACKAGE_ALIAS}:test")
  1310. assert response.status_code == 200, response.content
  1311. assert [int(r["id"]) for r in response.json()] == [
  1312. release_1_g_1,
  1313. release_1_g_2,
  1314. ]
  1315. response = self.get_response(
  1316. sort_by="date", limit=10, query=f"{SEMVER_PACKAGE_ALIAS}:test2"
  1317. )
  1318. assert response.status_code == 200, response.content
  1319. assert [int(r["id"]) for r in response.json()] == [
  1320. release_2_g_1,
  1321. ]
  1322. def test_semver_build(self):
  1323. release_1 = self.create_release(version="test@1.2.3+123")
  1324. release_2 = self.create_release(version="test2@1.2.4+124")
  1325. release_1_g_1 = self.store_event(
  1326. data={
  1327. "timestamp": iso_format(before_now(minutes=1)),
  1328. "fingerprint": ["group-1"],
  1329. "release": release_1.version,
  1330. },
  1331. project_id=self.project.id,
  1332. ).group.id
  1333. release_1_g_2 = self.store_event(
  1334. data={
  1335. "timestamp": iso_format(before_now(minutes=2)),
  1336. "fingerprint": ["group-2"],
  1337. "release": release_1.version,
  1338. },
  1339. project_id=self.project.id,
  1340. ).group.id
  1341. release_2_g_1 = self.store_event(
  1342. data={
  1343. "timestamp": iso_format(before_now(minutes=3)),
  1344. "fingerprint": ["group-3"],
  1345. "release": release_2.version,
  1346. },
  1347. project_id=self.project.id,
  1348. ).group.id
  1349. self.login_as(user=self.user)
  1350. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_BUILD_ALIAS}:123")
  1351. assert response.status_code == 200, response.content
  1352. assert [int(r["id"]) for r in response.json()] == [
  1353. release_1_g_1,
  1354. release_1_g_2,
  1355. ]
  1356. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_BUILD_ALIAS}:124")
  1357. assert response.status_code == 200, response.content
  1358. assert [int(r["id"]) for r in response.json()] == [
  1359. release_2_g_1,
  1360. ]
  1361. def test_aggregate_stats_regression_test(self):
  1362. self.store_event(
  1363. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1364. project_id=self.project.id,
  1365. )
  1366. self.login_as(user=self.user)
  1367. response = self.get_response(
  1368. sort_by="date", limit=10, query="times_seen:>0 last_seen:-1h date:-1h"
  1369. )
  1370. assert response.status_code == 200
  1371. assert len(response.data) == 1
  1372. def test_skipped_fields(self):
  1373. event = self.store_event(
  1374. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1375. project_id=self.project.id,
  1376. )
  1377. self.store_event(
  1378. data={
  1379. "timestamp": iso_format(before_now(seconds=200)),
  1380. "fingerprint": ["group-1"],
  1381. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  1382. },
  1383. project_id=self.project.id,
  1384. )
  1385. query = "server:example.com"
  1386. query += " status:unresolved"
  1387. query += " first_seen:" + iso_format(before_now(seconds=500))
  1388. self.login_as(user=self.user)
  1389. response = self.get_response(sort_by="date", limit=10, query=query)
  1390. assert response.status_code == 200
  1391. assert len(response.data) == 1
  1392. assert int(response.data[0]["id"]) == event.group.id
  1393. assert response.data[0]["lifetime"] is not None
  1394. assert response.data[0]["filtered"] is not None
  1395. def test_inbox_fields(self):
  1396. event = self.store_event(
  1397. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1398. project_id=self.project.id,
  1399. )
  1400. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  1401. query = "status:unresolved"
  1402. self.login_as(user=self.user)
  1403. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1404. assert response.status_code == 200
  1405. assert len(response.data) == 1
  1406. assert int(response.data[0]["id"]) == event.group.id
  1407. assert response.data[0]["inbox"] is not None
  1408. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  1409. assert response.data[0]["inbox"]["reason_details"] is None
  1410. remove_group_from_inbox(event.group)
  1411. snooze_details = {
  1412. "until": None,
  1413. "count": 3,
  1414. "window": None,
  1415. "user_count": None,
  1416. "user_window": 5,
  1417. }
  1418. add_group_to_inbox(event.group, GroupInboxReason.UNIGNORED, snooze_details)
  1419. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1420. assert response.status_code == 200
  1421. assert len(response.data) == 1
  1422. assert int(response.data[0]["id"]) == event.group.id
  1423. assert response.data[0]["inbox"] is not None
  1424. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.UNIGNORED.value
  1425. assert response.data[0]["inbox"]["reason_details"] == snooze_details
  1426. def test_expand_string(self):
  1427. event = self.store_event(
  1428. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1429. project_id=self.project.id,
  1430. )
  1431. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  1432. query = "status:unresolved"
  1433. self.login_as(user=self.user)
  1434. response = self.get_response(sort_by="date", limit=10, query=query, expand="inbox")
  1435. assert response.status_code == 200
  1436. assert len(response.data) == 1
  1437. assert int(response.data[0]["id"]) == event.group.id
  1438. assert response.data[0]["inbox"] is not None
  1439. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  1440. assert response.data[0]["inbox"]["reason_details"] is None
  1441. def test_expand_owners(self):
  1442. event = self.store_event(
  1443. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1444. project_id=self.project.id,
  1445. )
  1446. query = "status:unresolved"
  1447. self.login_as(user=self.user)
  1448. # Test with no owner
  1449. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1450. assert response.status_code == 200
  1451. assert len(response.data) == 1
  1452. assert int(response.data[0]["id"]) == event.group.id
  1453. assert response.data[0]["owners"] is None
  1454. # Test with owners
  1455. GroupOwner.objects.create(
  1456. group=event.group,
  1457. project=event.project,
  1458. organization=event.project.organization,
  1459. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1460. user=self.user,
  1461. )
  1462. GroupOwner.objects.create(
  1463. group=event.group,
  1464. project=event.project,
  1465. organization=event.project.organization,
  1466. type=GroupOwnerType.OWNERSHIP_RULE.value,
  1467. team=self.team,
  1468. )
  1469. GroupOwner.objects.create(
  1470. group=event.group,
  1471. project=event.project,
  1472. organization=event.project.organization,
  1473. type=GroupOwnerType.CODEOWNERS.value,
  1474. team=self.team,
  1475. )
  1476. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1477. assert response.status_code == 200
  1478. assert len(response.data) == 1
  1479. assert int(response.data[0]["id"]) == event.group.id
  1480. assert response.data[0]["owners"] is not None
  1481. assert len(response.data[0]["owners"]) == 3
  1482. assert response.data[0]["owners"][0]["owner"] == f"user:{self.user.id}"
  1483. assert response.data[0]["owners"][1]["owner"] == f"team:{self.team.id}"
  1484. assert response.data[0]["owners"][2]["owner"] == f"team:{self.team.id}"
  1485. assert (
  1486. response.data[0]["owners"][0]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.SUSPECT_COMMIT]
  1487. )
  1488. assert (
  1489. response.data[0]["owners"][1]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.OWNERSHIP_RULE]
  1490. )
  1491. assert response.data[0]["owners"][2]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.CODEOWNERS]
  1492. @override_settings(SENTRY_SELF_HOSTED=False)
  1493. def test_ratelimit(self):
  1494. self.login_as(user=self.user)
  1495. with freeze_time("2000-01-01"):
  1496. for i in range(10):
  1497. self.get_success_response()
  1498. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  1499. def test_filter_not_unresolved(self):
  1500. event = self.store_event(
  1501. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1502. project_id=self.project.id,
  1503. )
  1504. event.group.update(status=GroupStatus.RESOLVED)
  1505. self.login_as(user=self.user)
  1506. response = self.get_response(
  1507. sort_by="date", limit=10, query="!is:unresolved", expand="inbox", collapse="stats"
  1508. )
  1509. assert response.status_code == 200
  1510. assert [int(r["id"]) for r in response.data] == [event.group.id]
  1511. def test_collapse_stats(self):
  1512. event = self.store_event(
  1513. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1514. project_id=self.project.id,
  1515. )
  1516. self.login_as(user=self.user)
  1517. response = self.get_response(
  1518. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1519. )
  1520. assert response.status_code == 200
  1521. assert len(response.data) == 1
  1522. assert int(response.data[0]["id"]) == event.group.id
  1523. assert "stats" not in response.data[0]
  1524. assert "firstSeen" not in response.data[0]
  1525. assert "lastSeen" not in response.data[0]
  1526. assert "count" not in response.data[0]
  1527. assert "userCount" not in response.data[0]
  1528. assert "lifetime" not in response.data[0]
  1529. assert "filtered" not in response.data[0]
  1530. def test_collapse_lifetime(self):
  1531. event = self.store_event(
  1532. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1533. project_id=self.project.id,
  1534. )
  1535. self.login_as(user=self.user)
  1536. response = self.get_response(
  1537. sort_by="date", limit=10, query="is:unresolved", collapse="lifetime"
  1538. )
  1539. assert response.status_code == 200
  1540. assert len(response.data) == 1
  1541. assert int(response.data[0]["id"]) == event.group.id
  1542. assert "stats" in response.data[0]
  1543. assert "firstSeen" in response.data[0]
  1544. assert "lastSeen" in response.data[0]
  1545. assert "count" in response.data[0]
  1546. assert "lifetime" not in response.data[0]
  1547. assert "filtered" in response.data[0]
  1548. def test_collapse_filtered(self):
  1549. event = self.store_event(
  1550. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1551. project_id=self.project.id,
  1552. )
  1553. self.login_as(user=self.user)
  1554. response = self.get_response(
  1555. sort_by="date", limit=10, query="is:unresolved", collapse="filtered"
  1556. )
  1557. assert response.status_code == 200
  1558. assert len(response.data) == 1
  1559. assert int(response.data[0]["id"]) == event.group.id
  1560. assert "stats" in response.data[0]
  1561. assert "firstSeen" in response.data[0]
  1562. assert "lastSeen" in response.data[0]
  1563. assert "count" in response.data[0]
  1564. assert "lifetime" in response.data[0]
  1565. assert "filtered" not in response.data[0]
  1566. def test_collapse_lifetime_and_filtered(self):
  1567. event = self.store_event(
  1568. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1569. project_id=self.project.id,
  1570. )
  1571. self.login_as(user=self.user)
  1572. response = self.get_response(
  1573. sort_by="date", limit=10, query="is:unresolved", collapse=["filtered", "lifetime"]
  1574. )
  1575. assert response.status_code == 200
  1576. assert len(response.data) == 1
  1577. assert int(response.data[0]["id"]) == event.group.id
  1578. assert "stats" in response.data[0]
  1579. assert "firstSeen" in response.data[0]
  1580. assert "lastSeen" in response.data[0]
  1581. assert "count" in response.data[0]
  1582. assert "lifetime" not in response.data[0]
  1583. assert "filtered" not in response.data[0]
  1584. def test_collapse_base(self):
  1585. event = self.store_event(
  1586. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1587. project_id=self.project.id,
  1588. )
  1589. self.login_as(user=self.user)
  1590. response = self.get_response(
  1591. sort_by="date", limit=10, query="is:unresolved", collapse=["base"]
  1592. )
  1593. assert response.status_code == 200
  1594. assert len(response.data) == 1
  1595. assert int(response.data[0]["id"]) == event.group.id
  1596. assert "title" not in response.data[0]
  1597. assert "hasSeen" not in response.data[0]
  1598. assert "stats" in response.data[0]
  1599. assert "firstSeen" in response.data[0]
  1600. assert "lastSeen" in response.data[0]
  1601. assert "count" in response.data[0]
  1602. assert "lifetime" in response.data[0]
  1603. assert "filtered" in response.data[0]
  1604. def test_collapse_stats_group_snooze_bug(self):
  1605. # There was a bug where we tried to access attributes on seen_stats if this feature is active
  1606. # but seen_stats could be null when we collapse stats.
  1607. event = self.store_event(
  1608. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1609. project_id=self.project.id,
  1610. )
  1611. GroupSnooze.objects.create(
  1612. group=event.group,
  1613. user_count=10,
  1614. until=timezone.now() + timedelta(days=1),
  1615. count=10,
  1616. state={"times_seen": 0},
  1617. )
  1618. self.login_as(user=self.user)
  1619. # The presence of the group above with attached GroupSnooze would have previously caused this error.
  1620. response = self.get_response(
  1621. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1622. )
  1623. assert response.status_code == 200
  1624. assert len(response.data) == 1
  1625. assert int(response.data[0]["id"]) == event.group.id
  1626. def test_only_release_committers(self):
  1627. release = self.create_release(project=self.project, version="1.0.0")
  1628. event = self.store_event(
  1629. data={
  1630. "timestamp": iso_format(before_now(seconds=500)),
  1631. "fingerprint": ["group-1"],
  1632. "release": release.version,
  1633. },
  1634. project_id=self.project.id,
  1635. )
  1636. repo = Repository.objects.create(
  1637. organization_id=self.project.organization_id, name=self.project.name
  1638. )
  1639. user2 = self.create_user()
  1640. self.create_member(organization=self.organization, user=user2)
  1641. author = self.create_commit_author(project=self.project, user=user2)
  1642. # External author
  1643. author2 = CommitAuthor.objects.create(
  1644. external_id="github:santry",
  1645. organization_id=self.project.organization_id,
  1646. email="santry@example.com",
  1647. name="santry",
  1648. )
  1649. commit = Commit.objects.create(
  1650. organization_id=self.project.organization_id,
  1651. repository_id=repo.id,
  1652. key="a" * 40,
  1653. author=author,
  1654. )
  1655. commit2 = Commit.objects.create(
  1656. organization_id=self.project.organization_id,
  1657. repository_id=repo.id,
  1658. key="b" * 40,
  1659. author=author,
  1660. )
  1661. commit3 = Commit.objects.create(
  1662. organization_id=self.project.organization_id,
  1663. repository_id=repo.id,
  1664. key="c" * 40,
  1665. author=author2,
  1666. )
  1667. ReleaseCommit.objects.create(
  1668. organization_id=self.project.organization_id,
  1669. release=release,
  1670. commit=commit,
  1671. order=1,
  1672. )
  1673. ReleaseCommit.objects.create(
  1674. organization_id=self.project.organization_id,
  1675. release=release,
  1676. commit=commit2,
  1677. order=2,
  1678. )
  1679. ReleaseCommit.objects.create(
  1680. organization_id=self.project.organization_id,
  1681. release=release,
  1682. commit=commit3,
  1683. order=3,
  1684. )
  1685. query = "status:unresolved"
  1686. self.login_as(user=self.user)
  1687. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1688. assert response.status_code == 200
  1689. assert len(response.data) == 1
  1690. assert int(response.data[0]["id"]) == event.group.id
  1691. owners = response.data[0]["owners"]
  1692. assert len(owners) == 1
  1693. assert owners[0]["owner"] == f"user:{user2.id}"
  1694. assert owners[0]["type"] == "releaseCommit"
  1695. def test_multiple_committers(self):
  1696. release = self.create_release(project=self.project, version="1.0.0")
  1697. event = self.store_event(
  1698. data={
  1699. "timestamp": iso_format(before_now(seconds=500)),
  1700. "fingerprint": ["group-1"],
  1701. "release": release.version,
  1702. },
  1703. project_id=self.project.id,
  1704. )
  1705. repo = Repository.objects.create(
  1706. organization_id=self.project.organization_id, name=self.project.name
  1707. )
  1708. user2 = self.create_user()
  1709. self.create_member(organization=self.organization, user=user2)
  1710. author = self.create_commit_author(project=self.project, user=user2)
  1711. commit = Commit.objects.create(
  1712. organization_id=self.project.organization_id,
  1713. repository_id=repo.id,
  1714. key="a" * 40,
  1715. author=author,
  1716. )
  1717. ReleaseCommit.objects.create(
  1718. organization_id=self.project.organization_id,
  1719. release=release,
  1720. commit=commit,
  1721. order=1,
  1722. )
  1723. query = "status:unresolved"
  1724. self.login_as(user=self.user)
  1725. # Test with owners
  1726. GroupOwner.objects.create(
  1727. group=event.group,
  1728. project=event.project,
  1729. organization=event.project.organization,
  1730. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1731. user=self.user,
  1732. )
  1733. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1734. assert response.status_code == 200
  1735. assert len(response.data) == 1
  1736. assert int(response.data[0]["id"]) == event.group.id
  1737. owners = response.data[0]["owners"]
  1738. assert len(owners) == 2
  1739. assert owners[0]["owner"] == f"user:{self.user.id}"
  1740. assert owners[0]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.SUSPECT_COMMIT]
  1741. assert owners[1]["owner"] == f"user:{user2.id}"
  1742. assert owners[1]["type"] == "releaseCommit"
  1743. @region_silo_test
  1744. class GroupUpdateTest(APITestCase, SnubaTestCase):
  1745. endpoint = "sentry-api-0-organization-group-index"
  1746. method = "put"
  1747. def setUp(self):
  1748. super().setUp()
  1749. self.min_ago = timezone.now() - timedelta(minutes=1)
  1750. def get_response(self, *args, **kwargs):
  1751. if not args:
  1752. org = self.project.organization.slug
  1753. else:
  1754. org = args[0]
  1755. return super().get_response(org, **kwargs)
  1756. def assertNoResolution(self, group):
  1757. assert not GroupResolution.objects.filter(group=group).exists()
  1758. def test_global_resolve(self):
  1759. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1760. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1761. group3 = self.create_group(status=GroupStatus.IGNORED)
  1762. group4 = self.create_group(
  1763. project=self.create_project(slug="foo"),
  1764. status=GroupStatus.UNRESOLVED,
  1765. )
  1766. self.login_as(user=self.user)
  1767. response = self.get_success_response(
  1768. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1769. )
  1770. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1771. # the previously resolved entry should not be included
  1772. new_group1 = Group.objects.get(id=group1.id)
  1773. assert new_group1.status == GroupStatus.RESOLVED
  1774. assert new_group1.resolved_at is None
  1775. # this wont exist because it wasn't affected
  1776. assert not GroupSubscription.objects.filter(user=self.user, group=new_group1).exists()
  1777. new_group2 = Group.objects.get(id=group2.id)
  1778. assert new_group2.status == GroupStatus.RESOLVED
  1779. assert new_group2.resolved_at is not None
  1780. assert GroupSubscription.objects.filter(
  1781. user=self.user, group=new_group2, is_active=True
  1782. ).exists()
  1783. # the ignored entry should not be included
  1784. new_group3 = Group.objects.get(id=group3.id)
  1785. assert new_group3.status == GroupStatus.IGNORED
  1786. assert new_group3.resolved_at is None
  1787. assert not GroupSubscription.objects.filter(user=self.user, group=new_group3)
  1788. new_group4 = Group.objects.get(id=group4.id)
  1789. assert new_group4.status == GroupStatus.UNRESOLVED
  1790. assert new_group4.resolved_at is None
  1791. assert not GroupSubscription.objects.filter(user=self.user, group=new_group4)
  1792. assert not GroupHistory.objects.filter(
  1793. group=group1, status=GroupHistoryStatus.RESOLVED
  1794. ).exists()
  1795. assert GroupHistory.objects.filter(
  1796. group=group2, status=GroupHistoryStatus.RESOLVED
  1797. ).exists()
  1798. assert not GroupHistory.objects.filter(
  1799. group=group3, status=GroupHistoryStatus.RESOLVED
  1800. ).exists()
  1801. assert not GroupHistory.objects.filter(
  1802. group=group4, status=GroupHistoryStatus.RESOLVED
  1803. ).exists()
  1804. def test_resolve_member(self):
  1805. group = self.create_group(status=GroupStatus.UNRESOLVED)
  1806. member = self.create_user()
  1807. self.create_member(
  1808. organization=self.organization, teams=group.project.teams.all(), user=member
  1809. )
  1810. self.login_as(user=member)
  1811. response = self.get_success_response(
  1812. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1813. )
  1814. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1815. assert response.status_code == 200
  1816. def test_bulk_resolve(self):
  1817. self.login_as(user=self.user)
  1818. for i in range(200):
  1819. self.store_event(
  1820. data={
  1821. "fingerprint": [i],
  1822. "timestamp": iso_format(self.min_ago - timedelta(seconds=i)),
  1823. },
  1824. project_id=self.project.id,
  1825. )
  1826. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  1827. assert len(response.data) == 100
  1828. response = self.get_success_response(qs_params={"status": "unresolved"}, status="resolved")
  1829. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1830. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  1831. assert len(response.data) == 0
  1832. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  1833. def test_resolve_with_integration(self, mock_sync_status_outbound):
  1834. self.login_as(user=self.user)
  1835. org = self.organization
  1836. integration = Integration.objects.create(provider="example", name="Example")
  1837. integration.add_organization(org, self.user)
  1838. event = self.store_event(
  1839. data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id
  1840. )
  1841. group = event.group
  1842. OrganizationIntegration.objects.filter(
  1843. integration_id=integration.id, organization_id=group.organization.id
  1844. ).update(
  1845. config={
  1846. "sync_comments": True,
  1847. "sync_status_outbound": True,
  1848. "sync_status_inbound": True,
  1849. "sync_assignee_outbound": True,
  1850. "sync_assignee_inbound": True,
  1851. }
  1852. )
  1853. external_issue = ExternalIssue.objects.get_or_create(
  1854. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  1855. )[0]
  1856. GroupLink.objects.get_or_create(
  1857. group_id=group.id,
  1858. project_id=group.project_id,
  1859. linked_type=GroupLink.LinkedType.issue,
  1860. linked_id=external_issue.id,
  1861. relationship=GroupLink.Relationship.references,
  1862. )[0]
  1863. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1864. assert len(response.data) == 1
  1865. with self.tasks():
  1866. with self.feature({"organizations:integrations-issue-sync": True}):
  1867. response = self.get_success_response(
  1868. qs_params={"status": "unresolved"}, status="resolved"
  1869. )
  1870. group = Group.objects.get(id=group.id)
  1871. assert group.status == GroupStatus.RESOLVED
  1872. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1873. mock_sync_status_outbound.assert_called_once_with(
  1874. external_issue, True, group.project_id
  1875. )
  1876. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1877. assert len(response.data) == 0
  1878. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  1879. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  1880. release = self.create_release(project=self.project, version="abc")
  1881. group = self.create_group(status=GroupStatus.RESOLVED)
  1882. org = self.organization
  1883. integration = Integration.objects.create(provider="example", name="Example")
  1884. integration.add_organization(org, self.user)
  1885. OrganizationIntegration.objects.filter(
  1886. integration_id=integration.id, organization_id=group.organization.id
  1887. ).update(
  1888. config={
  1889. "sync_comments": True,
  1890. "sync_status_outbound": True,
  1891. "sync_status_inbound": True,
  1892. "sync_assignee_outbound": True,
  1893. "sync_assignee_inbound": True,
  1894. }
  1895. )
  1896. GroupResolution.objects.create(group=group, release=release)
  1897. external_issue = ExternalIssue.objects.get_or_create(
  1898. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  1899. )[0]
  1900. GroupLink.objects.get_or_create(
  1901. group_id=group.id,
  1902. project_id=group.project_id,
  1903. linked_type=GroupLink.LinkedType.issue,
  1904. linked_id=external_issue.id,
  1905. relationship=GroupLink.Relationship.references,
  1906. )[0]
  1907. self.login_as(user=self.user)
  1908. with self.tasks():
  1909. with self.feature({"organizations:integrations-issue-sync": True}):
  1910. response = self.get_success_response(
  1911. qs_params={"id": group.id}, status="unresolved"
  1912. )
  1913. assert response.status_code == 200
  1914. assert response.data == {"status": "unresolved", "statusDetails": {}}
  1915. group = Group.objects.get(id=group.id)
  1916. assert group.status == GroupStatus.UNRESOLVED
  1917. self.assertNoResolution(group)
  1918. assert GroupSubscription.objects.filter(
  1919. user=self.user, group=group, is_active=True
  1920. ).exists()
  1921. mock_sync_status_outbound.assert_called_once_with(
  1922. external_issue, False, group.project_id
  1923. )
  1924. def test_self_assign_issue(self):
  1925. group = self.create_group(status=GroupStatus.UNRESOLVED)
  1926. user = self.user
  1927. uo1 = UserOption.objects.create(key="self_assign_issue", value="1", project=None, user=user)
  1928. self.login_as(user=user)
  1929. response = self.get_success_response(qs_params={"id": group.id}, status="resolved")
  1930. assert response.data["assignedTo"]["id"] == str(user.id)
  1931. assert response.data["assignedTo"]["type"] == "user"
  1932. assert response.data["status"] == "resolved"
  1933. assert GroupAssignee.objects.filter(group=group, user=user).exists()
  1934. assert GroupSubscription.objects.filter(user=user, group=group, is_active=True).exists()
  1935. uo1.delete()
  1936. def test_self_assign_issue_next_release(self):
  1937. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  1938. release.add_project(self.project)
  1939. group = self.create_group(status=GroupStatus.UNRESOLVED)
  1940. uo1 = UserOption.objects.create(
  1941. key="self_assign_issue", value="1", project=None, user=self.user
  1942. )
  1943. self.login_as(user=self.user)
  1944. response = self.get_success_response(
  1945. qs_params={"id": group.id}, status="resolvedInNextRelease"
  1946. )
  1947. assert response.data["status"] == "resolved"
  1948. assert response.data["statusDetails"]["inNextRelease"]
  1949. assert response.data["assignedTo"]["id"] == str(self.user.id)
  1950. assert response.data["assignedTo"]["type"] == "user"
  1951. group = Group.objects.get(id=group.id)
  1952. assert group.status == GroupStatus.RESOLVED
  1953. assert GroupResolution.objects.filter(group=group, release=release).exists()
  1954. assert GroupSubscription.objects.filter(
  1955. user=self.user, group=group, is_active=True
  1956. ).exists()
  1957. activity = Activity.objects.get(
  1958. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  1959. )
  1960. assert activity.data["version"] == ""
  1961. uo1.delete()
  1962. def test_in_semver_projects_group_resolution_stores_current_release_version(self):
  1963. """
  1964. Test that ensures that when we resolve a group in the next release, then
  1965. GroupResolution.current_release_version is set to the latest release associated with a
  1966. Group, when the project follows semantic versioning scheme
  1967. """
  1968. release_1 = self.create_release(version="fake_package@21.1.0")
  1969. release_2 = self.create_release(version="fake_package@21.1.1")
  1970. release_3 = self.create_release(version="fake_package@21.1.2")
  1971. self.store_event(
  1972. data={
  1973. "timestamp": iso_format(before_now(seconds=10)),
  1974. "fingerprint": ["group-1"],
  1975. "release": release_2.version,
  1976. },
  1977. project_id=self.project.id,
  1978. )
  1979. group = self.store_event(
  1980. data={
  1981. "timestamp": iso_format(before_now(seconds=12)),
  1982. "fingerprint": ["group-1"],
  1983. "release": release_1.version,
  1984. },
  1985. project_id=self.project.id,
  1986. ).group
  1987. self.login_as(user=self.user)
  1988. response = self.get_success_response(
  1989. qs_params={"id": group.id}, status="resolvedInNextRelease"
  1990. )
  1991. assert response.data["status"] == "resolved"
  1992. assert response.data["statusDetails"]["inNextRelease"]
  1993. # The current_release_version should be to the latest (in semver) release associated with
  1994. # a group
  1995. grp_resolution = GroupResolution.objects.filter(group=group)
  1996. assert len(grp_resolution) == 1
  1997. grp_resolution = grp_resolution.first()
  1998. assert grp_resolution.current_release_version == release_2.version
  1999. # "resolvedInNextRelease" with semver releases is considered as "resolvedInRelease"
  2000. assert grp_resolution.type == GroupResolution.Type.in_release
  2001. assert grp_resolution.status == GroupResolution.Status.resolved
  2002. # Add release that is between 2 and 3 to ensure that any release after release 2 should
  2003. # not have a resolution
  2004. release_4 = self.create_release(version="fake_package@21.1.1+1")
  2005. for release in [release_1, release_2]:
  2006. assert GroupResolution.has_resolution(group=group, release=release)
  2007. for release in [release_3, release_4]:
  2008. assert not GroupResolution.has_resolution(group=group, release=release)
  2009. # Ensure that Activity has `current_release_version` set on `Resolved in next release`
  2010. activity = Activity.objects.filter(
  2011. group=grp_resolution.group,
  2012. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2013. ident=grp_resolution.id,
  2014. ).first()
  2015. assert "current_release_version" in activity.data
  2016. assert activity.data["current_release_version"] == release_2.version
  2017. def test_in_non_semver_projects_group_resolution_stores_current_release_version(self):
  2018. """
  2019. Test that ensures that when we resolve a group in the next release, then
  2020. GroupResolution.current_release_version is set to the most recent release associated with a
  2021. Group, when the project does not follow semantic versioning scheme
  2022. """
  2023. release_1 = self.create_release(
  2024. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2025. )
  2026. release_2 = self.create_release(version="foobar 2")
  2027. group = self.store_event(
  2028. data={
  2029. "timestamp": iso_format(before_now(seconds=12)),
  2030. "fingerprint": ["group-1"],
  2031. "release": release_1.version,
  2032. },
  2033. project_id=self.project.id,
  2034. ).group
  2035. self.login_as(user=self.user)
  2036. response = self.get_success_response(
  2037. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2038. )
  2039. assert response.data["status"] == "resolved"
  2040. assert response.data["statusDetails"]["inNextRelease"]
  2041. # Add a new release that is between 1 and 2, to make sure that if a the same issue/group
  2042. # occurs in that issue, then it should not have a resolution
  2043. release_3 = self.create_release(
  2044. date_added=timezone.now() - timedelta(minutes=30), version="foobar 3"
  2045. )
  2046. grp_resolution = GroupResolution.objects.filter(group=group)
  2047. assert len(grp_resolution) == 1
  2048. assert grp_resolution[0].current_release_version == release_1.version
  2049. assert GroupResolution.has_resolution(group=group, release=release_1)
  2050. for release in [release_2, release_3]:
  2051. assert not GroupResolution.has_resolution(group=group, release=release)
  2052. def test_in_non_semver_projects_store_actual_current_release_version_not_cached_version(self):
  2053. """
  2054. Test that ensures that the current_release_version is actually the latest version
  2055. associated with a group, not the cached version because currently
  2056. `group.get_last_release` fetches the latest release associated with a group and caches
  2057. that value, and we don't want to cache that value when resolving in next release in case a
  2058. new release appears to be associated with a group because if we store the cached rather
  2059. than the actual latest release, we might have unexpected results with the regression
  2060. algorithm
  2061. """
  2062. release_1 = self.create_release(
  2063. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2064. )
  2065. release_2 = self.create_release(version="foobar 2")
  2066. group = self.store_event(
  2067. data={
  2068. "timestamp": iso_format(before_now(seconds=12)),
  2069. "fingerprint": ["group-1"],
  2070. "release": release_1.version,
  2071. },
  2072. project_id=self.project.id,
  2073. ).group
  2074. # Call this function to cache the `last_seen` release to release_1
  2075. # i.e. Set the first last observed by Sentry
  2076. assert group.get_last_release() == release_1.version
  2077. self.login_as(user=self.user)
  2078. self.store_event(
  2079. data={
  2080. "timestamp": iso_format(before_now(seconds=0)),
  2081. "fingerprint": ["group-1"],
  2082. "release": release_2.version,
  2083. },
  2084. project_id=self.project.id,
  2085. )
  2086. # Cached (i.e. first last observed release by Sentry) is returned here since `use_cache`
  2087. # is set to its default of `True`
  2088. assert Group.objects.get(id=group.id).get_last_release() == release_1.version
  2089. response = self.get_success_response(
  2090. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2091. )
  2092. assert response.data["status"] == "resolved"
  2093. assert response.data["statusDetails"]["inNextRelease"]
  2094. # Changes here to release_2 and actual latest because `resolvedInNextRelease`,
  2095. # sets `use_cache` to False when fetching the last release associated with a group
  2096. assert Group.objects.get(id=group.id).get_last_release() == release_2.version
  2097. grp_resolution = GroupResolution.objects.filter(group=group)
  2098. assert len(grp_resolution) == 1
  2099. assert grp_resolution[0].current_release_version == release_2.version
  2100. def test_in_non_semver_projects_resolved_in_next_release_is_equated_to_in_release(self):
  2101. """
  2102. Test that ensures that if we basically know the next release when clicking on Resolved
  2103. In Next Release because that release exists, then we can short circuit setting
  2104. GroupResolution to type "inNextRelease", and then having `clear_expired_resolutions` run
  2105. once a new release is created to convert GroupResolution to in_release and set Activity.
  2106. Basically we treat "ResolvedInNextRelease" as "ResolvedInRelease" when there is a release
  2107. that was created after the last release associated with the group being resolved
  2108. """
  2109. release_1 = self.create_release(
  2110. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2111. )
  2112. release_2 = self.create_release(version="foobar 2")
  2113. self.create_release(version="foobar 3")
  2114. group = self.store_event(
  2115. data={
  2116. "timestamp": iso_format(before_now(seconds=12)),
  2117. "fingerprint": ["group-1"],
  2118. "release": release_1.version,
  2119. },
  2120. project_id=self.project.id,
  2121. ).group
  2122. self.login_as(user=self.user)
  2123. response = self.get_success_response(
  2124. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2125. )
  2126. assert response.data["status"] == "resolved"
  2127. assert response.data["statusDetails"]["inNextRelease"]
  2128. grp_resolution = GroupResolution.objects.filter(group=group)
  2129. assert len(grp_resolution) == 1
  2130. grp_resolution = grp_resolution[0]
  2131. assert grp_resolution.current_release_version == release_1.version
  2132. assert grp_resolution.release.id == release_2.id
  2133. assert grp_resolution.type == GroupResolution.Type.in_release
  2134. assert grp_resolution.status == GroupResolution.Status.resolved
  2135. activity = Activity.objects.filter(
  2136. group=grp_resolution.group,
  2137. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2138. ident=grp_resolution.id,
  2139. ).first()
  2140. assert activity.data["version"] == release_2.version
  2141. def test_selective_status_update(self):
  2142. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2143. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2144. group3 = self.create_group(status=GroupStatus.IGNORED)
  2145. group4 = self.create_group(
  2146. project=self.create_project(slug="foo"),
  2147. status=GroupStatus.UNRESOLVED,
  2148. )
  2149. self.login_as(user=self.user)
  2150. with self.feature("organizations:global-views"):
  2151. response = self.get_success_response(
  2152. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, status="resolved"
  2153. )
  2154. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2155. new_group1 = Group.objects.get(id=group1.id)
  2156. assert new_group1.resolved_at is not None
  2157. assert new_group1.status == GroupStatus.RESOLVED
  2158. new_group2 = Group.objects.get(id=group2.id)
  2159. assert new_group2.resolved_at is not None
  2160. assert new_group2.status == GroupStatus.RESOLVED
  2161. assert GroupSubscription.objects.filter(
  2162. user=self.user, group=new_group2, is_active=True
  2163. ).exists()
  2164. new_group3 = Group.objects.get(id=group3.id)
  2165. assert new_group3.resolved_at is None
  2166. assert new_group3.status == GroupStatus.IGNORED
  2167. new_group4 = Group.objects.get(id=group4.id)
  2168. assert new_group4.resolved_at is None
  2169. assert new_group4.status == GroupStatus.UNRESOLVED
  2170. def test_set_resolved_in_current_release(self):
  2171. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2172. release.add_project(self.project)
  2173. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2174. self.login_as(user=self.user)
  2175. response = self.get_success_response(
  2176. qs_params={"id": group.id}, status="resolved", statusDetails={"inRelease": "latest"}
  2177. )
  2178. assert response.data["status"] == "resolved"
  2179. assert response.data["statusDetails"]["inRelease"] == release.version
  2180. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2181. group = Group.objects.get(id=group.id)
  2182. assert group.status == GroupStatus.RESOLVED
  2183. resolution = GroupResolution.objects.get(group=group)
  2184. assert resolution.release == release
  2185. assert resolution.type == GroupResolution.Type.in_release
  2186. assert resolution.status == GroupResolution.Status.resolved
  2187. assert resolution.actor_id == self.user.id
  2188. assert GroupSubscription.objects.filter(
  2189. user=self.user, group=group, is_active=True
  2190. ).exists()
  2191. activity = Activity.objects.get(
  2192. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2193. )
  2194. assert activity.data["version"] == release.version
  2195. assert GroupHistory.objects.filter(
  2196. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2197. ).exists()
  2198. def test_set_resolved_in_explicit_release(self):
  2199. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2200. release.add_project(self.project)
  2201. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  2202. release2.add_project(self.project)
  2203. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2204. self.login_as(user=self.user)
  2205. response = self.get_success_response(
  2206. qs_params={"id": group.id},
  2207. status="resolved",
  2208. statusDetails={"inRelease": release.version},
  2209. )
  2210. assert response.data["status"] == "resolved"
  2211. assert response.data["statusDetails"]["inRelease"] == release.version
  2212. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2213. assert "activity" in response.data
  2214. group = Group.objects.get(id=group.id)
  2215. assert group.status == GroupStatus.RESOLVED
  2216. resolution = GroupResolution.objects.get(group=group)
  2217. assert resolution.release == release
  2218. assert resolution.type == GroupResolution.Type.in_release
  2219. assert resolution.status == GroupResolution.Status.resolved
  2220. assert resolution.actor_id == self.user.id
  2221. assert GroupSubscription.objects.filter(
  2222. user=self.user, group=group, is_active=True
  2223. ).exists()
  2224. activity = Activity.objects.get(
  2225. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2226. )
  2227. assert activity.data["version"] == release.version
  2228. def test_in_semver_projects_set_resolved_in_explicit_release(self):
  2229. release_1 = self.create_release(version="fake_package@3.0.0")
  2230. release_2 = self.create_release(version="fake_package@2.0.0")
  2231. release_3 = self.create_release(version="fake_package@3.0.1")
  2232. group = self.store_event(
  2233. data={
  2234. "timestamp": iso_format(before_now(seconds=10)),
  2235. "fingerprint": ["group-1"],
  2236. "release": release_1.version,
  2237. },
  2238. project_id=self.project.id,
  2239. ).group
  2240. self.login_as(user=self.user)
  2241. response = self.get_success_response(
  2242. qs_params={"id": group.id},
  2243. status="resolved",
  2244. statusDetails={"inRelease": release_1.version},
  2245. )
  2246. assert response.data["status"] == "resolved"
  2247. assert response.data["statusDetails"]["inRelease"] == release_1.version
  2248. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2249. assert "activity" in response.data
  2250. group = Group.objects.get(id=group.id)
  2251. assert group.status == GroupStatus.RESOLVED
  2252. resolution = GroupResolution.objects.get(group=group)
  2253. assert resolution.release == release_1
  2254. assert resolution.type == GroupResolution.Type.in_release
  2255. assert resolution.status == GroupResolution.Status.resolved
  2256. assert resolution.actor_id == self.user.id
  2257. assert GroupSubscription.objects.filter(
  2258. user=self.user, group=group, is_active=True
  2259. ).exists()
  2260. activity = Activity.objects.get(
  2261. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2262. )
  2263. assert activity.data["version"] == release_1.version
  2264. assert GroupResolution.has_resolution(group=group, release=release_2)
  2265. assert not GroupResolution.has_resolution(group=group, release=release_3)
  2266. def test_set_resolved_in_next_release(self):
  2267. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2268. release.add_project(self.project)
  2269. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2270. self.login_as(user=self.user)
  2271. response = self.get_success_response(
  2272. qs_params={"id": group.id}, status="resolved", statusDetails={"inNextRelease": True}
  2273. )
  2274. assert response.data["status"] == "resolved"
  2275. assert response.data["statusDetails"]["inNextRelease"]
  2276. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2277. assert "activity" in response.data
  2278. group = Group.objects.get(id=group.id)
  2279. assert group.status == GroupStatus.RESOLVED
  2280. resolution = GroupResolution.objects.get(group=group)
  2281. assert resolution.release == release
  2282. assert resolution.type == GroupResolution.Type.in_next_release
  2283. assert resolution.status == GroupResolution.Status.pending
  2284. assert resolution.actor_id == self.user.id
  2285. assert GroupSubscription.objects.filter(
  2286. user=self.user, group=group, is_active=True
  2287. ).exists()
  2288. activity = Activity.objects.get(
  2289. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2290. )
  2291. assert activity.data["version"] == ""
  2292. def test_set_resolved_in_next_release_legacy(self):
  2293. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2294. release.add_project(self.project)
  2295. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2296. self.login_as(user=self.user)
  2297. response = self.get_success_response(
  2298. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2299. )
  2300. assert response.data["status"] == "resolved"
  2301. assert response.data["statusDetails"]["inNextRelease"]
  2302. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2303. assert "activity" in response.data
  2304. group = Group.objects.get(id=group.id)
  2305. assert group.status == GroupStatus.RESOLVED
  2306. resolution = GroupResolution.objects.get(group=group)
  2307. assert resolution.release == release
  2308. assert resolution.type == GroupResolution.Type.in_next_release
  2309. assert resolution.status == GroupResolution.Status.pending
  2310. assert resolution.actor_id == self.user.id
  2311. assert GroupSubscription.objects.filter(
  2312. user=self.user, group=group, is_active=True
  2313. ).exists()
  2314. assert GroupHistory.objects.filter(
  2315. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2316. ).exists()
  2317. activity = Activity.objects.get(
  2318. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2319. )
  2320. assert activity.data["version"] == ""
  2321. def test_set_resolved_in_explicit_commit_unreleased(self):
  2322. repo = self.create_repo(project=self.project, name=self.project.name)
  2323. commit = self.create_commit(project=self.project, repo=repo)
  2324. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2325. self.login_as(user=self.user)
  2326. response = self.get_success_response(
  2327. qs_params={"id": group.id},
  2328. status="resolved",
  2329. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2330. )
  2331. assert response.data["status"] == "resolved"
  2332. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2333. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2334. assert "activity" not in response.data
  2335. group = Group.objects.get(id=group.id)
  2336. assert group.status == GroupStatus.RESOLVED
  2337. link = GroupLink.objects.get(group_id=group.id)
  2338. assert link.linked_type == GroupLink.LinkedType.commit
  2339. assert link.relationship == GroupLink.Relationship.resolves
  2340. assert link.linked_id == commit.id
  2341. assert GroupSubscription.objects.filter(
  2342. user=self.user, group=group, is_active=True
  2343. ).exists()
  2344. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2345. assert activity.data["commit"] == commit.id
  2346. assert GroupHistory.objects.filter(
  2347. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2348. ).exists()
  2349. def test_set_resolved_in_explicit_commit_released(self):
  2350. release = self.create_release(project=self.project)
  2351. repo = self.create_repo(project=self.project, name=self.project.name)
  2352. commit = self.create_commit(project=self.project, repo=repo, release=release)
  2353. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2354. self.login_as(user=self.user)
  2355. response = self.get_success_response(
  2356. qs_params={"id": group.id},
  2357. status="resolved",
  2358. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2359. )
  2360. assert response.data["status"] == "resolved"
  2361. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2362. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2363. assert "activity" in response.data
  2364. group = Group.objects.get(id=group.id)
  2365. assert group.status == GroupStatus.RESOLVED
  2366. link = GroupLink.objects.get(group_id=group.id)
  2367. assert link.project_id == self.project.id
  2368. assert link.linked_type == GroupLink.LinkedType.commit
  2369. assert link.relationship == GroupLink.Relationship.resolves
  2370. assert link.linked_id == commit.id
  2371. assert GroupSubscription.objects.filter(
  2372. user=self.user, group=group, is_active=True
  2373. ).exists()
  2374. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2375. assert activity.data["commit"] == commit.id
  2376. resolution = GroupResolution.objects.get(group=group)
  2377. assert resolution.type == GroupResolution.Type.in_release
  2378. assert resolution.status == GroupResolution.Status.resolved
  2379. assert GroupHistory.objects.filter(
  2380. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2381. ).exists()
  2382. def test_set_resolved_in_explicit_commit_missing(self):
  2383. repo = self.create_repo(project=self.project, name=self.project.name)
  2384. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2385. self.login_as(user=self.user)
  2386. response = self.get_response(
  2387. qs_params={"id": group.id},
  2388. status="resolved",
  2389. statusDetails={"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  2390. )
  2391. assert response.status_code == 400
  2392. assert (
  2393. response.data["statusDetails"]["inCommit"]["commit"][0]
  2394. == "Unable to find the given commit."
  2395. )
  2396. assert not GroupHistory.objects.filter(
  2397. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2398. ).exists()
  2399. def test_set_unresolved(self):
  2400. release = self.create_release(project=self.project, version="abc")
  2401. group = self.create_group(status=GroupStatus.RESOLVED)
  2402. GroupResolution.objects.create(group=group, release=release)
  2403. self.login_as(user=self.user)
  2404. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2405. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2406. group = Group.objects.get(id=group.id)
  2407. assert group.status == GroupStatus.UNRESOLVED
  2408. assert GroupHistory.objects.filter(
  2409. group=group, status=GroupHistoryStatus.UNRESOLVED
  2410. ).exists()
  2411. self.assertNoResolution(group)
  2412. assert GroupSubscription.objects.filter(
  2413. user=self.user, group=group, is_active=True
  2414. ).exists()
  2415. def test_set_unresolved_on_snooze(self):
  2416. group = self.create_group(status=GroupStatus.IGNORED)
  2417. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  2418. self.login_as(user=self.user)
  2419. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2420. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2421. group = Group.objects.get(id=group.id)
  2422. assert group.status == GroupStatus.UNRESOLVED
  2423. assert GroupHistory.objects.filter(
  2424. group=group, status=GroupHistoryStatus.UNRESOLVED
  2425. ).exists()
  2426. def test_basic_ignore(self):
  2427. group = self.create_group(status=GroupStatus.RESOLVED)
  2428. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  2429. self.login_as(user=self.user)
  2430. assert not GroupHistory.objects.filter(
  2431. group=group, status=GroupHistoryStatus.IGNORED
  2432. ).exists()
  2433. response = self.get_success_response(qs_params={"id": group.id}, status="ignored")
  2434. # existing snooze objects should be cleaned up
  2435. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  2436. group = Group.objects.get(id=group.id)
  2437. assert group.status == GroupStatus.IGNORED
  2438. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.IGNORED).exists()
  2439. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  2440. def test_snooze_duration(self):
  2441. group = self.create_group(status=GroupStatus.RESOLVED)
  2442. self.login_as(user=self.user)
  2443. response = self.get_success_response(
  2444. qs_params={"id": group.id}, status="ignored", ignoreDuration=30
  2445. )
  2446. snooze = GroupSnooze.objects.get(group=group)
  2447. snooze.until = snooze.until
  2448. now = timezone.now()
  2449. assert snooze.count is None
  2450. assert snooze.until > now + timedelta(minutes=29)
  2451. assert snooze.until < now + timedelta(minutes=31)
  2452. assert snooze.user_count is None
  2453. assert snooze.user_window is None
  2454. assert snooze.window is None
  2455. response.data["statusDetails"]["ignoreUntil"] = response.data["statusDetails"][
  2456. "ignoreUntil"
  2457. ]
  2458. assert response.data["status"] == "ignored"
  2459. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2460. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2461. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2462. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2463. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2464. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2465. def test_snooze_count(self):
  2466. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  2467. self.login_as(user=self.user)
  2468. response = self.get_success_response(
  2469. qs_params={"id": group.id}, status="ignored", ignoreCount=100
  2470. )
  2471. snooze = GroupSnooze.objects.get(group=group)
  2472. assert snooze.count == 100
  2473. assert snooze.until is None
  2474. assert snooze.user_count is None
  2475. assert snooze.user_window is None
  2476. assert snooze.window is None
  2477. assert snooze.state["times_seen"] == 1
  2478. assert response.data["status"] == "ignored"
  2479. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2480. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2481. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2482. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2483. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2484. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2485. def test_snooze_user_count(self):
  2486. event = {}
  2487. for i in range(10):
  2488. event = self.store_event(
  2489. data={
  2490. "fingerprint": ["put-me-in-group-1"],
  2491. "user": {"id": str(i)},
  2492. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  2493. },
  2494. project_id=self.project.id,
  2495. )
  2496. group = Group.objects.get(id=event.group.id)
  2497. group.status = GroupStatus.RESOLVED
  2498. group.save()
  2499. self.login_as(user=self.user)
  2500. response = self.get_success_response(
  2501. qs_params={"id": group.id}, status="ignored", ignoreUserCount=10
  2502. )
  2503. snooze = GroupSnooze.objects.get(group=group)
  2504. assert snooze.count is None
  2505. assert snooze.until is None
  2506. assert snooze.user_count == 10
  2507. assert snooze.user_window is None
  2508. assert snooze.window is None
  2509. assert snooze.state["users_seen"] == 10
  2510. assert response.data["status"] == "ignored"
  2511. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2512. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2513. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2514. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2515. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2516. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2517. def test_set_bookmarked(self):
  2518. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2519. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2520. group3 = self.create_group(status=GroupStatus.IGNORED)
  2521. group4 = self.create_group(
  2522. project=self.create_project(slug="foo"),
  2523. status=GroupStatus.UNRESOLVED,
  2524. )
  2525. self.login_as(user=self.user)
  2526. with self.feature("organizations:global-views"):
  2527. response = self.get_success_response(
  2528. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isBookmarked="true"
  2529. )
  2530. assert response.data == {"isBookmarked": True}
  2531. bookmark1 = GroupBookmark.objects.filter(group=group1, user=self.user)
  2532. assert bookmark1.exists()
  2533. assert GroupSubscription.objects.filter(
  2534. user=self.user, group=group1, is_active=True
  2535. ).exists()
  2536. bookmark2 = GroupBookmark.objects.filter(group=group2, user=self.user)
  2537. assert bookmark2.exists()
  2538. assert GroupSubscription.objects.filter(
  2539. user=self.user, group=group2, is_active=True
  2540. ).exists()
  2541. bookmark3 = GroupBookmark.objects.filter(group=group3, user=self.user)
  2542. assert not bookmark3.exists()
  2543. bookmark4 = GroupBookmark.objects.filter(group=group4, user=self.user)
  2544. assert not bookmark4.exists()
  2545. def test_subscription(self):
  2546. group1 = self.create_group()
  2547. group2 = self.create_group()
  2548. group3 = self.create_group()
  2549. group4 = self.create_group(project=self.create_project(slug="foo"))
  2550. self.login_as(user=self.user)
  2551. with self.feature("organizations:global-views"):
  2552. response = self.get_success_response(
  2553. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isSubscribed="true"
  2554. )
  2555. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  2556. assert GroupSubscription.objects.filter(
  2557. group=group1, user=self.user, is_active=True
  2558. ).exists()
  2559. assert GroupSubscription.objects.filter(
  2560. group=group2, user=self.user, is_active=True
  2561. ).exists()
  2562. assert not GroupSubscription.objects.filter(group=group3, user=self.user).exists()
  2563. assert not GroupSubscription.objects.filter(group=group4, user=self.user).exists()
  2564. def test_set_public(self):
  2565. group1 = self.create_group()
  2566. group2 = self.create_group()
  2567. self.login_as(user=self.user)
  2568. response = self.get_success_response(
  2569. qs_params={"id": [group1.id, group2.id]}, isPublic="true"
  2570. )
  2571. assert response.data["isPublic"] is True
  2572. assert "shareId" in response.data
  2573. new_group1 = Group.objects.get(id=group1.id)
  2574. assert bool(new_group1.get_share_id())
  2575. new_group2 = Group.objects.get(id=group2.id)
  2576. assert bool(new_group2.get_share_id())
  2577. def test_set_private(self):
  2578. group1 = self.create_group()
  2579. group2 = self.create_group()
  2580. # Manually mark them as shared
  2581. for g in group1, group2:
  2582. GroupShare.objects.create(project_id=g.project_id, group=g)
  2583. assert bool(g.get_share_id())
  2584. self.login_as(user=self.user)
  2585. response = self.get_success_response(
  2586. qs_params={"id": [group1.id, group2.id]}, isPublic="false"
  2587. )
  2588. assert response.data == {"isPublic": False, "shareId": None}
  2589. new_group1 = Group.objects.get(id=group1.id)
  2590. assert not bool(new_group1.get_share_id())
  2591. new_group2 = Group.objects.get(id=group2.id)
  2592. assert not bool(new_group2.get_share_id())
  2593. def test_set_has_seen(self):
  2594. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2595. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2596. group3 = self.create_group(status=GroupStatus.IGNORED)
  2597. group4 = self.create_group(
  2598. project=self.create_project(slug="foo"),
  2599. status=GroupStatus.UNRESOLVED,
  2600. )
  2601. self.login_as(user=self.user)
  2602. with self.feature("organizations:global-views"):
  2603. response = self.get_success_response(
  2604. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, hasSeen="true"
  2605. )
  2606. assert response.data == {"hasSeen": True}
  2607. r1 = GroupSeen.objects.filter(group=group1, user=self.user)
  2608. assert r1.exists()
  2609. r2 = GroupSeen.objects.filter(group=group2, user=self.user)
  2610. assert r2.exists()
  2611. r3 = GroupSeen.objects.filter(group=group3, user=self.user)
  2612. assert not r3.exists()
  2613. r4 = GroupSeen.objects.filter(group=group4, user=self.user)
  2614. assert not r4.exists()
  2615. @patch("sentry.api.helpers.group_index.update.uuid4")
  2616. @patch("sentry.api.helpers.group_index.update.merge_groups")
  2617. @patch("sentry.api.helpers.group_index.update.eventstream")
  2618. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  2619. eventstream_state = object()
  2620. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2621. mock_uuid4.return_value = self.get_mock_uuid()
  2622. group1 = self.create_group(times_seen=1)
  2623. group2 = self.create_group(times_seen=50)
  2624. group3 = self.create_group(times_seen=2)
  2625. self.create_group()
  2626. self.login_as(user=self.user)
  2627. response = self.get_success_response(
  2628. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2629. )
  2630. assert response.data["merge"]["parent"] == str(group2.id)
  2631. assert sorted(response.data["merge"]["children"]) == sorted(
  2632. [str(group1.id), str(group3.id)]
  2633. )
  2634. mock_eventstream.start_merge.assert_called_once_with(
  2635. group1.project_id, [group3.id, group1.id], group2.id
  2636. )
  2637. assert len(merge_groups.mock_calls) == 1
  2638. merge_groups.delay.assert_any_call(
  2639. from_object_ids=[group3.id, group1.id],
  2640. to_object_id=group2.id,
  2641. transaction_id="abc123",
  2642. eventstream_state=eventstream_state,
  2643. )
  2644. @patch("sentry.api.helpers.group_index.update.uuid4")
  2645. @patch("sentry.api.helpers.group_index.update.merge_groups")
  2646. @patch("sentry.api.helpers.group_index.update.eventstream")
  2647. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  2648. eventstream_state = object()
  2649. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2650. mock_uuid4.return_value = self.get_mock_uuid()
  2651. group1 = self.create_group(times_seen=1, type=GroupType.PERFORMANCE_SLOW_SPAN.value)
  2652. group2 = self.create_group(times_seen=50, type=GroupType.PERFORMANCE_SLOW_SPAN.value)
  2653. group3 = self.create_group(times_seen=2, type=GroupType.PERFORMANCE_SLOW_SPAN.value)
  2654. self.create_group()
  2655. self.login_as(user=self.user)
  2656. response = self.get_error_response(
  2657. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2658. )
  2659. assert response.status_code == 400, response.content
  2660. def test_assign(self):
  2661. group1 = self.create_group(is_public=True)
  2662. group2 = self.create_group(is_public=True)
  2663. user = self.user
  2664. self.login_as(user=user)
  2665. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo=user.username)
  2666. assert response.data["assignedTo"]["id"] == str(user.id)
  2667. assert response.data["assignedTo"]["type"] == "user"
  2668. assert GroupAssignee.objects.filter(group=group1, user=user).exists()
  2669. assert GroupHistory.objects.filter(
  2670. group=group1, status=GroupHistoryStatus.ASSIGNED
  2671. ).exists()
  2672. assert not GroupAssignee.objects.filter(group=group2, user=user).exists()
  2673. assert (
  2674. Activity.objects.filter(
  2675. group=group1, user=user, type=ActivityType.ASSIGNED.value
  2676. ).count()
  2677. == 1
  2678. )
  2679. assert GroupSubscription.objects.filter(user=user, group=group1, is_active=True).exists()
  2680. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo="")
  2681. assert response.data["assignedTo"] is None
  2682. assert not GroupAssignee.objects.filter(group=group1, user=user).exists()
  2683. assert GroupHistory.objects.filter(
  2684. group=group1, status=GroupHistoryStatus.UNASSIGNED
  2685. ).exists()
  2686. def test_assign_non_member(self):
  2687. group = self.create_group(is_public=True)
  2688. member = self.user
  2689. non_member = self.create_user("bar@example.com")
  2690. self.login_as(user=member)
  2691. response = self.get_response(qs_params={"id": group.id}, assignedTo=non_member.username)
  2692. assert not GroupHistory.objects.filter(
  2693. group=group, status=GroupHistoryStatus.ASSIGNED
  2694. ).exists()
  2695. assert response.status_code == 400, response.content
  2696. def test_assign_team(self):
  2697. self.login_as(user=self.user)
  2698. group = self.create_group()
  2699. other_member = self.create_user("bar@example.com")
  2700. team = self.create_team(
  2701. organization=group.project.organization, members=[self.user, other_member]
  2702. )
  2703. group.project.add_team(team)
  2704. assert not GroupHistory.objects.filter(
  2705. group=group, status=GroupHistoryStatus.ASSIGNED
  2706. ).exists()
  2707. response = self.get_success_response(
  2708. qs_params={"id": group.id}, assignedTo=f"team:{team.id}"
  2709. )
  2710. assert response.data["assignedTo"]["id"] == str(team.id)
  2711. assert response.data["assignedTo"]["type"] == "team"
  2712. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.ASSIGNED).exists()
  2713. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  2714. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  2715. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  2716. response = self.get_success_response(qs_params={"id": group.id}, assignedTo="")
  2717. assert response.data["assignedTo"] is None
  2718. assert GroupHistory.objects.filter(
  2719. group=group, status=GroupHistoryStatus.UNASSIGNED
  2720. ).exists()
  2721. def test_discard(self):
  2722. group1 = self.create_group(is_public=True)
  2723. group2 = self.create_group(is_public=True)
  2724. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  2725. user = self.user
  2726. self.login_as(user=user)
  2727. with self.tasks():
  2728. with self.feature("projects:discard-groups"):
  2729. response = self.get_response(qs_params={"id": group1.id}, discard=True)
  2730. assert response.status_code == 204
  2731. assert not Group.objects.filter(id=group1.id).exists()
  2732. assert Group.objects.filter(id=group2.id).exists()
  2733. assert GroupHash.objects.filter(id=group_hash.id).exists()
  2734. tombstone = GroupTombstone.objects.get(
  2735. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  2736. )
  2737. assert tombstone.message == group1.message
  2738. assert tombstone.culprit == group1.culprit
  2739. assert tombstone.project == group1.project
  2740. assert tombstone.data == group1.data
  2741. @override_settings(SENTRY_SELF_HOSTED=False)
  2742. def test_ratelimit(self):
  2743. self.login_as(user=self.user)
  2744. with freeze_time("2000-01-01"):
  2745. for i in range(5):
  2746. self.get_success_response()
  2747. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  2748. def test_set_inbox(self):
  2749. group1 = self.create_group()
  2750. group2 = self.create_group()
  2751. self.login_as(user=self.user)
  2752. response = self.get_success_response(qs_params={"id": [group1.id, group2.id]}, inbox="true")
  2753. assert response.data == {"inbox": True}
  2754. assert GroupInbox.objects.filter(group=group1).exists()
  2755. assert GroupInbox.objects.filter(group=group2).exists()
  2756. assert not GroupHistory.objects.filter(
  2757. group=group1, status=GroupHistoryStatus.REVIEWED
  2758. ).exists()
  2759. assert not GroupHistory.objects.filter(
  2760. group=group2, status=GroupHistoryStatus.REVIEWED
  2761. ).exists()
  2762. response = self.get_success_response(qs_params={"id": [group2.id]}, inbox="false")
  2763. assert response.data == {"inbox": False}
  2764. assert GroupInbox.objects.filter(group=group1).exists()
  2765. assert not GroupHistory.objects.filter(
  2766. group=group1, status=GroupHistoryStatus.REVIEWED
  2767. ).exists()
  2768. assert GroupHistory.objects.filter(
  2769. group=group2, status=GroupHistoryStatus.REVIEWED
  2770. ).exists()
  2771. assert not GroupInbox.objects.filter(group=group2).exists()
  2772. def test_set_resolved_inbox(self):
  2773. group1 = self.create_group()
  2774. group2 = self.create_group()
  2775. self.login_as(user=self.user)
  2776. response = self.get_success_response(
  2777. qs_params={"id": [group1.id, group2.id]}, status="resolved"
  2778. )
  2779. assert response.data["inbox"] is None
  2780. assert not GroupInbox.objects.filter(group=group1).exists()
  2781. assert not GroupInbox.objects.filter(group=group2).exists()
  2782. self.get_success_response(qs_params={"id": [group2.id]}, status="unresolved")
  2783. assert not GroupInbox.objects.filter(group=group1).exists()
  2784. assert not GroupInbox.objects.filter(group=group2).exists()
  2785. assert not GroupHistory.objects.filter(
  2786. group=group1, status=GroupHistoryStatus.UNRESOLVED
  2787. ).exists()
  2788. assert GroupHistory.objects.filter(
  2789. group=group2, status=GroupHistoryStatus.UNRESOLVED
  2790. ).exists()
  2791. @region_silo_test
  2792. class GroupDeleteTest(APITestCase, SnubaTestCase):
  2793. endpoint = "sentry-api-0-organization-group-index"
  2794. method = "delete"
  2795. def get_response(self, *args, **kwargs):
  2796. if not args:
  2797. org = self.project.organization.slug
  2798. else:
  2799. org = args[0]
  2800. return super().get_response(org, **kwargs)
  2801. @patch("sentry.api.helpers.group_index.delete.eventstream")
  2802. @patch("sentry.eventstream")
  2803. def test_delete_by_id(self, mock_eventstream_task, mock_eventstream_api):
  2804. eventstream_state = {"event_stream_state": uuid4()}
  2805. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  2806. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2807. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2808. group3 = self.create_group(status=GroupStatus.IGNORED)
  2809. group4 = self.create_group(
  2810. project=self.create_project(slug="foo"),
  2811. status=GroupStatus.UNRESOLVED,
  2812. )
  2813. hashes = []
  2814. for g in group1, group2, group3, group4:
  2815. hash = uuid4().hex
  2816. hashes.append(hash)
  2817. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2818. self.login_as(user=self.user)
  2819. with self.feature("organizations:global-views"):
  2820. response = self.get_response(
  2821. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2822. )
  2823. mock_eventstream_api.start_delete_groups.assert_called_once_with(
  2824. group1.project_id, [group1.id, group2.id]
  2825. )
  2826. assert response.status_code == 204
  2827. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  2828. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2829. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  2830. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2831. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  2832. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2833. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  2834. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2835. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  2836. with self.tasks():
  2837. with self.feature("organizations:global-views"):
  2838. response = self.get_response(
  2839. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2840. )
  2841. mock_eventstream_task.end_delete_groups.assert_called_once_with(eventstream_state)
  2842. assert response.status_code == 204
  2843. assert not Group.objects.filter(id=group1.id).exists()
  2844. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2845. assert not Group.objects.filter(id=group2.id).exists()
  2846. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2847. assert Group.objects.filter(id=group3.id).exists()
  2848. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2849. assert Group.objects.filter(id=group4.id).exists()
  2850. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2851. @patch("sentry.api.helpers.group_index.delete.eventstream")
  2852. @patch("sentry.eventstream")
  2853. def test_delete_performance_issue_by_id(self, mock_eventstream_task, mock_eventstream_api):
  2854. eventstream_state = {"event_stream_state": uuid4()}
  2855. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  2856. group1 = self.create_group(
  2857. status=GroupStatus.RESOLVED, type=GroupType.PERFORMANCE_SLOW_SPAN.value
  2858. )
  2859. group2 = self.create_group(
  2860. status=GroupStatus.UNRESOLVED, type=GroupType.PERFORMANCE_SLOW_SPAN.value
  2861. )
  2862. hashes = []
  2863. for g in group1, group2:
  2864. hash = uuid4().hex
  2865. hashes.append(hash)
  2866. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2867. self.login_as(user=self.user)
  2868. with self.feature("organizations:global-views"):
  2869. response = self.get_response(qs_params={"id": [group1.id, group2.id]})
  2870. assert response.status_code == 400
  2871. assert Group.objects.filter(id=group1.id).exists()
  2872. assert GroupHash.objects.filter(group_id=group1.id).exists()
  2873. assert Group.objects.filter(id=group2.id).exists()
  2874. assert GroupHash.objects.filter(group_id=group2.id).exists()
  2875. def test_bulk_delete(self):
  2876. groups = []
  2877. for i in range(10, 41):
  2878. groups.append(
  2879. self.create_group(
  2880. project=self.project,
  2881. status=GroupStatus.RESOLVED,
  2882. )
  2883. )
  2884. hashes = []
  2885. for group in groups:
  2886. hash = uuid4().hex
  2887. hashes.append(hash)
  2888. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  2889. self.login_as(user=self.user)
  2890. # if query is '' it defaults to is:unresolved
  2891. response = self.get_response(qs_params={"query": ""})
  2892. assert response.status_code == 204
  2893. for group in groups:
  2894. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  2895. assert not GroupHash.objects.filter(group_id=group.id).exists()
  2896. Group.objects.filter(id__in=[group.id for group in groups]).update(
  2897. status=GroupStatus.UNRESOLVED
  2898. )
  2899. with self.tasks():
  2900. response = self.get_response(qs_params={"query": ""})
  2901. assert response.status_code == 204
  2902. for group in groups:
  2903. assert not Group.objects.filter(id=group.id).exists()
  2904. assert not GroupHash.objects.filter(group_id=group.id).exists()
  2905. @override_settings(SENTRY_SELF_HOSTED=False)
  2906. def test_ratelimit(self):
  2907. self.login_as(user=self.user)
  2908. with freeze_time("2000-01-01"):
  2909. for i in range(5):
  2910. self.get_success_response()
  2911. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  2912. def test_bulk_delete_performance_issues(self):
  2913. groups = []
  2914. for i in range(10, 41):
  2915. groups.append(
  2916. self.create_group(
  2917. project=self.project,
  2918. status=GroupStatus.RESOLVED,
  2919. type=GroupType.PERFORMANCE_SLOW_SPAN.value,
  2920. )
  2921. )
  2922. hashes = []
  2923. for group in groups:
  2924. hash = uuid4().hex
  2925. hashes.append(hash)
  2926. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  2927. self.login_as(user=self.user)
  2928. # if query is '' it defaults to is:unresolved
  2929. with self.feature("organizations:performance-issues"):
  2930. response = self.get_response(qs_params={"query": ""})
  2931. assert response.status_code == 400
  2932. for group in groups:
  2933. assert Group.objects.filter(id=group.id).exists()
  2934. assert GroupHash.objects.filter(group_id=group.id).exists()