Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cce9733

Browse files
committedApr 5, 2024··
Add Sentry error and event tracking
1 parent 8bb69c6 commit cce9733

File tree

8 files changed

+90
-28
lines changed

8 files changed

+90
-28
lines changed
 

‎.github/workflows/approve-application.yml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
chmod +x ./processor
4040
./processor approve
4141
env:
42+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
4243
APPROVER_ID: ${{ needs.check.outputs.approver_id }}
4344
APPROVER_USERNAME: ${{ needs.check.outputs.approver_username }}
4445
OP_BOT_PAT: ${{ secrets.OP_BOT_PAT }}

‎.github/workflows/review-application.yml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
chmod +x ./processor
2424
./processor review
2525
env:
26+
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
2627
OP_BOT_PAT: ${{ secrets.OP_BOT_PAT }}
2728
ISSUE_NUMBER: ${{ github.event.issue.number }}
2829
REPOSITORY_OWNER: ${{ github.repository_owner }}

‎script/application.go

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/getsentry/sentry-go"
1213
"github.com/google/go-github/v60/github"
1314
)
1415

@@ -64,6 +65,7 @@ func (a *Application) Parse(issue *github.Issue) {
6465
if isTesting() {
6566
data, err := json.MarshalIndent(a.sections, "", "\t")
6667
if err != nil {
68+
sentry.CaptureException(err)
6769
log.Fatalf("Could not marshal Sections input data: %s", err.Error())
6870
}
6971

@@ -125,6 +127,7 @@ func (a *Application) RenderProblems() string {
125127
func (a *Application) GetData() []byte {
126128
data, err := json.MarshalIndent(a, "", "\t")
127129
if err != nil {
130+
sentry.CaptureException(err)
128131
log.Fatalf("Could not marshal Application data: %s", err.Error())
129132
}
130133

‎script/approver.go

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"log"
78
"path/filepath"
89
"strings"
10+
11+
"github.com/getsentry/sentry-go"
912
)
1013

1114
type Approver struct {
@@ -18,37 +21,43 @@ func (a *Approver) Approve() {
1821
a.application = Application{}
1922

2023
if err := a.application.SetApprover(); err != nil {
21-
a.printErrorAndExit(err)
24+
a.logErrorAndExit("could not set application approver", err)
2225
}
2326

2427
if err := a.gitHub.Init(); err != nil {
25-
a.printErrorAndExit(err)
28+
a.logErrorAndExit("could not initialize GitHub client", err)
2629
}
2730

2831
if *a.gitHub.Issue.State == "closed" {
29-
a.printErrorAndExit(errors.New("script run on closed issue"))
32+
a.logErrorAndExit(
33+
"script run on closed issue",
34+
errors.New(*a.gitHub.Issue.State),
35+
)
3036
}
3137

3238
if !a.gitHub.IssueHasLabel(LabelStatusApproved) {
33-
a.printErrorAndExit(
34-
fmt.Errorf("script run on issue that does not have required '%s' label", LabelStatusApproved),
39+
a.logErrorAndExit(
40+
fmt.Sprintf("script run on issue that does not have required '%s' label", LabelStatusApproved),
41+
fmt.Errorf("issue has labels %v", a.gitHub.Issue.Labels),
3542
)
3643
}
3744

3845
a.application.Parse(a.gitHub.Issue)
3946

4047
if !a.application.IsValid() {
41-
a.printErrorAndExit(
42-
fmt.Errorf("script run on issue with invalid application data:\n\n%s", a.renderProblems()),
48+
a.logErrorAndExit(
49+
"script run on issue with invalid application data",
50+
errors.New(a.renderProblems()),
4351
)
4452
}
4553

4654
// The reviewer may remove this label themselves, but
4755
// let's double check and remove it if they haven't
4856
if a.gitHub.IssueHasLabel(LabelStatusReviewing) {
4957
if err := a.gitHub.RemoveIssueLabel(LabelStatusReviewing); err != nil {
50-
a.printErrorAndExit(
51-
fmt.Errorf("could not remove issue label '%s': %s", LabelStatusReviewing, err.Error()),
58+
a.logErrorAndExit(
59+
fmt.Sprintf("could not remove issue label '%s'", LabelStatusReviewing),
60+
err,
5261
)
5362
}
5463
}
@@ -58,26 +67,41 @@ func (a *Approver) Approve() {
5867
a.application.GetData(),
5968
fmt.Sprintf("Added \"%s\" to program", a.application.Project.Name),
6069
); err != nil {
61-
a.printErrorAndExit(
62-
fmt.Errorf("could not create commit: %s", err.Error()),
70+
a.logErrorAndExit(
71+
"could not create commit",
72+
err,
6373
)
6474
}
6575

6676
if err := a.gitHub.CreateIssueComment(a.getApprovalMessage()); err != nil {
67-
a.printErrorAndExit(
68-
fmt.Errorf("could not create issue comment: %s", err.Error()),
77+
a.logErrorAndExit(
78+
"could not create issue comment",
79+
err,
6980
)
7081
}
7182

7283
if err := a.gitHub.CloseIssue(); err != nil {
73-
a.printErrorAndExit(
74-
fmt.Errorf("could not close issue: %s", err.Error()),
84+
a.logErrorAndExit(
85+
"could not close issue",
86+
err,
7587
)
7688
}
89+
90+
var appData map[string]interface{}
91+
err := json.Unmarshal(a.application.GetData(), &appData)
92+
if err != nil {
93+
log.Fatal(err)
94+
}
95+
sentry.CaptureEvent(&sentry.Event{
96+
Message: "Application approved",
97+
Level: sentry.LevelInfo,
98+
Extra: appData,
99+
})
77100
}
78101

79-
func (a *Approver) printErrorAndExit(err error) {
80-
log.Fatalf("Error approving application: %s\n", err.Error())
102+
func (a *Approver) logErrorAndExit(message string, err error) {
103+
sentry.CaptureException(err)
104+
log.Fatalf("Error approving issue: %s: %s\n", message, err.Error())
81105
}
82106

83107
func (a *Approver) renderProblems() string {

‎script/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
github.com/PuerkitoBio/goquery v1.9.1
7+
github.com/getsentry/sentry-go v0.27.0
78
github.com/google/go-github/v60 v60.0.0
89
github.com/russross/blackfriday/v2 v2.1.0
910
golang.org/x/oauth2 v0.18.0
@@ -14,6 +15,8 @@ require (
1415
github.com/golang/protobuf v1.5.3 // indirect
1516
github.com/google/go-querystring v1.1.0 // indirect
1617
golang.org/x/net v0.22.0 // indirect
18+
golang.org/x/sys v0.18.0 // indirect
19+
golang.org/x/text v0.14.0 // indirect
1720
google.golang.org/appengine v1.6.8 // indirect
1821
google.golang.org/protobuf v1.33.0 // indirect
1922
)

‎script/go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VP
22
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
33
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
44
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
5+
github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps=
6+
github.com/getsentry/sentry-go v0.27.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
57
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
68
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
79
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
@@ -42,6 +44,8 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
4244
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4345
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4446
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
48+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
4549
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
4650
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4751
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
@@ -53,6 +57,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
5357
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
5458
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
5559
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
60+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
61+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
5662
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5763
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
5864
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

‎script/main.go

+17
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"time"
8+
9+
"github.com/getsentry/sentry-go"
710
)
811

912
func printUsageAndExit() {
@@ -21,6 +24,20 @@ func getEnv(key string) (string, error) {
2124
}
2225

2326
func main() {
27+
sentryDsn, err := getEnv("SENTRY_DSN")
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
err = sentry.Init(sentry.ClientOptions{
33+
Dsn: sentryDsn,
34+
})
35+
if err != nil {
36+
log.Fatalf("Sentry initialization failed: %v\n", err)
37+
}
38+
39+
defer sentry.Flush(2 * time.Second)
40+
2441
if len(os.Args) < 2 {
2542
printUsageAndExit()
2643
}

‎script/reviewer.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"log"
6+
7+
"github.com/getsentry/sentry-go"
68
)
79

810
type Status int
@@ -24,7 +26,7 @@ func (r *Reviewer) Review() {
2426
r.application = Application{}
2527

2628
if err := r.gitHub.Init(); err != nil {
27-
r.printErrorAndExit(err)
29+
r.logErrorAndExit("could not initialize GitHub client", err)
2830
}
2931

3032
r.application.Parse(r.gitHub.Issue)
@@ -96,38 +98,43 @@ func (r *Reviewer) updateLabels(status Status, isClosed bool) {
9698
if r.application.IsValid() {
9799
if status == Invalid {
98100
if err := r.gitHub.RemoveIssueLabel(LabelStatusInvalid); err != nil {
99-
r.printErrorAndExit(
100-
fmt.Errorf("could not remove issue label '%s': %s", LabelStatusInvalid, err.Error()),
101+
r.logErrorAndExit(
102+
fmt.Sprintf("could not remove issue label '%s'", LabelStatusInvalid),
103+
err,
101104
)
102105
}
103106
}
104107

105108
if status != Reviewing {
106109
if err := r.gitHub.AddIssueLabel(LabelStatusReviewing); err != nil {
107-
r.printErrorAndExit(
108-
fmt.Errorf("could not add issue label '%s': %s", LabelStatusReviewing, err.Error()),
110+
r.logErrorAndExit(
111+
fmt.Sprintf("could not add issue label '%s'", LabelStatusReviewing),
112+
err,
109113
)
110114
}
111115
}
112116
} else {
113117
if status != Invalid {
114118
if err := r.gitHub.AddIssueLabel(LabelStatusInvalid); err != nil {
115-
r.printErrorAndExit(
116-
fmt.Errorf("could not add issue label '%s': %s", LabelStatusInvalid, err.Error()),
119+
r.logErrorAndExit(
120+
fmt.Sprintf("could not add issue label '%s'", LabelStatusInvalid),
121+
err,
117122
)
118123
}
119124
}
120125

121126
if status == Reviewing {
122127
if err := r.gitHub.RemoveIssueLabel(LabelStatusReviewing); err != nil {
123-
r.printErrorAndExit(
124-
fmt.Errorf("could not remove issue label '%s': %s", LabelStatusReviewing, err.Error()),
128+
r.logErrorAndExit(
129+
fmt.Sprintf("could not remove issue label '%s'", LabelStatusReviewing),
130+
err,
125131
)
126132
}
127133
}
128134
}
129135
}
130136

131-
func (r *Reviewer) printErrorAndExit(err error) {
132-
log.Fatalf("Error reviewing issue: %s\n", err.Error())
137+
func (r *Reviewer) logErrorAndExit(message string, err error) {
138+
sentry.CaptureException(err)
139+
log.Fatalf("Error reviewing issue: %s: %s\n", message, err.Error())
133140
}

0 commit comments

Comments
 (0)
Please sign in to comment.