Skip to content

Commit f2f33e2

Browse files
authored
feat: add InvokeSentryResult::Events() to extract events from envelopes (#137)
* feat: add InvokeSentryResult::Events() to extract events from envelopes * Update CHANGELOG.md * Replace try-catch blocks with -ErrorAction SilentlyContinue
1 parent 17cc15e commit f2f33e2

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add `extra-install-packages` input to install additional apt packages required by custom dangerfiles
1010
- Custom dangerfiles receive full Danger API access (`fail`, `warn`, `message`, `markdown`, `danger`)
1111
- Enables repositories to extend Danger checks without overwriting shared workflow comments
12+
- Sentry-CLI integration test action - Add `InvokeSentryResult::Events()` method to extract events from envelopes ([#137](https://github.com/getsentry/github-workflows/pull/137))
1213

1314
## 3.1.0
1415

sentry-cli/integration-test/action.psm1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ class InvokeSentryResult
3939
return $envelopes
4040
}
4141

42+
# Events are extracted from envelopes, each event body as single item.
43+
# Note: Unlike Envelopes(), this method discards potential duplicates based on event_id.
44+
[string[]]Events()
45+
{
46+
$ids = @()
47+
$events = @()
48+
foreach ($envelope in $this.Envelopes())
49+
{
50+
$lines = @($envelope -split "\\n")
51+
$header = $lines[0].Trim() | ConvertFrom-Json
52+
$eventId = $header | Select-Object -ExpandProperty event_id -ErrorAction SilentlyContinue
53+
if ($eventId -and $ids -notcontains $eventId)
54+
{
55+
$body = $lines | Select-Object -Skip 1 | Where-Object {
56+
($_ | ConvertFrom-Json | Select-Object -ExpandProperty event_id -ErrorAction SilentlyContinue) -eq $eventId
57+
} | Select-Object -First 1
58+
if ($body)
59+
{
60+
$ids += $eventId
61+
$events += $body
62+
}
63+
}
64+
}
65+
return $events
66+
}
67+
4268
[bool]HasErrors()
4369
{
4470
return $this.ServerStdErr.Length -gt 0

sentry-cli/integration-test/tests/action.Tests.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,30 @@ helloworld
9797
$result.Envelopes().Length | Should -Be 1
9898
$result.Envelopes()[0].Length | Should -Be 357
9999
}
100+
101+
It "discards duplicate events" {
102+
$result = Invoke-SentryServer {
103+
param([string]$url)
104+
Invoke-WebRequest -Uri "$url/api/0/envelope" -Method Post -Body @'
105+
{"dsn":"https://e12d836b15bb49d7bbf99e64295d995b:@sentry.io/42","sent_at":"2025-11-20T03:52:42.924Z"}
106+
{"type":"session","length":42}
107+
{"sid":"66356dadc138458a8d5cd9e258065175"}
108+
'@
109+
Invoke-WebRequest -Uri "$url/api/0/envelope" -Method Post -Body @'
110+
{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc","dsn":"https://e12d836b15bb49d7bbf99e64295d995b:@sentry.io/42","sent_at":"2025-11-20T03:53:38.929Z"}
111+
{"type":"event","length":47,"content_type":"application/json"}
112+
{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}
113+
'@
114+
Invoke-WebRequest -Uri "$url/api/0/envelope" -Method Post -Body @'
115+
{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc","dsn":"https://e12d836b15bb49d7bbf99e64295d995b:@sentry.io/42","sent_at":"2025-11-20T03:53:41.505Z"}
116+
{"type":"event","length":47,"content_type":"application/json"}
117+
{"event_id":"9ec79c33ec9942ab8353589fcb2e04dc"}
118+
'@
119+
}
120+
121+
Should -ActualValue $result.HasErrors() -BeFalse
122+
$result.Envelopes().Length | Should -Be 3
123+
$result.Events().Length | Should -Be 1
124+
$result.Events()[0].Length | Should -Be 47
125+
}
100126
}

0 commit comments

Comments
 (0)